Langchain集成管理prompt功能详解
作者:AudreyXu 发布时间:2022-12-13 22:56:31
LangChain是什么 如何使用
经过了chatGPT,大家都知道了prompt-based learning,也明白了prompt在生成式模型的重要性。假设问答任务要用prompt A, 摘要生成任务要用prompt B,那么如何管理不同的prompt呢?
Langchain主要的功能就是集成管理prompt。
安装
pip install langchain
一、需要大语言模型
使用langchain需要使用一个大语言模型。这个模型可以用openai的gpt-turbo-3.5,也可以用Hugging face hub里面的大模型。
用这些大模型就需要调用他们的api,所以就要去这些网站生成相应的token。
二、LangChain的模块
LangChain提供了许多模块,可以用于构建语言模型应用程序。这些模块可以组合在一起创建更复杂的应用程序,也可以单独用于简单的应用程序。
LangChain主要有以下模块
1. LLM:从语言模型中输出预测结果
例子:基于公司产品生成公司名称
# 导入LLM包装器。
from langchain.llms import OpenAI
# 初始化包装器,temperature越高结果越随机
llm = OpenAI(temperature=0.9)
# 进行调用
text = "What would be a good company name for a company that makes colorful socks?"
print(llm(text))
2. Prompt Templates: 管理LLMs的Prompts
一般来说我们不会直接把输入给模型,而是将输入和一些别的句子连在一起,形成prompts之后给模型。
例如之前根据产品取名的用例,在实际服务中我们可能只想输入"socks",那么"What would be a good company name for a company that makes"就是我们的template。
from langchain.prompts import PromptTemplate
prompt = PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)
那么,对于模型来说,真正的输入就是
print(prompt.format(product="colorful socks"))
What is a good name for a company that makes colorful socks?
3. Chains:将LLMs和prompts结合起来
很容易想到,我们的模型有很多,prompts也有很多,那么需要把他们组装起来,这就是Chains做的事情。
一个Chain包含一个Template和一个模型。例如LLMChain,就包含一个PromptTemplate和一个LLM。
这样我们的例子就可以
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)
我们可以创建一个LLMChain,然后将llm和prompt给chain。
from langchain.chains import LLMChain
chain = LLMChain(llm=llm, prompt=prompt)
然后可以运行这个chain
chain.run("colorful socks")
Socktastic!'
4. Agents:基于用户输入动态地调用chains
关于Agents,需要理解以下的概念:
Tool:输入是一个string,输出是一个string,作用是做某个特定任务。这个任务可以是做搜索、查数据库或者Python REPL.
LLM:语言模型
Agent:要使用的代理。这应该是一个字符串,引用一个支持代理类。这里就是调用其他服务的API。
这里有一个例子。假设想知道Taylor Swift的男友是谁,并且求出他的年龄的3次方。
from langchain.agents import laod_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
import os
os.environ["OPENAI_API_KEY"] = "xxxxxxxx"
os.environ["SERPAPI_API_KEY"] ="yyyyyyyy"
# 导入llm模型
llm = OpenAI(temperature=0)
# 导入一些tools,这里倒入serpapi和llm-math
# SerpApi是一个付费提供搜索结果API的第三方服务提供商。它允许用户通过简单的API调用访问各种搜索引擎的搜索结果,包括Google、Bing、Yahoo、Yandex等。
# llm-math是langchain里面的能做数学计算的模块
tools = load_tools(["serpapi", "llm-math"], llm=llm)
# 初始化tools,models 和使用的agent
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
# 输出结果
agent.run("Who isTaylor's boyfriend? What is his current age raised to the 3 power?")
输出
> Entering new AgentExecutor chain...
I need to find out who Taylor Swift's boyfriend is and then calculate his age raised to the 3 power.
Action: Search
Action Input: "Taylor Swift boyfriend"
Observation: Taylor Swift's romance with actor Joe Alwyn is her most serious yet secretive to date. Since 2016, their famously private relationship has ...
Thought: I need to find out Joe Alwyn's age.
Action: Search
Action Input: "Joe Alwyn age"
Observation: 32 years
Thought: I need to calculate 32 raised to the 3 power.
Action: Calculator
Action Input: 32^3
Observation: Answer: 32768
Thought: I now know the final answer.
Final Answer: Taylor Swift's boyfriend is Joe Alwyn and his current age raised to the 3 power is 32768.
分析这个输出可以知道,它的思路很清晰。
它的动作包括:
读题:Thought(理解题意)
执行:Action(做什么)、Action Input(输入是什么)、Observation(输出是什么)
总结:Final Answer(最终输出)
每一个输出之后紧跟着一个Thought,思考下一步做什么,如果发现任务全部完成就输出最终答案。
5. Memory
如果想做一个聊天机器人,那么要求机器人有短暂的记忆,记住对话的历史。
Langchain的ConversationChain就提供这样一个功能。
默认情况下,ConversationChain具有一种简单类型的内存,它会记住所有先前的输入/输出并将它们添加到传递的上下文中。
# ConversationChain用法
from langchain import OpenAI, ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True) # (将verbose设置为True,以便我们可以看到提示)
conversation.predict(input="Hi there!")
输出
> Entering new chain...
Prompt after formatting:
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:
Human: Hi there!
AI:> Finished chain.
' Hello! How are you today?
遇到的错误
ImportError: cannot import name 'load_tools' from 'langchain.agents'
我用的是python3.7,然后将python版本升级到了3.9就解决了。
参考
https://langchain.readthedocs.io/en/latest/getting_started/getting_started.html
来源:https://www.cnblogs.com/AudreyXu/p/17233964.html
猜你喜欢
- python对多国语言的处理是支持的很好的,它可以处理现在任意编码的字符,这里深入的研究一下python对多种不同语言的处理。有一点需要清楚
- 一次性验证码,英文是 One Time Password,简写为 OTP,又称动态密码或单次有效密码,是指计算机系统或其他数字设备上只能使用
- 本文实例讲述了PHP封装cURL工具类。分享给大家供大家参考,具体如下:CurlUtils工具类:<?php/** * cURL请求工
- 业务需求我们需要一个微信小程序码,但是是需要提供给别人扫码的但是只有一个纯粹的小程序码是不好看的,所以需要推广的海报图片。再结合文字最终效果
- 大家都在关注视觉的盛宴,西方的美学;今天就分享下,中国最为古老的美,也是身边随处可见的美学–中国汉字书法之美;古文者,仓颉做造也。仰观奎星园
- 1.GAN简述在GAN中,有两个模型,一个是生成模型,用于生成样本,一个是判别模型,用于判断样本是真还是假。但由于在GAN中,使用的JS散度
- 一、插补查找算法插补查找算法又称为插值查找,它是折半查找算法的改进版。插补查找是按照数据的分布,利用公式预测键值所在的位置,快速缩小键值所在
- Twig是一款快速、安全、灵活的PHP模板引擎,它内置了许多filter和tags,并且支持模板继承,能让你用最简洁的代码来描述你的模板。他
- 前言我们在日常开发中,经常会遇到一些关于文件的操作,例如,实现查看目录内容的功能。类似Linux下的tree命令。统计目录下指定后缀文件的行
- 五子棋游戏相信大部分人都玩过,今天我们用python来实现一次具体代码可以访问我的GitHub地址获取构建五子棋棋盘from collect
- 写在之前我们都知道 Python 中内置了许多标准的数据结构,比如列表,元组,字典等。与此同时标准库还提供了一些额外的数据结构,我们可以基于
- 如何做一个文本搜索? 比较简单,见下:<%Head = "搜索"SearchStri
- 源代码如下:#-*- coding:utf-8 -*- def check_exsit(process_name): import win3
- 因为函数或类都是对象,它们也能被四处传递。它们又是可变对象,可以被更改。在函数或类对象创建后但绑定到名字前更改之的行为为装饰(decorat
- 继Go 1.18支持泛型后,Go 将在下个版本中支持pdqsort排序算法再次引起了开发者们的热切讨论。目前,Go仓库的最新commit中提
- 引用是什么在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量
- 本文通过Python3+PyQt5实现自定义部件–分数滑块。它既能支持键盘也支持鼠标,使用物理(视口)坐标通过绘制方式显示。#!/usr/b
- 内部存储格式:世纪、年、月、日、小时、分钟、秒默认格式是:DD-MON-RR。SYSDATE 返回当前的系统时间。SELECT S
- 程序中经常需要使用excel文件,批量读取文件中的数据python读取excel文件可以使用xlrd模块pip install xlrd安装
- 本文实例讲述了Python扫描IP段查看指定端口是否开放的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/local/bin/p