java调用claude api实现ai问答示例代码
代码语言:java
所属分类:其他
代码描述:java调用claude api实现ai问答示例代码
代码标签: java 调用 claude api ai 问答 示例 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.URI; import org.json.JSONObject; import org.json.JSONArray; //申请key地址:https://console.anthropic.com/account/keys public class ClaudeApiClient { private static final String API_URL = "https://api.anthropic.com/v1/messages"; private static final String API_KEY = "YOUR_API_KEY_HERE"; public static void main(String[] args) { try { String response = sendMessage("What is the capital of France?"); System.out.println("Claude's answer: " + response); } catch (Exception e) { e.printStackTrace(); } } public static String sendMessage(String message) throws Exception { HttpClient client = HttpClient.newHttpClient(); // 创建请求体 JSONObject requestBody = new JSONObject(); requestBody.put("model", "claude-3-opus-20240229"); JSONArray messages = new JSONArray(); JSONObject userMessage = new JSONObject(); userMessage.put("ro.........完整代码请登录后点击上方下载按钮下载查看
网友评论0