
如何快速實現REST API集成以優化業務流程
通常,大型語言模型(LLM)通常通過RAG(Retrieval-Augmented Generation)架構來增強其存儲器能力。然而,LLM智能體(LLM Agents)不僅能增強存儲器,還將推理、工具、答案和操作都集成到了其系統中。
LLM是一種人工智能技術,可以生成類似人類語言的文本,而LLM智能體則是一種擴展了LLM的系統,它可以執行更復雜的任務,例如搜索信息、使用工具和執行操作。
簡單的“輸入-輸出”LLM使用方式與思維鏈、具有自洽性的思維鏈、思維樹等技術之間的視覺差異。
LLM的性能得到不斷的改進,有許多技術和方法用來提升。我們探討以下技術:
這些技術的應用有助于改善大型語言模型在各種自然語言處理任務上的性能,使其更加強大和靈活。
本文大量使用了Langsmith平臺,該平臺用于生產化LLM應用程序。例如,在構建思維樹提示時,將子提示保存在提示存儲庫中,然后進行加載:
from langchain import hub
from langchain.chains import SequentialChain
cot_step1 = hub.pull("rachnogstyle/nlw_jan24_cot_step1")
cot_step2 = hub.pull("rachnogstyle/nlw_jan24_cot_step2")
cot_step3 = hub.pull("rachnogstyle/nlw_jan24_cot_step3")
cot_step4 = hub.pull("rachnogstyle/nlw_jan24_cot_step4")
model = "gpt-3.5-turbo"
chain1 = LLMChain(
llm=ChatOpenAI(temperature=0, model=model),
prompt=cot_step1,
output_key="solutions"
)
chain2 = LLMChain(
llm=ChatOpenAI(temperature=0, model=model),
prompt=cot_step2,
output_key="review"
)
chain3 = LLMChain(
llm=ChatOpenAI(temperature=0, model=model),
prompt=cot_step3,
output_key="deepen_thought_process"
)
chain4 = LLMChain(
llm=ChatOpenAI(temperature=0, model=model),
prompt=cot_step4,
output_key="ranked_solutions"
)
overall_chain = SequentialChain(
chains=[chain1, chain2, chain3, chain4],
input_variables=["input", "perfect_factors"],
output_variables=["ranked_solutions"],
verbose=True
)
在這里想說明的是在像Langsmith這樣的LLMOps系統中定義推理步驟和版本化它們的正確過程。此外,還可以在公共存儲庫查看其他流行推理技術的示例,如ReAct或帶搜索功能的 Self-ask:
prompt = hub.pull("hwchase17/react")
prompt = hub.pull("hwchase17/self-ask-with-search")
其他值得注意的方法包括:
我們可以將大腦中不同類型的記憶映射到LLM架構的各個組成部分上
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain.agents import AgentExecutor
from langchain.agents import create_openai_functions_agent
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
tools = [retriever_tool]
agent = create_openai_functions_agent(
llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
message_history = ChatMessageHistory()
agent_with_chat_history = RunnableWithMessageHistory(
agent_executor,
lambda session_id: message_history,
input_messages_key="input",
history_messages_key="chat_history",
)
長期記憶:長期記憶存儲事實性知識和程序性指示。在人工智能模型中,這表現為用于訓練和微調的數據。此外,長期記憶還支持RAG框架的操作,使Agents能夠訪問并將已學信息合并到其響應中。就像是Agents提供有關和相關輸出的全面知識存儲庫一樣。在代碼中,通常將其添加為向量化數據庫:
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
loader = WebBaseLoader("https://neurons-lab.com/")
docs = loader.load()
documents = RecursiveCharacterTextSplitter(
chunk_size=1000, chunk_overlap=200
).split_documents(docs)
vector = FAISS.from_documents(documents, OpenAIEmbeddings())
retriever = vector.as_retriever()
在實際操作中,希望通過單獨的推理鏈來增強Agents(可以是另一種 LLM,即特定領域或另一種用于圖像分類的 ML 模型),或者使用基于規則或API的方法來增強Agents。
ChatGPT Plugins 和 OpenAI API 函數調用就是利用工具使用能力增強 LLM 在實踐中發揮作用的良好范例。
from langchain.utilities.tavily_search import TavilySearchAPIWrapper
from langchain.tools.tavily_search import TavilySearchResults
search = TavilySearchAPIWrapper()
tavily_tool = TavilySearchResults(api_wrapper=search)
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.0)
agent_chain = initialize_agent(
[retriever_tool, tavily_tool],
llm,
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
)
自定義工具:定義自己的工具也非常簡單。以計算字符串長度的工具為例進行分析。需要使用@tooldecorator讓Langchain知道這個工具。然后,不要忘記輸入和輸出的類型。但最重要的部分是函數注釋之間的""" """
?——這就是Agents如何知道這個工具是做什么的,并將此描述與其他工具的描述進行比較:
from langchain.pydantic_v1 import BaseModel, Field
from langchain.tools import BaseTool, StructuredTool, tool
@tool
def calculate_length_tool(a: str) -> int:
"""The function calculates the length of the input string."""
return len(a)
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.0)
agent_chain = initialize_agent(
[retriever_tool, tavily_tool, calculate_length_tool],
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
)
你可能會看到一個錯誤——它沒有正確提取Neurons Lab公司的描述,盡管調用了正確的自定義長度計算函數,最終結果卻是錯誤的。可以試著進行修復。
可以將所有架構部件組合在一起成為簡潔版本。注意,可以很容易地分解并單獨定義:
最終的Agents定義會像這樣簡單:
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_with_chat_history = RunnableWithMessageHistory(
agent_executor,
lambda session_id: message_history,
input_messages_key="input",
history_messages_key="chat_history",
)
我們定義了一個完整的架構,其中短期記憶起著至關重要的作用。Agents獲得了消息歷史記錄和一個作為推理結構的素描板,使其能夠拉取正確的網站描述并計算其長度。
本文章轉載微信公眾號@AI科技論談