外貿(mào)網(wǎng)站開發(fā)定制廣東廣州網(wǎng)點(diǎn)快速網(wǎng)站建設(shè)
有時(shí)會(huì)定義很多工具,而定義Agent的時(shí)候只想使用與問題相關(guān)的工具,這是可以通過向量數(shù)據(jù)庫來檢索相關(guān)的工具,傳遞給Agent
# Define which tools the agent can use to answer user queries
search = SerpAPIWrapper()
search_tool = Tool(name = "Search",func=search.run,description="useful for when you need to answer questions about current events")
def fake_func(inp: str) -> str:return "foo"
fake_tools = [Tool(name=f"foo-{i}", func=fake_func, description=f"a silly function that you can use to get more information about the number {i}") for i in range(99)
]
ALL_TOOLS = [search_tool] + fake_toolsfrom langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.schema import Document
docs = [Document(page_content=t.description, metadata={"index": i}) for i, t in enumerate(ALL_TOOLS)]
vector_store = FAISS.from_documents(docs, OpenAIEmbeddings())
retriever = vector_store.as_retriever()def get_tools(query):docs = retriever.get_relevant_documents(query)return [ALL_TOOLS[d.metadata["index"]] for d in docs]tool = get_tools("What is today weather")