Skip to content

Commit

Permalink
claude3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
caochengxiang committed Jun 21, 2024
1 parent 3f91cb4 commit 16f5df8
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit 16f5df8

Please sign in to comment.