Skip to content
LangSmith 文档版本 跟随上游 源快照 fe41ce50 更新于 2026-10-18

连接自定义模型

Playground 允许你使用自己的自定义模型。你可以部署一个模型服务器,通过 LangServe(一个用于服务 LangChain 应用的开源库)暴露模型的 API。在后台,Playground 会与你的模型服务器交互以生成响应。

部署自定义模型服务器

为方便起见,我们提供了一个示例模型服务器供你参考。我们强烈建议以该示例模型服务器作为起点。

根据你的模型是 instruct 风格还是 chat 风格,你需要分别实现 custom_model.py 或 custom_chat_model.py。

添加可配置字段

为模型配置不同的参数通常很有用,这些参数可能包括 temperature、model_name、max_tokens 等。

要让模型在 Playground 中可配置,你需要在模型服务器中添加可配置字段。这些字段可用于在 Playground 中更改模型参数。

你可以通过在 config.py 文件中实现 with_configurable_fields 函数来添加可配置字段。你可以

python
def with_configurable_fields(self) -> Runnable:
    """Expose fields you want to be configurable in the Playground. We will automatically expose these to the
    Playground. If you don't want to expose any fields, you can remove this method."""
    return self.configurable_fields(n=ConfigurableField(
        id="n",
        name="Num Characters",
        description="Number of characters to return from the input prompt.",
    ))

在 Playground 中使用模型

部署模型服务器后,你可以在 Playground 中使用它。进入 Playground,然后根据模型是 chat 风格还是 instruct 风格,选择 ChatCustomModel 或 CustomModel 提供商。

输入 URL。Playground 会自动检测可用的端点和可配置字段。然后你可以使用所需参数调用模型。

ChatCustomModel in Playground

如果一切设置正确,你应该能在 Playground 中看到模型的响应,以及在 with_configurable_fields 中指定的可配置字段。

更多信息请参阅如何保存模型配置以供后续使用。


BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。