LangChain usage research

Table of contents

1. What is LangChain

2. Main modules provided by LangChain

3. Example of using Agent

4. Comparison of the effects of zero-shot-react-description in ChatGPT and LLaMA-7B


1. What is LangChain

LangChain is a program framework that allows users to quickly build applications around the LLM (base).

LangChain can easily manage interactions with language models, link multiple components together, and integrate additional resources such as APIs and databases.

Its core idea is that different components can be "linked" together to create more advanced LLMs applications.

2. Main modules provided by LangChain

1. Prompts function

Includes hint management, hint optimization, and hint serialization

2、LLMs

Includes a common interface to all LLMs, as well as commonly used LLMs tools

3、Document Loaders

Includes a standard interface for loading documents, and integration with various text data sources.

4、Utils

Language models tend to be more powerful when interacted with other sources of knowledge or computation. This can include Python REPLs, embeddings, search engines, etc. LangChain provides a large number of commonly used tools.

5、Indexes

Language models tend to be more powerful when combined with their own text data

6、Agents

Agents involve an LLM that chooses an action to perform, performs that action, sees the observations, and repeats the process until completion.

7、Chat

Chat models are a different API than language models - instead of dealing with raw text, they process messages.

3. Example of using Agent

1. Examples

2. Agent type to instance mapping

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: Determine which tool to use based on the description of the tool and the requested string

react-docstor: use react framework, interact with docstore, use Search and Lookup tools, the former is used to search, and the latter is used to find terms, for example: Wipipedia tool

self-ask-with-search: This agent only uses one tool: Intermediate Answer, which will look for factual answers to questions (referring to non-gpt-generated answers, but existing in the network and text), such as Google search API tools

conversational-react-description: A proxy designed for conversational settings, its prompt will be designed to be conversational, and it will still use the ReAct framework to decide which tool to use, and store past conversational interactions in memory.

3. ZeroShotAgent's agent instance prompt description

Assemble prompt template input parameters:

Tool parameters :

All parameter information for all tools

Prefix parameters :

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

Suffix parameter:

'Begin!

Question: {input}

Thought:{agent_scratchpad}'

Format_instructions parameter :

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 operation flow chart

(1) Processing step context intermediate_steps

It is empty for the first time, other references are as follows:

[(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: use this tool when answering the latest events, news\nCalculator: use this tool when answering questions involving mathematical calculations\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: What is the weather in Hangzhou on May 20, 2023, and then multiply the value by -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:'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:'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 parses out intermediate results

[[Generation(text='This information needs to be searched online\nAction: Search\nAction Input: What is the weather in Hangzhou on May 20, 2023', 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 parses out the final result:

[[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'}

4. Comparison of the effects of zero-shot-react-description in ChatGPT and LLaMA-7B

Guess you like

Origin blog.csdn.net/benben044/article/details/130843326