成品網站nike源碼1688網絡推廣團隊哪家好
Large Language Models (LLMs) 在語義知識方面表現(xiàn)不錯,但也有一些不足,如:不能正確計算數學公式、無法獲取最新知識新聞
通過 Agents 可以賦予 LLMs 更多能力,讓LLM能夠計算、上網查詢
agent 簡單使用
from langchain import OpenAI
# 語言模型
llm = OpenAI(
openai_api_key="OPENAI_API_KEY",
temperature=0,
model_name="text-davinci-003"
)from langchain.chains import LLMMathChain
from langchain.agents import Tool
# 能計算數學公式的一個chain
llm_math = LLMMathChain(llm=llm)# initialize the math tool
math_tool = Tool(
name='Calculator',
func=llm_math.run,
description='Useful for when you need to answer questions about math.' # 描述工具能做什么
)
# when giving tools to LLM, we must pass as list of tools
tools = [math_tool]# 如果 langchain.agents 中有相關工具,則可以直接使用
#from langchain.agents import load_tools
#tools = load_tools(
#['llm-math'],
#llm=llm
)# 初始化 agent
from langchain.agents import initialize_agent
zero_shot_agent = initialize_agent(agent="zero-shot-react-description", # 無記憶的agenttools=tools, # tools 中只有math_tool,所以只能做計算llm=llm,verbose=True, # 顯示執(zhí)行過程max_iterations=3)
zero_shot_agent("what is (4.5*2.1)^2.2?")
上面的 tools 中只有math_tool,所以 zero_shot_agent 只能做計算,不能回答其它常識問題,可以在 tools 中添加更多工具,使得 zero_shot_agent 擁有更多能力。
# 可以在 tools 中新增聊天工具
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
prompt = PromptTemplate(
input_variables=["query"],
template="{query}"
)
llm_chain = LLMChain(llm=llm, prompt=prompt)# initialize the LLM tool
llm_tool = Tool(
name='Language Model',
func=llm_chain.run,
description='use this tool for general purpose queries and logic'
)
tools.append(llm_tool)
# reinitialize the agent
zero_shot_agent = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
llm=llm,
verbose=True,
max_iterations=3
)
agent 類型
zero-shot-react-description 無緩存的方式,聊天是單次的,無上下文緩存
zero_shot_agent = initialize_agent(
agent="zero-shot-react-description",
tools=tools,
llm=llm,
verbose=True,
max_iterations=3,
)
conversational-react-description 帶緩存
from langchain.memory import ConversationBufferMemorymemory = ConversationBufferMemory(memory_key="chat_history")conversational_agent = initialize_agent(
agent='conversational-react-description',
tools=tools,
llm=llm,
verbose=True,
max_iterations=3,
memory=memory,
)
react-docstore 可以檢索知識庫,無緩存
from langchain import Wikipedia
from langchain.agents.react.base import DocstoreExplorerdocstore=DocstoreExplorer(Wikipedia())
tools = [Tool(name="Search", # 信息檢索func=docstore.search, description='search wikipedia'),Tool(name="Lookup", # 匹配相近結果func=docstore.lookup, description='lookup a term in wikipedia')
]docstore_agent = initialize_agent(tools,llm,agent="react-docstore",verbose=True,max_iterations=3)
self-ask-with-search 將LLM與搜索引擎結合起來
from langchain import SerpAPIWrapper# initialize the search chain
search = SerpAPIWrapper(serpapi_api_key='serp_api_key')# create a search tool
tools = [Tool(name="Intermediate Answer",func=search.run,description='google search')]# initialize the search enabled agent
self_ask_with_search = initialize_agent(tools,llm,agent="self-ask-with-search",verbose=True)
參考:
Superpower LLMs with Conversational Agents