You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在langchain-go中指定结构化JSON响应的JSON Schema?

在langchain-go中指定返回的JSON Schema

要在langchain-go中实现自定义JSON Schema的结构化响应,你可以通过自定义LLM选项直接配置底层openai-go的参数来实现——因为langchain-go的OpenAI客户端封装了openai-go的核心能力,支持通过llms.WithCustomOption传入底层的配置参数。

具体步骤与代码示例

  1. 定义你的JSON Schema
    先准备好要指定的JSON Schema结构体,比如要求返回包含猫名字列表的结构:
// 定义JSON Schema参数
schemaParam := openai.ResponseFormatJSONSchema{
    Name:        openai.F("CatNamesList"),
    Description: openai.F("A list of cat names"),
    Type:        openai.F(openai.ResponseFormatJSONSchemaTypeObject),
    Properties: openai.F(map[string]openai.ResponseFormatJSONSchemaProperty{
        "names": {
            Type:        openai.F(openai.ResponseFormatJSONSchemaTypeArray),
            Description: openai.F("List of cat names"),
            Items: &openai.ResponseFormatJSONSchemaItems{
                Type: openai.F(openai.ResponseFormatJSONSchemaTypeString),
            },
        },
    }),
    Required: openai.F([]string{"names"}),
}
  1. 创建OpenAI LLM并传入自定义响应格式
    使用llms.WithCustomOption修改底层的ChatCompletionNewParams,传入包含自定义Schema的响应格式参数:
import (
    "context"
    "github.com/tmc/langchaingo/llms"
    "github.com/tmc/langchaingo/llms/openai"
)

func main() {
    ctx := context.Background()
    llm, err := openai.New()
    if err != nil {
        panic(err)
    }

    // 定义JSON Schema(同上)
    schemaParam := openai.ResponseFormatJSONSchema{
        Name:        openai.F("CatNamesList"),
        Description: openai.F("A list of cat names"),
        Type:        openai.F(openai.ResponseFormatJSONSchemaTypeObject),
        Properties: openai.F(map[string]openai.ResponseFormatJSONSchemaProperty{
            "names": {
                Type:        openai.F(openai.ResponseFormatJSONSchemaTypeArray),
                Description: openai.F("List of cat names"),
                Items: &openai.ResponseFormatJSONSchemaItems{
                    Type: openai.F(openai.ResponseFormatJSONSchemaTypeString),
                },
            },
        }),
        Required: openai.F([]string{"names"}),
    }

    completion, err := llms.GenerateFromSinglePrompt(
        ctx,
        llm,
        "Give a list of 5 cat names",
        // 传入自定义响应格式参数
        llms.WithCustomOption(func(p *openai.ChatCompletionNewParams) {
            p.ResponseFormat = openai.F[openai.ChatCompletionNewParamsResponseFormatUnion](
                openai.ResponseFormatJSONSchemaParam{
                    Type:       openai.F(openai.ResponseFormatJSONSchemaTypeJSONSchema),
                    JSONSchema: openai.F(schemaParam),
                },
            )
        }),
        llms.WithTemperature(0.0),
    )
    if err != nil {
        panic(err)
    }

    // 处理返回的结构化JSON结果
    println(completion.Generations[0].Text)
}

说明

  • llms.WithCustomOption允许直接修改底层openai-go的ChatCompletionNewParams参数,因此可以完全复用openai-go中指定JSON Schema的逻辑。
  • 无需再使用llms.WithJSONMode(),自定义的ResponseFormat已经明确指定了JSON Schema模式。

内容的提问来源于stack exchange,提问作者Lauri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 05:14:53