LLMChain使用初探 -- OLLaMA+LangChain搭建本地大模型

本文介绍了如何使用LLMChain创建一个基于用户输入的智能链,通过提示模板从LLM(如Qwen:7B、Llama2等)获取定制化的公司名称建议。OLLaMA提供了多种预训练和微调模型,适用于不同场景。
摘要由CSDN通过智能技术生成

LLMChain是一个简单的链,接受一个提示模板,使用用户输入格式化它并从LLM返回响应。
其中,prompt_template是一个非常关键的组件,可以让你创建一个非常简单的链,它将接收用户输入,使用它格式化提示,然后将其发送到LLM。
在这里插入图片描述

1. 配置OLLaMA

在使用LLMChain之前,需要先配置OLLaMA,OLLaMA可以运行本地大语言模型,我下载了llama2、openhermes、solar、qwen:7b
在这里插入图片描述

Ollama 提供了多个模型,每个都有其特点和适用场景:

  • Llama 2:这是一个预训练的大型语言模型,具有7B、13B和70B三种不同规模的模型。Llama 2增加了预训练语料,上下文长度从2048提升到4096,使得模型能够理解和生成更长的文本。
  • OpenHermes:这个模型专注于代码生成和编程任务,适合用于软件开发和脚本编写等场景。
  • Solar:这是一个基于Llama 2的微调版本,专为对话场景优化。Solar在安全性和有用性方面进行了人工评估和改进,旨在成为封闭源模型的有效替代品。
  • Qwen:7B:这是一个中文微调过的模型,特别适合处理中文文本。它需要至少8GB的内存进行推理,推荐配备16GB以流畅运行。

综上所述,这些模型各有侧重点,用户可以根据自己的需求选择合适的模型进行使用。

1.1 安装

ollama官网 https://ollama.com/
windows直接下载 - 无脑安装即可
在这里插入图片描述

1.2 下载模型

以通义千问模型为例:

# ollama run 模型名
ollama run qwen:7b

模型名见 https://ollama.com/library

第一次下载时间长点,后面再运行就不用下载了,直接进入对话环境。
在这里插入图片描述

2. langchain

实现目标:创建LLM链。假设我们想要创建一个公司名字

英文版

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import Ollama

prompt_template = "What is a good name for a company that makes {product}?"

ollama_llm = Ollama(model="qwen:7b")
llm_chain = LLMChain(
    llm = ollama_llm,
    prompt = PromptTemplate.from_template(prompt_template)
)
print(llm_chain("colorful socks"))

中文版

from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.llms import Ollama

prompt_template = "请给制作 {product} 的公司起个名字,只回答公司名即可"

ollama_llm = Ollama(model="qwen:7b")
llm_chain = LLMChain(
    llm = ollama_llm,
    prompt = PromptTemplate.from_template(prompt_template)
)
print(llm_chain("袜子"))
# print(llm_chain.run("袜子"))    # 加个.run也可
输出:{'product': '袜子', 'text': '"袜界精品"'}
print(llm_chain.predict("袜子"))
输出:袜梦工坊

runpredict的区别是

  • llm_chain.run:结合 输入{product} 和 大模型输出内容一起输出
  • llm_chain.predict :只给出大模型输出内容

来看下其他3个模型的回答,没有GPU,运行泰慢啦!

  • llama2(20min)

    prompt_template = "请给制作 {product} 的公司起个名字,只回答公司名即可"
    
    ollama_llm = Ollama(model="llama2")
    llm_chain = LLMChain(
    llm = ollama_llm,
    prompt = PromptTemplate.from_template(prompt_template)
    )
    llm_chain("袜子")
    
    {'product': '袜子',
    'text': 'Certainly! Here are some name suggestions for a company that manufactures socks:1. SoleMates - A play on the phrase "soul mates," suggesting that the socks are perfect companions for your feet.\n2. Footloose & Co. - A nod to the classic song and dance, and also highlighting the company\'s focus on providing loose-fitting socks.\n3. HipHuggers - A playful name that evokes the idea of the socks hugging your hips and providing comfort.\n4. ToeTastic - A fun, catchy name that suggests the socks are amazing and worth getting excited about.\n5. SoxAppeal - A clever name that combines "socks" and "appeal," implying that the company\'s products are appealing and desirable.\n6. AnkleAdorers - A name that suggests the socks are adorable and lovable, and also highlights their focus on providing ankle-friendly fits.\n7. FootFlair - A playful name that suggests the socks offer a touch of flair and personality to your outfit.\n8. SockSational - A cheesy but fun name that suggests the socks are remarkable and exceptional.\n9. HeelHappiness - A name that implies the socks will bring happiness to your heels and overall foot health.\n10. ToeTasticToes - A playful name that combines "toes" and "fantastic," suggesting that the socks are fantastic for your toes and overall foot comfort.'}
    
  • openhermes(13min)

    {'product': '袜子', 'text': '凯戴缝袜公司 (Kaida Gao Nu Gong Si)'}
    
  • solar(48min)

    {'product': '袜子',
    'text': '一格袜子 (Yi Ge Wu Zi) could be a company name for producing gloves. The characters used are in simplified Chinese and translate to "One Glove" in English.'}
    
  • 27
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
AUTOSAR(AUTomotive Open System ARchitecture)是一种用于汽车电子系统开发的标准化架构。EB tresos是一个用于AUTOSAR的开发工具,包含多个模块用于系统配置、运行时间环境和工具链的管理。 EB tresos具有强大的配置管理功能。用户可以在该工具中定义ECU(电控单元)的软硬件组成以及它们之间的通信。用户可以定义和配置ARXML文件,其中包含了ECU的所有配置信息。此外,EB tresos还提供了可视化的界面,使用户可以直观地添加、修改和删除ECU组件以及ECU之间的连接。 EB tresos还提供了强大的运行时间环境管理功能。用户可以配置ECU的运行时间环境和调度策略。EB tresos支持多核处理器,可以根据任务的优先级和执行时间要求进行任务调度。用户可以定义任务、事件和资源,并将它们映射到不同的核心或处理器。EB tresos还支持多任务管理和任务优先级的管理。 EB tresos还提供了一套完整的工具链,用于AUTOSAR代码的生成、编译和调试。用户可以在EB tresos中定义和配置AUTOSAR构件,并通过该工具链生成可执行代码。生成的代码遵循AUTOSAR标准,可以与其他AUTOSAR平台兼容。 综上所述,EB tresos是一个功能强大的AUTOSAR开发工具,提供了全面的配置管理、运行时间环境管理和工具链支持。它能够帮助开发人员更容易地开发和管理AUTOSAR系统,并提高系统的可靠性和性能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值