Discussions related to Visual Prolog
Rangarajan
Posts: 18
Joined: 1 Jun 2007 14:51

Accessing OpenAI, etc

Post by Rangarajan »

I am keen to use VP to interact with LLMs including OpenAI, Claude, etc. How do I get started? Is there any example?
User avatar
Thomas Linder Puls
VIP Member
Posts: 1466
Joined: 28 Feb 2000 0:01

Re: Accessing OpenAI, etc

Post by Thomas Linder Puls »

I am not aware of any examples and I have not attempted anything in that direction myself.

So you will be walking an "unsupported" road, and for that I believe it is important to "master" the host language.

So are you a skilled Visual Prolog programmer?

Alternatively, (not that I enjoy pointing you to other programming environments) I will suggest that you instead follow some OpenAI instructions and use whatever programming language that may be.

If you find someone using Visual Prolog then let us know :-).
Regards Thomas Linder Puls
PDC
User avatar
Thomas Linder Puls
VIP Member
Posts: 1466
Joined: 28 Feb 2000 0:01

Re: Accessing OpenAI, etc

Post by Thomas Linder Puls »

Having said that (and after spending "10 seconds" looking at it) it seems that OpenAI has some kind of JSON web service.

Visual Prolog have good facilities for calling such services.

Basically you would have to
  • construct the JSON input for the service
  • send (POST) the request to the service
  • parse the JSON result (assuming that you didn't) get an error
A hardcoded example of second bullet (based on Developer Quickstart) could look like this:

Code: Select all

class predicates     callOpenAI : (). clauses     callOpenAI() :-         try             XmlHttp = msxml_HTTP_api::newXmlhttp60(),             XmlHttp:open_predicate("POST", url, comDomains::boolean(false), comDomains::null, comDomains::null),             XmlHttp:setRequestHeader("Content-Type", "application/json-rpc; charset=UTF-8"),             XmlHttp:setRequestHeader("Authorization", authorization),             XmlHttp:send(comDomains::string(requestString)),             Response = XmlHttp:responseText,             stdio::writef("Response: %\n", Response)         catch TraceId do             if ComException = exception::tryGetDescriptor(TraceId, exceptionHandling_exception::genericComException)                 and unsigned(winErrors::inet_e_resource_not_found) =                      exception::tryGetExtraInfo(ComException, exceptionHandling_exception::hResult_parameter)             then                 stdio::writef("Cannot connect to %\n", url)             else                 ExtraInfo = [namedValue("request", string(requestString)), namedValue("url", string(url))],                 exception::continueDetailed(TraceId, exception::unknown, ExtraInfo)             end if         end try.   constants     url : string = "https://api.openai.com/v1/chat/completions".     authorization :string = "Bearer <openai-api-key>". % insert your key here     requestString : string = @'     {         "model": "gpt-4o-mini",         "messages": [             {                 "role": "system",                 "content": "You are a helpful assistant."             },             {                 "role": "user",                 "content": "Write a haiku that explains the concept of recursion."             }         ]     }'.
Since I didn't have KEY I get a response like this:
Response: {
"error": {
"message": "Incorrect API key provided: \u003copenai-****key\u003e. You can find your API key at https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"param": null,
"code": "invalid_api_key"
}
}
Regards Thomas Linder Puls
PDC
Rangarajan
Posts: 18
Joined: 1 Jun 2007 14:51

Re: Accessing OpenAI, etc

Post by Rangarajan »

Thanks! Will try.