TheoKanning / openai-java

OpenAI Api Client in Java
MIT License
4.68k stars 1.16k forks source link

Cannot access OpenRouter models #506

Open stevevestal opened 1 month ago

stevevestal commented 1 month ago

Another member of our team is able to access an OpenRouter model using his Python client. Here are the parameters (in Java-speak).

String userKey = "<user key for openrouter>";
String model = "mistralai/mistral-7b-instruct:free";
String baseURL ="https://openrouter.ai/api/v1/";

Below are two variations of code I have used. With the above parameters, I get an HTTP 404 when the final line in each variant is executed. If I use the parameters

      String userKey = "<user key for openai>";
      String baseURL = "https://api.openai.com/"; 
      String model = "babbage-002"; 

then both of the following at least get to the point of demanding more money.

This one uses #454

        OkHttpClient okHttpClient = OpenAiService.defaultClient(userKey, Duration.ofSeconds(10));
        Retrofit retrofitX = OpenAiService.defaultRetrofit(okHttpClient, OpenAiService.defaultObjectMapper()).newBuilder()
                .baseUrl(baseURL)
                .build();
        OpenAiApi openAiApi = retrofitX.create(OpenAiApi.class);
        OpenAiService service = new OpenAiService(openAiApi, okHttpClient.dispatcher().executorService());       
        System.out.println("\nCreating completion...");
        CompletionRequest completionRequest = CompletionRequest.builder()
                .model(model)
                .prompt("Somebody once told me the world is gonna roll me")
                .echo(true)
                .user("testing")
                .n(3)
                .build();
        System.out.println("\nCompletionRequest built");
        service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
        System.out.println("\nCompletion created");      

Here is another one I saw somewhere.

        ObjectMapper mapper = OpenAiService.defaultObjectMapper();
        OkHttpClient client = OpenAiService.defaultClient(userKey, Duration.ofSeconds(30))
                .newBuilder()
                .build();
        Retrofit retrofit = null;
        try {
            retrofit = OpenAiService.defaultRetrofit(client, mapper)
                    .newBuilder()
                    .baseUrl(new URL(baseURL))
                    .build();
        } catch (MalformedURLException e) {
            return;
        }
        OpenAiApi api = retrofit.create(OpenAiApi.class);
        OpenAiService service = new OpenAiService(api);      

        System.out.println("\nCreating completion...");
        CompletionRequest completionRequest = CompletionRequest.builder()
                .model(model)
                .prompt("Somebody once told me the world is gonna roll me")
                .echo(true)
                .user("testing")
                .n(3)
                .build();
        System.out.println("\nCompletionRequest built");
        service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
vacuityv commented 1 month ago

@stevevestal since this repo had no merge for a long time. I think you can try my repo.