Can we send prompts to Gemini or ChatGPT from LBB under program control and receive the response?
If TLS1.2 connection will be needed, can we do this with LBB?
AI Prompt Enginering with LBB
Re: AI Prompt Enginering with LBB
The requirement for SSL encryption is indeed probably the key stumbling block to accessing a ChatBot's API from LBB. It's possible, but it adds an extra layer of complexity which you may prefer not to embark on.
If you want to give it a go, this Wiki article describes what's involved. It's BBC BASIC code but can either be translated to LB syntax or incorporated directly in an LBB program by prefixing each statement with an exclamation point.
Re: AI Prompt Enginering with LBB
Yes, this is a good OpenSSL example in BBC BASIC which is fine with me.
I will give it a try but I still wonder how I can interact with Google Gemini to send prompts and scrape the results under software control.
Maybe, a DLL made with C++ doing this would be much easier for doing this especially if there are APIs for this.
I will give it a try but I still wonder how I can interact with Google Gemini to send prompts and scrape the results under software control.
Maybe, a DLL made with C++ doing this would be much easier for doing this especially if there are APIs for this.
Re: AI Prompt Enginering with LBB
I asked Gemini how to do it and got step by step instruction including a working LB code.
You must first get an API KEY from Google AI Studio and paste this into this LB code which makes CALLDLL to WININET.
It works perfectly!!!
You must first get an API KEY from Google AI Studio and paste this into this LB code which makes CALLDLL to WININET.
It works perfectly!!!
Code: Select all
' =========================================================================
' MASTER BLUEPRINT: API CALL, PARSE, AND SAVE TO CSV
' =========================================================================
' --- 1. Constants & Configuration ---
INTERNET.OPEN.TYPE.PRECONFIG = 0
INTERNET.SERVICE.HTTP = 3
INTERNET.FLAG.SECURE = 8388608
HTTPS.PORT = 443
ApiKey$ = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Server$ = "generativelanguage.googleapis.com"
' Using the Flash model which gave us the success
Path$ = "/v1beta/models/gemini-2.5-flash:generateContent?key=" + ApiKey$
' --- 2. Build JSON Payload ---
q$ = chr$(34)
UserQuery$ = "LIST US STATES IN CSV FORMAT AS STATE-SHORT,STATE-LONG"
[cite_start]' We instruct the AI to be clean for easier parsing [cite: 6]
Content$ = "{" + q$ + "contents" + q$ + ": [{" + q$ + "parts" + q$ + ":[{" + q$ + "text" + q$ + ": " + q$ + UserQuery$ + " .Output ONLY raw CSV data." + q$ + "}]}]}"
' --- 3. API Call Process ---
open "wininet.dll" for dll as #net
' 1. InternetOpen (Using INTERNET.OPEN.TYPE.PRECONFIG)
calldll #net, "InternetOpenA", "GeminiPOC" as ptr, INTERNET.OPEN.TYPE.PRECONFIG as long, _
"" as ptr, "" as ptr, 0 as long, hNet as ulong
' 2. InternetConnect (Using HTTPS.PORT and INTERNET.SERVICE.HTTP)
calldll #net, "InternetConnectA", hNet as ulong, Server$ as ptr, HTTPS.PORT as long, _
"" as ptr, "" as ptr, INTERNET.SERVICE.HTTP as long, 0 as long, 0 as long, hConn as ulong
' 3. HttpOpenRequest (Using INTERNET.FLAG.SECURE)
calldll #net, "HttpOpenRequestA", hConn as ulong, "POST" as ptr, Path$ as ptr, _
"HTTP/1.1" as ptr, "" as ptr, 0 as long, INTERNET.FLAG.SECURE as long, 0 as long, hReq as ulong
' 4. HttpSendRequest
Header$ = "Content-Type: application/json" + chr$(13) + chr$(10)
HDL = len(Header$)
CDL = len(Content$)
calldll #net, "HttpSendRequestA", hReq as ulong, Header$ as ptr, HDL as long, _
Content$ as ptr, CDL as long, res as ulong
' --- 5. Receive Buffer ---
FullResponse$ = ""
BUFFER.SIZE=8192
Buffer$ = space$(BUFFER.SIZE)
struct bRead, value as ulong
[readLoop]
calldll #net, "InternetReadFile", hReq as ulong, Buffer$ as ptr, BUFFER.SIZE as long, bRead as struct, ret as long
if bRead.value.struct > 0 then
FullResponse$ = FullResponse$ + left$(Buffer$, bRead.value.struct)
goto [readLoop]
end if
' --- 5. Parse and Save ---
' Extract the CSV content from the JSON envelope
ParsedCSV$ = parse.gemini.json$(FullResponse$)
if left$(ParsedCSV$, 5) <> "ERROR" then
' Save the clean CSV to a local file
fileName$ = "us_states_report.csv"
open fileName$ for output as #fOut
print #fOut, ParsedCSV$
close #fOut
print "SUCCESS: Parsed data saved to "; fileName$
RUN "NOTEPAD ";fileName$
else
print "FAILED: "; ParsedCSV$
end if
' --- 6. Cleanup ---
calldll #net, "InternetCloseHandle", hReq as ulong, r as long
calldll #net, "InternetCloseHandle", hNet as ulong, r as long
close #net
end
' =========================================================================
' CORE FUNCTIONS (The Parser)
' =========================================================================
function parse.gemini.json$(json$)
' Find the text field inside the JSON structure
q$ = chr$(34)
marker$ = q$ + "text" + q$ + ": " + q$
start = instr(json$, marker$)
if start = 0 then
parse.gemini.json$ = "ERROR: No CSV data found in response."
exit function
end if
contentStart = start + len(marker$)
contentEnd = instr(json$, q$, contentStart)
raw$ = mid$(json$, contentStart, contentEnd - contentStart)
' Convert \n to real newlines and remove any stray escape characters
clean$ = replstr$(raw$, "\n", chr$(13) + chr$(10))
parse.gemini.json$ = clean$
end function
function replstr$(s$, find$, replace$)
' Manual replacement for Liberty BASIC dot notation variables
res$ = ""
i = 1
while i <= len(s$)
if mid$(s$, i, len(find$)) = find$ then
res$ = res$ + replace$
i = i + len(find$)
else
res$ = res$ + mid$(s$, i, 1)
i = i + 1
end if
wend
replstr$ = res$
end function