Ole_GetWebPage with OpenAI ChatGPT API (OpenInsight 64-bit)
At 11 JUL 2023 10:55:12AM Ben Kelly wrote:
I'm trying to communicate with the Chat GPT Api in Oinsight.
Has anyone done this yet?
I'm trying to follow the OponAI instructions here
https://platform.openai.com/docs/quickstart/setup
It's telling us how to do it in Python,
I think part of my problem is that I'm including the Authorisation in the main payload whereas in the Python examples it's in a separate header. As far as I'm aware I can't separate it out like that in OI.
Function CHAT_GPT_API()
Error = ""
declare function url_encoder_decoder, Ole_GetWebPage, Contains, get_property, set_property
payload = ""
ApiKey = xlate("KEYS",1,"CHAT_GPT_API","X")
endpoint = "https://api.openai.com/v1/completions/"
prompt = "Say this is a test"
Params = "Content-Type":@FM
Values = "application/json":@FM
Params := "Authorization":@FM
Values := "Bearer ":ApiKey:@FM
Params := "model" :@FM
Values := "text-davinci-003":@FM
Params := "prompt":@FM
Values := prompt:@FM
Params := "max_tokens":@FM
Values := 7:@FM
Params := "temperature"
Values := "0"
Ctr = Count(Params,@FM) + (Params # "")
payload = endpoint
For x = 1 to Ctr
param = Params<x>
value = Values<x>
if value then
value = url_encoder_decoder(value)
payload := param:"=":value:"&"
End
next x
payload[-1,1] = ""
Retval = set_Property("CLIPBOARD",payload)
debug
Response = Ole_GetWebPage(payload, 'POST')
Retval = set_Property("CLIPBOARD",response)
Gosub ReadResponse
Return
ReadResponse:
Error = Contains(Response,'"error":')
If Error Then
ErrorResponse = Response
Swap "[{" with "" in ErrorResponse
Swap "}]" with "" in ErrorResponse
Swap '"' with "" in ErrorResponse
Swap ", " with '|' in ErrorResponse
Swap "{" with
in ErrorResponse Swap "}" with
in ErrorResponseSwap "error:" with '' in ErrorResponse
If ErrorResponses Then
ErrorResponses := "|":ErrorResponse
End Else
ErrorResponses = ErrorResponse
End
End Else
swap '":' with @VM in Response
swap ',"' with @FM in Response
swap '{' with "" in Response
swap '}' with "" in Response
swap '"' with "" in Response
Ctr = Count(Response,@FM) + (Response # "")
id = Response<Ctr,2>
End
Return
The Response I get from the API is
{
"error": {
"message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
At 11 JUL 2023 10:58AM Donald Bakke wrote:
I think part of my problem is that I'm including the Authorisation in the main payload whereas in the Python examples it's in a separate header. As far as I'm aware I can't separate it out like that in OI.
If you look at the documentation for OLE_GetWebPage, you'll notice that the fourth argument is where you can add request header information. See if that works for you.