偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Spring AI 入門與DeepSeek API融合實戰(zhàn)

人工智能
Spring AI 使用Deepseek大模型對話的簡單示例就完成了,如果使用了Ollama私有化部署了大模型也可以使用上面的流程,或者是引入Ollama的依賴。

Spring AI 作為統(tǒng)一的大模型接入框架,其Model API為開發(fā)者提供了多模型適配能力。無論是OpenAI、DeepSeek、Moonshot AI(月之暗面)、Perplexity AI、Google VertexAI Gemini 等若干主流云服務商模型,還是支持Ollama私有化部署的本地模型,均可通過標準化接口實現(xiàn)無縫集成。

本文將介紹基于Spring AI框架,分別調用云端DeepSeek API的完整對話實現(xiàn)方式,話不多說,搞起來。

引入依賴

第一步,先引入依賴:Springboot 3.4.5 、 Spring AI 1.0.0-M7 。因為deepseek支持open ai的標準接口,所以這里引入 open ai的依賴即可。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.5</version>
        <relativePath/>
    </parent>
    <groupId>site.qxkd</groupId>
    <artifactId>chat-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>chat-client</name>
    <description>chat-client</description>
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.0.0-M7</spring-ai.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>${spring-ai.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置文件

這只是列舉一些簡單配置文件,實際上配置還是挺多的,感興趣可以去官網(wǎng)看一下

spring.application.name=chat-client

# 大模型相關依賴
spring.ai.openai.base-url=https://api.siliconflow.cn
spring.ai.openai.api-key=sk-你的秘鑰
#spring.ai.openai.chat.options.model=deepseek-ai/DeepSeek-V3
spring.ai.openai.chat.options.model=deepseek-ai/DeepSeek-R1

測試

上面一切準備好之后,寫兩個接口測試一下,一個是一次性輸出結果的接口,一個是流式輸出的接口。

RestController
@RequestMapping("/deepseek")
publicclass DeepseekController {

    @Autowired
    private OpenAiChatModel chatModel;

    @GetMapping("/ai/generate")
    public Map<String , String> generate(@RequestParam(value = "message") String message) {
        String content = chatModel.call(message);
        return Map.of("generation", content);
    }

    /**
     * 生成流式結果
     * @param message
     * @return
     */
    @GetMapping("/ai/generateStream")
    public Flux<ChatResponse> generateStream(@RequestParam(value = "message") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        returnthis.chatModel.stream(prompt);
    }
}

先測試一次性輸出結果的接口,瀏覽器訪問http://localhost:8080/deepseek/ai/generate?message=你是誰 ,會輸出如下結果:

圖片圖片

下面再來測試一下流式輸出到接口,為了實現(xiàn)流式輸出的效果,我讓deepseek給我寫了一個網(wǎng)頁,簡單修改了一下,達到了如下圖中的效果。

圖片圖片

流式輸出返回的一段JSON是這樣的。

{
 "result": {
"metadata": {
   "finishReason": "",
   "contentFilters": [],
   "empty": true
  },
"output": {
   "messageType": "ASSISTANT",
   "metadata": {
    "refusal": "",
    "finishReason": "",
    "index": 0,
    "id": "019684b265266dbe29ab56f75eaa75cd",
    "role": "ASSISTANT",
    "messageType": "ASSISTANT"
   },
   "toolCalls": [],
   "media": [],
   "text": "DeepSeek)公司開發(fā)的智能助手"
  }
 },
"metadata": {
"id": "019684b265266dbe29ab56f75eaa75cd",
"model": "deepseek-ai/DeepSeek-R1",
"rateLimit": {
   "requestsRemaining": 0,
   "requestsLimit": 0,
   "tokensRemaining": 0,
   "tokensReset": "PT0S",
   "requestsReset": "PT0S",
   "tokensLimit": 0
  },
"usage": {
   "promptTokens": 6,
   "completionTokens": 64,
   "totalTokens": 70,
   "nativeUsage": {
    "completion_tokens": 64,
    "prompt_tokens": 6,
    "total_tokens": 70,
    "completion_tokens_details": {
     "reasoning_tokens": 38
    }
   }
  },
"promptMetadata": [],
"empty": false
 },
"results": [{
"metadata": {
   "finishReason": "",
   "contentFilters": [],
   "empty": true
  },
"output": {
   "messageType": "ASSISTANT",
   "metadata": {
    "refusal": "",
    "finishReason": "",
    "index": 0,
    "id": "019684b265266dbe29ab56f75eaa75cd",
    "role": "ASSISTANT",
    "messageType": "ASSISTANT"
   },
   "toolCalls": [],
   "media": [],
   "text": "DeepSeek)公司開發(fā)的智能助手"
  }
 }]
}

至此,一個簡單的Spring AI對話就完成了。細心的同學可能會發(fā)現(xiàn),這也就只能單輪對話,沒什么用呀,要連續(xù)對話才行。別急,下面就演示一下連續(xù)對話的demo。

連續(xù)對話

想要連續(xù)對話需要3個相關接口和API:

  • ChatMemory : 大模型(LLM)是無狀態(tài)的,這意味著它們不會保留有關以前交互的信息。在多個交互中維護上下文或狀態(tài)時,這就變成了一種限制。為了解決這個問題,Spring AI 提供了一個存儲和檢索與大模型多次對話信息的接口 ChatMemory。
  • ChatClient : ChatClient提供了一個流式API(fluent API),用于與AI模型進行通信,它同時支持同步和流式兩種編程模型。該流式API提供了一系列方法,用于逐步構建**提示詞(Prompt)**的各個組成部分,這些提示詞將作為輸入傳遞給AI模型。
  • Advisors APIAdvisors API為開發(fā)者提供了一種靈活而強大的方式,用于在 Spring 應用程序中攔截、修改和增強 AI 驅動的交互。它的核心優(yōu)勢包括:封裝常見的生成式 AI 模式,轉換發(fā)送給大語言模型(LLMs)的數(shù)據(jù)及處理其返回結果,以及實現(xiàn)跨不同模型和用例的可移植性。

話不多說,上代碼。

  • 先注入 ChatClient 和 ChatMemory 對象 本文使用InMemoryChatMemory將對話記錄存放在內存中,如果想實現(xiàn)將對話記錄存放在數(shù)據(jù)庫可以考慮實現(xiàn)ChatMemory。
@Configuration
publicclass ChatConfig {

    @Bean
    public ChatClient chatClient(OpenAiChatModel openAiChatModel) {
        ChatClient chatClient = ChatClient.builder(openAiChatModel)
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory()))  //  設置默認的MemoryAdvisor ,將對話記錄存放在內存中
                .build();
        return chatClient;
    }


    @Bean
    public ChatMemory chatMemory(){
        returnnew InMemoryChatMemory();
    }
}
  • 測試 測試使用流式輸出,調用接口時需要傳一個chatId ,這樣才能根據(jù)chatId到內存中查詢相關對話信息。
@Autowired
private ChatClient chatClient;

@GetMapping("/ai/chatContext")
public Flux<ChatResponse> chatContext(@RequestParam(value = "message") String message , String chatId) {

    Flux<ChatResponse> chatResponseFlux = chatClient.prompt()
            //連續(xù)對話的key
            .advisors(advisor -> advisor.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                    .param(AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100))
            .user(message).stream().chatResponse();

    return chatResponseFlux;
}

修改一下之前的聊天界面代碼進行測試:

圖片圖片

至此,Spring AI 使用Deepseek大模型對話的簡單示例就完成了,如果使用了Ollama私有化部署了大模型也可以使用上面的流程,或者是引入Ollama的依賴。

責任編輯:武曉燕 來源: 索碼理
相關推薦

2025-01-06 09:43:36

SpringAI?模型

2025-03-27 09:34:42

2025-03-06 07:48:02

2025-05-16 07:50:58

Spring AIMCPSSE

2025-02-19 12:00:00

SpringBootDeepSeekAI

2021-10-14 13:57:58

物聯(lián)網(wǎng)人工智能數(shù)據(jù)

2025-04-16 01:00:00

API工具AI

2024-05-31 08:12:19

2021-02-09 20:45:16

AIOOPO

2021-11-04 17:23:25

AI人工智能IOT

2025-02-10 00:00:00

DeepSeek技術人工智能

2024-03-01 14:34:19

5G人工智能AI

2025-02-12 16:13:34

2025-01-07 09:00:00

2025-02-24 10:07:10

2025-06-27 02:00:00

2020-07-24 22:05:49

AI云計算融合

2020-03-20 18:40:04

人工智能AI醫(yī)療行業(yè)
點贊
收藏

51CTO技術棧公眾號