Pesquisando bastante, descobri que o codigo não funciona porque a versão da assistente v1 foi descontinuada. Desta forma precisa atualizar o codigo para funcionar na versão v2. Porém ficou muito baixo nível e ainda não funciona. Teria como mostrar um exemplo utilizando a assistente na versao 2 ?
o stacktrace:
2025-01-04T18:35:44.110-03:00 ERROR 32112 --- [io-8080-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.Single<com.theokanning.openai.threads.Thread> for method OpenAiApi.createThread] with root cause
java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.Single<com.theokanning.openai.threads.Thread>. Tried:
- retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
- retrofit2.CompletableFutureCallAdapterFactory
########################################CODIGO_FONTE#########################################
public String enviarRequisicaoAssistant(DadosRequisicaoChatCompletion dados) {
var messageRequest = MessageRequest
.builder()
.role(ChatMessageRole.USER.value())
.content(dados.promptUsuario())
.build();
createMessageAssistant(messageRequest);
var ruRequest = RunCreateRequest
.builder()
.assistantId(assistantId)
.build();
var run = service.createRun(threadId, ruRequest);
try {
while (!"completed".equalsIgnoreCase(run.getStatus())) {
java.lang.Thread.sleep(2000);
run = service.retrieveRun(threadId, run.getId());
}
} catch (InterruptedException e) {
java.lang.Thread.currentThread().interrupt();
throw new RuntimeException("Erro ao aguardar o processamento da execução.", e);
} catch (Exception e) {
throw new RuntimeException("Erro durante a execução da requisição.", e);
}
var messages = service.listMessages(threadId);
return messages
.getData()
.stream()
.filter(msg -> "assistant".equalsIgnoreCase(msg.getRole()))
.max(Comparator.comparingInt(Message::getCreatedAt))
.map(msg -> msg.getContent().get(0).getText().getValue())
.orElse("Erro: Nenhuma resposta encontrada.");
}
private OpenAiApi configOpenAiApi(String apiKey) {
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request request = original.newBuilder()
.header("Authorization", "Bearer " + apiKey)
.header("OpenAI-Beta", "assistants=v2")
.header("Content-Type", "application/json")
.build();
return chain.proceed(request);
})
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.openai.com/v1/assistants/")
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.build();
return retrofit.create(OpenAiApi.class);
}
private void createMessageAssistant(MessageRequest messageRequest) {
Thread thread;
if (threadId == null) {
var threadRequest = ThreadRequest
.builder()
.messages(Collections.singletonList(messageRequest))
.build();
thread = service.createThread(threadRequest);
threadId = thread.getId();
} else {
service.createMessage(threadId, messageRequest);
}
}