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效果对比

Langchain是一个基于Python的自然语言处理工具,可以用于文本切分和问答系统。以下是使用手册: 1. 安装Langchain ```python pip install langchain ``` 2. 创建向量存储 ```python from langchain.vectorizer import Vectorizer # 创建一个向量化器 vectorizer = Vectorizer() # 向向量化器中添加文本 vectorizer.add_text("This is a sample text.") vectorizer.add_text("Another text for testing.") # 获取向量化后的结果 vectors = vectorizer.get_vectors() # 打印结果 print(vectors) ``` 3. 不同类型的chain链 ```python from langchain.chain import Chain # 创建一个Chain对象 chain = Chain() # 添加文本 chain.add_text("This is a sample text.") chain.add_text("Another text for testing.") # 获取chain链 chain_list = chain.get_chain() # 打印结果 print(chain_list) ``` 4. 整体框架 ```python from langchain.qa_system import QASystem # 创建一个QASystem对象 qa_system = QASystem() # 添加问题和答案 qa_system.add_qa("What is Langchain?", "Langchain is a natural language processing tool.") qa_system.add_qa("How to use Langchain?", "You can use Langchain for text splitting and QA system.") # 回答问题 answer = qa_system.answer("What is Langchain?") # 打印答案 print(answer) ``` 5. 文本切分 ```python from langchain.text_splitter import TextSplitter # 创建一个TextSplitter对象 splitter = TextSplitter() # 添加文本 splitter.add_text("This is a sample text. Another text for testing.") # 获取切分结果 sentences = splitter.get_sentences() # 打印结果 print(sentences) ``` 6. 图解流程 ![Langchain流程图](https://github.com/hwchase17/langchain/blob/master/images/langchain.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值