Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Function AskGoogleGemini(numberToConvert As String) As String
- Dim geminiRequest As MSXML2.XMLHTTP60 ' add reference to Microsoft, XML v6.0
- Dim apiKey As String
- Dim apiURL As String
- Dim apiResponse As String
- Dim apiStatus As String
- Dim apiQuestion As String
- Dim apiResult As String
- apiURL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key="
- apiKey = "******* CREATE A KEY OF YOUR OWN ***************"
- apiQuestion = "Convert this number " & numberToConvert & " to a sentence of words"
- Set geminiRequest = New MSXML2.XMLHTTP60
- With geminiRequest
- .Open "POST", apiURL & apiKey, False
- .setRequestHeader "Content-Type", "application/json"
- .send "{""contents"":{""parts"":[{""text"":""" & apiQuestion & """}]},""generationConfig"":{""temperature"":0.5}}"
- apiStatus = .Status
- apiResponse = .responseText
- End With
- If apiStatus = 200 Then
- apiResult = ExtractContent(apiResponse)
- Else
- apiResult = "Error : " & ExtractError(apiResponse)
- End If
- Set geminiRequest = Nothing
- AskGoogleGemini = apiResult
- End Function
- Function ExtractContent(jsonString As String) As String
- Dim startPos As Long
- Dim endPos As Long
- Dim TextValue As String
- startPos = InStr(jsonString, """text"": """) + Len("""text"": """)
- endPos = InStr(startPos, jsonString, """") ' Find the position of the next double quote character
- TextValue = Mid(jsonString, startPos, endPos - startPos)
- Content = Trim(Replace(TextValue, "\""", Chr(34)))
- 'Fix for excel formulas as response
- If Left(Trim(Content), 1) = "=" Then
- Content = "'" & Content
- End If
- Content = Replace(Content, vbCrLf, "")
- Content = Replace(Content, vbLf, "")
- Content = Replace(Content, vbCr, "")
- Content = Replace(Content, "\n", "")
- If Right(Content, 1) = """" Then
- Content = Left(Content, Len(Content) - 1)
- End If
- ExtractContent = Content
- End Function
- Function ExtractError(jsonString As String) As String
- Dim startPos As Long
- Dim endPos As Long
- Dim TextValue As String
- startPos = InStr(jsonString, """message"": """) + Len("""message"": """)
- endPos = InStr(startPos, jsonString, """") ' Find the position of the next double quote character
- TextValue = Mid(jsonString, startPos, endPos - startPos)
- Content = Trim(Replace(TextValue, "\""", Chr(34)))
- 'Fix for excel formulas as response
- If Left(Trim(Content), 1) = "=" Then
- Content = "'" & Content
- End If
- Content = Replace(Content, vbCrLf, "")
- Content = Replace(Content, vbLf, "")
- Content = Replace(Content, vbCr, "")
- If Right(Content, 1) = """" Then
- Content = Left(Content, Len(Content) - 1)
- End If
- ExtractError = Content
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement