Open Menu dzosoft
Close Menu dzosoft

   ALL ABOUT COMPUTER SCIENCE AND TECHNOLOGY


                             










 
 
 

Using ChatGPT in VB .Net

 


In this tutorial we will try to show by writing an example how to integrate the chatgpt api in a program written in VB.Net

First we start by creating a VB ChatGPT project

 
Using ChatGPT in VB .Net
 

 
Using ChatGPT in VB .Net
 

Once created,we will have the first form already inserted in our project

 
Using ChatGPT in VB .Net
 
We will need the speech library, you go to the nuget package for installing the library

 
Using ChatGPT in VB .Net
 

 
Using ChatGPT in VB .Net
 

Once done, we begin to insert all our objects necessary to build our chatgpt, the first object is textEdit multiline where we will have the answer from the server, we give it the name txtAnswer
Then we insert the txtQuestion object where we write our questions, we insert the send button to send our request I try giving it the name btnSend
We insert the lblSpeech label which is used to receive part of the text each time we receive the answer.

Now we insert the randomness object associated with the temperature variable. Temperature is a hyperparameter used in some natural language processing models,including ChatGPT, to control the level of randomnes

After we insert the objet MaxTokens (TextEdit)

The models understand and process text by breaking it down into tokens.
Tokens can be words or just chunks of characters. For example, the word “hamburger” gets broken up into the tokens “ham”, “bur” and “ger”, while a short and common word like “pear” is a single token. Many tokens start with a whitespace, for example “ hello” and “ bye”.
The number of tokens processed in a given API request depends on the length of both your inputs and outputs. As a rough rule of thumb, 1 token is approximately 4 characters or 0.75 words for English text.
One limitation to keep in mind is that your text prompt and generated completion combined must be no more than the model's maximum context length (for most models this is 2048 tokens, or about 1500 words).
Check out the tokenizer tool to learn more about how text translates to tokens.
Then we insert a checkbox to put on pause listening and another to mute. We also use a combobox for the models.
The API is powered by a set of models with different capabilities and price points. the openai base GPT-3 models are called Davinci, Curie, Babbage and Ada. The Codex series is a descendant of GPT-3 that’s been trained on both natural language and code. To learn more, visit the models documentation of the openai website.
And another for the existing voices, which we must loadby sending a request to the server

 
Using ChatGPT in VB .Net
 

Now we start writing VB .Net code

We start by importing all the necessary modules:

Imports System.Net
Imports System.IO
Imports System.Configuration
Imports System.Security.Cryptography
Imports System.Speech.Synthesis
Imports System.Speech.Recognition


We need the constant openai_api_key , then go back to the site

https://platform.openai.com/account/api-keys , either we can initialize it with the code or in the project configuration file app.config.

	Dim OPENAI_API_KEY As String = "" 'https://beta.openai.com/account/api-keys
    Dim oSpeechRecognitionEngine As SpeechRecognitionEngine = Nothing
    Dim oSpeechSynthesizer As System.Speech.Synthesis.SpeechSynthesizer = Nothing


We need oSpeechRecognitonEngine and oSpeechSynthesizer variables in the load procedure of the form we read the value of openai_api_key xml configuration file appi.config
Now we try to load the combo that contains the different voices that we can use.

Private Sub frmGPTChat_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim oAppSettingsReader As New AppSettingsReader()
        Dim sApiKey As String = oAppSettingsReader.GetValue("OPENAI_API_KEY", GetType(String)) & ""

If sApiKey = "" Then MsgBox("Please enter your OpenAI API key in the App.config file.") End Else OPENAI_API_KEY = sApiKey End If
'SetModels() We can load all the models or load only the three models that I've noticed working cbModel.Items.Clear() cbModel.Items.Add("text-davinci-003") cbModel.Items.Add("text-davinci-002") cbModel.Items.Add("code-davinci-002")
cbModel.SelectedIndex = 0

cbVoice.Items.Clear() Dim synth As New SpeechSynthesizer() For Each voice In synth.GetInstalledVoices() cbVoice.Items.Add(voice.VoiceInfo.Name) Next cbVoice.SelectedIndex = 0
End Sub


When you click on cklisten if selected, you see the lblSpeech label and we call the function speechtoText

 Private Sub chkListen_CheckedChanged(sender As Object, e As EventArgs) Handles chkListen.CheckedChanged
        If chkListen.Checked Then
            lblSpeech.Text = ""
            lblSpeech.Visible = True
            SpeechToText()
        Else
            oSpeechRecognitionEngine.RecognizeAsyncStop()
            lblSpeech.Visible = False
        End If
    End Sub
    Private Sub chkMute_CheckedChanged(sender As Object, e As EventArgs) Handles chkMute.CheckedChanged

If chkMute.Checked Then lblVoice.Visible = False cbVoice.Visible = False Else lblVoice.Visible = True cbVoice.Visible = True End If
End Sub


If the oSpeechRecognitionEngine object is not initialized, we initialize it

Private Sub SpeechToText()

If oSpeechRecognitionEngine IsNot Nothing Then oSpeechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple) Exit Sub End If
oSpeechRecognitionEngine = New SpeechRecognitionEngine(New System.Globalization.CultureInfo("en-US")) oSpeechRecognitionEngine.LoadGrammar(New DictationGrammar()) AddHandler oSpeechRecognitionEngine.SpeechRecognized, AddressOf OnSpeechRecognized AddHandler oSpeechRecognitionEngine.SpeechHypothesized, AddressOf OnSpeechHypothesized oSpeechRecognitionEngine.SetInputToDefaultAudioDevice() oSpeechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple) End Sub

We also associate the OnSpeechRecognized event which allows us to prepare our question before sending it, we associate the OnSpeechHypoSynthesized event which allows us to visualize the text in the lblSpeech label.

Private Sub OnSpeechRecognized(sender As Object, e As SpeechRecognizedEventArgs)
lblSpeech.Text = "" 'Reset Hypothesized text

If txtQuestion.Text <> "" Then txtQuestion.Text += vbCrLf End If
Dim text As String = e.Result.Text txtQuestion.Text += text End Sub
Private Sub OnSpeechHypothesized(sender As Object, e As SpeechHypothesizedEventArgs) Dim text As String = e.Result.Text lblSpeech.Text = text End Sub


Now we go to the send button which allows us to send the request thanks to the sendMsg function using the http protocol by invoking the openai API

 Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click

Dim sQuestion As String = txtQuestion.Text If sQuestion = "" Then MsgBox("Type in your question!") txtQuestion.Focus() Exit Sub End If
If txtAnswer.Text <> "" Then txtAnswer.AppendText(vbCrLf) End If
txtAnswer.AppendText("Me: " & sQuestion & vbCrLf) txtQuestion.Text = ""
Try Dim sAnswer As String = SendMsg(sQuestion) txtAnswer.AppendText("Chat GPT: " & Replace(sAnswer, vbLf, vbCrLf)) SpeechToText(sAnswer) Catch ex As Exception txtAnswer.AppendText("Error: " & ex.Message) End Try
End Sub


We have the endpoint api.openai.com/v1/completion and the request is done with post method with json content
Our request consists of maxtokens with value 2048, randomness 0.5, the user id for this example is 1 but we can associate the requests to differents users
We prepare the data variable with all the necessary parameters at the end we read our response in json format.

 Function SendMsg(ByVal sQuestion As String)

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3 Or System.Net.SecurityProtocolType.Tls12 Or System.Net.SecurityProtocolType.Tls11 Or System.Net.SecurityProtocolType.Tls
Dim apiEndpoint As String = "https://api.openai.com/v1/completions" Dim request As HttpWebRequest = WebRequest.Create(apiEndpoint) request.Method = "POST" request.ContentType = "application/json" request.Headers.Add("Authorization", "Bearer " & OPENAI_API_KEY)
Dim iMaxTokens As Integer = txtMaxTokens.Text '2048
Dim dTemperature As Double = txtTemperature.Text '0.5 If dTemperature < 0 Or dTemperature > 1 Then MsgBox("Randomness has to be between 0 and 1 with higher values resulting in more random text") Return "" End If
Dim sUserId As String = "1" Dim sModel As String = cbModel.Text 'text-davinci-002, text-davinci-003
'https://beta.openai.com/docs/api-reference/completions/create Dim data As String = "{" data += " ""model"":""" & sModel & """," data += " ""prompt"": """ & PadQuotes(sQuestion) & """," data += " ""max_tokens"": " & iMaxTokens & "," data += " ""user"": """ & sUserId & """, " data += " ""temperature"": " & dTemperature & ", " data += " ""frequency_penalty"": 0.0" & ", " 'Number between -2.0 and 2.0 Positive value decrease the model's likelihood to repeat the same line verbatim. data += " ""presence_penalty"": 0.0" & ", " ' Number between -2.0 and 2.0. Positive values increase the model's likelihood to talk about new topics. data += " ""stop"": [""#"", "";""]" 'Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. data += "}"
Using streamWriter As New StreamWriter(request.GetRequestStream()) streamWriter.Write(data) streamWriter.Flush() streamWriter.Close() End Using
Dim response As HttpWebResponse = request.GetResponse() Dim streamReader As New StreamReader(response.GetResponseStream()) Dim sJson As String = streamReader.ReadToEnd() 'Return sJson
Dim oJavaScriptSerializer As New System.Web.Script.Serialization.JavaScriptSerializer Dim oJson As Hashtable = oJavaScriptSerializer.Deserialize(Of Hashtable)(sJson) Dim sResponse As String = oJson("choices")(0)("text")
Return sResponse
End Function

The setmodels function allows to initialize the combo with the possible models, for the moment I noted that only 3 models that I can use the others don't work, maybe because my registration in the openai site is free

 Private Sub SetModels()
        'https://beta.openai.com/docs/models/gpt-3

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3 Or System.Net.SecurityProtocolType.Tls12 Or System.Net.SecurityProtocolType.Tls11 Or System.Net.SecurityProtocolType.Tls
Dim apiEndpoint As String = "https://api.openai.com/v1/models" Dim request As HttpWebRequest = WebRequest.Create(apiEndpoint) request.Method = "GET" request.ContentType = "application/json" request.Headers.Add("Authorization", "Bearer " & OPENAI_API_KEY)
Dim response As HttpWebResponse = request.GetResponse() Dim streamReader As New StreamReader(response.GetResponseStream()) Dim sJson As String = streamReader.ReadToEnd() 'Return sJson
cbModel.Items.Clear()
Dim oSortedList As SortedList = New SortedList() Dim oJavaScriptSerializer As New System.Web.Script.Serialization.JavaScriptSerializer Dim oJson As Hashtable = oJavaScriptSerializer.Deserialize(Of Hashtable)(sJson) Dim oList As Object() = oJson("data") For i As Integer = 0 To oList.Length - 1 Dim sId As String = oList(i)("id") oSortedList.Add(sId, sId) Next
'Text-davinci - 3 'Text-davinci - 2 'code-davinci - 2
For Each oItem As DictionaryEntry In oSortedList cbModel.Items.Add(oItem.Key) Next

End Sub


Let's not forget to replace the different characters not allowed using a separate function called PdQuotes before sending the message

Private Function PadQuotes(ByVal s As String) As String

If s.IndexOf("\") <> -1 Then s = Replace(s, "\", "\\") End If
If s.IndexOf(vbCrLf) <> -1 Then s = Replace(s, vbCrLf, "\n") End If
If s.IndexOf(vbCr) <> -1 Then s = Replace(s, vbCr, "\r") End If
If s.IndexOf(vbLf) <> -1 Then s = Replace(s, vbLf, "\f") End If
If s.IndexOf(vbTab) <> -1 Then s = Replace(s, vbTab, "\t") End If
If s.IndexOf("""") = -1 Then Return s Else Return Replace(s, """", "\""") End If End Function
End Class


Once the code part is finished, we will look for the key, we must make a registration in the website openai.com, it's free and we paste the key in our xml file api.config

You could download the whole project here   Download this project

It might also interest you


All about computer science and technology

How to create Paint tool in VB .Net

Use VB .Net and Active Directory

Webcam Capture in VB.NET

3D shape, 3D Curve and 3D Ball in VB .Net using GDI+


Leave comment
          

Save nickname and email in this browser for the next time.



Brandon
Thank you so much for this tutorial! It's easy to understand - and you even provided the completed project sample! Thank you so much you're really awesome.

posted 393 days a go



dzosoft
Thank you Brandon :) 

posted 357 days a go



Loading...