This is just a very simple way to use the well know Openai API from within PB.
Code: Select all
Procedure SendPrompt(sPrompt.s)
sUrl. s ="https://api.openai.com/v1/chat/completions"
NewMap Header$()
Header$("content-type") = "application/json"
Header$("Authorization") = "Bearer [API KEY]"
nMaxTokens.l = 3500
sModel.s = "gpt-3.5-turbo"
sNumberOfResponses = 1
sTemperature.f =0.7
sBody.s = "{"
sBody.s = sBody.s + Chr(34) + "max_tokens" + Chr(34) + ": " + Str(nMaxTokens) + ", "
sBody.s = sBody.s + Chr(34) + "model" + Chr(34) + ": " + Chr(34) + sModel + Chr(34) + ", "
sBody.s = sBody.s + Chr(34) + "temperature" + Chr(34) + ": " + Str(sTemperature) + ", "
sBody.s + Chr(34) + "n" + Chr(34) + ": " + Str(sNumberOfResponses) + ", "
sBody.s = sBody.s + Chr(34) + "messages" + Chr(34) + ": [{" + Chr(34) + "role" + Chr(34) + ": " + Chr(34) + "user" + Chr(34) + ", " + Chr(34) + "content" + Chr(34) + ": " + Chr(34) + sPrompt + Chr(34) + "}]"
sBody.s + "}"
Debug sBody
; Realizar la solicitud a la API de OpenAI
Define httpReq
httpReq = HTTPRequest(#PB_HTTP_Post, sUrl, sBody, 0, Header$())
If httpReq
Debug "StatusCode: " + HTTPInfo(httpReq, #PB_HTTP_StatusCode)
Response$ = HTTPInfo(httpReq, #PB_HTTP_Response)
Debug "Response: " + Response$
FinishHTTP(httpReq)
Else
Debug "Request creation failed"
EndIf
EndProcedure
sPrompt.s = "please write an article about purebasic"
SendPrompt(sPrompt)