> ## Documentation Index
> Fetch the complete documentation index at: https://javaai.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 常见问题

<Card title="写在最前：问题咨询请直接点击此处提交 ISSUE" icon="star" href="https://github.com/pig-mesh/deepseek4j/issues">
  * ⭐️ **请先 Star 本项目后再提问，作者精力有限，未 Star 直接 Close**
  * 🐞 [GitHub ISSUE](https://github.com/pig-mesh/deepseek4j/issues) 需包含：问题截图 + SDK版本 + 配置代码
</Card>

## 依赖版本冲突问题

<Warning>目前遇到最多的是 SpringBoot 兼容性问题</Warning>

### Spring Boot 兼容性问题

当使用 Spring Boot 2.4.x 以下版本时，可能会遇到 Jackson 和 OkHttp 的版本冲突问题。这是因为较低版本的 Spring Boot 依赖的这些库版本与 deepseek4j 所需版本不兼容。

#### 解决方案

<Warning>请将以下依赖管理配置添加到项目根 pom.xml 文件的 dependencyManagement 节点中，以确保全局依赖版本的正确覆盖。</Warning>

```xml theme={"system"}
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp-sse</artifactId>
            <version>4.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson</groupId>
            <artifactId>jackson-bom</artifactId>
            <version>2.12.4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
```

## 中文编码问题

### 问题描述

如果开发环境的字符编码不是 UTF-8，或在输出响应时遇到中文乱码问题，可能会出现如下情况：

<img src="https://minio.pigx.vip/oss/202502/1739196317.png" alt="中文乱码示例" />

### 解决方案

在 Spring Controller 中设置响应的 Content-Type 时，需要明确指定字符编码为 UTF-8：

```java theme={"system"}
@GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE + ";charset=UTF-8")
public Flux<ChatCompletionResponse> chat(String prompt) {
    // 你的业务逻辑
}
```

## 多厂商动态切换

### 问题描述

在某些场景下，可能需要根据不同的业务需求动态切换不同的模型厂商。例如，在某些场景下，可能需要根据不同的业务需求动态切换不同的模型厂商。

### 解决方案

* 不再引入 deepseek-spring-boot-starter 依赖，而是引入 deepseek4j-core 依赖自行配置 DeepSeekClient

```java theme={"system"}
@Bean
public DeepSeekClient aliyunDeepSeekClient() {
    DeepSeekClient.Builder builder = DeepSeekClient.builder().baseUrl(deepSeekProperties.getBaseUrl())
            .model(deepSeekProperties.getModel()).openAiApiKey(deepSeekProperties.getApiKey())
            .logRequests(deepSeekProperties.isLogRequests()).logResponses(deepSeekProperties.isLogResponses());
    return builder.build();
}


@Bean
public DeepSeekClient txDeepSeekClient() {

    DeepSeekClient.Builder builder = DeepSeekClient.builder().baseUrl(deepSeekProperties.getBaseUrl())
            .model(deepSeekProperties.getModel()).openAiApiKey(deepSeekProperties.getApiKey())
            .logRequests(deepSeekProperties.isLogRequests()).logResponses(deepSeekProperties.isLogResponses());
    return builder.build();
}
```

## API 使用量统计

### 获取请求用量统计

Deepseek 官方 API 会在流式响应的最后返回本次请求的用量统计信息。你可以通过以下方式获取：

```java theme={"system"}
return deepSeekClient.chatFluxCompletion(prompt)
    .doOnNext(response -> {
        if (Objects.nonNull(response.usage())) {
            // 在这里处理用量统计信息
            // 可以记录到日志或数据库中
        }
    });
```

<Card title="PIG AI应用开发平台 | 适合中大型企业构建自主可控的AI中台" icon="link" href="https://ai.pig4cloud.com">
  **为Java开发者提供全栈式AI工程化解决方案**，强类型/高可维护性架构，内置30+主流大模型支持。

  * **🔍 知识引擎体系**：RAG 知识引擎全自动化多模态解决方案
  * **📝 AI-OCR 中枢**：复杂非标场景高精度识别
  * **⚙️ 业务智能融合**：函数编排 + Chat2SQL，无缝对接现有业务系统
  * **🛡️ N维风控体系**：敏感词/IP/Token/User 规则控制引擎
</Card>

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/deepseek/qa.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/deepseek/qa.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>
