Post by Ultraman on Apr 1, 2024 21:18:23 GMT
I won't be providing an API key for obvious reasons, but here is some very basic code I wrote for interfacing with the Perplexity AI models. You can simply use the Perplexity function and pass it your query and the model number you want to use, 1-7. Give it a couple of seconds, and you've got a response from one of the most powerful AI systems out there! If you want to see how the billing works for the AI, check here.
Option Explicit
$NoPrefix
$Console:Only
Const INTERNET_OPEN_TYPE_DIRECT = 1
Const INTERNET_DEFAULT_HTTP_PORT = 80
Const INTERNET_DEFAULT_HTTPS_PORT = 443
Const INTERNET_SERVICE_HTTP = 3
Const INTERNET_FLAG_SECURE = &H00800000
Const INTERNET_FLAG_RELOAD = &H80000000
Const HTTP_QUERY_CONTENT_LENGTH = 5
Const TRUE = 1
Const FALSE = 0
Const CLIENT_SECRET = "getyourowndamnapikey"
Const SW_SHOWNORMAL = 1
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 Unsigned Long, Byval lpszUserName As Offset, Byval lpszPassword As Offset, Byval dwService As Unsigned Long, Byval dwFlags As Unsigned Long, Byval dwContext As Unsigned Long)
Function HttpOpenRequest%& Alias "HttpOpenRequestA" (ByVal hConnect As Offset, Byval lpszVerb As Offset, Byval lpszObjectName As Offset, Byval lpszVersion As Offset, Byval lpszReferer As Offset, Byval lplpszAcceptTypes As Offset, Byval dwFlags As Unsigned Long, Byval dwContext As Unsigned Offset)
Function HttpSendRequest& Alias "HttpSendRequestA" (ByVal hRequest As Offset, Byval lpszHeaders As Offset, Byval dwHeadersLength As Unsigned Long, Byval lpOptional As Offset, Byval dwOptionalLength As Unsigned Long)
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)
Sub InternetReadFile (ByVal hFile As Offset, Byval lpBuffer As Offset, Byval dwNumberOfBytesToRead As Unsigned Long, Byval lpdwNumberOfBytesRead As Offset)
Sub InternetCloseHandle (ByVal hInternet As Offset)
End Declare
Print Perplexity("Write up a nice greeting to the users of QB64 and introduce yourself", 1)
Models:
Data "mistral-7b-instruct"
Data "mixtral-8x7b-instruct"
Data "sonar-small-chat"
Data "sonar-medium-chat"
Data "sonar-small-online"
Data "sonar-medium-online"
Data "codellama-70b-instruct"
Function Perplexity$ (query As String, model As Unsigned Long)
Dim As String URL: URL = "https://api.perplexity.ai/chat/completions"
Dim As String server, apipath
server = Mid$(URL, InStr(URL, "/") + 2)
server = Mid$(server, 1, InStr(server, "/") - 1)
apipath = Mid$(URL, InStr(URL, server) + Len(server)) + Chr$(0)
server = server + Chr$(0)
Dim As Offset hInternet: hInternet = InternetOpen(0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0)
If hInternet = 0 Then Exit Function
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 = "application/json" + 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
Restore Models
Dim As String modelName
Dim As String models(1 To 7)
Dim As Long x
For x = 1 To 7
Read modelName
models(x) = modelName
Next
Dim As String opt: opt = "{" + Q("model") + ": " + Q(models(model)) + "," + Q("messages") + ": [{" + Q("role") + ": " + Q("user") + "," + Q("content") + ": " + Q(query) + "}]}"
Dim As String headers: headers = "Authorization: Bearer " + CLIENT_SECRET + Chr$(13) + Chr$(10) + "Content-Type: application/json" + Chr$(0)
'Print "HttpSendRequest"
If HttpSendRequest(hRequest, Offset(headers), -1, Offset(opt), Len(opt)) <> TRUE Then
InternetCloseHandle hRequest
InternetCloseHandle hConnect
InternetCloseHandle hInternet
Exit Function
End If
Dim As String queryinfo: queryinfo = Space$(1024)
Dim As Unsigned Long querylen: querylen = Len(queryinfo) - 1
'Print "HttpQueryInfo"
If HttpQueryInfo(hRequest, HTTP_QUERY_CONTENT_LENGTH, Offset(queryinfo), Offset(querylen), 0) <> 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
'Print "InternetReadFile"
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
response = Trim$(response)
Dim As String aiResponse
If Len(response) > 0 And response <> "" Then
aiResponse = GetKey(response, "content")
Else
aiResponse = "Some kind of error occurred"
End If
Perplexity = String.Replace(aiResponse, "\n", Chr$(10))
End Function
Function GetKey$ (JSON As String, keyname As String)
Dim jkey As String
jkey = JSON
If InStr(jkey, Chr$(34) + keyname + Chr$(34)) Then
jkey = Mid$(jkey, InStr(jkey, Chr$(34) + keyname + Chr$(34)) + Len(keyname))
jkey = Mid$(jkey, InStr(jkey, ":") + 2)
jkey = String.Replace(jkey, "\" + Chr$(34), "'")
If Mid$(jkey, 1, 1) = Chr$(34) Then
jkey = Mid$(jkey, 2)
End If
jkey = Mid$(jkey, 1, InStr(jkey, Chr$(34)) - 1)
If Right$(jkey, 1) = "," Then
jkey = Mid$(jkey, 1, Len(jkey) - 1)
End If
Else
GetKey = ""
End If
GetKey = jkey
End Function
Function String.Replace$ (a As String, b As String, c As String)
Dim j
Dim r
Dim r$
j = InStr(a, b)
If j > 0 Then
r$ = Left$(a, j - 1) + c + String.Replace(Right$(a, Len(a) - j + 1 - Len(b)), b, c)
Else
r$ = a
End If
String.Replace = r$
End Function
Function Q$ (in As String)
Q$ = Chr$(34) + in + Chr$(34)
End Function