文本是《工具配置(共30篇)》专题的第 29 篇。阅读本文前,建议先阅读前面的文章:
- 1.沉浸式翻译插件深度解析:从基础配置到高级定制与跨平台应用
- 2.沉浸式翻译:智能双语翻译工具,提升阅读体验与翻译精准度
- 3.ChatBox 配置指南:多平台AI对话工具,支持API Key与本地模型配置
- 4.Continue 插件安装与配置指南:JetBrains IDE 的 AI 辅助利器
- 5.Cursor 安装与配置全攻略:AI 驱动的智能编程助手
- 6.CherryStudio:跨平台AI模型管理与对话工具安装与配置全攻略
- 7.Dify:开源低代码 AI 应用平台 | 一站式构建与部署
- 8.AingDesk | 开源跨平台 AI 桌面客户端 · Windows / macOS / Docker 一站式部署
- 9.VS Code 与 Cline 插件安装及 AI 对话扩展使用指南
- 10.Zed 轻量级现代代码编辑器:性能、协作与 AI 集成
- 11.DeepChat 免费桌面智能助手|多模型接入·多模态交互·隐私安全
- 12.Void AI编辑器完全指南:免费开源Cursor替代品安装配置教程 | AI编程工具
- 13.探索前沿AI对话:LibreChat部署的深度洞察与最佳实践
- 14.Sider 配置AI模型指南
- 15.Cursor AI代码编辑器完整使用指南 – 下载安装配置教程2025
- 16.Trae AI 安装与使用教程 | 最强 AI 编程助手配置指南
- 17.2025最新IntelliJ IDEA 安装与使用全指南:版本选择、插件配置与AI助手集成
- 18.Glarity浏览器插件完整指南:免费开源AI网页摘要与翻译助手
- 19.Claude Code CLI 安装与配置完整教程 | 支持 Windows 与 macOS 的 AI 编程助手
- 20.91协商写作平台
- 21.Claude-Python示例代码
- 22.OpenAI-image-Python示例代码
- 23.Gemini-Python示例代码
- 24.OpenAI-Java示例代码
- 25.Rerank-python代码配置
- 26.Python分析文件代码示例
- 27.Python配置openAI使用音视频图片对话
- 28.OpenAI-Java示例代码
1. 资源准备
- API Key:此项配置填写在一步API官网创建API令牌,一键直达API令牌创建页面
- 创建API令牌步骤请参考API Key的获取和使用
- API Host:此项配置填写https://yibuapi.com/v1
- 查看支持的模型请参在这里复制模型在线查询
2. pom依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
3. 实现代码
import com.google.gson.*;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class Java8AnthropicExample {
/**
* 这里填写在https://yibuapi.com网站上创建的API Key
*/
private static final String API_KEY = "sk-xxxx";
/**
* 这里填写https://yibuapi.com/v1
*/
private static final String BASE_URL = "https://yibuapi.com/v1";
private static final String QUESTIONS = "生成三个虚构的中文书名及其作者和类型的清单。\n" +
"使用以下键以 JSON 格式提供它们:book_id, title, author, genre.";
public static void main(String[] args) {
try {
String response = getAnthropicResponse(QUESTIONS, API_KEY, BASE_URL);
System.out.println("回答: " + response);
} catch (Exception e) {
System.out.println("请求失败: " + e.getMessage());
}
}
private static String getAnthropicResponse(String question, String apiKey, String baseUrl) throws Exception {
HttpURLConnection connection = null;
try {
// 创建连接
URL url = new URL(baseUrl + "/messages");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("x-api-key", apiKey);
connection.setRequestProperty("anthropic-version", "2023-06-01");
connection.setDoOutput(true);
String requestBody = buildRequestBody(question);
// 调试输出请求体(生产环境建议删除)
System.out.println("请求体内容:\n" + requestBody);
// 发送请求
try (OutputStream os = connection.getOutputStream()) {
byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// 处理响应
int statusCode = connection.getResponseCode();
if (statusCode != 200) {
// 获取错误响应详情
String errorResponse = readErrorStream(connection);
return "请求失败: HTTP " + statusCode + "\n错误详情: " + errorResponse;
}
try (BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine);
}
return parseResponse(response.toString());
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
private static String buildRequestBody(String question) {
JsonObject message = new JsonObject();
message.addProperty("role", "user");
message.addProperty("content", question); // 直接使用字符串内容
JsonArray messages = new JsonArray();
messages.add(message);
JsonObject requestBody = new JsonObject();
requestBody.addProperty("model", "claude-3-7-sonnet-latest");
requestBody.addProperty("max_tokens", 1024);
requestBody.add("messages", messages);
return requestBody.toString();
}
private static String parseResponse(String jsonResponse) throws Exception {
try {
JsonObject obj = new JsonParser().parse(jsonResponse).getAsJsonObject();
JsonArray content = obj.getAsJsonArray("content");
if (content.size() == 0) {
throw new Exception("响应内容为空");
}
JsonObject firstContent = content.get(0).getAsJsonObject();
return firstContent.get("text").getAsString();
} catch (JsonSyntaxException e) {
throw new Exception("JSON解析失败: " + e.getMessage());
}
}
// 新增错误流读取方法
private static String readErrorStream(HttpURLConnection connection) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine);
}
return response.toString();
} catch (Exception e) {
return "无法读取错误信息: " + e.getMessage();
}
}
}
4. 运行效果
您已阅读完《工具配置(共30篇)》专题的第 29 篇。请继续阅读该专题下面的文章: