langchain
2023-06-09 14:06:46 0 举报
AI智能生成
langchain介绍
作者其他创作
大纲/内容
整体介绍
开源大语言模型(LLM)开发框架,支持开发者快速搭建基于大语言模型的应用
具备多轮对话、文档分析&提取摘要、问答等能力
具备多轮对话、文档分析&提取摘要、问答等能力
22年10月发布,支持Python和JS,开发者:Harrison Chase
获得2笔共3千万美元融资,估值2亿美元
官网:https://langchain.com/
博客:https://blog.langchain.dev/
获得2笔共3千万美元融资,估值2亿美元
官网:https://langchain.com/
博客:https://blog.langchain.dev/
https://github.com/hwchase17/langchain
46k stars,1M downloads/month
46k stars,1M downloads/month
模块化组件 + 非常多组合用例
models
prompts
Answer the following questions as best you can. You have access to the following tools:
search: a search engine. useful for when you need to answer questions about current
events. input should be a search query.
calculator: useful for getting the result of a math expression. The input to this
tool should be a valid mathematical expression that could be executed
by a simple calculator.
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 [search, calculator]
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
Begin!
Question: ${question}
Thought:
search: a search engine. useful for when you need to answer questions about current
events. input should be a search query.
calculator: useful for getting the result of a math expression. The input to this
tool should be a valid mathematical expression that could be executed
by a simple calculator.
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 [search, calculator]
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
Begin!
Question: ${question}
Thought:
indexes
chains
agents
服务化结构化
对openai的服务做封装,具备失败重试等能力
prompt模版化,变量可控制,eg.“翻译成{语言}”
只需配置输出数据协议,即可通过内置的“json输出”prompt控制gpt的返回,且支持按协议解析返回的内容成结构化数据
The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "\`\`\`json" and "\`\`\`":
show code time
总结:能将chatgpt的对话式API变成带数据结构的APIgpt
多轮对话存储
普通对话存储
prompt设计
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Current conversation:
Human: Hi, my name is yangyang
AI: Hello yangyang, it's nice to meet you! I am an AI language model designed to have conversations with humans. How can I assist you today?
human: what's my name?
AI: Hello yangyang, it's nice to meet you! I am an AI language model designed to have conversations with humans. How can I assist you today?
human: what's my name?
对话对象设计
human / AI / System
human / AI / System
对话信息全存储
对话轮次可控
token数量可控
token数量根据不同llm有不同算法
总结存储
迭代总结 prompt
Progressively summarize the lines of conversation provided, adding onto the previous summary returning a new summary.
EXAMPLE
Current summary:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good.
New lines of conversation:
Human: Why do you think artificial intelligence is a force for good?
AI: Because artificial intelligence will help humans reach their full potential.
New summary:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good because it will help humans reach their full potential.
END OF EXAMPLE
Current summary:
{summary}
New lines of conversation:
{new_lines}
New summary:
EXAMPLE
Current summary:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good.
New lines of conversation:
Human: Why do you think artificial intelligence is a force for good?
AI: Because artificial intelligence will help humans reach their full potential.
New summary:
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good because it will help humans reach their full potential.
END OF EXAMPLE
Current summary:
{summary}
New lines of conversation:
{new_lines}
New summary:
带总结对话 prompt
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
{history}
Human: {input}
AI:
Current conversation:
{history}
Human: {input}
AI:
处理长文本,超出token数限制后很方便使用;总结能力很强,但是会丢失细节信息,然后可能会瞎编
其他
其他存储协议
向量数据存储
实体数据存储
多种存储一起使用
支持使用外部存储
chain
将多个结构化数据服务组合成更复杂的服务
基于文档的问答
原理
1,文档切块并向量化
2,query向量化并查找相似文档块
3,将相似文档块和query作为prompt发给LLM,让其给出答案
prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
system_template = """Use the following pieces of context to answer the users question.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
----------------
{context}"""
messages = [
SystemMessagePromptTemplate.from_template(system_template),
HumanMessagePromptTemplate.from_template("{question}"),
]
CHAT_PROMPT = ChatPromptTemplate.from_messages(messages)
PROMPT_SELECTOR = ConditionalPromptSelector(
default_prompt=PROMPT, conditionals=[(is_chat_model, CHAT_PROMPT)]
)
{context}
Question: {question}
Helpful Answer:"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
system_template = """Use the following pieces of context to answer the users question.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
----------------
{context}"""
messages = [
SystemMessagePromptTemplate.from_template(system_template),
HumanMessagePromptTemplate.from_template("{question}"),
]
CHAT_PROMPT = ChatPromptTemplate.from_messages(messages)
PROMPT_SELECTOR = ConditionalPromptSelector(
default_prompt=PROMPT, conditionals=[(is_chat_model, CHAT_PROMPT)]
)
几类方法
stuff method:
简单有效,将所有相似文档块一次都给LLM,缺点是容易超限
简单有效,将所有相似文档块一次都给LLM,缺点是容易超限
map_reduce method:
powerful,将文档块+query并行独立发给LLM,拿到一批结果后再用总结模型发给LLM得到最终答案,缺点是成本高
powerful,将文档块+query并行独立发给LLM,拿到一批结果后再用总结模型发给LLM得到最终答案,缺点是成本高
refine method:
顺序执行所有文档块,依次将已有答案和当前文档块+query发给LLM,生成新的答案,所有文档遍历完之后得到最终结果,缺点是只能依次进行,耗时长
顺序执行所有文档块,依次将已有答案和当前文档块+query发给LLM,生成新的答案,所有文档遍历完之后得到最终结果,缺点是只能依次进行,耗时长
map_rerank method:
将文档块+query并行发给LLM,并要求对答案打分,取分数最高的作为最终答案
将文档块+query并行发给LLM,并要求对答案打分,取分数最高的作为最终答案
评估
1,基于文档通过LLM生成问答
You are a teacher coming up with questions to ask on a quiz.
Given the following document, please generate a question and answer based on that document.
Example Format:
<Begin Document>
...
<End Document>
QUESTION: question here
ANSWER: answer here
These questions should be detailed and be based explicitly on information in the document. Begin!
<Begin Document>
{doc}
<End Document>
Given the following document, please generate a question and answer based on that document.
Example Format:
<Begin Document>
...
<End Document>
QUESTION: question here
ANSWER: answer here
These questions should be detailed and be based explicitly on information in the document. Begin!
<Begin Document>
{doc}
<End Document>
2,再将文档和query向LLM提问
System: Use the following pieces of context to answer the users question.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
----------------
{content}
Human: {query}
If you don't know the answer, just say that you don't know, don't try to make up an answer.
----------------
{content}
Human: {query}
3,将query,正确答案,回答给到LLM,生成打分
You are a teacher grading a quiz.
You are given a question, the student's answer, and the true answer, and are asked to score the student answer as either CORRECT or INCORRECT.
Example Format:
QUESTION: question here
STUDENT ANSWER: student's answer here
TRUE ANSWER: true answer here
GRADE: CORRECT or INCORRECT here
Grade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin!
QUESTION: {query}
STUDENT ANSWER: {result}
TRUE ANSWER: {answer}
GRADE:
You are given a question, the student's answer, and the true answer, and are asked to score the student answer as either CORRECT or INCORRECT.
Example Format:
QUESTION: question here
STUDENT ANSWER: student's answer here
TRUE ANSWER: true answer here
GRADE: CORRECT or INCORRECT here
Grade the student answers based ONLY on their factual accuracy. Ignore differences in punctuation and phrasing between the student answer and true answer. It is OK if the student answer contains more information than the true answer, as long as it does not contain any conflicting statements. Begin!
QUESTION: {query}
STUDENT ANSWER: {result}
TRUE ANSWER: {answer}
GRADE:
tips:langchain.debug = True可以输出过程信息
代理(插件)
llm-math
agent("What is the 25% of 300?")
Thought: We need to calculate 25% of 300, which involves multiplication and division.
Action:
```
{
"action": "Calculator",
"action_input": "300*0.25"
}
```
Observation: Answer: 75.0
Thought:We have the answer to the question.
Final Answer: 75.0
Action:
```
{
"action": "Calculator",
"action_input": "300*0.25"
}
```
Observation: Answer: 75.0
Thought:We have the answer to the question.
Final Answer: 75.0
wikipedia
Tom M. Mitchell is an American computer scientist
and the Founders University Professor at Carnegie Mellon University (CMU)
what book did he write?
and the Founders University Professor at Carnegie Mellon University (CMU)
what book did he write?
Thought: I should use Wikipedia to find the answer to this question.
Action:
```
{
"action": "Wikipedia",
"action_input": "Tom M. Mitchell"
}
```
Observation:
Page: Tom M. Mitchell
Summary: Tom Michael Mitchell (born August 9, 1951) is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU).
...
Page: Tom Mitchell (Australian footballer)
Summary: Thomas Mitchell (born 31 May 1993) is a professional Australian rules footballer playing for the Collingwood Football Club in the Australian Football League (AFL).
...
Action:
```
{
"action": "Wikipedia",
"action_input": "Tom M. Mitchell"
}
```
Observation:
Page: Tom M. Mitchell
Summary: Tom Michael Mitchell (born August 9, 1951) is an American computer scientist and the Founders University Professor at Carnegie Mellon University (CMU).
...
Page: Tom Mitchell (Australian footballer)
Summary: Thomas Mitchell (born 31 May 1993) is a professional Australian rules footballer playing for the Collingwood Football Club in the Australian Football League (AFL).
...
Thought:The book that Tom M. Mitchell wrote is "Machine Learning".
Action:
```
{
"action": "Wikipedia",
"action_input": "Machine Learning (book)"
}
```
Observation: Page: Machine learning
Summary: Machine learning (ML) is a field devoted to understanding and building methods that let machines "learn"
...
Page: Timeline of machine learning ...
Page: Quantum machine learning ...
Action:
```
{
"action": "Wikipedia",
"action_input": "Machine Learning (book)"
}
```
Observation: Page: Machine learning
Summary: Machine learning (ML) is a field devoted to understanding and building methods that let machines "learn"
...
Page: Timeline of machine learning ...
Page: Quantum machine learning ...
Thought:Tom M. Mitchell wrote the book "Machine Learning".
Final Answer: Machine Learning
Final Answer: Machine Learning
PythonREPLTool
customer_list = [["Harrison", "Chase"],
["Lang", "Chain"],
["Dolly", "Too"],
["Elle", "Elem"],
["Geoff","Fusion"],
["Trance","Former"],
["Jen","Ayai"]
]
agent.run(f"""Sort these customers by \
last name and then first name \
and print the output: {customer_list}""")
["Lang", "Chain"],
["Dolly", "Too"],
["Elle", "Elem"],
["Geoff","Fusion"],
["Trance","Former"],
["Jen","Ayai"]
]
agent.run(f"""Sort these customers by \
last name and then first name \
and print the output: {customer_list}""")
I can use the sorted() function to sort the list of customers by last name and then first name. I will need to provide a key function to sorted() that returns a tuple of the last name and first name in that order.
Action: Python REPL
Action Input:
```
customers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]
sorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))
for customer in sorted_customers:
print(customer)
```
Observation: ['Jen', 'Ayai']
['Lang', 'Chain']
['Harrison', 'Chase']
['Elle', 'Elem']
['Trance', 'Former']
['Geoff', 'Fusion']
['Dolly', 'Too']
Thought:
The customers have been sorted by last name and then first name, and the output has been printed.
Final Answer: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]
> Finished chain.
Out[11]: "[['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]"
Action: Python REPL
Action Input:
```
customers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]
sorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))
for customer in sorted_customers:
print(customer)
```
Observation: ['Jen', 'Ayai']
['Lang', 'Chain']
['Harrison', 'Chase']
['Elle', 'Elem']
['Trance', 'Former']
['Geoff', 'Fusion']
['Dolly', 'Too']
Thought:
The customers have been sorted by last name and then first name, and the output has been printed.
Final Answer: [['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]
> Finished chain.
Out[11]: "[['Jen', 'Ayai'], ['Lang', 'Chain'], ['Harrison', 'Chase'], ['Elle', 'Elem'], ['Trance', 'Former'], ['Geoff', 'Fusion'], ['Dolly', 'Too']]"
过程
chain:AgentExecutor
chain:LLMChain
llm:ChatOpenAI
Human: You are an agent designed to write and execute python code to answer questions.
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question.
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
Python REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.
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 [Python REPL]
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
Begin!
Question: Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]
Thought:
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question.
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
Python REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.
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 [Python REPL]
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
Begin!
Question: Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]
Thought:
tool:Python REPL
chain:LLMChain
llm:ChatOpenAI
Human: You are an agent designed to write and execute python code to answer questions.
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question.
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
Python REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.
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 [Python REPL]
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
Begin!
Question: Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]
Thought:I can use the sorted() function to sort the list of customers by last name and then first name. I will need to provide a key function to sorted() that returns a tuple of the last name and first name in that order.
Action: Python REPL
Action Input:
```
customers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]
sorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))
for customer in sorted_customers:
print(customer)
```
Observation: ['Jen', 'Ayai']
['Lang', 'Chain']
['Harrison', 'Chase']
['Elle', 'Elem']
['Trance', 'Former']
['Geoff', 'Fusion']
['Dolly', 'Too']
Thought:
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question.
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
Python REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.
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 [Python REPL]
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
Begin!
Question: Sort these customers by last name and then first name and print the output: [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]
Thought:I can use the sorted() function to sort the list of customers by last name and then first name. I will need to provide a key function to sorted() that returns a tuple of the last name and first name in that order.
Action: Python REPL
Action Input:
```
customers = [['Harrison', 'Chase'], ['Lang', 'Chain'], ['Dolly', 'Too'], ['Elle', 'Elem'], ['Geoff', 'Fusion'], ['Trance', 'Former'], ['Jen', 'Ayai']]
sorted_customers = sorted(customers, key=lambda x: (x[1], x[0]))
for customer in sorted_customers:
print(customer)
```
Observation: ['Jen', 'Ayai']
['Lang', 'Chain']
['Harrison', 'Chase']
['Elle', 'Elem']
['Trance', 'Former']
['Geoff', 'Fusion']
['Dolly', 'Too']
Thought:
自定义插件
写个获取当天日期的方法
query:whats the date today?
Thought: I need to use the `time` tool to get today's date.
Action:
```
{
"action": "time",
"action_input": ""
}
```
Observation: 2023-06-07
Thought:I have successfully retrieved today's date using the `time` tool.
Final Answer: Today's date is 2023-06-07.
Action:
```
{
"action": "time",
"action_input": ""
}
```
Observation: 2023-06-07
Thought:I have successfully retrieved today's date using the `time` tool.
Final Answer: Today's date is 2023-06-07.
0 条评论
下一页