怎么看网站哪个公司做的,房地产微网站模板,简洁游戏企业网站,wordpress插件 占用内存LangChain 是什么 就是一个框架或者说是一个工具#xff0c;用来写 AI 应用。对#xff0c;没有错#xff01;AI小白也可以#xff0c;有手就行#xff01; LangChain有几个核心模块#xff1a;Models、Prompts、Chains、Indexes、Memory、Agents。 这篇主要介绍Models、… LangChain 是什么 就是一个框架或者说是一个工具用来写 AI 应用。对没有错AI小白也可以有手就行 LangChain有几个核心模块Models、Prompts、Chains、Indexes、Memory、Agents。 这篇主要介绍Models、Prompts这2个最基本的模块。 配置环境 开发之前需要先配置好开发环境变量我使用Python语言然后安装LangChain即可。这2项都可以通过pip 命令完成 pip install Pythonpip install langchain Models LangChain 最核心的功能就是能够集成各种大模型LLM后续所有的功能实现以及扩展都是基于此功能的基础之上。 云端大模型 API LangChain 官网列举了它能够集成调用的所有大模型如下图 几乎你在网上能看到的模型LangChain都支持。以 OpenAi 使用为例可以通过以下方式导入并创建OpenAi大模型 from langchain.llms import OpenAI
llm OpenAI() 上述代码中的llm 就是创建的OpenAi模型对象。接下来只要传入相应的Prompt(提示词)并调用invoke方法就可以拿到OpenAi大模型的输出如下 response llm.invoke(List the seven wonders of the world.)
print(response) List the seven wonders of the world. 就是传入大模型的Prompt(提示词), response就是调用大模型invoke方法之后的结果将结果打印出来如下 1. Great Pyramid of Giza
2. Hanging Gardens of Babylon
3. Temple of Artemis at Ephesus, Turkey
4. Statue of Zeus at Olympia, Greece
5. Colossus of Rhodes, Greece
6. Lighthouse of Alexandria, Egypt
7. Mausoleum at Halicarnassus, Turkey 本地 local 模型 除了 OpenAi 这些收费的大模型之外LangChain也可以集成本地大模型。以Meta公司的Llama举例。在LangChain官网的支持的大模型介绍中有如下描述 可以看出使用 Llama.cpp 加载本地的gguf格式大模型。 因此我们需要下载一个大模型到电脑本地我使用的是Mistral的免费模型可以在 HugginFace 网站中找到如下 下载好 gguf 格式大模型文件并保存后在同级目录下创建langchain_intro.py文件目录结构如下 然后在 langchain_intro.py 中导入 LlamaCpp并创建模型实例。如下 from langchain_community.llms import LlamaCpp# 声明大模型的路径
MODEL_NAME mistral-7b-instruct-v0.1.Q4_K_M.gguf# 加载本地 mistal 大模型
llm LlamaCpp(
model_pathMODEL_NAME,
n_gpu_layers40,
n_batch512, # Batch size for model processing
verboseFalse, # Enable detailed logging for debugging
) 创建好 llm 模型实例之后就可以构建模型需要的Prompt(提示词)。最终调用 llm 模型的 invoke 方法就可以获取大模型的返回结果了。如下 question What is a good name for a company that makes Chocolate Milk ?result llm.invoke(question)
print(result) 最终打印结果如下 This is an open-ended question, so there are many potential names that could work well depending on your specific vision and goals for the company. Here are some suggestions to get you started:1. Creamy Delight: This name plays off the creamy texture of chocolate milk and suggests a delicious treat.
2. Chocolate Bliss: A simple and straightforward name that captures the pleasure of enjoying chocolate milk.
3. Sweet Milk: This name emphasizes the sweetness of chocolate milk and could appeal to customers who enjoy a sweet drink.
4. Velvet Chocolate: The velvety texture of chocolate milk is highlighted in this name, which also suggests a rich and indulgent taste.
5. Rich and Creamy: This name emphasizes the richness and creaminess of chocolate milk, making it sound like a luxurious treat.
6. Milk Chocolate Dreams: This name plays off the idea of enjoying chocolate milk as a way to escape from everyday stresses and indulge in a sweet moment of bliss.
7. Cocoa Delight: A name that emphasizes the cocoa content of chocolate milk, which could appeal to customers who enjoy the rich taste of Prompt 在上面加载 LLM 的实例代码中直接使用了 What is a good name for a company that makes Chocolate Milk ? 来当做大模型的文本输入。 但是这种文本输入格式不具备灵活性。比如我们做的大模型应用是根据用户的输入来动态设置制作的产品也就是将 Chocolate Milk 设置为一个变量。 这就需要 LangChain 中的 Prompts 模块发挥作用。具体通过 PromptTemplate 来创建 Prompt 模版。如下所示 from langchain_community.llms import LlamaCpp
from langchain.prompts import PromptTemplateMODEL_NAME mistral-7b-instruct-v0.1.Q4_K_M.gguf# 加载本地 mistal 大模型
llm LlamaCpp(model_pathMODEL_NAME,n_gpu_layers40,n_batch512, # Batch size for model processingverboseFalse, # Enable detailed logging for debugging
)# 在 question 中添加占位符
question What is a good name for a company that makes {product}?# 构建 PromptTemplate
prompt PromptTemplate(input_variables[product],templatequestion,
) 可以看到对 question 进行了修改,主要是添加了占位符{product}。然后创建PromptTemplate实例并设置LangChain的第一个输入将占位符进行替换。 建好 PromptTemplate 之后就可以通过LangChain将 LLM 和 prompt 对象进行组合并调用大模型方法获取结果。如下 product_name candy
product_format prompt.format(productproduct_name)
print(final prompt is product_format)
# 使用 LLM Chain 组合 Prompt 和 LLM
llm_chain prompt | llm# 执行 llm_chain,并打印结果
answer llm_chain.invoke(product_name)
print(answer, \n) 可以看到设置了 productcandy。最终打印结果如下 使用 LangChain 实现简易聊天机器人 通过加载 Models并创建合适的Prompts。我们已经可以调用本地大模型获取相应的输出结果。可以在此基础上再加上循环接收用户输入。并将输入设置到 PromptTemplate 中并最终从本地大模型中获取结果。完整代码如下 from langchain_community.llms import LlamaCpp
from langchain.prompts import PromptTemplateMODEL_NAME mistral-7b-instruct-v0.1.Q4_K_M.gguf# 加载本地 mistal 大模型
llm LlamaCpp(model_pathMODEL_NAME,n_gpu_layers40,n_batch512, # Batch size for model processingverboseFalse, # Enable detailed logging for debugging
)# 在 question 中添加占位符
question What is a good name for a company that makes {product}?# 构建 PromptTemplate
prompt PromptTemplate(input_variables[product],templatequestion,
)# 使用 LLM Chain 组合 Prompt 和 LLM
llm_chain prompt | llmprint(请输入产品名称:)while True:# 由用户输入 product_name,并设置到PromptTemplate中product_name input( )product_format prompt.format(productproduct_name)print(final prompt is product_format)# 执行 llm_chain,并打印结果answer llm_chain.invoke(product_name)print(answer, \n) 运行效果如下 如果你喜欢本文 长按二维码关注