Advertisement
jdelano

Untitled

Feb 23rd, 2025 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.94 KB | None | 0 0
  1. Public Function AskGoogleGemini(numberToConvert As String) As String
  2.  
  3.     Dim geminiRequest As MSXML2.XMLHTTP60   ' add reference to Microsoft, XML v6.0
  4.    
  5.     Dim apiKey As String
  6.     Dim apiURL As String
  7.     Dim apiResponse As String
  8.     Dim apiStatus As String
  9.     Dim apiQuestion As String
  10.     Dim apiResult As String
  11.        
  12.     apiURL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key="
  13.     apiKey = "******* CREATE A KEY OF YOUR OWN  ***************"
  14.  
  15.     apiQuestion = "Convert this number " & numberToConvert & " to a sentence of words"
  16.    
  17.     Set geminiRequest = New MSXML2.XMLHTTP60
  18.     With geminiRequest
  19.         .Open "POST", apiURL & apiKey, False
  20.         .setRequestHeader "Content-Type", "application/json"
  21.         .send "{""contents"":{""parts"":[{""text"":""" & apiQuestion & """}]},""generationConfig"":{""temperature"":0.5}}"
  22.         apiStatus = .Status
  23.         apiResponse = .responseText
  24.     End With
  25.    
  26.     If apiStatus = 200 Then
  27.       apiResult = ExtractContent(apiResponse)
  28.     Else
  29.       apiResult = "Error : " & ExtractError(apiResponse)
  30.     End If
  31.    
  32.     Set geminiRequest = Nothing
  33.    
  34.     AskGoogleGemini = apiResult
  35.        
  36. End Function
  37.  
  38. Function ExtractContent(jsonString As String) As String
  39.     Dim startPos As Long
  40.     Dim endPos As Long
  41.     Dim TextValue As String
  42.    
  43.     startPos = InStr(jsonString, """text"": """) + Len("""text"": """)
  44.     endPos = InStr(startPos, jsonString, """") ' Find the position of the next double quote character
  45.     TextValue = Mid(jsonString, startPos, endPos - startPos)
  46.     Content = Trim(Replace(TextValue, "\""", Chr(34)))
  47.    
  48.     'Fix for excel formulas as response
  49.     If Left(Trim(Content), 1) = "=" Then
  50.       Content = "'" & Content
  51.     End If
  52.    
  53.     Content = Replace(Content, vbCrLf, "")
  54.     Content = Replace(Content, vbLf, "")
  55.     Content = Replace(Content, vbCr, "")
  56.     Content = Replace(Content, "\n", "")
  57.    
  58.     If Right(Content, 1) = """" Then
  59.       Content = Left(Content, Len(Content) - 1)
  60.     End If
  61.    
  62.     ExtractContent = Content
  63. End Function
  64.  
  65. Function ExtractError(jsonString As String) As String
  66.     Dim startPos As Long
  67.     Dim endPos As Long
  68.     Dim TextValue As String
  69.    
  70.     startPos = InStr(jsonString, """message"": """) + Len("""message"": """)
  71.     endPos = InStr(startPos, jsonString, """") ' Find the position of the next double quote character
  72.     TextValue = Mid(jsonString, startPos, endPos - startPos)
  73.     Content = Trim(Replace(TextValue, "\""", Chr(34)))
  74.    
  75.     'Fix for excel formulas as response
  76.     If Left(Trim(Content), 1) = "=" Then
  77.       Content = "'" & Content
  78.     End If
  79.    
  80.     Content = Replace(Content, vbCrLf, "")
  81.     Content = Replace(Content, vbLf, "")
  82.     Content = Replace(Content, vbCr, "")
  83.    
  84.     If Right(Content, 1) = """" Then
  85.       Content = Left(Content, Len(Content) - 1)
  86.     End If
  87.    
  88.     ExtractError = Content
  89. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement