如何使用LangChain的SQLDatabaseChain带内存?

问题描述 投票:0回答:2

我想创建一个链来对我的数据库进行查询。我还想给这个链添加内存。

我想看的对话示例:

查询:域名为domain.com的网站的所有者是谁? 答案:波巴·波波维奇 查询:告诉我他的电子邮件 答案:Boba Bobovich 的电子邮件是 [电子邮件受保护]

我有这个代码:

import os
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain, PromptTemplate
from langchain.memory import ConversationBufferMemory

memory = ConversationBufferMemory()
db = SQLDatabase.from_uri(os.getenv("DB_URI"))
llm = OpenAI(temperature=0, verbose=True)
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, memory=memory)

db_chain.run("Who is owner of the website with domain https://damon.name")
db_chain.run("Tell me his email")
print(memory.load_memory_variables({}))

它给出:

> Entering new  chain...
Who is owner of the website with domain https://damon.name
SQLQuery:SELECT first_name, last_name FROM owners JOIN websites ON owners.id = websites.owner_id WHERE domain = 'https://damon.name' LIMIT 5;
SQLResult: [('Geo', 'Mertz')]
Answer:Geo Mertz is the owner of the website with domain https://damon.name.
> Finished chain.
    
> Entering new  chain...
Tell me his email
SQLQuery:SELECT email FROM owners WHERE first_name = 'Westley' AND last_name = 'Waters'
SQLResult: [('[email protected]',)]
Answer:Westley Waters' email is [email protected].
> Finished chain.
{'history': "Human: Who is owner of the website with domain https://damon.name\nAI: Geo Mertz is the owner of the website with domain https://damon.name.\nHuman: Tell me his email\nAI: Westley Waters' email is [email protected]."}

嗯,它会将上下文保存到内存中,但链不会使用它来给出正确的答案(错误的电子邮件)。怎么解决?

我也不想使用代理,因为我想首先通过一个简单的链来做到这一点。告诉我简单的链条是否不可能。

python artificial-intelligence openai-api langchain py-langchain
2个回答
1
投票

如果您不想使用代理,那么您可以向您的 llm 添加一个模板,该模板具有聊天历史记录字段,然后将其添加为 ConversationBufferMemory() 中的内存密钥。

像这样:

template = """You are a chatbot having a conversation with a human.

{chat_history} Human: {human_input} Chatbot:"""

prompt = PromptTemplate(input_variables=["chat_history", "human_input"], template=template ) 
memory = ConversationBufferMemory(memory_key="chat_history")

llm_chain = LLMChain(
    llm=OpenAI(),
    prompt=prompt,
    verbose=True,
    memory=memory)

查看官方文档: https://python.langchain.com/docs/expression_language/cookbook/memory

如果您改变主意并选择使用代理,请按照以下步骤使用后缀中的 chat_history 进行操作:

prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" 
suffix = """Begin!"

{chat_history} Question: {input} {agent_scratchpad}"""

prompt = ZeroShotAgent.create_prompt(
    tools,
    prefix=prefix,
    suffix=suffix,
    input_variables=["input", "chat_history", "agent_scratchpad"], )

memory = ConversationBufferMemory(memory_key="chat_history", chat_memory=message_history )

查看官方文档https://python.langchain.com/docs/modules/memory/agent_with_memory_in_db


0
投票

虽然我自己没有尝试过,但我认为我们可以通过立即查看所有用户的历史问题来生成单个查询来解决这个问题。 在您的情况下,第一个查询被认为是准确的。让我们假设您内存中有用户的第一个问题。 关于第二个问题,您将这两个问题作为单一提示传递给 LLM 来生成 SQL 查询。,理想情况下应该生成如下查询:

select email from owners JOIN websites ON owners.id = websites.owner_id WHERE domain = 'https://damon.name' LIMIT 5;

© www.soinside.com 2019 - 2024. All rights reserved.