
API網關如何發展:更輕、更智能、云原生
? ? ? ?LangChain中的Agent使用LLM(語言學習模型)來確定要采取的操作以及順序。
? ? ? ?Zero-shot ReAct Agent是一種語言生成模型,即使不經過特定數據的訓練,也可以創建真實的上下文。它可以用于各種任務,如生成創造性的文本格式、語言翻譯和生成不同類型的創造性內容。
from langchain.agents import initialize_agent, load_tools, AgentTypefrom langchain.llms import OpenAIllm = OpenAI(openai_api_key="your_api_key")tools = load_tools(["wikipedia", "llm-math"], llm=llm)agent = initialize_agent(tools , llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)output_1=agent.run("4 + 5 is ")output_2=agent.run("when you add 4 and 5 the result comes 10.")print(output_1)print(output_2)
? ? ? ?在上面的代碼中,導入了LangChain庫,并通過設置OpenAI API Key來初始化OpenAI語言模型(LLM)。該代碼使用維基百科和數學信息工具設置AI代理,將代理類型指定為ZERO_SHOT_REACT_DESCRIPTION代理。然后,代碼提供兩個提示來演示與代理的一次性交互。
此代理是為在會話設置中使用而設計的,它結合了React框架來確定使用哪個工具,并利用內存來記住以前的對話交互。
from langchain.agents import initialize_agent, load_toolsfrom langchain.llms import OpenAIfrom langchain.memory import ConversationBufferMemoryllm = OpenAI(openai_api_key="...")tools = load_tools(["llm-math"], llm=llm)memory = ConversationBufferMemory(memory_key="chat_history")conversational_agent = initialize_agent( agent="conversational-react-description", tools=tools, llm=llm, verbose=True, max_iterations=3, memory=memory,)output_1=conversational_agent.run("when you add 4 and 5 the result comes 10.")output_2=conversational_agent.run("4 + 5 is ")print(output_1)print(output_2)
? ? ? 上面的代碼通過導入必要的模塊、設置了OpenAI LLM API Key、加載LLM-math等特定工具進行數學運算以及創建會話緩沖內存來演示LangChain庫的使用。然后使用指定的代理類型、工具、LLM和其他參數初始化會話代理。該代碼展示了與代理交互的兩個提示。
該代理使用React框架與文檔存儲進行通信。它要求提供名稱相同的“搜索”工具和“查找”工具。“搜索”工具用于搜索文檔,而“查找”工具則在最近找到的文檔中查找術語。
from langchain.agents import initialize_agent, Toolfrom langchain.llms import OpenAIfrom langchain import Wikipediafrom langchain.agents.react.base import DocstoreExplorerllm = OpenAI(openai_api_key="...")docstore = DocstoreExplorer(Wikipedia())tools=[ Tool(name="Search", func=docstore.search, description="useful for when you need to ask with search"), Tool(name="Lookup", func=docstore.lookup, description="useful for when you need to ask with lookup")]react_agent= initialize_agent(tools, llm, agent="react-docstore")print(react_agent.run("Full name of Narendra Modi is Narendra Damodardas Modi?")) # look on the keywords then go for searchprint(react_agent.run("Full name of Narendra Modi is Narendra Damodardas Modi."))
? ? ? ? 上面的代碼從LangChain導入必要的模塊,并使用API Key初始化OpenAI語言模型(LLM)。它建立了一個以維基百科為源的文檔存儲資源管理器。定義了兩個工具,“搜索”和“查找”,其中“搜索”工具搜索文檔,“查找”工具執行術語查找。
此代理使用中間回答工具進行自我提問。
from langchain.agents import initialize_agent, Toolfrom langchain.llms import OpenAIfrom langchain import Wikipediallm = OpenAI(openai_api_key="...")wikipedia = Wikipedia()tools = [ Tool( name="Intermediate Answer", func=wikipedia.search, description='wikipedia search' )]agent = initialize_agent( tools=tools, llm=llm, agent="self-ask-with-search", verbose=True,)print(agent.run("what is the capital of Japan?"))
? ? ? ?上述代碼從LangChain庫導入必要的模塊,包括代理和語言模型,它設置了一個具有特定代理配置的會話代理,稱為“self-ask-with-search”。該代理使用“Intermediate Answer”工具來執行維基百科搜索。
問:在LangChain中,Chain和Agent有什么區別?
答:LangChain中Agent和Chain之間的主要區別在于,Agent使用語言模型來確定其動作,而Chain是由開發人員設置的預定義動作序列。Agent使用語言模型根據用戶輸入和可用工具生成響應,而Chain遵循固定的輸入/輸出過程。
問:Verbose()做什么?
答:verbose選項可以在屏幕上顯示詳細的運行信息。
問:LangChain的溫度是多少?
答:默認情況下,LangChain聊天模型的創建溫度值為0.7。溫度參數控制輸出的隨機性。較高的值(如0.7)使輸出更隨機,而較低的值(例如0.2)使輸出更有重點和確定性。
[1]?https://bakshiharsh55.medium.com/agents-in-langchain-3eb92f206a5f
文章轉自微信公眾號@ArronAI