1
resposta

Erro com assistant_id

Boa noite. Fiz a alteração da OpenAi para pegar a versão v2 do assistant, porém, está dando o erro abaixo.

2
025-02-17T22:34:19.175-03:00 ERROR 12164 --- [nio-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: com.theokanning.openai.OpenAiHttpException: Missing required parameter: 'assistant_id'. You provided 'assistantId', did you mean to provide 'assistant_id'?] with root cause

retrofit2.adapter.rxjava2.HttpException: HTTP 400 
 public OpenAIClient(@Value("${openai.api.key}") String apiKey,
                        @Value("${openai.assistant.id}") String apiAssistantId) {
        this.apiKey = apiKey;
        this.apiAssistantId = apiAssistantId;
        this.service = new OpenAiService(configOpenAiApi(apiKey));
    }

    public String enviarRequisicaoAssistant(DadosRequisicaoChatCompletion dados) {
        var messageRequest = MessageRequest
                .builder()
                .role(ChatMessageRole.USER.value())
                .content(dados.promptUsuario())
                .build();

        if (this.threadId == null) {
            var threadRequest = ThreadRequest
                    .builder()
                    .messages(Arrays.asList(messageRequest))
                    .build();

            var thread = service.createThread(threadRequest);
            this.threadId = thread.getId();
        } else {
            service.createMessage(this.threadId, messageRequest);
        }


        var runRequest = RunCreateRequest
                .builder()
                .assistantId(apiAssistantId)
                .build();
        var run = service.createRun(threadId, runRequest);

        try {
            while (!run.getStatus().equalsIgnoreCase("completed")) {
                Thread.sleep(1000 * 10);
                run = service.retrieveRun(threadId, run.getId());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        var mensagens = service.listMessages(threadId);
        var respostaAssistente = mensagens
                .getData()
                .stream()
                .sorted(Comparator.comparingInt(Message::getCreatedAt).reversed())
                .findFirst().get().getContent().get(0).getText().getValue();

        return respostaAssistente;
    }
    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(RxJava2CallAdapterFactory.create())
                .build();

        return retrofit.create(OpenAiApi.class);
    }
1 resposta

Eu dei um sysout no RunCreateRequest e apresentou isso, porém não estou conseguindo converter o objeto para enviar o dado conforme o necessário. Poderiam ajudar?

headers=[Authorization:Bearer{{myApiKey}}, OpenAI-Beta:assistants=v2, Content-Type:application/json], tags={class retrofit2.Invocation=com.theokanning.openai.client.OpenAiApi.createRun() [thread{{MythreadId}}, RunCreateRequest(assistantId={{myAssistantId}}JYFwV, model=null, instructions=null, tools=null, metadata=null)]}}