[AI]算法小抄-你不知道的LangChain原理

LangChain的原理

系列文章主要目的快速厘清不同方法的原理差异和应用场景,

对于理论的细节请参考文末的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:"""

从上面的代码片段可以主要分成PRIFIXFORMAT_INSTRUCTIONS, SUFFIX,当你建构好基础的工程代码后,其实LangCahin底层会将这些PRIFIXFORMAT_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描述之外PRIFIXFORMAT_INSTRUCTIONS, SUFFIX分别按照固定的格式加入到对agent的请求,而agent返回的时候你会发现确实就是按照prompt指定的格式进行返回,这就是LLMs强大的所在。

总结

 如果在LangChain原始工程中搜索promt.py,你会发现有非常多的相关文件。这是因为对于LangChain底层不同的功能,都是需要依赖不同的prmpt进行控制,虽然简单粗暴好理解,但是也不是没有副作用的,以下总结这种框架共有优缺点,这个优缺点同样适用于ChatGPT Plugin和LlmaIndex…等,Prompt Egineering框架

优势

  • 简单快速:不需要训练特定任务模型就能完成各种应用的适配,而且代码入口单一简洁,简单拆解LangChain底层无非就是Prompt指定大模型API,以及三方应用API调用三个个核心模块。
  • 泛用性广:基于自然语言对任务的描述进行模型控制,对于任务类型没有任何限制,只有说不出来,没有做不到的事情。这也是ChatGPT Plugin能够快速接入各种应用的主要原因。

劣势

  • 大模型替换困难:LangChain主要是基于GPT系列框架进行设计,其适用的Prompt不代表其他大模型也能有相同表现,所以如果要自己更换不同的大模型(如:文心一言,通义千问…等)。则很有可能底层prompt都需要跟著微调。
  • 迭代优化困难:在实际应用中,我们很常定期使用用户反馈的bad cases持续迭代模型,但是Prompt Engeering的工程是非常难进行的微调的,往往多跟少一句话对于效果影响巨大,因此这类型产品达到80分是很容易的,但是要持续迭代到90分甚至更高基本上是不太很能的。

Reference

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/

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年8月17日
下一篇 2023年8月17日

相关推荐