系列文章主要目的快速厘清不同方法的原理差异和应用场景,
对于理论的细节请参考文末的Reference,
Reference中会筛选较为正确,细节的说明
你知道ChatGPT Plugin,AutoGPT和AgentGPT的工作原理吗?其实主要都是基于对于LLMs的Prompt工程,这篇文章主要就是透过目前最活跃的开源框架LangChain进行原理剖析,一览这类型框架背后的工作原理
Langchaing是一个语言模型的开发框架,主要是利用大型LLMs的强大得few-shot以及zero-shot泛化能力作为基础,以Prompt控制为核心基础,让开发者可以根据需求,往上快速堆叠应用,简单来说:
LangChain 是基于提示词工程(Prompt Engineering),提供一个桥接大型语言模型(LLMs)以及实际应用App的胶水层框架。
具体LangChain的原理是什么呢?我们基于LangChain的源码快速剖析:
LangChain最底层的核心其实都放在了langchain/agents/structured_chat/prompt.py中
# flake8: noqa
PREFIX = """Respond to the human as helpfully and accurately as possible. You have access to the following tools:"""
FORMAT_INSTRUCTIONS = """Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).
Valid "action" values: "Final Answer" or {tool_names}
Provide only ONE action per $JSON_BLOB, as shown:
```
{{{{
"action": $TOOL_NAME,
"action_input": $INPUT
}}}}
```
Follow this format:
Question: input question to answer
Thought: consider previous and subsequent steps
Action:
```
$JSON_BLOB
```
Observation: action result
... (repeat Thought/Action/Observation N times)
Thought: I know what to respond
Action:
```
{{{{
"action": "Final Answer",
"action_input": "Final response to human"
}}}}
```"""
SUFFIX = """Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation:.
Thought:"""
从上面的代码片段可以主要分成PRIFIX, FORMAT_INSTRUCTIONS, SUFFIX,当你建构好基础的工程代码后,其实LangCahin底层会将这些PRIFIX, FORMAT_INSTRUCTIONS, SUFFIX加入到LLMs的语言模型中,这里举个简单的例子
toolbox.description = ["use this tool used to find weather information",
"this tool is used to find travel information",
"this tool if use to find food",
"this tool if use to search information on websites",
"Multiply the provided floats",
"add the provided floats"]
首先基于LangChain建构了6种工具提供给agent,然后简单运行一下agent
llm = OpenAI(temperature=1, model="text-davinci-003", max_tokens=2048)
agent_executor = initialize_agent(
tools,
llm,
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True)
从运行结果可以很明显看到除了我们设置好的tools描述之外PRIFIX, FORMAT_INSTRUCTIONS, SUFFIX分别按照固定的格式加入到对agent的请求,而agent返回的时候你会发现确实就是按照prompt指定的格式进行返回,这就是LLMs强大的所在。
如果在LangChain原始工程中搜索promt.py,你会发现有非常多的相关文件。这是因为对于LangChain底层不同的功能,都是需要依赖不同的prmpt进行控制,虽然简单粗暴好理解,但是也不是没有副作用的,以下总结这种框架共有优缺点,这个优缺点同样适用于ChatGPT Plugin和LlmaIndex...等,Prompt Egineering框架
GitHub - hwchase17/langchain: ⚡ Building applications with LLMs through composability ⚡
Welcome to LangChain — LangChain 0.0.180
https://www.reddit.com/r/MachineLearning/comments/10o96k8/d_gptindex_vs_langchain/