openai-java
openai-java copied to clipboard
Question: Is `OpenAiService` concurrency safe ?
Should I create a new OpenAiService instance every time I use it in multithreaded environment ?
I think the dependent libraries for okhttp3 and others are threadsafe. Depending on how big your environment is, there are limitations per openAI key calls per minute, but also you may want to actually build and tune your own ExecutorService and fold it in depending on what you are trying to do.
public static final ExecutorService executorService = Executors.newFixedThreadPool(16);
public static final HttpClient httpClient = HttpClient.newBuilder()
.executor(executorService)
.version(HttpClient.Version.HTTP_2)
.connectTimeout(timeout)
.build();
I also think that okhttp and retrofit are thread safe, so OpenAiService should be too.