Skip to content

common-openai-starter

1. 功能介绍

1.统一封装调用逻辑

  • 封装了 OpenAI 的 API 调用逻辑,提供 askAI 方法,通过指定模型和输入内容快速获取 AI 的响应。

2.灵活的配置支持

  • 通过配置文件动态设置 API Key 和 API URL,适应不同环境的调用需求。
  • 支持通过 Base64 编码对输入内容进行压缩。

3.高效的请求处理

  • 使用 OkHttpClient 提供高性能的 HTTP 请求支持,设置了合理的超时时间,确保调用稳定。

2. 配置示例

yaml
sun-rays:
  openai:
    api-key: ${OPENAI_APIKEY} # 必填,OpenAI API Key
    api-url: https://api.openai.com/v1/chat/completions # 可选,有默认值,为了防止以后 URL 变化

3.案例演示

1.创建模块

CleanShot 2025-01-20 at 14.25.52@2x

2.目录结构

CleanShot 2025-01-20 at 14.30.12@2x

3.pom.xml

1.基本配置
xml
<!-- 通过properties来指定版本号 -->
<properties>
    <!-- 指定编译版本 -->
    <java.version>1.8</java.version>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!-- 指定Sunrays-Framework的版本 -->
    <sunrays.version>1.0.0</sunrays.version>
</properties>

<dependencyManagement>
<!-- 使用sunrays-dependencies来管理依赖,则依赖无需加版本号 -->
<dependencies>
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>sunrays-dependencies</artifactId>
        <version>${sunrays.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>
2.引入依赖
xml
<dependencies>
    <!-- 引入common-openai-starter模块 -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-openai-starter</artifactId>
    </dependency>
    <!-- common-log4j2-starter 是必须引入的!!! -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-log4j2-starter</artifactId>
    </dependency>

    <!-- web模块用于测试 -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-web-starter</artifactId>
    </dependency>
    <!-- env模块确保数据安全,可以不引入 -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-env-starter</artifactId>
    </dependency>
</dependencies>

4.application.yml 配置日志根目录、.env文件的绝对路径、openai

yaml
sun-rays:
  log4j2:
    home: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/common-openai-starter-demo/logs # 日志根目录(默认./logs)
  env:
    path: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/common-openai-starter-demo # .env文件的绝对路径
  openai:
    api-key: ${OPENAI_APIKEY} # 必填,OpenAI API Key
    api-url: https://api.openai.com/v1/chat/completions # 可选,有默认值,为了防止以后 URL 变化

5..env 填写openai配置

properties
OPENAI_APIKEY=你的apikey

6.OpenAiController.java 测试询问ai

java
package cn.sunxiansheng.openai.controller;

import cn.sunxiansheng.openai.client.OpenAiClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Description: OpenAI 控制器类
 *
 * @Author sun
 * @Create 2024/12/14 12:27
 * @Version 1.0
 */
@RestController
public class OpenAiController {

    @Resource
    private OpenAiClient openAiClient;

    @RequestMapping("/ask")
    public String ask(String question) {
        String res = openAiClient.askAI("gpt-4o", question, false);
        return "AI回答:" + res;
    }
}

7.OpenAiApplication.java 启动类

java
package cn.sunxiansheng.openai;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: OpenAiApplication
 *
 * @Author sun
 * @Create 2025/1/20 14:28
 * @Version 1.0
 */
@SpringBootApplication
public class OpenAiApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenAiApplication.class, args);
    }
}

8.测试

CleanShot 2025-01-20 at 14.36.24@2x