You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 6, 2024. It is now read-only.
publicstaticvoidmain(String[] args) throwsUnknownHostException, InterruptedException {
try {
ObjectMappermapper = defaultObjectMapper();
OkHttpClientclient = defaultClient("", Duration.of(10000L,ChronoUnit.SECONDS))
.newBuilder()
.build();
Retrofitretrofit = defaultRetrofit(client, mapper);
Class<Retrofit> clazz = Retrofit.class;
FieldbaseUrl = clazz.getDeclaredField("baseUrl");
baseUrl.setAccessible(true);
baseUrl.set(retrofit, HttpUrl.get(BASE_URL));
OpenAiApiapi = retrofit.create(OpenAiApi.class);
OpenAiServiceservice = newOpenAiService(api);
List<ChatMessage> messages = Lists.newArrayList();
messages.add(newChatMessage("system", "Please use the functions provided below to determine what function needs to be called for the user's problem. " +
"If the necessary parameters are missing when calling the function, please return to the user in this format and prompt the user to pass the necessary parameters:\n" +
"We also need the following information to complete your request: Required Parameter 1, Required Parameter 2\n" +
"Make sure your prompts are accurate, polite, and the directly relevant information is obvious and understandable to users"));
Scannerscanner = newScanner(System.in);
//"Tell me the weather"messages.add(newChatMessage("user", scanner.nextLine()));
while (true) {
ChatFunctionDynamicchatFunctionDynamic = getChatFunctionDynamic();
ChatCompletionRequestchatCompletionRequest = ChatCompletionRequest
.builder()
.model("qwen15-110b.credit-llm")
.messages(messages)
.n(1)
.maxTokens(256)
.functions(Lists.newArrayList(chatFunctionDynamic))
.functionCall(ChatCompletionRequest.ChatCompletionRequestFunctionCall.of("auto"))
.build();
Flowable<ChatCompletionChunk> flowable = service.streamChatCompletion(chatCompletionRequest);
AtomicBooleanisFirst = newAtomicBoolean(true);
ChatMessageresponseMessage = service.mapStreamToAccumulator(flowable).doOnNext(accumulator -> {
if (accumulator.isFunctionCall()) {
ChatFunctionCallfunctionCall = accumulator.getAccumulatedChatFunctionCall();
if (isFirst.getAndSet(false)) {
System.out.println("Executing function " + functionCall.getName() + "...");
}
} else {
if (isFirst.getAndSet(false)) {
System.out.print("Response: ");
}
if (accumulator.getMessageChunk().getContent() != null) {
System.out.print(accumulator.getMessageChunk().getContent());
}
}
})
.doOnComplete(System.out::println)
.lastElement()
.blockingGet()
.getAccumulatedMessage();
messages.add(responseMessage);
ChatFunctionCallfunctionCall = responseMessage.getFunctionCall();
if (functionCall != null) {
if (functionCall.getName().equals("get_weather")) {
Stringlocation = functionCall.getArguments().get("location").asText();
Stringunit = functionCall.getArguments().get("unit").asText();
WeatherResponseweather = getWeather(location, unit);
ChatMessageweatherMessage = newChatMessage(ChatMessageRole.FUNCTION.value(), JSON.toJSONString(weather), "get_weather");
messages.add(weatherMessage);
continue;
}
}
System.out.print("Next Query: ");
StringnextLine = scanner.nextLine();
if (nextLine.equalsIgnoreCase("exit")) {
System.exit(0);
}
messages.add(newChatMessage(ChatMessageRole.USER.value(), nextLine));
}
} catch (Exceptione) {
e.printStackTrace();
}
}
privatestaticWeatherResponsegetWeather(Stringlocation, Stringunit) {
returnnewWeatherResponse(location, WeatherUnit.valueOf(unit), newRandom().nextInt(40), "sunny");
}
publicstaticChatFunctionDynamicgetChatFunctionDynamic() {
returnChatFunctionDynamic.builder()
.name("get_weather")
.description("Get the current weather of a location")
.addProperty(ChatFunctionProperty.builder()
.name("location")
.type("string")
.description("City and state, for example: León, Guanajuato")
.build())
.addProperty(ChatFunctionProperty.builder()
.name("unit")
.type("string")
.description("The temperature unit, can be 'CELSIUS' or 'FAHRENHEIT'")
.enumValues(newHashSet<>(Arrays.asList("CELSIUS", "FAHRENHEIT")))
.required(true)
.build())
.build();
}
这个是我的示例代码:
对应的报错信息,在String location = functionCall.getArguments().get("location").asText();该行报错
debug代码查看

com.theokanning.openai.service.OpenAiService#mapStreamToAccumulator方法中messageChunk中的arguments类型为objectNode,从而导致asText()方法返回的结果为""
请问我可以用什么简单的方法在不修改源代码的情况下来解决这个问题,非常感谢!