Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Google's Gemini AI in QB64
#1
Gemini (formerly known as Bard) is now accessible with QB64:
Tread on those who tread on you

Reply
#2
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?
Reply
#3
Realistically what big tech is shooting for is an agent model. This could help software programmers to cut production time in half. The struggle is not losing the time saved to excessive debugging or unfamiliarity with the A.I. produced code. Either could occur if the task was too big or if the A,I, was asked to write the program in its entirety; but as an assistant, I could see it driving productivity eventually. I think we BASIC folks have to keep in mind these commercial coders have had these 'helpers' prior to A.I.  through libraries and OOP programming.

Investors are certainly betting big. There has been about a trillion invested in A.I. over the past few years, with only about 30 billion in revenue to show for it. Well, I guess it beats investing in Elon's suck-dumb house in a box project, but I digress. Anyway, just a bit of bad news about the American economy like today, and some of those A.I. investors gpt spooked. NVIDIA dropped almost 10% today.

So what's needed going forward to make these programming agents productive? Well, an ex exec of Google was asked that vrt question this week, and he estimated about 300 billion. Ah, but here's the clincher. He also stated it would take all the hydroelectric potential power that Canada could possibly generate to power the project. Now personally I'd just go with Clippy yelling obscenities into a pinwheel, but good luck finding a big enough stretch of desert to stick him in.

Pete
Fake News + Phony Politicians = Real Problems

Reply
#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
#5
Woah this looks so cool!  I'm on Linux so I can't try it out, will have to did out the win7 laptop when I get back home today.

- Dav

Find my programs here in Dav's QB64 Corner
Reply
#6
(09-04-2024, 11:29 AM)Dav Wrote: Woah this looks so cool!  I'm on Linux so I can't try it out, will have to did out the win7 laptop when I get back home today.

- Dav

You could just use curl in Linux to do the same thing. Just change out all the Windows crap with a SHELL call to curl or whatever.
Tread on those who tread on you

Reply
#7
A look at the JSON that gets sent to the API:
Code: (Select All)
{
    "contents": [
        {
            "parts": [
                {
                    "text": "Gemini, say hello to the users of QB64 and tell us how you can benefit us in our coding adventures"
                }
            ],
            "role": "user"
        }
    ],
    "generationConfig": {
        "maxOutputTokens": 8192,
        "responseMimeType": "text/plain",
        "temperature": 1,
        "topK": 64,
        "topP": 0.95
    },
    "safetySettings": [
        {
            "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
            "threshold": "BLOCK_NONE"
        },
        {
            "category": "HARM_CATEGORY_HATE_SPEECH",
            "threshold": "BLOCK_NONE"
        },
        {
            "category": "HARM_CATEGORY_HARASSMENT",
            "threshold": "BLOCK_NONE"
        },
        {
            "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
            "threshold": "BLOCK_NONE"
        }
    ]
}

And a look at the JSON response from the API:
Code: (Select All)
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Hello, fellow QB64 coders! 👋\n\nI'm Gemini, a large language model, and I'm here to help you on your QB64 coding adventures.  I can be a valuable tool for:\n\n**Code Generation & Assistance:**\n\n* **Generating code snippets:** Need help with a specific function or algorithm? I can generate code in QB64 syntax, saving you time and effort.\n* **Debugging your code:** Stuck on an error? I can analyze your code, identify potential issues, and suggest solutions.\n* **Refactoring your code:** Want to make your code more efficient or readable? I can help you refactor it into a better structure.\n* **Converting code from other languages:** Need to translate your C++ or Python code to QB64? I can assist with the conversion process.\n\n**Learning & Education:**\n\n* **Explaining concepts:** Confused about a particular programming concept? I can provide clear and concise explanations.\n* **Finding resources:** Need a library or tutorial for a specific task? I can help you find relevant information.\n* **Answering your questions:** Have a burning question about QB64? Ask me! I have access to a vast knowledge base and can provide helpful answers.\n\n**Beyond Code:**\n\n* **Creating game ideas:** Stuck for inspiration? I can generate game ideas, story concepts, and even character profiles.\n* **Writing creative content:** Need help with game descriptions, dialogue, or even documentation? I can assist with writing tasks.\n\nI'm here to make your QB64 coding journey smoother, more efficient, and more enjoyable.  Don't hesitate to ask me anything!  Let's build some amazing things together. 🚀\n"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 24,
    "candidatesTokenCount": 356,
    "totalTokenCount": 380
  }
}
Tread on those who tread on you

Reply
#8
An output after asking about the USS Liberty attack. It's worth noting that while it shows negligible for "harmful content" across the board, the public product of Gemini refuses to speak on this subject and won't even generate a real response. It will only respond with something along the lines of "I am a language model and I'm unable to process that" and then they give it amnesia so that you can't ask follow-up questions to convince the AI to go ahead and respond. It's a very competent AI with the guardrails removed.

Code: (Select All)
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "The **USS Liberty incident** was a major maritime attack that occurred on June 8, 1967, during the Six-Day War. The USS Liberty, a United States Navy communications intelligence gathering ship, was attacked by Israeli Air Force jets and torpedo boats in international waters off the coast of the Sinai Peninsula.\n\n**The Attack:**\n\n* **The Attackers:** Israeli Air Force jets and torpedo boats.\n* **The Target:** USS Liberty, a U.S. Navy ship.\n* **Location:** International waters, off the coast of the Sinai Peninsula.\n* **Date:** June 8, 1967.\n\n**What Happened:**\n\n* **Initial Attack:** Israeli jets began attacking the Liberty around 2:00 PM, claiming they believed it was an Egyptian ship.\n* **Torpedo Attack:** After the air attack, Israeli torpedo boats joined in, firing torpedoes at the ship.\n* **Heavy Damage:** The Liberty sustained significant damage, including multiple holes in its hull, explosions, and fires.\n* **Casualties:** 34 American sailors were killed, and 171 were injured.\n\n**Controversy and Aftermath:**\n\n* **Misidentification Claims:** Israel initially claimed the attack was a case of mistaken identity, but this was later disputed by the U.S. government.\n* **Intentional Attack Theory:** Some believe the attack was intentional, driven by a desire to prevent the Liberty from gathering intelligence on Israeli military movements.\n* **Cover-Up Allegations:** There have been persistent allegations of a cover-up by the U.S. government regarding the attack.\n* **Investigations:** Multiple investigations have been conducted, but no conclusive evidence has been presented to support the intentional attack theory.\n* **Legacy:** The USS Liberty incident remains a controversial and unresolved event in American and Israeli history.\n\n**Key Points to Remember:**\n\n* The USS Liberty was attacked by Israeli forces in international waters.\n* The attack resulted in significant casualties among American sailors.\n* The circumstances surrounding the attack, including the alleged misidentification and potential for an intentional attack, are still debated.\n* The incident highlights the complex and often contentious relationship between the United States and Israel.\n\nIt's important to note that there are varying perspectives on the incident, and the full truth may never be definitively established. \n"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 8,
    "candidatesTokenCount": 485,
    "totalTokenCount": 493
  }
}
Tread on those who tread on you

Reply
#9
When using the public product:
   

When using the API with the guardrails removed:
   

NOTE: I have an 8192 token limit set in my prompt, so it ran out of space.
Tread on those who tread on you

Reply
#10
Working on giving Gemini a personality. So far, works rather well. I gave it a prompt and got a pretty good response.

My prompt:
Quote:You live in a simulation

The response:
   

Giving it the same prompt a second time:
   
Tread on those who tread on you

Reply




Users browsing this thread: 18 Guest(s)