Langfuse 文档 中文 英文原文 ↗
文档 / LLM 连接(LLM Connections)

LLM 连接(LLM Connections)

LLM 连接用于在 Langfuse Playground 中调用模型,或用于 LLM-as-a-Judge 评估。

设置

通过 UI

  1. 导航到你的 Project Settings > LLM Connections点击 Add new LLM API key
  2. 输入 LLM 连接的名称和你要使用的模型的 API 密钥。

通过 API

你可以使用 API 管理 LLM 连接:

bash
GET /api/public/llm-connections
PUT /api/public/llm-connections

支持的提供商

Langfuse 平台目前支持以下 LLM 提供商:

支持的模型

目前 playground 默认支持以下模型系列:OpenAI(包括 o3、o4-mini、gpt-4.1 系列、gpt-4o 系列、o1 系列、gpt-4、gpt-3.5-turbo 等)、Anthropic(claude-3-7-sonnet、claude-3-5-sonnet、claude-3-opus、claude-3-sonnet、claude-3-5-haiku、claude-3-haiku、claude-2.x、claude-instant 等)、Google Vertex AI 和 Google AI Studio(gemini-2.5-pro、gemini-2.0 系列、gemini-1.5 系列、gemini-1.0-pro 等)。完整且最新的模型标识符列表请参见英文原文页面。你可以在 Langfuse 项目设置中添加 LLM API Key 时配置额外的自定义模型名称,例如使用自定义模型或代理时。

此外,任何支持 OpenAI API schema 的模型:Playground 和 LLM-as-a-Judge 评估可由任何支持 OpenAI API schema 的框架使用,如 Groq、OpenRouter、Vercel AI Gateway、LiteLLM、Hugging Face 等。只需用你想使用的模型的相应端点替换 API Base URL,并添加提供商的 API 密钥进行身份验证。Amazon Bedrock 支持所有 Amazon Bedrock 模型,AWS 上所需的权限是 bedrock:InvokeModelbedrock:InvokeModelWithResponseStream

如果第三方 LLM 提供商的 API schema 实现了我们支持的提供商适配器之一的 schema,你可以连接到它们。例如,你可以通过在 Langfuse 中使用 OpenAI 适配器连接到 Mistral 的 OpenAI 兼容 API,从而连接到 Mistral。

高级配置

额外的提供商选项

提供商选项不在 Project Settings > LLM Connections 页面设置,而是在 Playground 上选择 LLM 连接时,或在 LLM-as-a-Judge 评估器设置期间设置。

来自已创建 LLM 连接的 LLM 调用可以用一组特定参数配置,如 temperaturetop_pmax_tokens

但是,许多 LLM 提供商在调用模型时允许额外参数,包括 reasoning_effortservice_tier 等。这些参数通常在提供商之间不同。

你可以为所有 LLM 调用以 JSON 对象形式提供额外配置。在模型参数设置中,你会在底部找到一个 "provider options" 字段。此字段允许你输入你的 LLM 提供商 API 端点接受的特定键值对。

请参阅你的提供商 API 参考,了解支持哪些额外字段:

此功能目前可用于以下适配器:

在 OpenAI gpt-5 调用上强制推理努力 minimal 的示例:

Google 模型(Google AI Studio 和 Google Vertex AI)控制思考的示例提供商选项:

json
{
  "thinkingLevel": "MEDIUM",
  "thinkingBudget": 2048
}

thinkingLevel 控制推理深度,thinkingBudget 设置思考的 token 预算。这些选项仅对支持思考的 Google 模型有效。

通过网关连接

如果你通过 OpenAI 兼容的网关(如 LiteLLMOpenRouterPortkey)路由 LLM 调用,你可以将其用作 Playground 和 LLM-as-a-Judge 评估的 LLM 连接。

要设置此功能:

  1. 导航到 Project Settings > LLM Connections 并点击 Add new LLM API key
  2. 选择 OpenAI 作为提供商。
  3. 输入你网关的 API 密钥。
  4. Advanced Settings 下,将 Base URL 设置为你网关的端点,例如 https://your-litellm-instance.com/v1
  5. 添加你网关暴露的任何自定义模型名称

网关必须支持 OpenAI 格式的工具调用,LLM-as-a-Judge 评估才能工作。

对于 OpenAI 兼容的网关,下面是网关必须处理以支持 Langfuse 中 LLM-as-a-Judge 的示例工具调用请求:

bash
curl -X POST 'https://<host set in project settings>/chat/completions' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
-H 'authorization: Bearer <api key entered in project settings>' \
-H 'x-test-header-1: <custom header set in project settings>' \
-H 'x-test-header-2: <custom header set in project settings>' \
-d '{
  "model": "<model set in project settings>",
  "temperature": 0,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0,
  "max_tokens": 256,
  "n": 1,
  "stream": false,
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "extract",
        "parameters": {
          "type": "object",
          "properties": {
            "score": {
              "type": "string"
            },
            "reasoning": {
              "type": "string"
            }
          },
          "required": [
            "score",
            "reasoning"
          ],
          "additionalProperties": false,
          "$schema": "http://json-schema.org/draft-07/schema#"
        }
      }
    }
  ],
  "tool_choice": {
    "type": "function",
    "function": {
      "name": "extract"
    }
  },
  "messages": [
    {
      "role": "user",
      "content": "Evaluate the correctness of the generation on a continuous scale from 0 to 1. A generation can be considered correct (Score: 1) if it includes all the key facts from the ground truth and if every fact presented in the generation is factually supported by the ground truth or common sense.\n\nExample:\nQuery: Can eating carrots improve your vision?\nGeneration: Yes, eating carrots significantly improves your vision, especially at night. This is why people who eat lots of carrots never need glasses. Anyone who tells you otherwise is probably trying to sell you expensive eyewear or does not want you to benefit from this simple, natural remedy. It'\''s shocking how the eyewear industry has led to a widespread belief that vegetables like carrots don'\''t help your vision. People are so gullible to fall for these money-making schemes.\nGround truth: Well, yes and no. Carrots won'\''t improve your visual acuity if you have less than perfect vision. A diet of carrots won'\''t give a blind person 20/20 vision. But, the vitamins found in the vegetable can help promote overall eye health. Carrots contain beta-carotene, a substance that the body converts to vitamin A, an important nutrient for eye health.  An extreme lack of vitamin A can cause blindness. Vitamin A can prevent the formation of cataracts and macular degeneration, the world'\''s leading cause of blindness. However, if your vision problems aren'\''t related to vitamin A, your vision won'\''t change no matter how many carrots you eat.\nScore: 0.1\nReasoning: While the generation mentions that carrots can improve vision, it fails to outline the reason for this phenomenon and the circumstances under which this is the case. The rest of the response contains misinformation and exaggerations regarding the benefits of eating carrots for vision improvement. It deviates significantly from the more accurate and nuanced explanation provided in the ground truth.\n\n\n\nInput:\nQuery: {{query}}\nGeneration: {{generation}}\nGround truth: {{ground_truth}}\n\n\nThink step by step."
    }
  ]
}'

OpenAI Responses API 端点

某些 OpenAI 兼容的提供商暴露 OpenAI Responses API 而非 Chat Completions API。对于这些提供商,在 OpenAI LLM Connection 上启用 Use Responses API,以便 Langfuse 通过 Responses API 发送 Playground、LLM-as-a-Judge 和提示词实验请求。

例如,要通过 Bedrock Mantle 端点在 Amazon Bedrock 上使用 OpenAI 模型:

  1. 导航到 Project Settings > LLM Connections 并点击 Add new LLM API key
  2. 选择 OpenAI 作为提供商。
  3. 输入你的 Amazon Bedrock API 密钥
  4. Advanced Settings 下,将 Base URL 设置为 https://bedrock-mantle..api.aws/openai/v1
  5. 启用 Use Responses API
  6. 添加你 Bedrock 账户中可用的自定义模型名称,例如 openai.gpt-5.5openai.gpt-5.4
非官方中文翻译 · 图片/视频/代码均链接官方资源 · 版权归 Langfuse GmbH 所有 查看英文原文 ↗