Hi buddies,
I’m trying to access gpt api via swi-prolog, and have tried stable version 9.0.3 and development 9.1.10. I have to use proxy for this here but always got time out errors. Below is the message. Any helps are greatly appreciated!
- proxy is ok because python program works well:
arthurwang@ArthurdeMacBook-Pro python % echo $all_proxy
http://127.0.0.1:10077
arthurwang@ArthurdeMacBook-Pro python % python3 gptapi.py
你好,你怎么样?
arthurwang@ArthurdeMacBook-Pro python % cat gptapi.py
import requests
import json
API_KEY = ""
API_ENDPOINT = "https://api.openai.com/v1/chat/completions"
def generate_chat_completion(messages, model="gpt-3.5-turbo", temperature=1, max_tokens=None):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}",
}
data = {
"model": model,
"messages": messages,
"temperature": temperature,
}
if max_tokens is not None:
data["max_tokens"] = max_tokens
response = requests.post(API_ENDPOINT, headers=headers, data=json.dumps(data))
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
raise Exception(f"Error {response.status_code}: {response.text}")
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Translate the following English text to Chinese: 'Hello, how are you?'"}
]
response_text = generate_chat_completion(messages)
print(response_text)
- prolog code:
%server.pl
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
% :- use_module(library(http/http_unix_daemon)).
:- use_module(library(http/http_files)).
:- use_module(library(http/http_json)).
:- use_module(library(http/http_client)).
:- multifile http:location/3.
http:location(files, root(files), []).
server(Port) :-
http_server(http_dispatch, [port(Port)]).
:- http_handler(root(.), http_reply_from_files('.', []), [prefix]). % defaults to index.html
:- http_handler(files(.), serve_files_in_directory(folders), [prefix]).
:- http_handler('/api', handle_api, []).
handle_api(Request) :-
member(method(post), Request), !,
http_read_json_dict(Request, Query),
query_gpt(Query, Solution),
reply_json_dict(Solution), !.
handle_api(Request) :-
reply_json_dict(_{req:Request}).
query_gpt(_{query:Query}, Solution) :-
Url = 'https://api.openai.com/v1/chat/completions',
Headers = headers([content_type('application/json; charset="utf-8"')]),
Data = _{
model: 'gpt-3.5-turbo',
temperature:0.7,
messages:[
_{
role: user,
content: Query
}
]
},
http_post(Url, Data, Solution, [Headers, authorization(bearer('my key'))]).
- call query_gpt:
arthurwang@ArthurdeMacBook-Pro gptapi % swipl server.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 9.0.3)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- query_gpt(_{query:'Say hello in Chinese'}, Reply).
ERROR: Socket error: Operation timed out
ERROR: In:
ERROR: [21] throw(error(socket_error(etimedout,'Operation timed out'),_6888))
ERROR: [19] catch('<garbage_collected>','<garbage_collected>','<garbage_collected>') at /usr/local/Cellar/swi-prolog/9.0.3/libexec/lib/swipl/boot/init.pl:565
ERROR: [17] socket:tcp_connect('api.openai.com':443,_6954,[visited(...),...|...]) at /usr/local/Cellar/swi-prolog/9.0.3/libexec/lib/swipl/library/socket.pl:385
ERROR: [16] http_open:open_socket('<garbage_collected>',_7010,[visited(...),...|...]) at /usr/local/Cellar/swi-prolog/9.0.3/libexec/lib/swipl/library/http/http_open.pl:945
ERROR: [14] http_open:try_http_proxy(direct,[uri('https://api.openai.com/v1/chat/completions'),...|...],_7062,'<garbage_collected>') at /usr/local/Cellar/swi-prolog/9.0.3/libexec/lib/swipl/library/http/http_open.pl:491
ERROR: [12] http_client:http_get('https://api.openai.com/v1/chat/completions',_7112,[post(...),...|...]) at /usr/local/Cellar/swi-prolog/9.0.3/libexec/lib/swipl/library/http/http_client.pl:141
ERROR: [9] toplevel_call('<garbage_collected>') at /usr/local/Cellar/swi-prolog/9.0.3/libexec/lib/swipl/boot/toplevel.pl:1173
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
?-
- I’ve tried adding proxy in option argument, not working either.
- I’ve tried proxychain, failed too
I have no idea what’s wrong.
Thank you very much for help!
With best regards,
Arthur Wang