langchain 入门指南 - JSON 形式输出大模型的响应

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站

在一些入门例子中,我们会发现,我们可以告诉 LLM 如何输出,然后输出的结果真的是我们想要的,比如下面这个例子:

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.5, max_tokens=200)

summarizing_prompt_template = """
输出为 JSON 格式,包含字段 content、summary。

总结以下文本为一个 20 字以内的句子:
---
{content}
"""
prompt = PromptTemplate.from_template(summarizing_prompt_template)

summarizing_chain = prompt | llm | StrOutputParser()

print(summarizing_chain.invoke({"content": "这是一个测试。"}))

在实际使用中,content 可能是一个很长的文本。

输出:

{
  "content": "这是一个测试。",
  "summary": "这是一个测试。"
}

正如某些例子上经常写的 “You are a helpful assistant”,其实从某种程度上来说,我们确实可以把 LLM 看作是我们的一名得力助手。
这名助手是可以理解我们说的话并作出回应的。

因此,我们就可以告诉 LLM,我们希望输出的格式是 JSON,然后我们可以在 JSON 中定义我们希望输出的字段。

langchain 中的 JSON 输出

在上面这个例子中,其实是等于我们给了 LLM 一个指令,告诉它我们希望输出的格式是 JSON,然后我们定义了 JSON 的格式。
既然很多时候我们都想要给我们的 LLM 一个指令,那为何不把这些逻辑固定下来呢?

为了解决这个问题,langchain 的 PromptTemplate 为我们提供了指定输出的指令的通用解决方案。

from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model_name="gpt-3.5-turbo",
    temperature=0.5,
    max_tokens=200
)

response_schemas = [
    ResponseSchema(name="content", description="The original content"),
    ResponseSchema(name="summary", description="The summary of the content"),
]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()

summarizing_prompt_template = """
{format_instructions}

总结以下文本为一个 20 字以内的句子:
---
{content}
"""
prompt = PromptTemplate.from_template(summarizing_prompt_template, partial_variables={'format_instructions': format_instructions})

summarizing_chain = prompt | llm | output_parser

print(summarizing_chain.invoke({"content": "这是一个测试。"}))

输出:

{
  "content": "这是一个测试。",
  "summary": "这是一个测试。"
}

说明:

  1. ResponseSchema 用于定义输出的字段,name 为字段名,description 为字段描述。这些信息是给 LLM 看的。LLM 会根据这些信息来输出我们想要的结果。
  2. partial_variables 用于传递部分变量给模板,剩下的变量会在调用 LLM 的时候再传递。

在上面这个例子中,我们实际传递给 LLM 的模板是:

The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```":

```json
{
	"content": string  // The original content
	"summary": string  // The summary of the content
}
```

总结以下文本为一个 20 字以内的句子:
---
这是一个测试。

这个模板告诉 LLM,我们希望输出的格式是 JSON,然后我们定义了 JSON 的格式。

总结

在 langchain 中,我们可以通过 ResponseSchema 来定义我们希望输出的字段,然后生成一个 prompt,传递给 LLM,让 LLM 知道我们希望输出的格式是什么。

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

白如意i

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

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

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

打赏作者

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

抵扣说明:

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

余额充值