Skip to content
LangChain4j 文档版本 1.22.0 源快照 4258012d 更新于 2026-10-14

TypeSafe

TypeSafe 通过其 System One API 提供决策模型,例如 Jev。 TypeSafeDecisionModel 在其之上实现了 DecisionModel API。

同样的 API 也被其他服务器实现,例如 OpenRouter,以及运行开放模型的推理服务器如 SGLang。 通过设置 base URL,TypeSafeDecisionModel 可与其中任意一家配合使用(见下文)。

NOTE

本集成尚处于实验阶段,未来版本可能变化。

Maven 依赖

xml
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-typesafe</artifactId>
    <version>1.22.0-beta32</version>
</dependency>

用量

java
DecisionModel decisionModel = TypeSafeDecisionModel.builder()
        .apiKey(System.getenv("TYPESAFE_API_KEY"))
        .modelName("jev-1.13.0")
        .build();

DecisionRequest request = DecisionRequest.builder()
        .input("Help! My payouts have been failing for 3 days.")
        .question("team", ChoiceQuestion.builder()
                .text("Which team should handle this ticket?")
                .option("billing", "Payments, payouts, invoices, refunds")
                .option("support", "Problems using the product")
                .build())
        .question("urgent", YesNoQuestion.of("Does this need attention today?"))
        .build();

DecisionResponse response = decisionModel.decide(request);

ChoiceAnswer team = response.choice("team");
YesNoAnswer urgent = response.yesNo("urgent");

关于所有问题类型及如何使用答案,参见决策模型教程。

配置

java
TypeSafeDecisionModel decisionModel = TypeSafeDecisionModel.builder()
        .apiKey(...)              // required for api.typesafe.ai, optional for other servers
        .modelName(...)           // required here or on each request (otherwise decide() fails), e.g. "jev-1.13.0"
        .baseUrl(...)             // defaults to "https://api.typesafe.ai"
        .apiPath(...)             // path of the System One endpoint, relative to baseUrl; defaults to "v1/systemone"
        .timeout(...)             // defaults to the HTTP client builder's timeouts, otherwise 15s connect / 60s read
        .maxRetries(...)          // retries after the first attempt; defaults to 2
        .customHeaders(...)       // additional HTTP headers, as a Map or a Supplier<Map>
        .listeners(...)           // DecisionModelListeners, see Observability in the Decision Models tutorial
        .logRequests(...)         // defaults to false
        .logResponses(...)        // defaults to false
        .logger(...)              // an alternate SLF4J logger for requests and responses
        .httpClientBuilder(...)   // see below
        .build();

可用模型列于 TypeSafe 文档。 诸如 jev-latest 的别名随时可能指向新版本;在生产中请使用固定版本。

HTTP 客户端可用 httpClientBuilder(...) 定制,参见可定制 HTTP 客户端。

decide() 与 decideAsync() 均受支持。

该模型支持 YesNoQuestion、ChoiceQuestion 与 ScaleQuestion 三种问题类型。 在 TypeSafe 文档中,是/否问题被称为 "noul" 问题,量表问题被称为 "score" 问题,输入则被称为 "state"。带有描述的量表级别(level(label, description)) 会作为一个含 label 与 description 的对象发送。 该 API 仅接受文本:文本内容按其文本发送,而图像会抛出 UnsupportedFeatureException 且不调用 API。 选择与量表答案的置信度由服务器计算。

答案会依据请求进行校验:缺失的答案、类型错误的答案、未被提供的选项、超出 0 到 1 的概率, 或超出级别的量表均值都会抛出 InvalidDecisionResponseException。未上报概率的量表答案级别其概率被置为 0。

输入与问题会被发送到你配置的服务器。使用托管服务时,请查阅其数据 处理与保留条款,并参见数据保护。

其他服务器

任何实现了 System One API 的服务器都可通过设置 base URL 使用。请求会发送到 base URL 后接 API 路径,默认为 v1/systemone;若服务器在另一个路径暴露该 API,请用 apiPath(...) 设置。例如,使用 OpenRouter 时:

java
DecisionModel decisionModel = TypeSafeDecisionModel.builder()
        .baseUrl("https://openrouter.ai/api")
        .apiKey(System.getenv("OPENROUTER_API_KEY"))
        .modelName("typesafe/jev-1.13")
        .build();

对于同样实现了该 API 的 Ollama,请改用 OllamaDecisionModel:它支持图像,并将 Ollama 上报为 提供者。

本地与自托管服务器通常无需 API 密钥。没有 GPU 时,一次请求可能耗时数秒,因此考虑 设置更长的 timeout(...)。

TypeSafeDecisionModel 已针对 TypeSafe API 与 OpenRouter 进行测试。

各服务器所施加的限制(例如最大选项数)、置信度的计算方式以及答案质量都可能不同: 小型模型和作为决策模型提供的通用语言模型,通常不如专用决策模型准确、果断。 请查阅你所用服务器的文档,并在你自己的数据上评估其答案。

无论 base URL 为何,该模型都会将 ModelProvider.TYPESAFE 作为其提供者上报,例如在监听器与指标中。

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