coclass 'OpenAI'
require 'convert/pjson'
load 'api/curl'
coinsert 'jcurl'
NULL =: <0
create =: 3 : 0
if. 0 = $y do.
url =: 'https://api.openai.com/v1/chat/completions'
else.
url =: y
end.
curl_global_init <CURL_GLOBAL_ALL
data_1 =: ''
)
destroy =: 3 : 0
curl_global_cleanup ''
codestroy''
)
cdcallback=: 3 : 0
y=. 15!:17''
if. 4=#y do. writedata y end.
)
writedata=: 3 : 0
'data size nmemb userp'=. y
rsize=. size*nmemb
name=. 'data_',":userp
(name)=: name~, memr data,0,rsize,2
rsize
)
NB. chat with model
chatWithModel =: 3 : 0
'prompt model' =. y
NB. set callback with local scope variable
f=. [: 15!:13 (IFWIN#'+') , ' x' $~ +:@>:
NB. curl_global_init <CURL_GLOBAL_ALL
hcurl =. curl_easy_init''
if. hcurl = 0 do. echo 'curl init failed' return. end.
NB. headers =: mema 8
headers =: <0
headers =: curl_slist_append (0);'Content-Type: application/json'
headers =: curl_slist_append headers;'Authorization: Bearer 1'
messages =. <('role';'user'),:'content';prompt
rt =. ('model';model),('messages';<messages),:'temperature';0.7
tjson =: enc_pjson_ rt
res =. curl_easy_setopt_str hcurl;CURLOPT_URL;setopt_variadic, <url
if. res~:CURLE_OK do. echo memr 0 _1,~ curl_easy_strerror <res end.
res =. curl_easy_setopt_str hcurl;CURLOPT_POSTFIELDS;setopt_variadic,<tjson
if. res~:CURLE_OK do. echo memr 0 _1,~ curl_easy_strerror <res end.
res =. curl_easy_setopt_ptr hcurl;CURLOPT_HTTPHEADER;setopt_variadic,<<headers
if. res~:CURLE_OK do. echo memr 0 _1,~ curl_easy_strerror <res end.
res=. curl_easy_setopt hcurl; CURLOPT_FOLLOWLOCATION; setopt_variadic, <1
if. res~:CURLE_OK do. echo memr 0 _1,~ curl_easy_strerror <res end.
res=. curl_easy_setopt hcurl; CURLOPT_WRITEFUNCTION; setopt_variadic, <(f 4)
if. res~:CURLE_OK do. echo memr 0 _1,~ curl_easy_strerror <res end.
res=. curl_easy_setopt hcurl; CURLOPT_WRITEDATA; setopt_variadic, <1
if. res~:CURLE_OK do. echo memr 0 _1,~ curl_easy_strerror <res end.
res=. curl_easy_perform <hcurl
if. res~:CURLE_OK do. echo memr 0 _1,~ curl_easy_strerror <res end.
curl_slist_free_all <headers
curl_easy_cleanup <hcurl
NB. decode JSON response
parsed =: dec_pjson_ data_1
echo parsed
)
NB. chat api call
NB. usage:
NB. chat__<instance_name> '<prompt_string>';'<model_name>'
chat =: 3 : 0
chatWithModel y
data_1
)
load '~temp/jopenai.ijs’
mchat =: 'http://127.0.0.1:8080/v1/chat/completions' conew 'OpenAI'
chat__mchat 'Tell me something interesting about space';'deepseek-coder-33b-instruct'
That code will ultimately return a big boxed structure of json code returned from the LLM. There is one big problem I don’t understand when I first power this up both the llama-server and the program the first access is an error. If I repeat the command then it works without a problem.
headers =: <0
headers =: curl_slist_append (0);'Content-Type: application/json'
headers =: curl_slist_append headers;'Authorization: Bearer ','sk-proj-. . .’ NB. your own API Key here
Now after reloading the script:
mchat =: 'https://api.openai.com/v1/chat/completions' conew 'OpenAI'
chat__mchat 'Tell me something interesting about space';'gpt-4o'
{
"id": "chatcmpl-CITpI9fvRtGVSvGayHcbjEj5OANFT",
"object": "chat.completion",
"created": 1758520592,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Certainly! One fascinating aspect of space is the existence of \"rogue planets,\" which are planets that do not orbit a star. Unlike the planets in our solar system, these rogue planets drift through the galaxy independently. Scientists...
"refusal": null,
"annotations": []
},
"logprobs": null,
"finish_reason": "stop"
}
chat__mchat 'Can you name the first 8 planets?';'gpt-4o'
{
"id": "chatcmpl-CITqfuJ1blOaEGhXhWblc1tHtRm7V",
"object": "chat.completion",
"created": 1758520677,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The first eight planets in our solar system, starting from the one closest to the Sun, are:\n\n1. Mercury\n2. Venus\n3. Earth\n4. Mars\n5. Jupiter\n6. Saturn\n7. Uranus\n8. Neptune",
"refusal": null,
"annotations": []
},
"logprobs": null,
"finish_reason": "stop"
}
I can make subsequent chat requests with no additional setup. I bolded the 2 chat requests and provided a partial printout of the JSON returned.
You do need a pay as you go account or a pro account so that the API keys have access to the chat interface. I print out a lot of stuff with the current script because I am trying to figure out how to parse the JSON in boxed form with J.
Tom
On Sep 22, 2025, at 12:50 AM, bill lam <bbil...@gmail.com> wrote: