I've been using MMA (v13.1.0) to experiment with various apis provided by OpenAI (https://platform.openai.com/docs/api-reference). For example, OpenAI provides a chat completion endpoint using the 'text-davinci-003' model. An example of a simple MMA code snippet to invoke the endpoint is
callChatGPT0[myPrompt_, myAPIKey_] := Module[{myRequest, myResponse},
myRequest = HTTPRequest["https://api.openai.com/v1/completions",
<|Method -> "POST",
"Body" ->
ExportString[{"model" -> "text-davinci-003",
"prompt" -> myPrompt, "max_tokens" -> 300}, "JSON"],
"ContentType" -> "application/json",
"Headers" -> {"Authorization" -> "Bearer " <> myAPIKey}|>];
myResponse = URLExecute[myRequest]
]
where myAPIKey is a string containing my OpenAI access key. An example of the code output is:
In[26]:= callChatGPT0["Write a haiku about Wolfram Mathematica", apikey]
Out[26]= {"id" -> "cmpl-7YfCX8EIngy8U4E8mdto1Zjdosj0i",
"object" -> "text_completion", "created" -> 1688495885,
"model" -> "text-davinci-003", "choices" -> {{"text" -> "
Symbolic powers
A brain of mathematics loves
Computing its solutions", "index" -> 0, "logprobs" -> Null,
"finish_reason" -> "stop"}},
"usage" -> {"prompt_tokens" -> 10, "completion_tokens" -> 17,
"total_tokens" -> 27}}
I would like to switch to a more recent OpenAI model ('gpt-3.5-turbo-16k' in my case), which allows for longer prompts and improved chat completions. The format of this updated chat completions api has changed (see documentation here: https://platform.openai.com/docs/api-reference/chat/create. I've tried to update my code to reflect the new api format:
callChatGPT1[myPrompt_, myAPIKey_] := Module[{myRequest, myResponse},
myRequest = HTTPRequest["https://api.openai.com/v1/chat/completions",
<|
Method -> "POST",
"Body" -> ExportString[
{"model" -> "gpt-3.5-turbo-16k",
"messages" -> "[{role:user,content:" <> myPrompt <> "}]",
"temperature" -> 0.0},
"JSON"],
"ContentType" -> "application/json",
"Headers" -> {"Authorization" -> "Bearer " <> myAPIKey}
|>];
myResponse = URLExecute[myRequest]
]
This code, however, produces an error
In[27]:= callChatGPT1["Write a haiku about Wolfram Mathematica", apikey]
Out[27]= {"error" -> {"message" ->
"'[{role:user,content:Write a haiku about Wolfram Mathematica}]' \
is not of type 'array' - 'messages'",
"type" -> "invalid_request_error", "param" -> Null,
"code" -> Null}}
The error looks to involve the formatting of the messages array argument. I've tried a few changes but with no success. Any suggestions would be most helpful!
Thanks,
Mark
