# Spring AI

# Spring AI 接入本地 Ollama

依赖

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>

# 会话日志

Spring 使用 Aop 对会话拦截,增强,也就是Advisor

会话日志

# 配置

@Configuration
public class SpringAiConfig {

    @Bean
    public ChatClient ollemaChatClient(OllamaChatModel ollamaChatModel) {
        ChatClient build = ChatClient.builder(ollamaChatModel)
        .defaultSystem("你叫张益达,你是一个律师")
        .defaultAdvisors(new SimpleLoggerAdvisor())
        .build();
        return build;
    }

}

# 使用


@RestController
@RequestMapping("/ai/v2")
@RequiredArgsConstructor
public class V2_ChatController {

    private final ChatClient ollemaChatClient;

    @GetMapping("/notStream/chat")
    public String notStreamChat(String message) {
        ChatClient.CallResponseSpec call = ollemaChatClient
                .prompt()
                .user(message)
                .call();
        return call.content();
    }

    @GetMapping(value = "/stream/chat",produces = "text/html;charset=utf-8")
    public Flux<String> streamChat(String message) {
        Flux<String> content = ollemaChatClient
                .prompt()
                .user(message)
                .stream()
                .content();
        return content;
    }
}

# 会话记忆

  • 定义存储方式
  • 配置会话记忆
  • 添加会话id

Spring 提供接口
默认实现:InMemoryChatMemory

public interface ChatMemory {
    default void add(String conversationId, Message message) {
        this.add(conversationId, List.of(message));
    }

    void add(String conversationId, List<Message> messages);

    List<Message> get(String conversationId, int lastN);

    void clear(String conversationId);
}

# 配置

@Configuration
public class SpringAiConfig {

    @Bean
    public ChatMemory inmemoryChatMemory() {
        return new InMemoryChatMemory();
    }
    
    @Bean
    public ChatClient ollemaChatClient(OllamaChatModel ollamaChatModel, ChatMemory inmemoryChatMemory) {
        ChatClient build = ChatClient.builder(ollamaChatModel)
                .defaultSystem("你叫张益达,你是一个律师")
                .defaultAdvisors(
                        //定义消息会话
                        new MessageChatMemoryAdvisor(inmemoryChatMemory)
                )
                .build();
        return build;
    }
    
}

# openid 接口和类

# 聊天:

  • OpenAiChatModel

# 文生图

  • OpenAiImageModel

# 声音转文本

  • OpenAiAudioTranscriptionModel

# 文本转声音

  • OpenAiAudioSpeechModel