Java大模型调用案例

wen java案例 2

本文目录导读:

Java大模型调用案例

  1. OpenAI API调用示例
  2. 使用Spring Boot封装完整服务
  3. 控制器使用示例
  4. 配置文件
  5. 使用示例
  6. 注意事项

我来为您提供一个完整的Java调用大模型(LLM)的案例,涵盖主流API调用方式。

OpenAI API调用示例

import okhttp3.*;
import com.google.gson.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class OpenAIClient {
    private static final String API_KEY = "your-api-key";
    private static final String API_URL = "https://api.openai.com/v1/chat/completions";
    private final OkHttpClient client;
    private final Gson gson;
    public OpenAIClient() {
        this.client = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .build();
        this.gson = new GsonBuilder().create();
    }
    /**
     * 调用ChatGPT模型
     */
    public String chat(String userMessage) throws IOException {
        // 构建请求体
        JsonObject requestBody = new JsonObject();
        requestBody.addProperty("model", "gpt-3.5-turbo");
        requestBody.addProperty("temperature", 0.7);
        requestBody.addProperty("max_tokens", 1000);
        // 添加消息
        JsonArray messages = new JsonArray();
        JsonObject systemMessage = new JsonObject();
        systemMessage.addProperty("role", "system");
        systemMessage.addProperty("content", "You are a helpful assistant.");
        messages.add(systemMessage);
        JsonObject userMsg = new JsonObject();
        userMsg.addProperty("role", "user");
        userMsg.addProperty("content", userMessage);
        messages.add(userMsg);
        requestBody.add("messages", messages);
        // 构建HTTP请求
        Request request = new Request.Builder()
            .url(API_URL)
            .addHeader("Authorization", "Bearer " + API_KEY)
            .addHeader("Content-Type", "application/json")
            .post(RequestBody.create(
                MediaType.parse("application/json"),
                gson.toJson(requestBody)
            ))
            .build();
        // 发送请求
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("API调用失败: " + response.code());
            }
            String responseBody = response.body().string();
            return parseResponse(responseBody);
        }
    }
    /**
     * 解析响应
     */
    private String parseResponse(String jsonResponse) {
        JsonObject jsonObject = gson.fromJson(jsonResponse, JsonObject.class);
        JsonArray choices = jsonObject.getAsJsonArray("choices");
        if (choices.size() > 0) {
            JsonObject firstChoice = choices.get(0).getAsJsonObject();
            JsonObject message = firstChoice.getAsJsonObject("message");
            return message.get("content").getAsString();
        }
        return "";
    }
}

使用Spring Boot封装完整服务

// pom.xml 依赖配置
/*
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.12.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.10.1</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>
*/
// 配置类
@Configuration
@ConfigurationProperties(prefix = "llm")
@Data
public class LLMConfig {
    private String apiKey;
    private String baseUrl;
    private String model = "gpt-3.5-turbo";
    private int maxTokens = 1000;
    private double temperature = 0.7;
    private int timeoutSeconds = 60;
}
// 服务类
@Service
@Slf4j
public class LLMService {
    @Autowired
    private LLMConfig config;
    private OkHttpClient client;
    private Gson gson;
    @PostConstruct
    public void init() {
        this.client = new OkHttpClient.Builder()
            .connectTimeout(config.getTimeoutSeconds(), TimeUnit.SECONDS)
            .readTimeout(config.getTimeoutSeconds(), TimeUnit.SECONDS)
            .writeTimeout(config.getTimeoutSeconds(), TimeUnit.SECONDS)
            .build();
        this.gson = new GsonBuilder().create();
    }
    /**
     * 基础对话
     */
    public ChatResponse chat(ChatRequest request) throws LLMException {
        try {
            JsonObject payload = buildPayload(request);
            Request httpRequest = buildHttpRequest(payload);
            try (Response response = client.newCall(httpRequest).execute()) {
                if (!response.isSuccessful()) {
                    throw new LLMException("API调用失败,状态码: " + response.code());
                }
                String responseBody = response.body().string();
                return parseChatResponse(responseBody);
            }
        } catch (IOException e) {
            log.error("LLM调用异常", e);
            throw new LLMException("LLM服务调用失败", e);
        }
    }
    /**
     * 流式调用
     */
    public void chatStream(ChatRequest request, StreamCallback callback) {
        try {
            JsonObject payload = buildPayload(request);
            Request httpRequest = new Request.Builder()
                .url(config.getBaseUrl() + "/chat/completions")
                .addHeader("Authorization", "Bearer " + config.getApiKey())
                .addHeader("Content-Type", "application/json")
                .post(RequestBody.create(
                    MediaType.parse("application/json"),
                    gson.toJson(payload)
                ))
                .build();
            // 异步流式处理
            client.newCall(httpRequest).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    callback.onError(e);
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (!response.isSuccessful()) {
                        callback.onError(new IOException("HTTP " + response.code()));
                        return;
                    }
                    try (BufferedReader reader = new BufferedReader(
                            new InputStreamReader(response.body().byteStream()))) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            if (line.startsWith("data: ")) {
                                String data = line.substring(6);
                                if (!"[DONE]".equals(data)) {
                                    callback.onToken(parseStreamToken(data));
                                }
                            }
                        }
                        callback.onComplete();
                    }
                }
            });
        } catch (Exception e) {
            callback.onError(e);
        }
    }
    /**
     * 构建请求负载
     */
    private JsonObject buildPayload(ChatRequest request) {
        JsonObject payload = new JsonObject();
        payload.addProperty("model", request.getModel() != null ? 
            request.getModel() : config.getModel());
        payload.addProperty("temperature", config.getTemperature());
        payload.addProperty("max_tokens", config.getMaxTokens());
        JsonArray messages = new JsonArray();
        // 添加系统消息
        JsonObject systemMsg = new JsonObject();
        systemMsg.addProperty("role", "system");
        systemMsg.addProperty("content", request.getSystemPrompt() != null ? 
            request.getSystemPrompt() : "You are a helpful assistant.");
        messages.add(systemMsg);
        // 添加用户消息(支持多轮对话)
        for (Message msg : request.getMessages()) {
            JsonObject message = new JsonObject();
            message.addProperty("role", msg.getRole());
            message.addProperty("content", msg.getContent());
            messages.add(message);
        }
        payload.add("messages", messages);
        // 如果需要流式输出
        if (request.isStream()) {
            payload.addProperty("stream", true);
        }
        return payload;
    }
    /**
     * 构建HTTP请求
     */
    private Request buildHttpRequest(JsonObject payload) {
        return new Request.Builder()
            .url(config.getBaseUrl() + "/chat/completions")
            .addHeader("Authorization", "Bearer " + config.getApiKey())
            .addHeader("Content-Type", "application/json")
            .post(RequestBody.create(
                MediaType.parse("application/json"),
                gson.toJson(payload)
            ))
            .build();
    }
    /**
     * 解析聊天响应
     */
    private ChatResponse parseChatResponse(String jsonResponse) {
        JsonObject jsonObject = gson.fromJson(jsonResponse, JsonObject.class);
        ChatResponse chatResponse = new ChatResponse();
        // 解析消息
        JsonArray choices = jsonObject.getAsJsonArray("choices");
        if (choices.size() > 0) {
            JsonObject firstChoice = choices.get(0).getAsJsonObject();
            JsonObject message = firstChoice.getAsJsonObject("message");
            chatResponse.setContent(message.get("content").getAsString());
            chatResponse.setRole(message.get("role").getAsString());
        }
        // 解析使用情况
        if (jsonObject.has("usage")) {
            JsonObject usage = jsonObject.getAsJsonObject("usage");
            UsageInfo usageInfo = new UsageInfo();
            usageInfo.setPromptTokens(usage.get("prompt_tokens").getAsInt());
            usageInfo.setCompletionTokens(usage.get("completion_tokens").getAsInt());
            usageInfo.setTotalTokens(usage.get("total_tokens").getAsInt());
            chatResponse.setUsage(usageInfo);
        }
        // 解析ID和时间戳
        if (jsonObject.has("id")) {
            chatResponse.setId(jsonObject.get("id").getAsString());
        }
        if (jsonObject.has("created")) {
            chatResponse.setCreated(jsonObject.get("created").getAsLong());
        }
        return chatResponse;
    }
}
// 请求模型类
@Data
@Builder
public class ChatRequest {
    private String systemPrompt;
    private List<Message> messages;
    private String model;
    private boolean stream;
    public static ChatRequestBuilder of() {
        return ChatRequest.builder();
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Message {
    private String role; // "user", "assistant", "system"
    private String content;
}
// 响应模型类
@Data
public class ChatResponse {
    private String id;
    private String role;
    private String content;
    private long created;
    private UsageInfo usage;
}
@Data
public class UsageInfo {
    private int promptTokens;
    private int completionTokens;
    private int totalTokens;
}
// 异常类
public class LLMException extends Exception {
    public LLMException(String message) {
        super(message);
    }
    public LLMException(String message, Throwable cause) {
        super(message, cause);
    }
}
// 流式回调接口
public interface StreamCallback {
    void onToken(String token);
    void onComplete();
    void onError(Exception e);
}

控制器使用示例

@RestController
@RequestMapping("/api/llm")
@Slf4j
public class LLMController {
    @Autowired
    private LLMService llmService;
    /**
     * 简单对话接口
     */
    @PostMapping("/chat")
    public ResponseEntity<ChatResponse> chat(@RequestBody ChatRequest request) {
        try {
            ChatResponse response = llmService.chat(request);
            return ResponseEntity.ok(response);
        } catch (LLMException e) {
            log.error("LLM调用失败", e);
            return ResponseEntity.status(HttpStatus.BAD_GATEWAY)
                .body(null);
        }
    }
    /**
     * 流式对话接口
     */
    @PostMapping("/chat/stream")
    public ResponseEntity<ChatResponse> chatStream(@RequestBody ChatRequest request) {
        try {
            // 这里可以改为异步处理和SSE推送
            ChatResponse response = llmService.chat(request);
            return ResponseEntity.ok(response);
        } catch (LLMException e) {
            log.error("LLM调用失败", e);
            return ResponseEntity.status(HttpStatus.BAD_GATEWAY)
                .body(null);
        }
    }
    /**
     * 测试接口
     */
    @GetMapping("/test")
    public String test() {
        ChatRequest request = ChatRequest.of()
            .messages(Arrays.asList(
                new Message("user", "你好,请介绍一下你自己")
            ))
            .build();
        try {
            ChatResponse response = llmService.chat(request);
            return response.getContent();
        } catch (LLMException e) {
            return "调用失败: " + e.getMessage();
        }
    }
}

配置文件

# application.yml
llm:
  api-key: your-api-key-here
  base-url: https://api.openai.com/v1
  model: gpt-3.5-turbo
  max-tokens: 1000
  temperature: 0.7
  timeout-seconds: 60
spring:
  application:
    name: llm-spring-boot-demo

使用示例

// 测试调用
public class LLMTest {
    public static void main(String[] args) {
        OpenAIClient client = new OpenAIClient();
        try {
            String response = client.chat("解释一下什么是机器学习?");
            System.out.println("响应: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注意事项

  1. API密钥安全:不要在代码中硬编码API密钥,应使用环境变量或配置中心
  2. 错误处理:实现完善的错误重试机制和降级策略
  3. 限流控制:注意API调用的频率限制
  4. 数据安全:敏感数据不要发送到LLM服务
  5. 成本控制:合理设置max_tokens和缓存策略
  6. 并发处理:使用线程池管理并发请求

这个案例提供了完整的Java调用大模型的实现,可以根据实际需求调整适配不同的LLM服务提供商(如OpenAI、Anthropic、百度文心等)。

抱歉,评论功能暂时关闭!