gpt4 book ai didi

python - 如何为使用 Langchain 的代理按特定顺序选择正确的工具?

转载 作者:行者123 更新时间:2023-12-02 05:49:01 27 4
gpt4 key购买 nike

我想我不明白代理如何选择工具。我有一个矢量数据库(Chroma),其中嵌入了我希望代理首先查看的所有内部知识。然后,如果答案不在 Chroma 数据库中,它应该使用 OpenAI 用于训练的信息(外部知识)来回答问题。如果问题是“自然对话”,我希望代理在回答问题中发挥作用。这是我尝试过的代码,但它只使用知识外部库工具。我希望它能决定最好的工具。

from langchain.agents import Tool
from langchain.chat_models import ChatOpenAI
from langchain.chains.conversation.memory import ConversationBufferWindowMemory
from langchain.chains import RetrievalQA
from langchain.agents import initialize_agent
from chroma_database import ChromaDatabase
from langchain.embeddings import OpenAIEmbeddings
from parameters import EMBEDDING_MODEL, BUCKET_NAME, COLLECTION_NAME

embeddings = OpenAIEmbeddings(model=EMBEDDING_MODEL)
chroma = ChromaDatabase(embedding_function=embeddings,
persist_directory='database/vectors/',
bucket_name=BUCKET_NAME,
collection_name=COLLECTION_NAME)


# chat completion llm
llm = ChatOpenAI(
model_name='gpt-3.5-turbo',
temperature=0.0
)
# conversational memory
conversational_memory = ConversationBufferWindowMemory(
memory_key='chat_history',
k=0,
return_messages=True
)
# retrieval qa chain
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=chroma.db.as_retriever()
)

tools = [
Tool(
name='Knowledge Internal Base',
func=qa.run,
description=(
'use this tool when answering internal knowledge queries. Search in the internal database retriever'
)
),
Tool(
name='Knowledge External Base',
func=qa.run,
description=(
'use this tool when the answer is not retrieved in the Knowledge Internal Base tool'
)
),
Tool(
name='Natural Conversation',
func=qa.run,
description=(
'use this tool when the answer is related to a natural conversation, act as friendly person'
)
)
]

agent = initialize_agent(
agent='chat-conversational-react-description',
tools=tools,
llm=llm,
verbose=True,
max_iterations=3,
early_stopping_method='generate',
memory=conversational_memory
)

agent.run("What Pepito said?") #Pepito conversation is stored as embedding in Chroma
agent.run("What Tom Cruise said in the movie Impossible Mission 1?") #I don't have anything about Tom Cruise in Chroma
agent.run("Hello, how are you?") #I want the answer looks like: "I'm pretty fine, how about you?"

我应该怎么做才能拥有一个正确的计划执行/编排代理,以正确的顺序使用正确的工具?

最佳答案

文档在这里会很有帮助;但是,显然您可以在初始化时向代理提供具体说明...

agent_instructions = "Try 'Knowledge Internal Base' tool first, Use the other tools if these don't work."

agent = initialize_agent(
agent='chat-conversational-react-description',
tools=tools,
llm=llm,
agent_instructions=agent_instructions
verbose=True,
max_iterations=3,
early_stopping_method='generate',
memory=conversational_memory
)

关于python - 如何为使用 Langchain 的代理按特定顺序选择正确的工具?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76366589/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com