【LangChain】LLM

28 篇文章 12 订阅

LangChain学习文档


概述

LLMChain 是一个简单的链,它围绕语言模型添加了一些功能。它在整个LangChain中广泛使用,包括在其他链和代理中。
LLMChainPromptTemplate语言模型(LLM 或聊天模型)组成。它使用提供的输入键值对(以及内存键值对,如果可用的话)作用于格式化提示模板,并将格式化后的字符串传递给 LLM 并返回 LLM 输出。

内容

from langchain import PromptTemplate, OpenAI, LLMChain

# 提示模板
prompt_template = "What is a good name for a company that makes {product}?"

llm = OpenAI(temperature=0)
llm_chain = LLMChain(
    llm=llm,
    prompt=PromptTemplate.from_template(prompt_template)
)
llm_chain("colorful socks")

结果:

    {'product': 'colorful socks', 'text': '\n\nSocktastic!'}

运行 LLM Chain 的其他方式

除了所有 Chain 对象共享的 __call__run 方法之外,LLMChain 还提供了几种调用链逻辑的方法:

apply

允许您针对输入列表运行链:

input_list = [
    {"product": "socks"},
    {"product": "computer"},
    {"product": "shoes"}
]

llm_chain.apply(input_list)

结果如下:

    [{'text': '\n\nSocktastic!'},
     {'text': '\n\nTechCore Solutions.'},
     {'text': '\n\nFootwear Factory.'}]

generate

generate 与 apply 类似,只是它返回一个 LLMResult 而不是字符串。 LLMResult 通常包含有用的生成,例如token使用情况和完成原因。

llm_chain.generate(input_list)

结果:

LLMResult(
generations=[
	[Generation(text='\n\nSocktastic!', generation_info={'finish_reason': 'stop', 'logprobs': None})], 
	[Generation(text='\n\nTechCore Solutions.', generation_info={'finish_reason': 'stop', 'logprobs': None})], 
	[Generation(text='\n\nFootwear Factory.', generation_info={'finish_reason': 'stop', 'logprobs': None})]], 
llm_output={'token_usage': {'prompt_tokens': 36, 'total_tokens': 55, 'completion_tokens': 19}, 'model_name': 'text-davinci-003'})

predict

Predictrun 方法类似,只不过输入参数不再是Python字典,而是关键字参数。

# Single input example
llm_chain.predict(product="colorful socks")

# 结果
    '\n\nSocktastic!'
# Multiple inputs example
# 多个输入的例子:
template = """Tell me a {adjective} joke about {subject}."""
prompt = PromptTemplate(template=template, input_variables=["adjective", "subject"])
llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0))

llm_chain.predict(adjective="sad", subject="ducks")

结果:

    '\n\nQ: What did the duck say when his friend died?\nA: Quack, quack, goodbye.'

解析输出(Parsing the outputs)

默认情况下,LLMChain 不会解析输出,即使底层提示对象具有输出解析器。如果您想在LLM输出上应用该输出解析器,请使用predict_and_parse而不是predict,使用apply_and_parse而不是apply

predict的例子:

from langchain.output_parsers import CommaSeparatedListOutputParser

output_parser = CommaSeparatedListOutputParser()
template = """List all the colors in a rainbow"""
prompt = PromptTemplate(template=template, input_variables=[], output_parser=output_parser)
llm_chain = LLMChain(prompt=prompt, llm=llm)

llm_chain.predict()

结果:

    '\n\nRed, orange, yellow, green, blue, indigo, violet'

predict_and_parser的例子:

llm_chain.predict_and_parse()

结果:

    ['Red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

从字符串中初始化

您还可以直接从字符串模板构造LLMChain。

template = """Tell me a {adjective} joke about {subject}."""
llm_chain = LLMChain.from_string(llm=llm, template=template)

llm_chain.predict(adjective="sad", subject="ducks")

结果:

    '\n\nQ: What did the duck say when his friend died?\nA: Quack, quack, goodbye.'

总结

本文主要讲述如何构建LLMChain,由:LLM和Prompt两个部分组成。

  1. 运行LLMChain的方法有:
方法名描述
run只打印一个key的value
apply输入允许传入List
generate与 apply 类似,只是它返回一个 LLMResult 而不是字符串。通常包含:token使用和完成情况
Predict与 run 方法类似,只不过输入键被指定为关键字参数而不是 Python 字典。
  1. 解析输出

apply() 改为 apply_and_parse()
predict() 改为 predict_and_parse()

  1. 直接用字符串初始化LLMChain,不用PromptTemplate(xxx)
template = """Tell me a {adjective} joke about {subject}."""
llm_chain = LLMChain.from_string(llm=llm, template=template)

参考地址:

https://python.langchain.com/docs/modules/chains/foundational/llm_chain

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值