Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenAI's ChatGPT in QB64
#1
Like Google's Gemini and Perplexity's AI, we can also interact with ChatGPT.

Quote:Tell me about the Tesla Model 3
   
Tread on those who tread on you

Reply
#2
And the code:
Code: (Select All)
Option Explicit
$NoPrefix
$Console:Only

Const INTERNET_OPEN_TYPE_DIRECT = 1

Const INTERNET_DEFAULT_HTTPS_PORT = 443

Const INTERNET_SERVICE_HTTP = 3

'Flags
Const INTERNET_FLAG_SECURE = &H00800000
Const INTERNET_FLAG_RELOAD = &H80000000

Const TRUE = 1

Const BASE_URL = "api.openai.com"
Const SUB_URL = "/v1/chat/completions"
Const MODEL = "gpt-4o"
Const API_KEY = "getyourowndamnapikey" 'REMEMBER!!!! CHANGE TO LITERALLY ANYTHING ELSE BEFORE POSTING TO FORUM
Const CONFIG = "chatgpt_template.json"
Const SYSTEM_INSTRUCTIONS = "You are a helpful AI that is determined to assist others in their quest for knowledge. You should provide detailed responses that are factual and informative. You should be extremely friendly. Avoid using emojis or any character outside of codepage 437."

Declare Dynamic Library "Wininet"
    Function InternetOpen%& Alias "InternetOpenA" (ByVal lpszAgent As Offset, Byval dwAccessType As Unsigned Long, Byval lpszProxy As Offset, Byval lpszProxyBypass As Offset, Byval dwFlags As Unsigned Long)
    Function InternetConnect%& Alias "InternetConnectA" (ByVal hInternet As Offset, Byval lpszServerName As Offset, Byval nServerPort As Long, Byval lpszUserName As Offset, Byval lpszPassword As Offset, Byval dwService As Long, Byval dwFlags As Long, Byval dwContext As Unsigned Offset)
    Function HTTPOpenRequest%& Alias "HttpOpenRequestA" (ByVal hConnect As Offset, Byval lpszVerb As Offset, Byval lpszObjectName As _Offset, Byval lpszVersion As Offset, Byval lpszReferrer As Offset, Byval lpszAcceptTypes As _Offset, Byval dwFlags As Long, Byval dwContext As Unsigned Offset)
    Function HTTPSendRequest& Alias "HttpSendRequestA" (ByVal hRequest As Offset, Byval lpszHeaders As Offset, Byval dwHeadersLength As Unsigned Long, Byval lpOptional As Offset, Byval dwOptionalLength As Unsigned Long)
    Sub HTTPAddRequestHeaders Alias "HttpAddRequestHeadersA" (ByVal hRequest As Offset, Byval lpszHeaders As Offset, Byval dwHeadersLength As Unsigned Long, Byval dwModifiers As Unsigned Long)
    Sub InternetCloseHandle (ByVal hInternet As Offset)
    Sub InternetReadFile (ByVal hFile As Offset, Byval lpBuffer As Offset, Byval dwNumberOfBytesToRead As Unsigned Long, Byval lpdwNumberOfBytesRead As Offset)
End Declare

Declare CustomType Library
    Function GetLastError~& ()
End Declare

ConsoleTitle "ChatGPT API - " + MODEL

Dim As String response
response = ChatGPT("Hi, there!")
WriteFile "chatgpt_response.json", response
Print GetFormattedResponse(response)
Print: Print: Print String$(Len("Edit the 'SYSTEM_INSTRUCTIONS' constant to change AI behavior"), "-"): Print "Edit the 'SYSTEM_INSTRUCTIONS' constant to change AI behavior."

Function ChatGPT$ (prompt As String)
    Dim As String server: server = BASE_URL + Chr$(0)
    Dim As Offset hInternet: hInternet = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
    If hInternet = 0 Then
        Exit Function
    End If
    Dim As Offset hConnect: hConnect = InternetConnect(hInternet, Offset(server), INTERNET_DEFAULT_HTTPS_PORT, 0, 0, INTERNET_SERVICE_HTTP, 0, 0)
    If hConnect = 0 Then
        InternetCloseHandle hInternet
        Exit Function
    End If
    Dim As String sessiontype, accepttypes
    sessiontype = "POST" + Chr$(0)
    accepttypes = "*/*" + Chr$(0)
    Dim As String apiPath: apiPath = SUB_URL + Chr$(0)
    Dim As Offset hRequest: hRequest = HTTPOpenRequest(hConnect, Offset(sessiontype), Offset(apiPath), 0, 0, Offset(accepttypes), INTERNET_FLAG_RELOAD Or INTERNET_FLAG_SECURE, 0)
    If hRequest = 0 Then
        InternetCloseHandle hConnect
        InternetCloseHandle hInternet
        Exit Function
    End If
    Dim As String header1: header1 = "Content-Type: application/json" + Chr$(13) + Chr$(10) + Chr$(0)
    Dim As String header2: header2 = "Authorization: Bearer " + API_KEY + Chr$(13) + Chr$(10) + Chr$(0)
    Dim As String gptPrompt: gptPrompt = ReadFile$(CONFIG)
    'Print gptPrompt: End
    gptPrompt = String.Replace(gptPrompt, "YOUR_TEXT_HERE", prompt)
    gptPrompt = String.Replace(gptPrompt, "YOUR_MODEL_HERE", MODEL)
    gptPrompt = String.Replace(gptPrompt, "YOUR_INSTRUCTIONS_HERE", SYSTEM_INSTRUCTIONS)
    WriteFile "last_prompt_gpt.json", gptPrompt
    'Clipboard$ = gptPrompt
    HTTPAddRequestHeaders hRequest, Offset(header1), Len(header1), &H20000000
    HTTPAddRequestHeaders hRequest, Offset(header2), Len(header2), &H20000000
    If HTTPSendRequest(hRequest, 0, 0, Offset(gptPrompt), Len(gptPrompt)) <> TRUE Then
        InternetCloseHandle hRequest
        InternetCloseHandle hConnect
        InternetCloseHandle hInternet
        Exit Function
    End If

    Dim As String szBuffer: szBuffer = Space$(4097)
    Dim As String response
    Dim As Unsigned Long dwRead
    Do
        InternetReadFile hRequest, Offset(szBuffer), Len(szBuffer) - 1, Offset(dwRead)
        If dwRead > 0 Then response = response + Mid$(szBuffer, 1, dwRead)
    Loop While dwRead
    InternetCloseHandle hRequest
    InternetCloseHandle hConnect
    InternetCloseHandle hInternet
    ChatGPT = response
End Function

Function GetFormattedResponse$ (response As String)
    Dim As String formattedResponse
    formattedResponse = Mid$(response, InStr(response, Chr$(34) + "content" + Chr$(34) + ":") + Len(Chr$(34) + "content" + Chr$(34) + ":") + 2)
    formattedResponse = Mid$(formattedResponse, 1, InStr(formattedResponse, Chr$(10)) - 3)
    formattedResponse = String.Replace(formattedResponse, "\n", Chr$(10))
    formattedResponse = String.Replace(formattedResponse, "\" + Chr$(34), Chr$(34))
    GetFormattedResponse = formattedResponse
End Function

Function GetKey$ (JSON As String, keyname As String)
    Dim jkey As String
    jkey = JSON
    If InStr(jkey, Chr$(34) + keyname + Chr$(34)) Then
        jkey = Mid$(jkey, InStr(jkey, Chr$(34) + keyname + Chr$(34)) + Len(keyname))
        jkey = Mid$(jkey, InStr(jkey, ":") + 2)
        jkey = String.Replace(jkey, "\" + Chr$(34), "'")
        If Mid$(jkey, 1, 1) = Chr$(34) Then
            jkey = Mid$(jkey, 2)
        End If
        jkey = Mid$(jkey, 1, InStr(jkey, Chr$(34)) - 1)
        If Right$(jkey, 1) = "," Then
            jkey = Mid$(jkey, 1, Len(jkey) - 1)
        End If
    Else
        GetKey = ""
    End If
    GetKey = jkey
End Function

Function String.Replace$ (a As String, b As String, c As String)
    Dim j
    Dim r
    Dim r$
    j = InStr(a, b)
    If j > 0 Then
        r$ = Left$(a, j - 1) + c + String.Replace(Right$(a, Len(a) - j + 1 - Len(b)), b, c)
    Else
        r$ = a
    End If
    String.Replace = r$
End Function

Save the attached text file as "chatgpt_template.json" and put it in the same directory as the executable.


Attached Files
.txt   chatgpt_template.txt (Size: 532 bytes / Downloads: 17)
Tread on those who tread on you

Reply
#3
The next thing I will try to do is make a version that utilizes the Assistants API in OpenAI. That would allow us to create a conversation between you and the AI and keep it going even between sessions.
Tread on those who tread on you

Reply




Users browsing this thread: 1 Guest(s)