Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我想用java连接这个api,这么写有什么问题吗,总是只有404 #40

Closed
Stillily opened this issue Jun 16, 2023 · 3 comments

Comments

@Stillily
Copy link

OkHttpClient client = new OkHttpClient();

    String messages="[{'role': 'user','content': '鲁迅和周树人的关系'},]";
    JSONObject requestBody = new JSONObject();
    requestBody.put("model", "gpt-3.5-turbo");
    requestBody.put("messages", messages);
    requestBody.put("stream", false);
    RequestBody body = RequestBody.create(MediaType.parse("application/json"), requestBody.toString());
    Request request = new Request.Builder()
            .url("https://api.chatanywhere.com.cn/v1")
            .addHeader("Authorization", "Bearer sk-Wstozi(……)kyQgyfUOxrjpDG3Y")
            .post(body)
            .build();
    Call call = client.newCall(request);
    Response response = call.execute();
    String responseBody = response.body().string();
    System.out.println(responseBody);
@loukai99
Copy link

看起来你是接入点url是错的,应该是https://api.chatanywhere.com.cn/v1/chat/completions

@Stillily
Copy link
Author

看起来你是接入点url是错的,应该是https://api.chatanywhere.com.cn/v1/chat/completions

谢谢,然后就报这个错,应该是服务器传过来的,是表示我的消息的格式不对吗

JSON parse error: Cannot deserialize value of type java.util.ArrayList<cn.chatanywhere.openaiapi.entity.chat.Message> from String value (token JsonToken.VALUE_STRING); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type java.util.ArrayList<cn.chatanywhere.openaiapi.entity.chat.Message> from String value (token JsonToken.VALUE_STRING)
at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 28] (through reference chain: cn.chatanywhere.openaiapi.entity.chat.ChatCompletion["messages"])

@Stillily
Copy link
Author

OK了,终于可以了,String json = "{"model": "gpt-3.5-turbo","stream": true,"messages": [{"role": "user","content": "鲁迅和周树人的关系"}]}";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants