❝
LangChain 是一个用于构建基于语言模型的应用程序的框架。它旨在简化与大型语言模型(如 ChatGPT、LLaMA 等)的交互,并提供一系列工具和组件来帮助开发者快速构建复杂的应用。LangChain 的设计目标是提高开发效率,使得开发者可以更专注于应用逻辑,而不是底层的技术细节。
❞
-
langchain-core
:基础抽象和 `表达式语言。 -
langchain-community
:第三方集成。 -
合作伙伴包(例如 langchain-openai
、langchain-anthropic
等):某些集成已进一步拆分为自己的轻量级包,这些包仅依赖于langchain-core
-
langchain
:构成应用认知架构的链、代理和检索策略。 -
LangGraph
:通过将步骤建模为图中的边和节点,构建强大且有状态的多参与者 LLM 应用。与LangChain
无缝集成,但也可以单独使用。 -
LangServe
:将LangChain
链部署为REST API
。 -
LangSmith
:一个开发者平台,可让您调试、测试、评估和监控 LLM 应用。

提示模版
❝
LangChain 的提示词模板(Prompt Template)是用于构建和管理提示词的工具,能够提高与大型语言模型交互的效率。首先需要安装
langchain_coze
❞
pip install langchain_coze
PromptTemplate
基本的字符串提示模板,用于生成简单的文本提示。它允许开发者定义一个基本的文本结构,并用输入变量进行格式化。
# 从 langchain_core.prompts 模块导入 PromptTemplate 类
from langchain_core.prompts import PromptTemplate
# 创建一个基本的提示模板
# PromptTemplate 是一个类,用于定义和格式化提示模板
# input_variables 是一个列表,包含模板中的变量名
# template 是一个字符串,定义了提示的格式,其中包含占位符 {name} 和 {age}
template = PromptTemplate(
input_variables=["name", "age"], # 定义模板中的变量名
template="My name is {name} and I am {age} years old." # 定义提示的格式
)
# 格式化提示
# 使用 format 方法将变量值填入模板中
# name 变量的值是 "Alice",age 变量的值是 30
prompt = template.format(name="Alice", age=30)
# 打印格式化后的提示
# 输出: My name is Alice and I am 30 years old.
print(prompt)
ChatPromptTemplate
用于聊天模型的提示模板,支持多角色消息的组合和管理。适用于需要模拟对话的场景。
# 从 langchain_core.prompts 模块导入 ChatPromptTemplate 类
from langchain_core.prompts import ChatPromptTemplate
# 创建聊天提示模板
# ChatPromptTemplate 是一个类,用于定义和格式化聊天提示模板
# messages 是一个列表,包含多个消息模板
# 每个消息模板都是一个元组,包含消息的角色和内容
template = ChatPromptTemplate([
("system", "You are a helpful AI bot. Your name is {name}."), # 系统消息,包含占位符 {name}
("human", "Hello, how are you doing?"), # 用户消息
("ai", "I'm doing well, thanks!"), # AI 消息
("human", "{user_input}"), # 用户消息,包含占位符 {user_input}
])
# 调用模板并传入参数
# 使用 invoke 方法将变量值填入模板中
# name 变量的值是 "Bob",user_input 变量的值是 "What is your name?"
prompt_value = template.invoke(
{
"name": "Bob",
"user_input": "What is your name?"
}
)
# 输出结果
# 打印格式化后的消息
print(prompt_value)
FewShotPromptTemplate
包含少量示例的提示模板,帮助模型更好地理解任务。适用于需要示例来引导模型的场景。
# 从 langchain_core.prompts 模块导入 FewShotPromptTemplate 和 PromptTemplate 类
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate
# 定义示例提示
# PromptTemplate 是一个类,用于定义和格式化提示模板
# template 参数是一个字符串,定义了提示的格式,其中包含占位符 {animal} 和 {characteristic}
# input_variables 参数是一个列表,包含模板中的变量名
example_prompt = PromptTemplate(
template="The {animal} is known for its {characteristic}.",
input_variables=["animal", "characteristic"]
)
# 创建 FewShotPromptTemplate
# FewShotPromptTemplate 是一个类,用于定义和格式化少样本提示模板
# example_prompt 参数是一个 PromptTemplate 对象,定义了示例提示的格式
# examples 参数是一个列表,包含多个示例,每个示例是一个字典,包含变量名和值
# prefix 参数是一个字符串,定义了提示的前缀
# suffix 参数是一个字符串,定义了提示的后缀
# example_separator 参数是一个字符串,定义了示例之间的分隔符
few_shot_template = FewShotPromptTemplate(
example_prompt=example_prompt,
examples=[
{"animal": "dog", "characteristic": "loyalty"},
{"animal": "cat", "characteristic": "independence"},
{"animal": "elephant", "characteristic": "intelligence"},
],
prefix="Here are some facts about animals:",
suffix="Based on the examples above, what can you tell me about a rabbit?",
example_separator="nn"
)
# 使用格式化方法生成提示
# 使用 format 方法将变量值填入模板中
# animal 变量的值是 "rabbit",characteristic 变量的值是 "playfulness"
formatted_prompt = few_shot_template.format(animal="rabbit", characteristic="playfulness")
# 打印格式化后的提示
print(formatted_prompt)
调用模型
❝
Langchain支持的国内模型包括月之暗面科技的moonshot系列、百川智能的baichuan系列、阿里云的通义千问、智谱AI的glm-4以及讯飞星火3.0等。首先要下载
langchain_community
。❞
pip install langchain_community
智谱
目前glm-4-flash
是免费的,可以使用glm-4-flash
进行学习。
# 导入必要的模块和类
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.prompts import PromptTemplate
import os
# 获取智谱AI的API密钥
# 从环境变量中获取智谱AI的API密钥
api_key = os.environ.get("ZHIPUAI_API_KEY")
# print(api_key) # 可以打印API密钥进行调试,但在生产环境中不建议打印敏感信息
# 创建ChatZhipuAI模型
# 使用智谱AI的GLM模型,并传入API密钥、模型名称、温度和最大令牌数
model = ChatZhipuAI(api_key=api_key, model="glm-4-flash", temperature=0.7, max_tokens=1024)
# 创建PromptTemplate模板
# 定义提示模板,包含占位符 {text}
template = "请用英语翻译以下句子:{text}"
# 创建PromptTemplate对象,传入模板和输入变量
prompt_template = PromptTemplate(template=template, input_variables=["text"])
# 格式化提示模板
# 使用format方法将变量值填入模板中
my = prompt_template.format(text="你好,世界!")
# 调用模型生成响应
# 使用invoke方法将格式化后的提示传递给模型,并获取响应
response = model.invoke(my)
# 打印模型的响应内容
print(response.content)
然后ChatZhipu
中添加streaming=True
参数启用流式传输功能,然后输出写一下代码
# 流式传输响应
for response in model.stream(my):
print(response.content)
其他模型AI大模型列表
模型名称 | 简要介绍 |
---|---|
AzureChatOpenAI | 微软Azure平台上的OpenAI服务 |
BedrockChat | AWS Bedrock平台的聊天模型 |
ChatAnthropic | Anthropic公司的Claude大模型 |
ChatAnyscale | 提供可扩展的AI模型服务平台 |
ChatBaichuan | 百川智能的大语言模型 |
ChatCohere | Cohere提供的自然语言处理模型 |
ChatCoze | Coze平台的聊天模型 |
ChatOctoAI | OctoAI提供的AI模型服务 |
ChatDatabricks | Databricks平台的聊天模型 |
ChatDeepInfra | DeepInfra提供的AI模型服务 |
ChatEdenAI | EdenAI平台的聊天模型 |
ChatEverlyAI | EverlyAI提供的AI服务 |
ChatFireworks | Fireworks AI平台 |
ChatFriendli | Friendli提供的AI服务 |
ChatGooglePalm | Google PaLM大语言模型 |
ChatHuggingFace | Hugging Face平台的模型 |
ChatHunyuan | 腾讯混元大模型 |
ChatJavelinAIGateway | Javelin AI Gateway服务 |
ChatKinetica | Kinetica提供的AI模型 |
ChatKonko | Konko AI平台 |
ChatLiteLLM | LiteLLM轻量级模型服务 |
ChatLiteLLMRouter | LiteLLM路由服务 |
ChatMLX | Apple MLX框架的模型 |
ChatMLflowAIGateway | MLflow AI网关 |
ChatMaritalk | Maritalk聊天模型 |
ChatMlflow | MLflow平台的模型 |
ChatNebula | Nebula AI平台 |
ChatOCIGenAI | Oracle Cloud Infrastructure生成式AI |
ChatOllama | Ollama本地运行的开源模型 |
ChatOpenAI | OpenAI的GPT模型 |
ChatPerplexity | Perplexity AI搜索模型 |
ChatPremAI | PremAI平台的模型 |
ChatSparkLLM | 讯飞星火大模型 |
ChatSnowflakeCortex | Snowflake Cortex AI服务 |
ChatTongyi | 阿里通义千问大模型 |
ChatVertexAI | Google Vertex AI平台 |
ChatYandexGPT | Yandex的GPT模型 |
ChatYuan2 | 元语义Yuan 2.0模型 |
ChatZhipuAI | 智谱清言大模型 |
ChatLlamaCpp | Llama C++实现的模型 |
ErnieBotChat | 百度文心一言大模型 |
FakeListChatModel | 模拟聊天模型 |
GPTRouter | GPT路由服务 |
GigaChat | Sber的GigaChat模型 |
HumanInputChatModel | 人工输入模型 |
JinaChat | Jina AI的聊天模型 |
LlamaEdgeChatService | Llama Edge聊天服务 |
MiniMaxChat | MiniMax大模型 |
MoonshotChat | 月之暗面(Moonshot)大模型 |
PaiEasChatEndpoint | 阿里云PAI平台 |
PromptLayerChatOpenAI | PromptLayer增强的OpenAI模型 |
QianfanChatEndpoint | 百度千帆大模型平台 |
SolarChat | Upstage的Solar大模型 |
VolcEngineMaasChat | 火山引擎大模型服务 |
ChatYi | 零一万物Yi大模型 |
原文始发于微信公众号(索隆程序员):使用 LangChain 框架进行大模型应用开发 Ⅰ
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/305780.html