侧边栏壁纸
博主头像
AI中文站

开拓MCP洪荒时代

  • 累计撰写 28 篇文章
  • 累计创建 3 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

SpringAI-接入Anthropic 3 Chat

Anthropic Claude 是一系列基础 AI 模型,可用于多种应用。开发者和企业可利用 API 访问权限,直接在 Anthropic 的 AI 基础设施上构建。

Spring AI 支持 Anthropic 消息 API,用于同步和流式文本生成。

Anthropic 的 Claude 模型也可通过 Amazon Bedrock Converse 使用。Spring AI 同时提供专用的 Amazon Bedrock Converse Anthropic 客户端实现。


先决条件


您需要在 Anthropic 门户创建 API 密钥。

在 Anthropic API 仪表板创建账户,并在获取 API 密钥页面生成 API 密钥。

Spring AI 项目定义了名为 spring.ai.anthropic.api-key 的配置属性,应设置为从 anthropic.com 获取的 API 密钥值。

您可在 application.properties 文件中设置此配置属性:

spring.ai.anthropic.api-key=<您的-anthropic-api密钥>

为增强处理敏感信息(如 API 密钥)时的安全性,可使用 Spring 表达式语言(SpEL)引用自定义环境变量:

在 application.yml 中

spring:
  ai:
    anthropic:
      api-key: ${ANTHROPIC_API_KEY}

在您的环境或 .env 文件中

export ANTHROPIC_API_KEY=<您的-anthropic-api密钥>

也可在应用程序代码中以编程方式设置此配置:

// 从安全源或环境变量检索 API 密钥
String apiKey = System.getenv("ANTHROPIC_API_KEY");

添加仓库和 BOM

Spring AI 构件发布在 Maven Central 和 Spring Snapshot 仓库。参考构件仓库部分将这些仓库添加到构建系统。

为帮助依赖管理,Spring AI 提供 BOM(材料清单)确保整个项目使用一致的 Spring AI 版本。参考依赖管理部分将 Spring AI BOM 添加到构建系统。

自动配置

Spring AI 自动配置和 starter 模块的构件名称已发生重大变更。请参考升级说明获取更多信息。

Spring AI 为 Anthropic 聊天客户端提供 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Maven pom.xml 或 Gradle build.gradle 文件:

Maven

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-anthropic</artifactId>
</dependency>

Gradle

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-anthropic'
}


参考依赖管理部分将 Spring AI BOM 添加到构建文件。


聊天属性

重试属性
前缀 spring.ai.retry 用作属性前缀,用于配置 Anthropic 聊天模型的重试机制。

Property

Description

Default

spring.ai.retry.max-attempts

Maximum number of retry attempts.

10

spring.ai.retry.backoff.initial-interval

Initial sleep duration for the exponential backoff policy.

2 sec.

spring.ai.retry.backoff.multiplier

Backoff interval multiplier.

5

spring.ai.retry.backoff.max-interval

Maximum backoff duration.

3 min.

spring.ai.retry.on-client-errors

If false, throw a NonTransientAiException, and do not attempt retry for 4xx client error codes

false

spring.ai.retry.exclude-on-http-codes

List of HTTP status codes that should NOT trigger a retry (e.g. to throw NonTransientAiException).

empty

spring.ai.retry.on-http-codes

List of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException).

empty

当前重试策略不适用于流式 API。


连接属性

前缀 spring.ai.anthropic 用作属性前缀,用于连接到 Anthropic。

Property

Description

Default

spring.ai.anthropic.base-url

The URL to connect to

api.anthropic.com

spring.ai.anthropic.completions-path

The path to append to the base URL.

/v1/chat/completions

spring.ai.anthropic.version

Anthropic API version

2023-06-01

spring.ai.anthropic.api-key

The API Key

-

spring.ai.anthropic.beta-version

Enables new/experimental features. If set to max-tokens-3-5-sonnet-2024-07-15 the output tokens limit is increased from 4096 to 8192 tokens (for claude-3-5-sonnet only).

tools-2024-04-04

配置属性

聊天自动配置的启用和禁用现在通过顶级属性配置,前缀为 spring.ai.model.chat。

启用:spring.ai.model.chat=anthropic(默认启用)

禁用:spring.ai.model.chat=none(或任何不匹配 anthropic 的值)

此变更允许配置多个模型。

前缀 spring.ai.anthropic.chat 是配置 Anthropic 聊天模型实现的属性前缀。

Property

Description

Default

spring.ai.anthropic.chat.enabled (Removed and no longer valid)

Enable Anthropic chat model.

true

spring.ai.model.chat

Enable Anthropic chat model.

anthropic

spring.ai.anthropic.chat.options.model

This is the Anthropic Chat model to use. Supports: claude-3-7-sonnet-latest, claude-3-5-sonnet-latest, claude-3-opus-20240229, claude-3-sonnet-20240229, claude-3-haiku-20240307

claude-3-7-sonnet-latest

spring.ai.anthropic.chat.options.temperature

The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction of these two settings is difficult to predict.

0.8

spring.ai.anthropic.chat.options.max-tokens

The maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model’s context length.

500

spring.ai.anthropic.chat.options.stop-sequence

Custom text sequences that will cause the model to stop generating. Our models will normally stop when they have naturally completed their turn, which will result in a response stop_reason of "end_turn". If you want the model to stop generating when it encounters custom strings of text, you can use the stop_sequences parameter. If the model encounters one of the custom sequences, the response stop_reason value will be "stop_sequence" and the response stop_sequence value will contain the matched stop sequence.

-

spring.ai.anthropic.chat.options.top-p

Use nucleus sampling. In nucleus sampling, we compute the cumulative distribution over all the options for each subsequent token in decreasing probability order and cut it off once it reaches a particular probability specified by top_p. You should either alter temperature or top_p, but not both. Recommended for advanced use cases only. You usually only need to use temperature.

-

spring.ai.anthropic.chat.options.top-k

Only sample from the top K options for each subsequent token. Used to remove "long tail" low probability responses. Learn more technical details here. Recommended for advanced use cases only. You usually only need to use temperature.

-

spring.ai.anthropic.chat.options.toolNames

List of tools, identified by their names, to enable for tool calling in a single prompt requests. Tools with those names must exist in the toolCallbacks registry.

-

spring.ai.anthropic.chat.options.toolCallbacks

Tool Callbacks to register with the ChatModel.

-

spring.ai.anthropic.chat.options.internal-tool-execution-enabled

If false, the Spring AI will not handle the tool calls internally, but will proxy them to the client. Then it is the client’s responsibility to handle the tool calls, dispatch them to the appropriate function, and return the results. If true (the default), the Spring AI will handle the function calls internally. Applicable only for chat models with function calling support

true

(deprecated - replaced by toolNames) spring.ai.anthropic.chat.options.functions

List of functions, identified by their names, to enable for function calling in a single prompt requests. Functions with those names must exist in the functionCallbacks registry.

-

(deprecated - replaced by toolCallbacks) spring.ai.anthropic.chat.options.functionCallbacks

Tool Function Callbacks to register with the ChatModel.

-

(deprecated - replaced by a negated internal-tool-execution-enabled) spring.ai.anthropic.chat.options.proxy-tool-calls

If true, the Spring AI will not handle the function calls internally, but will proxy them to the client. Then is the client’s responsibility to handle the function calls, dispatch them to the appropriate function, and return the results. If false (the default), the Spring AI will handle the function calls internally. Applicable only for chat models with function calling support

false

spring.ai.anthropic.chat.options.http-headers

Optional HTTP headers to be added to the chat completion request.

-

所有以 spring.ai.anthropic.chat.options 为前缀的属性可在运行时通过向 Prompt 调用添加请求特定的运行时选项来覆盖。

运行时选项

AnthropicChatOptions.java 提供模型配置,如使用的模型、温度、最大令牌数等。

启动时,可通过 AnthropicChatModel(api, options) 构造函数或 spring.ai.anthropic.chat.options.* 属性配置默认选项。

运行时可通过在 Prompt 调用中添加新的请求特定选项来覆盖默认选项。例如为特定请求覆盖默认模型和温度:

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        AnthropicChatOptions.builder()
            .model("claude-3-7-sonnet-latest")
            .temperature(0.4)
        .build()
    ));

除模型特定的 AnthropicChatOptions 外,您还可使用通过 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。

Tool/Function Calling

您可向 AnthropicChatModel 注册自定义 Java 工具,让 Anthropic Claude 模型智能选择输出包含参数的 JSON 对象来调用一个或多个注册函数。这是将 LLM 能力与外部工具和 API 连接的强大技术。阅读更多关于工具调用的信息。

多模态

多模态指模型同时理解和处理多种来源信息的能力,包括文本、pdf、图像、数据格式。

图像

当前,Anthropic Claude 3 支持图像的 base64 源类型,以及 image/jpeg、image/png、image/gif 和 image/webp 媒体类型。查看视觉指南获取更多信息。Anthropic Claude 3.5 Sonnet 还支持 application/pdf 文件的 pdf 源类型。

Spring AI 的 Message 接口通过引入 Media 类型支持多模态 AI 模型。此类型包含消息中媒体附件的数据和信息,使用 Spring 的 org.springframework.util.MimeType 和用于原始媒体数据的 java.lang.Object。

以下是 AnthropicChatModelIT.java 中提取的简单代码示例,演示用户文本与图像的组合。

var imageData = new ClassPathResource("/multimodal.test.png");

var userMessage = new UserMessage("Explain what do you see on this picture?",
        List.of(new Media(MimeTypeUtils.IMAGE_PNG, this.imageData)));

ChatResponse response = chatModel.call(new Prompt(List.of(this.userMessage)));

logger.info(response.getResult().getOutput().getContent());

它以 multimodal.test.png 图像作为输入:


附带文本消息"解释你在这张图片中看到了什么?",并生成类似响应:

The image shows a close-up view of a wire fruit basket containing several pieces of fruit.
...


PDF

从 Sonnet 3.5 开始提供 PDF 支持(测试版)。使用 application/pdf 媒体类型将 PDF 文件附加到消息:

var pdfData = new ClassPathResource("/spring-ai-reference-overview.pdf");

var userMessage = new UserMessage(
        "You are a very professional document summarization specialist. Please summarize the given document.",
        List.of(new Media(new MimeType("application", "pdf"), pdfData)));

var response = this.chatModel.call(new Prompt(List.of(userMessage)));

Sample Controller

创建新的 Spring Boot 项目,并将 spring-ai-starter-model-anthropic 添加到您的 pom(或 gradle)依赖项。

在 src/main/resources 目录下添加 application.properties 文件,以启用和配置 Anthropic 聊天模型:

spring.ai.anthropic.api-key=YOUR_API_KEY

spring.ai.anthropic.chat.options.model=claude-3-5-sonnet-latest

spring.ai.anthropic.chat.options.temperature=0.7

spring.ai.anthropic.chat.options.max-tokens=450

将 api-key 替换为您的 Anthropic 凭证。
这将创建 AnthropicChatModel 实现,您可将其注入到您的类中。以下是使用聊天模型进行文本生成的简单 @Controller 类示例。

@RestController
public class ChatController {

    private final AnthropicChatModel chatModel;

    @Autowired
    public ChatController(AnthropicChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", this.chatModel.call(message));
    }

    @GetMapping("/ai/generateStream")
	public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatModel.stream(prompt);
    }
}


手动配置

AnthropicChatModel 实现 ChatModel 和 StreamingChatModel,并使用低级 AnthropicApi 客户端连接到 Anthropic 服务。

将 spring-ai-anthropic 依赖项添加到项目的 Maven pom.xml 文件:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-anthropic</artifactId>
</dependency>

Gradle

dependencies {
    implementation 'org.springframework.ai:spring-ai-anthropic'
}


参考依赖管理部分将 Spring AI BOM 添加到构建文件。


接下来,创建 AnthropicChatModel 并将其用于文本生成:

var anthropicApi = new AnthropicApi(System.getenv("ANTHROPIC_API_KEY"));

var chatModel = new AnthropicChatModel(this.anthropicApi,
        AnthropicChatOptions.builder()
            .model("claude-3-opus-20240229")
            .temperature(0.4)
            .maxTokens(200)
        .build());

ChatResponse response = this.chatModel.call(
    new Prompt("Generate the names of 5 famous pirates."));

// Or with streaming responses
Flux<ChatResponse> response = this.chatModel.stream(
    new Prompt("Generate the names of 5 famous pirates."));

AnthropicChatOptions 为聊天请求提供配置信息。AnthropicChatOptions.Builder 是流畅的选项构建器。

低级 AnthropicApi 客户端

AnthropicApi 是 Anthropic 消息 API 的轻量级 Java 客户端。

以下类图说明 AnthropicApi 聊天接口和构建块:


AnthropicApi 事件模型


以下是编程使用 API 的简单片段:

AnthropicApi anthropicApi =
    new AnthropicApi(System.getenv("ANTHROPIC_API_KEY"));

AnthropicMessage chatCompletionMessage = new AnthropicMessage(
        List.of(new ContentBlock("Tell me a Joke?")), Role.USER);

// Sync request
ResponseEntity<ChatCompletionResponse> response = this.anthropicApi
    .chatCompletionEntity(new ChatCompletionRequest(AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(),
            List.of(this.chatCompletionMessage), null, 100, 0.8, false));

// Streaming request
Flux<StreamResponse> response = this.anthropicApi
    .chatCompletionStream(new ChatCompletionRequest(AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(),
            List.of(this.chatCompletionMessage), null, 100, 0.8, true));

遵循 AnthropicApi.java 的 JavaDoc 获取更多信息。

低级 API 示例
AnthropicApiIT.java 测试提供了一些使用轻量级库的通用示例。

0

评论区