hello, I've been asking some questions about the technology that is most talked about at the moment, and I implemented a simple app. I hope it's useful to everyone.
Description
Description
Attachments
Last edited:
'This class is based on:
'https://www.b4x.com/android/forum/threads/gpt-3.145654/#content
'by Abdull Cadre
'
'All I have done is extract Abdull's code essential component (subroutine
'[generate_gpt3_response] in [ChatUI] activity) turned it into a class, tidied
'it up a bit, anglicized it and documented extensively
'
'All basically for my own education - many thanks to Abdull for doing all
'original research
Sub Class_Globals
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
End Sub
Public Sub Query(query_string As String) As ResumableSub
Try
'Following parameter explanations I have primarily taken from
'https://accessibleai.dev/post/generating_text_with_gpt_and_python/
'with a few additions from various googlings
'More googling and I found the definitive source:
'https://platform.openai.com/docs/api-reference/introduction
'there are a lot more options than used in this class
'"n": 1
'number of completions to generate
'"stop": "None"
'an optional setting to control response generation
'"model": "text-davinci-003"
'model to be used
'see https://beta.openai.com/docs/models/gpt-3
'"max_tokens": 350
'maximum tokens in prompt AND response
'"temperature": 0.5
'level of creativity in response
'a higher value means model will take more risks, try 0.9 for more
'creative applications, and 0 for ones with a well-defined answer
Dim m As Map = CreateMap("n": 1, "stop": "None", "model": "text-davinci-003", _
"prompt": query_string, "max_tokens": 350, "temperature": 0.5)
Dim js As JSONGenerator
js.Initialize(m)
'Uncomment this if you want to see raw JSON string generated
'Log(js.ToString)
Dim response As String
Dim req As HttpJob
req.Initialize("", Me)
req.PostString("https://api.openai.com/v1/completions", js.ToString)
'Abdull has supplied his own account API key which is very generous of
'him but you should not use it
'req.GetRequest.SetHeader("Authorization","Bearer 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")
'You can quite easily generate your own account API key by following
'https://accessibleai.dev/post/generating_text_with_gpt_and_python/
'under heading [Getting a GPT-3 API Key]
req.GetRequest.SetHeader("Authorization", "Bearer sk-youraccountAPIkey")
'If you generate your own account API key then Abdull's organisation
'key will be of no use to you
'req.GetRequest.SetHeader("OpenAI-Organization", "org-TV3YOqDRg5DXvAUcL7dC6lI9")
'If your account default organisation is "Personal" then you can supply
'a blank organisation key - or just comment this line out
req.GetRequest.SetHeader("OpenAI-Organization", "")
req.GetRequest.SetContentType("application/json")
Wait For (req) JobDone(req As HttpJob)
If req.Success Then
'Uncomment this line if you want to see raw JSON response
'Log(req.GetString)
Dim parser As JSONParser
parser.Initialize(req.GetString)
Dim jRoot As Map = parser.NextObject
Dim choices As List = jRoot.Get("choices")
For Each colchoices As Map In choices
Dim text As String = colchoices.Get("text")
If response <> "" Then response = response & CRLF
response = response & text.Trim
Next
Else
response = "ERROR: " & req.ErrorMessage
End If
req.Release
Catch
response = "ERROR: " & LastException
End Try
Return response
End Sub
Private wrk_chat As ChatGPT
wrk_chat.Initialize
Wait For (wrk_chat.Query("what is the absolute best investment")) Complete (response As String)
Log(response)
Return
java.net.SocketTimeoutException: timeout
at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.kt:677)
at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.kt:686)
at okhttp3.internal.http2.Http2Stream.takeHeaders(Http2Stream.kt:143)
at okhttp3.internal.http2.Http2ExchangeCodec.readResponseHeaders(Http2ExchangeCodec.kt:96)
at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:106)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:79)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:34)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:175)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:172)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:220)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:463)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
at java.lang.Thread.run(Thread.java:1012)
ResponseError. Reason: java.net.SocketTimeoutException: timeout, Response:
java.net.SocketTimeoutException: timeout
Log reader error: java.io.InterruptedIOException: read interrupted
-1 received
writer error
java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2056)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2090)
at java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:417)
at anywheresoftware.b4a.remotelogger.Connector$Writer.run(Connector.java:160)
at java.lang.Thread.run(Thread.java:1012)
Starting remote logger. Port: 8152
After accept
Sometimes the following error occurs:
ResponseError. Reason: java.net.SocketTimeoutException: timeout, Response::java.net.SocketTimeoutException: timeout at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.kt:677)
Maybe it has something to do with the phone (Xiaomi Redmi Note 9).I wonder if GPT-3 is experiencing the same downtime issues as ChatGPT is. If so, that would explain the timeout errors. I can barely use ChatGPT these days. It is constantly unavailable due to heavy usage loads.
You could try increasing the timeout threshold, see:Sometimes the following error occurs:
B4X:java.net.SocketTimeoutException: timeout
I try to addYou could try increasing the timeout threshold, see:
https://www.b4x.com/android/forum/t...fault-http-connection-timeout.142501/#content
for some possibilities.
req.GetRequest.Timeout=60000
Well try Timeout=120000 for 2 minutes...I try to add
But sometimes error appears after 1 min, sometimes it worksB4X:req.GetRequest.Timeout=60000