openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Question: Is `OpenAiService` concurrency safe ?

Open jiangying000 opened this issue 1 year ago • 1 comments

Should I create a new OpenAiService instance every time I use it in multithreaded environment ?

jiangying000 avatar Mar 07 '23 15:03 jiangying000

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();

cryptoapebot avatar Mar 07 '23 16:03 cryptoapebot

I also think that okhttp and retrofit are thread safe, so OpenAiService should be too.

TheoKanning avatar Mar 08 '23 01:03 TheoKanning