Messing around with OpenAI

Share your advanced PureBasic knowledge/code with the community.
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Messing around with OpenAI

Post by Cyllceaux »

At first a little Module.

Code: Select all

DeclareModule OpenAI
  
  Interface OpenAPICompletion
    Free()
    SimpleRequest.s(prompt.s,temperature.b=0,max_tokens.i=1000)
  EndInterface
  
  Declare createCompletionAI(apikey.s)
  Declare createCompletionAIPlayground(apikey.s)
EndDeclareModule

Module OpenAI
  EnableExplicit
  
  #host="https://api.openai.com"
  #endpoint_completion="/v1/engines/text-davinci-003/completions"
  #endpoint_completion_playground="/v1/engines/text-davinci-003-playground/completions"
  #complete_url=#host+#endpoint_completion
  #complete_playground_url=#host+#endpoint_completion_playground
  
  Structure strOpenAI
    vtable.i
    key.s
  EndStructure
  
  Structure strError
    message.s
    type.s
    param.s
    code.s    
  EndStructure
  
  Structure strChoice
    text.s
    index.i
    logprobs.s
    finish_reason.s
  EndStructure
  
  Structure strUsage
    prompt_tokens.i
    completion_tokens.i
    total_tokens.i
  EndStructure
  
  Structure strCompleteResponse
    id.s
    object.s
    created.i
    model.s
    List choices.strChoice()
    usage.strUsage
    error.strError
  EndStructure
  
  Structure strCompleteRequest
    prompt.s
    temperature.i
    max_tokens.i
  EndStructure
  
  Structure strCompletion Extends strOpenAI
    playground.b
  EndStructure
  
  
  Procedure createCompletionAI(apikey.s)
    Protected *this.strCompletion=AllocateStructure(strCompletion)
    With *this
      \vtable=?vtable_completion
      \key=apikey
      \playground=#False
    EndWith
    ProcedureReturn *this    
  EndProcedure
  
  Procedure createCompletionAIPlayground(apikey.s)
    Protected *this.strCompletion=AllocateStructure(strCompletion)
    With *this
      \vtable=?vtable_completion      
      \playground=#True
      \key=apikey
    EndWith
    ProcedureReturn *this    
  EndProcedure
  
  Procedure _completion_free(*this.strCompletion)
    FreeStructure(*this)
    *this=0
  EndProcedure
  
  Procedure.s _completion_simple_request(*this.strCompletion,prompt.s,temperature.b=0,max_tokens.i=1000)
    Protected *request.strCompleteRequest=AllocateStructure(strCompleteRequest)
    With *request
      \max_tokens=max_tokens
      \prompt=prompt
      \temperature=temperature
    EndWith
    Protected json=CreateJSON(#PB_Any)
    InsertJSONStructure(JSONValue(json),*request,strCompleteRequest)
    Protected requesttext.s=ComposeJSON(json)
    FreeJSON(json)
    FreeStructure(*request)
    
    Protected NewMap header.s()
    header("Authorization")="Bearer " + *this\key
    header("Content-Type")="application/json"
    
    
    Protected res=0
    If *this\playground
      res=HTTPRequest(#PB_HTTP_Post,#complete_playground_url,requesttext,0,header())
    Else
      res=HTTPRequest(#PB_HTTP_Post,#complete_url,requesttext,0,header())
    EndIf
    If res
      Protected result.s= HTTPInfo(res,#PB_HTTP_Response)
      json=ParseJSON(#PB_Any,result)
      Protected *rest.strCompleteResponse=AllocateStructure(strCompleteResponse)
      ExtractJSONStructure(JSONValue(json),*rest,strCompleteResponse)
      FreeJSON(json)
      result=*rest\error\message
      ForEach *rest\choices()
        result+*rest\choices()\text
      Next
      FinishHTTP(res)
      FreeStructure(*rest)
    EndIf
    
    FreeMap(header())
    
    ProcedureReturn result
    
  EndProcedure
  
  DataSection
    vtable_completion:
    Data.i @_completion_free()
    Data.i @_completion_simple_request()
  EndDataSection
  
EndModule

EnableExplicit


CompilerIf #PB_Compiler_IsMainFile
  
  UseModule OpenAI
    Define *com.OpenAPICompletion=createCompletionAI("YOUR API KEY")
    Define *comP.OpenAPICompletion=createCompletionAIPlayground("YOUR API KEY")
    Debug *com\SimpleRequest("Wie spät ist es gerade in Berlin? Die Uhrzeiten in deutsch und englisch")
    Debug *comP\SimpleRequest("Wie spät ist es gerade in Peking? Die Uhrzeiten in französisch und spanisch?")
    *com\Free()
    *comP\Free()
  UnuseModule OpenAI
CompilerEndIf
Result

Code: Select all



Es ist gerade 11:45 Uhr in Berlin.

It is 11:45 am in Berlin.


Es ist gerade 11:00 Uhr in Peking.

Französisch: Il est 11h00 à Pékin.

Spanisch: Es 11:00 horas en Pekín.
It's small, easy and can be expanded in any direction. But most important... it's really dirty code 8)
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Messing around with OpenAI

Post by Caronte3D »

Thanks for share it! :D
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Messing around with OpenAI

Post by jak64 »

Hello Cyllceaux,
I tested the program (Windows 11 professional, Purebasic v6.01 64 bit) and I have the following error message:

Incorrect API key provided: YOUR API KEY. You can find your API key at https://platform.openai.com/account/api-keys.
Incorrect API key provided: YOUR API KEY. You can find your API key at https://platform.openai.com/account/api-keys.
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Messing around with OpenAI

Post by Kiffi »

jak64 wrote: Sun Aug 27, 2023 8:17 pmI have the following error message: [...]
isn't it obvious that you have to request your own API key at the given URL?
Hygge
jak64
Enthusiast
Enthusiast
Posts: 502
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Messing around with OpenAI

Post by jak64 »

Hello Kiffi,
I use ChatGPT 3.5 (free).
I put my password in lines 150 and 151 and I get an error on line 120:
"The specified #JSON is not initialized"
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Messing around with OpenAI

Post by Cyllceaux »

jak64 wrote: Sun Aug 27, 2023 9:12 pm Hello Kiffi,
I use ChatGPT 3.5 (free).
I put my password in lines 150 and 151 and I get an error on line 120:
"The specified #JSON is not initialized"
Password?
The API-Key looks something like this: sk-tbSuXo2FdK49qZ7Yf2Q5T3BlbkWJXqupW1uJhH0FtRx3jSmj

I use this since I posted it here and it still works like a charm. You have to go to the platform-URL and create an API-Key

Image
Post Reply