langchain学习记录(1)

一、安装

首先,使用以下命令安装 LangChain:
pip install langchain
或者使用conda虚拟环境安装(需要提前切换到相应的虚拟环境)
conda install langchain -c conda-forge
同时还需要安装你要使用的基础模型相应的库,我这里以openai为例,使用pip或者conda安装openaiSDK:
pip install openai # pip 安装
使用conda安装(需要提前切换到相应的虚拟环境):
conda install openai

二、 简单示例

以下是通过环境变量的方式设置openai的访问sk以及访问地址,同时通过简单的提示词实现语言翻译的功能:

import os

from langchain.chat_models import ChatOpenAI

# 将这里换成你的密钥
os.environ['OPENAI_API_KEY'] = 'sk-JgW7z************************CacvymQafuFKSV'
os.environ['OPENAI_API_BASE'] = 'https://api.openai.com/v1'

llm= ChatOpenAI(
    temperature=0.9,
)


# 样例语言,要翻译的目标语言,大模型可自动识别语言类型,并将translate(要翻译的文字翻译为样例的语言)
prompt = """
If you are a translator, I will provide you with a language sample and a paragraph of text in that language for you to translate:

Language Sample: {sample}
Text to be translated: {text}

You need to answer in the following format:

Final answer: Translated text based on the language source of the sample
"""

resp = llm.predict(prompt.format(**{'sample': 'what are you doing?', 'text': '你可真是个小可爱'}))
print(resp)

就是这么简单的几行便可以实现一个翻译机器人

三、 使用提示词模板

调用 LLM 是很好的第一步,但这仅仅是个开始。

通常在应用程序中使用 LLM 时,不会将用户输入直接发送到 LLM

相反,您可能接受用户输入并构造一个提示词,然后将其发送给 LLM

例如,在前一个示例中,我们可以通过一些简单的描述来指定大模型来帮我们做一些事,例如将你的一段文字翻译成你需要的目标语言。
接下来是通过利用LangChain内置的一些提示词工具,来完成相同的任务

首先让我们定义提示模板:

from langchain.prompts import PromptTemplate


template = """
If you are a translator, I will provide you with a language sample and a paragraph of text in that language for you to translate:

Language Sample: {sample}
Text to be translated: {text}

You need to answer in the following format:

Final answer: Translated text based on the language source of the sample
"""

prompt = PromptTemplate(
    input_variables=["sample", "text"],
    template=template,
)

到目前为止,我们已经自己处理了单独的 PromptTemplateLLM
但是,真正的应用程序不仅仅是一个,而是它们的组合。
LangChain,链是由链组成的,可以是 LLM 这样的原始链,也可以是其他链。最核心的链类型是 LLMChain,它由PromptTemplateLLM 组成。
扩展前面的示例,我们可以构造一个LLMChain,它接受用户输入,使用 PromptTemplate 对其进行格式化,然后将格式化后的响应传递给LLM
以下是完整代码:

import os

from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate


# 将这里换成你的密钥
os.environ['OPENAI_API_KEY'] = 'sk-JgW7z************************CacvymQafuFKSV'
os.environ['OPENAI_API_BASE'] = 'https://api.openai.com/v1'

template = """
If you are a translator, I will provide you with a language sample and a paragraph of text in that language for you to translate:

Language Sample: {sample}
Text to be translated: {text}

You need to answer in the following format:

Final answer: Translated text based on the language source of the sample
"""

llm = ChatOpenAI(
    temperature=0.9,
)

prompt = PromptTemplate(
    input_variables=["sample", "text"],
    template=template,
)

chain = LLMChain(llm=llm, prompt=prompt)

resp = chain.run({'sample': 'what are you doing?', 'text': '你可真是个小可爱'})
print(resp)

这篇文章先简单做个开始,后面接着写其他的用法,欢迎点赞收藏加关注

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值