I note that there are similar topics here, but this is a bit different.
First, I’m trying to make API calls using the http_client and http_json libraries. I can make some queries, but not all. I believe the JSON format needs to be different for the different models, and I cannot get gpt-3.5-turbo to work for me in Prolog.
I can make the queries using curl, and will include the curl command for reference in a bit. I thought it would be simple enough to convert the query to Prolog, but I think there are tricks for formatting JSON which are preventing me. Anyhow, I know the correct query syntax, and the API key and endpoint are fine.
There is a project on Github for ChatGPT integration with SWI Prolog. It does not work with all models, in particular gpt-3.5-turbo. Again, I believe that the JSON syntax is different for the newer model. I just get a “BAD REQUEST” when I try.
Here is the curl command:
curl -X POST 'https://api.openai.com/v1/chat/completions' -H 'Authorization: Bearer <API-KEY>' -H 'Content-Type: application/json' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "What is your favorite OpenAI model?"}
],
"temperature":0.7,
"max_tokens": 50
}'
I hesitate to show my attempts so far, but will do so. Please realize that I have been flailing at different attempts, and have tried all sorts of permutations. A vague return value like “BAD REQUEST” doesn’t give me much information to go on.
chat_test(Response) :-
EndPoint = 'https://api.openai.com/v1/chat/completions',
APIKey = '<API-KEY>',
Model = 'gpt-3.5-turbo',
atom_json_term(Messages, json([{role='user',content=Prompt}]), []),
Prompt= 'What is your favorite programming language?',
atom_json_term(Data, json([model=Model, temperature=0.7,
max_tokens=50, messages=Messages]), []),
http_post(EndPoint, Data, Response, [authorization(bearer(APIKey)), application/json]).