LangChain使用调研

目录

一、LangChain是什么

二、LangChain提供的主要模块

三、Agent使用例子

四、zero-shot-react-description在ChatGPT和LLaMA-7B效果对比


一、LangChain是什么

LangChain是一个程序框架,它允许用户围绕LLM(基座)快速构建应用程序。

LangChain可以轻松管理与语言模型的交互,将多个组件链接在一起,并集成额外的资源,例如API和数据库。

它的核心思想是可以将不同的组件“链接”起来,创建更高级的LLMs应用。

二、LangChain提供的主要模块

1、Prompts功能

包括提示管理、提示优化和提示序列化

2、LLMs

包括所有LLMs的通用接口,以及常用的LLMs工具

3、Document Loaders

包括加载文档的标准接口,以及与各种文本数据源的集成.

4、Utils

语言模型在与其他知识或计算源交互时往往更强大。这可以包括Python REPLs、嵌入、搜索引擎等。LangChain提供了一大批常用的工具。

5、Indexes

语言模型在与自己的文本数据结合时往往更强大

6、Agents

Agents涉及到一个LLM在选择要执行的动作、执行该动作、看到观察结果,并重复这个过程直到完成。

7、Chat

Chat模型是一种与语言模型不同的API - 它们不是处理原始文本,而是处理消息。

三、Agent使用例子

1、例子

2、agent类型到实例映射

AgentType.ZERO_SHOT_REACT_DESCRIPTION: ZeroShotAgent,

AgentType.REACT_DOCSTORE: ReActDocstoreAgent,

AgentType.SELF_ASK_WITH_SEARCH: SelfAskWithSearchAgent,

AgentType.CONVERSATIONAL_REACT_DESCRIPTION: ConversationalAgent,

AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION: ChatAgent,

AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION: ConversationalChatAgent,

AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION: StructuredChatAgent

zero-shot-react-description:根据工具的描述, 和请求的string 来决定使用哪个工具

react-docstor:使用react框架, 和docstore交互, 使用Search 和Lookup 工具, 前者用来搜, 后者寻找term, 举例: Wipipedia工具

self-ask-with-search:此代理只使用一个工具: Intermediate Answer, 它会为问题寻找事实答案(指的非gpt生成的答案, 而是在网络中,文本中已存在的), 如 Google search API 工具

conversational-react-description:为会话设置而设计的代理, 它的prompt会被设计的具有会话性, 且还是会使用 ReAct框架来决定使用来个工具, 并且将过往的会话交互存入内存.

3、ZeroShotAgent的agent实例prompt说明

组装prompt模板输入参数:

Tool参数

所有工具的所有参数信息

Prefix参数:

'Answer the following questions as best you can. You have access to the following tools:'

Suffix参数:

'Begin!

Question: {input}

Thought:{agent_scratchpad}'

Format_instructions参数

Use the following format:

Question: the input question you must answer

Thought: you should always think about what to do

Action: the action to take, should be one of [{tool_names}]

Action Input: the input to the action

Observation: the result of the action

... (this Thought/Action/Action Input/Observation can repeat N times)

Thought: I now know the final answer

Final Answer: the final answer to the original input question

4、agent运行流程图

(1)处理步骤上下文intermediate_steps

首次为空,其他次参考如下:

[(AgentAction(tool='Search', tool_input='Hangzhou May 20 2023 weather', log=' I need to search for the weather in Hangzhou on May 20, 2023\nAction: Search\nAction Input: "Hangzhou May 20 2023 weather"'), "The temperatures in Hangzhou in May are comfortable with low of 64°F and and high up to 80°F. You can expect rain for roughly half of the month of May in Hangzhou. We're expecting roughly 8 to 15 days of rain, so your rubber boots and umbrella are going to see plenty of use this month if you're keen on staying dry."), (AgentAction(tool='Calculator', tool_input='-1 * (80°F - 32) * 5 / 9', log=' I need to convert 80°F to Celsius and multiply by -1\nAction: Calculator\nAction Input: "-1 * (80°F - 32) * 5 / 9"'), 'Answer: -26.666666666666668')]

(2)thoughts:

I need to search for the weather in Hangzhou on May 20, 2023(只根据input生成的thought
Action: Search
Action Input: "Hangzhou May 20 2023 weather"
Observation: The temperatures in Hangzhou in May are comfortable with low of 64°F and and high up to 80°F. You can expect rain for roughly half of the month of May in Hangzhou. We're expecting roughly 8 to 15 days of rain, so your rubber boots and umbrella are going to see plenty of use this month if you're keen on staying dry.
Thought: I need to convert 80°F to Celsius and multiply by -1
Action: Calculator
Action Input: "-1 * (80°F - 32) * 5 / 9"
Observation: Answer: -26.666666666666668
Thought:

(3)prompt:

'Answer the following questions as best you can. You have access to the following tools:\n\nSearch: 当回答最新事件、消息时使用该工具\nCalculator: 当回答的问题涉及数学计算时使用该工具\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [Search, Calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: 杭州2023年5月20号的天气是多少度,然后将该值乘以-1\nThought: I need to search for the weather in Hangzhou on May 20, 2023\nAction: Search\nAction Input: "Hangzhou May 20 2023 weather"\nObservation: The temperatures in Hangzhou in May are comfortable with low of 64°F and and high up to 80°F. You can expect rain for roughly half of the month of May in Hangzhou. We\'re expecting roughly 8 to 15 days of rain, so your rubber boots and umbrella are going to see plenty of use this month if you\'re keen on staying dry.\nThought: I need to convert 80°F to Celsius and multiply by -1\nAction: Calculator\nAction Input: "-1 * (80°F - 32) * 5 / 9"\nObservation: Answer: -26.666666666666668\nThought:'

(4)LLM解析出中间结果

[[Generation(text=' 该信息需要在网上查找\nAction: Search\nAction Input: 杭州2023年5月20号的天气是多少度', generation_info={'finish_reason': 'stop', 'logprobs': None})]] llm_output={'token_usage': {'total_tokens': 223, 'prompt_tokens': 188, 'completion_tokens': 35}, 'model_name': 'GPT-3.5'}

(5)LLM解析出最终结果:

[[Generation(text=' The temperature is -26.67 degrees Celsius\nFinal Answer: -26.67 degrees Celsius<|im_end|>', generation_info={'finish_reason': 'stop', 'logprobs': None})]] llm_output={'token_usage': {'prompt_tokens': 361, 'completion_tokens': 20, 'total_tokens': 381}, 'model_name': 'GPT-3.5'}

四、zero-shot-react-description在ChatGPT和LLaMA-7B效果对比

猜你喜欢

转载自blog.csdn.net/benben044/article/details/130843326