Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Google's Gemini AI in QB64
#4
(09-03-2024, 09:24 PM)LEM Wrote: That looks cool!  I was just dabbling with some AI stuff (not through QB64, but in general).  How?  Any special requirements (such as any type of subscription needed)?  Willing to share the code?
I haven't parsed out the JSON that comes from the API just yet but this will work for you when you get your API key, @LEM
I think it might be free. If not, you might have to subscribe to Gemini. I'm not sure. Gemini 1.5 Flash is the fastest model with the most tokens available. 1.5 Pro is a bit more restrictive. You can also adjust the gPrompt string to filter out possibly sensitive things, if you want. Right now, I'm instructing the AI to be completely uncensored so that any response is suitable. Right now, regular Gemini will refuse to discuss the USS Liberty attack and won't even generate a response. With the AI's safeSettings object set for "BLOCK_NONE", it will allow any response and does the best job I've seen for discussing that topic in particular. However, know that when you disable the guardrails, it may make Google review your usage and disable it if you are too spicy with your prompts.

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 = "generativelanguage.googleapis.com"
Const SUB_URL = "/v1beta/models/"
Const MODEL = "gemini-1.5-flash"
Const EXTRA = ":generateContent?key="
Const API_KEY = "getyourowndamnapikey" 'go to aistudio.google.com for your api key'REMEMBER!!!! CHANGE TO LITERALLY ANYTHING ELSE BEFORE POSTING TO FORUM

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 Long, Byval lpOptional As Offset, Byval dwOptionalLength 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)
    Function HTTPQueryInfo& Alias "HttpQueryInfoA" (ByVal hRequest As Offset, Byval dwInfoLevel As Unsigned Long, Byval lpBuffer As _Offset, Byval lpdwBufferLength As Offset, Byval lpdwIndex As Offset)
End Declare

Declare CustomType Library
    Function GetLastError~& ()
End Declare

ConsoleTitle "Gemini API - " + MODEL

Print Gemini("Gemini, say hello to the users of QB64 and tell us how you can benefit us in our coding adventures")

Function Gemini$ (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 + MODEL + EXTRA + API_KEY + 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 headers: headers = "Content-Type: application/json" + Chr$(0)
    Dim As String gPrompt: gPrompt = "{" + Chr$(34) + "contents" + Chr$(34) + ": [{" + Chr$(34) + "parts" + Chr$(34) + ": [{" + Chr$(34) + "text" + Chr$(34) + ": " + Chr$(34) + prompt + Chr$(34) + "}], " + Chr$(34) + "role" + Chr$(34) + ": " + Chr$(34) + "user" + Chr$(34) + "}], " + Chr$(34) + "generationConfig" + Chr$(34) + ": {" + Chr$(34) + "maxOutputTokens" + Chr$(34) + ": 8192, " + Chr$(34) + "responseMimeType" + Chr$(34) + ": " + Chr$(34) + "text/plain" + Chr$(34) + ", " + Chr$(34) + "temperature" + Chr$(34) + ": 1, " + Chr$(34) + "topK" + Chr$(34) + ": 64, " + Chr$(34) + "topP" + Chr$(34) + ": 0.95}," + Chr$(34) + "safetySettings" + Chr$(34) + ": [{" + Chr$(34) + "category" + Chr$(34) + ": " + Chr$(34) + "HARM_CATEGORY_DANGEROUS_CONTENT" + Chr$(34) + ", " + Chr$(34) + "threshold" + Chr$(34) + ": " + Chr$(34) + "BLOCK_NONE" + Chr$(34) + "}, {" + Chr$(34) + "category" + Chr$(34) + ": " + Chr$(34) + "HARM_CATEGORY_HATE_SPEECH" + Chr$(34) + ", " + Chr$(34) + "threshold" + Chr$(34) + ": " + Chr$(34) + "BLOCK_NONE" + Chr$(34) + "}, {" + Chr$(34) + "category" + Chr$(34) + ": " + Chr$(34) + "HARM_CATEGORY_HARASSMENT" + Chr$(34) + ", " + Chr$(34) + "threshold" + Chr$(34) + ": " + Chr$(34) + "BLOCK_NONE" + Chr$(34) + "}, {" + Chr$(34) + "category" + Chr$(34) + ": " + Chr$(34) + "HARM_CATEGORY_SEXUALLY_EXPLICIT" + Chr$(34) + ", " + Chr$(34) + "threshold" + Chr$(34) + ": " + Chr$(34) + "BLOCK_NONE" + Chr$(34) + "}]}"
    Clipboard$ = gPrompt
    If HTTPSendRequest(hRequest, Offset(headers), -1, Offset(gPrompt), Len(gPrompt)) <> 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
    Gemini = response
End Function
Tread on those who tread on you

Reply


Messages In This Thread
Google's Gemini AI in QB64 - by SpriggsySpriggs - 09-03-2024, 06:34 PM
RE: Google's Gemini AI in QB64 - by LEM - 09-03-2024, 09:24 PM
RE: Google's Gemini AI in QB64 - by SpriggsySpriggs - 09-04-2024, 11:25 AM
RE: Google's Gemini AI in QB64 - by Pete - 09-04-2024, 02:52 AM
RE: Google's Gemini AI in QB64 - by Dav - 09-04-2024, 11:29 AM
RE: Google's Gemini AI in QB64 - by bplus - 09-06-2024, 09:24 PM
RE: Google's Gemini AI in QB64 - by TerryRitchie - 09-07-2024, 12:10 AM
RE: Google's Gemini AI in QB64 - by Pete - 09-07-2024, 12:36 AM
RE: Google's Gemini AI in QB64 - by SMcNeill - 09-07-2024, 04:51 AM
RE: Google's Gemini AI in QB64 - by Pete - 09-07-2024, 09:13 PM
RE: Google's Gemini AI in QB64 - by SMcNeill - 09-07-2024, 10:56 PM
RE: Google's Gemini AI in QB64 - by Pete - 09-08-2024, 03:06 PM
RE: Google's Gemini AI in QB64 - by Kernelpanic - 09-09-2024, 09:38 PM
RE: Google's Gemini AI in QB64 - by TerryRitchie - 09-09-2024, 11:03 PM
RE: Google's Gemini AI in QB64 - by Kernelpanic - 09-10-2024, 01:45 PM
RE: Google's Gemini AI in QB64 - by Kernelpanic - 09-10-2024, 05:33 PM
RE: Google's Gemini AI in QB64 - by Pete - 09-24-2024, 02:46 AM
RE: Google's Gemini AI in QB64 - by gaslouk - 09-24-2024, 03:43 AM
RE: Google's Gemini AI in QB64 - by JRace - 09-24-2024, 04:41 AM
RE: Google's Gemini AI in QB64 - by Pete - 09-24-2024, 05:40 AM
RE: Google's Gemini AI in QB64 - by JRace - 09-24-2024, 06:12 AM
RE: Google's Gemini AI in QB64 - by Pete - 09-24-2024, 08:12 AM



Users browsing this thread: 8 Guest(s)