From 16f5df8cad3cd01e33422e94dc348a3e342383f5 Mon Sep 17 00:00:00 2001 From: zhangping17 Date: Fri, 21 Jun 2024 20:20:04 +0800 Subject: [PATCH] claude3.5 --- .../java/run/mone/ai/google/CloudeClient.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.java b/jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.java index 63f71c457..67e929be4 100644 --- a/jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.java +++ b/jcommon/ai/google/src/main/java/run/mone/ai/google/CloudeClient.java @@ -84,4 +84,35 @@ public ResponsePayload call(String token, RequestPayload requestPayload) { return null; } + public ResponsePayload call(String url ,String token, RequestPayload requestPayload) { + OkHttpClient client = new OkHttpClient.Builder().readTimeout(5, TimeUnit.MINUTES).build(); + MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(mediaType, new Gson().toJson(requestPayload)); + Request request = new Request.Builder() + .url(url) + .post(body) + .addHeader("Authorization", "Bearer " + token) + .addHeader("Content-Type", "application/json; charset=utf-8") + .build(); + + try (Response response = client.newCall(request).execute()) { + if (response.code() == 429) { + ResponsePayload res = new ResponsePayload(); + Content content = new Content(); + content.setText(gson.toJson(ImmutableMap.of("message", "被claude3限流了", "code", "429"))); + log.info("claude res:{}", content.getText()); + res.setContent(Lists.newArrayList(content)); + return res; + } + if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); + // Handle the response + String res = response.body().string(); + log.info("claude3 res:{}", res); + return new Gson().fromJson(res, ResponsePayload.class); + } catch (Throwable e) { + log.error(e.getMessage(), e); + } + return null; + } + }