Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

One way to change the base_url #484

Open
jiangying000 opened this issue Mar 26, 2024 · 3 comments
Open

One way to change the base_url #484

jiangying000 opened this issue Mar 26, 2024 · 3 comments

Comments

@jiangying000
Copy link

public class CustomOpenAiService extends OpenAiService {
    private final ExecutorService executorService;
    private final String openaiBaseUrl = "https://YOU_URL";

    public CustomOpenAiService(String token, Duration timeout) {
        super(token, timeout);
        ObjectMapper mapper = OpenAiService.defaultObjectMapper();
        OkHttpClient client = OpenAiService.defaultClient(token, timeout);

        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(openaiBaseUrl)
            .client(client)
            .addConverterFactory(JacksonConverterFactory.create(mapper))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();

        OpenAiApi customApi = retrofit.create(OpenAiApi.class);

        // 使用反射设置 OpenAiService 类的 api 字段
        try {
            Field apiField = OpenAiService.class.getDeclaredField("api");
            apiField.setAccessible(true);
            apiField.set(this, customApi);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            // 处理异常
            e.printStackTrace();
        }

        this.executorService = client.dispatcher().executorService();
    }
}

use it like:

            OpenAiService service = new CustomOpenAiService(openAiKey.getKey(), Duration.ofSeconds(150));

@jiangying000 jiangying000 changed the title A method to change base_url One way to change the base_url Mar 26, 2024
@jiangying000
Copy link
Author

I tested this, works for my use cases

@Truongkhai97
Copy link

Finally i found it. Thank you so much!

@xyifhgvnlo286
Copy link

I am currently using https://github.com/Lambdua/openai4j As an alternative

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

No branches or pull requests

3 participants