赞
踩
LLMChain是一个简单的链,接受一个提示模板,使用用户输入格式化它并从LLM返回响应。
其中,prompt_template是一个非常关键的组件,可以让你创建一个非常简单的链,它将接收用户输入,使用它格式化提示,然后将其发送到LLM。
在使用LLMChain之前,需要先配置OLLaMA,OLLaMA可以运行本地大语言模型,我下载了llama2、openhermes、solar、qwen:7b
Ollama 提供了多个模型,每个都有其特点和适用场景:
综上所述,这些模型各有侧重点,用户可以根据自己的需求选择合适的模型进行使用。
ollama官网 https://ollama.com/
windows直接下载 - 无脑安装即可
以通义千问模型为例:
# ollama run 模型名
ollama run qwen:7b
模型名见 https://ollama.com/library
第一次下载时间长点,后面再运行就不用下载了,直接进入对话环境。
实现目标:创建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("袜子"))
输出:袜梦工坊
run
和 predict
的区别是
run
:结合 输入{product} 和 大模型输出内容一起输出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.'}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。