JavaでChatGPTクローンを作りたくて,色々な記事を見ながら動かしてみたのですが,エラーがでてうまくいかないため助けてほしいです.
主に参考にしているのはこちらの記事で,TheoKanning/openai-javaを利用しています.
EclipseでMavenを使って下記のコードを実行しても,結果表示が出る前にエラーとなってしまいます.
どうやら20行目のところでタイムアウトしているようですが,原因がよくわからず困っています.
あとこの件と関係があるのかはわかりませんが,Eclipse上でソースコードのOpenAiService
の部分に取り消し線がついているのですが,これはなぜなのでしょうか.
以上の問題について,ご回答いただけますと幸いです.
Java初心者なため分かっていないことが多いと思います.何か不足している情報があれば申し付けください.
問題のJavaソースコード
Java
1package test_gpt; 2 3import com.theokanning.openai.OpenAiService; 4import com.theokanning.openai.completion.CompletionChoice; 5import com.theokanning.openai.completion.CompletionRequest; 6 7@SuppressWarnings("deprecation") 8public class GPT3Chatbot { 9 public static void main(final String[] args) { 10 final var token = "APIキー"; 11 final var service = new OpenAiService(token); 12 final var prompt = "JavaでGPT3のAPIを使う方法を教えてください."; 13 14 System.out.println(prompt); 15 final var completionRequest = CompletionRequest.builder() 16 .model("text-davinci-003") 17 .prompt(prompt) 18 .maxTokens(512) 19 .build(); 20 final var completionResult = service.createCompletion(completionRequest); 21 System.out.println(">> "); 22 final var choiceList = completionResult.getChoices(); 23 for (final CompletionChoice choice : choiceList) { 24 System.out.println(choice); 25 } 26 } 27}
エラーメッセージ
JavaをでGPT3のAPIを使う方法を教えてください. Exception in thread "main" java.lang.RuntimeException: java.net.SocketTimeoutException: timeout at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:45) at io.reactivex.internal.observers.BlockingMultiObserver.blockingGet(BlockingMultiObserver.java:90) at io.reactivex.Single.blockingGet(Single.java:2002) at com.theokanning.openai.OpenAiService.createCompletion(OpenAiService.java:122) at test_gpt.GPT3Chatbot.main(GPT3Chatbot.java:20) Caused by: java.net.SocketTimeoutException: timeout at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.java:678) at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:686) at okhttp3.internal.http2.Http2Stream.takeHeaders(Http2Stream.java:154) at okhttp3.internal.http2.Http2ExchangeCodec.readResponseHeaders(Http2ExchangeCodec.java:136) at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115) at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) at com.theokanning.openai.AuthenticationInterceptor.intercept(AuthenticationInterceptor.java:26) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229) at okhttp3.RealCall.execute(RealCall.java:81) at retrofit2.OkHttpCall.execute(OkHttpCall.java:204) at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:46) at io.reactivex.Observable.subscribe(Observable.java:10151) at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:35) at io.reactivex.Observable.subscribe(Observable.java:10151) at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35) at io.reactivex.Single.subscribe(Single.java:2517) at io.reactivex.Single.blockingGet(Single.java:2001) ... 2 more
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/05/05 09:42
2023/05/06 01:18 編集
2023/05/06 03:39