核心模块(Modules)
模型 I/O(ModelIO)
聊天(Chat)
结构输出(StructuredOutput)

结构化输出

让 LLM 返回结构化输出往往至关重要。这是因为 LLM 的输出经常用于下游应用,而这些应用需要特定的参数。让 LLM 可靠地返回结构化输出对此是必要的。

有几种不同的高层策略可用来实现这一点:

  • 提示(Prompting):即你(非常礼貌地)要求 LLM 以期望的格式(JSON、XML)返回输出。它的好处是适用于所有 LLM。不好之处在于无法保证 LLM 会以正确格式返回输出。
  • 函数调用(Function calling):即 LLM 经过微调,不仅能生成补全,还能生成函数调用。LLM 可以调用的函数通常作为额外参数传给模型 API。函数名称和描述应被视为提示的一部分(它们通常计入令牌数,并被 LLM 用来决定该做什么)。
  • 工具调用(Tool calling):一种与函数调用类似的技术,但允许 LLM 同时调用多个函数。
  • JSON 模式:即保证 LLM 返回 JSON。

不同的模型可能支持这些的不同变体,参数略有差异。为了让 LLM 轻松返回结构化输出,我们为 LangChain 模型加入了一个通用接口:.with_structured_output。

通过调用该方法(并传入一个 JSON schema 或 Pydantic 模型),模型会加入获取结构化输出所需的任何模型参数 + 输出解析器。实现方式可能不止一种(例如函数调用 vs JSON 模式)——你可以通过向该方法传参来配置使用哪种方式。

让我们看一些实际的例子!

我们将使用 Pydantic 来轻松构造响应 schema。

from langchain_core.pydantic_v1 import BaseModel, Field
 
 
class Joke(BaseModel):
    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")

OpenAI

OpenAI 提供了几种不同的方式来获得结构化输出。

API 参考 (opens in a new tab)

from langchain_openai import ChatOpenAI

工具/函数调用

默认情况下,我们会使用 function_calling。

model = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = model.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
Joke(setup='Why was the cat sitting on the computer?', punchline='To keep an eye on the mouse!')

JSON 模式

我们也支持 JSON 模式。注意,我们需要在提示中指定它应该以什么格式响应。

structured_llm = model.with_structured_output(Joke, method="json_mode")
structured_llm.invoke(
    "Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
)
Joke(setup='Why was the cat sitting on the computer?', punchline='Because it wanted to keep an eye on the mouse!')

Fireworks

Fireworks (opens in a new tab) 同样为部分模型支持函数调用和 JSON 模式。

API 参考 (opens in a new tab)

from langchain_fireworks import ChatFireworks

工具/函数调用

默认情况下,我们会使用 function_calling。

model = ChatFireworks(model="accounts/fireworks/models/firefunction-v1")
structured_llm = model.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
Joke(setup="Why don't cats play poker in the jungle?", punchline='Too many cheetahs!')

JSON 模式

我们也支持 JSON 模式。注意,我们需要在提示中指定它应该以什么格式响应。

structured_llm = model.with_structured_output(Joke, method="json_mode")
structured_llm.invoke(
    "Tell me a joke about dogs, respond in JSON with `setup` and `punchline` keys"
)
Joke(setup='Why did the dog sit in the shade?', punchline='To avoid getting burned.')

Mistral

我们也支持 Mistral 模型的结构化输出,不过我们只支持函数调用。

API 参考 (opens in a new tab)

from langchain_mistralai import ChatMistralAI
model = ChatMistralAI(model="mistral-large-latest")
structured_llm = model.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
Joke(setup="Why don't cats play poker in the jungle?", punchline='Too many cheetahs!')

Together

由于 TogetherAI (opens in a new tab) 只是 OpenAI 的直接替代品,我们可以直接使用 OpenAI 集成。

import os
 
from langchain_openai import ChatOpenAI
model = ChatOpenAI(
    base_url="https://api.together.xyz/v1",
    api_key=os.environ["TOGETHER_API_KEY"],
    model="mistralai/Mixtral-8x7B-Instruct-v0.1",
)
structured_llm = model.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
Joke(setup='Why did the cat sit on the computer?', punchline='To keep an eye on the mouse!')

Groq

Groq 提供兼容 OpenAI 的函数调用 API。

API 参考 (opens in a new tab)

from langchain_groq import ChatGroq

工具/函数调用

默认情况下,我们会使用 function_calling。

model = ChatGroq()
structured_llm = model.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
Joke(setup="Why don't cats play poker in the jungle?", punchline='Too many cheetahs!')

JSON 模式

我们也支持 JSON 模式。注意,我们需要在提示中指定它应该以什么格式响应。

structured_llm = model.with_structured_output(Joke, method="json_mode")
structured_llm.invoke(
    "Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
)
Joke(setup="Why don't cats play poker in the jungle?", punchline='Too many cheetahs!')

Anthropic

Anthropic 的工具调用 API 可用于构造结构化输出。注意,目前无法通过 API 强制进行工具调用,因此正确地提示模型仍然很重要。

API 参考 (opens in a new tab)

from langchain_anthropic import ChatAnthropic
 
model = ChatAnthropic(model="claude-3-opus-20240229", temperature=0)
structured_llm = model.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats. Make sure to call the Joke function.")
Joke(setup='What do you call a cat that loves to bowl?', punchline='An alley cat!')

Google Vertex AI

Google 的 Gemini 模型支持 函数调用 (opens in a new tab),我们可以通过 Vertex AI 访问它,并用它来构造结构化输出。

API 参考 (opens in a new tab)

from langchain_google_vertexai import ChatVertexAI
 
llm = ChatVertexAI(model="gemini-pro", temperature=0)
structured_llm = llm.with_structured_output(Joke)
structured_llm.invoke("Tell me a joke about cats")
Joke(setup='Why did the scarecrow win an award?', punchline='Why did the scarecrow win an award? Because he was outstanding in his field.')