How to use gpt4 in vb.net?
22:52 12 Jul 2023

I am trying to make a simple chat app with gpt4 but i got stuck, and gpt itself cant help me :D

So, i tried few end points i saw online, but nothing works. I get allways "The API response is missing or empty.". What is missing here?

My code:

    Imports System.Net.Http
Imports System.Text
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class Form1
    Private apiKey As String = "YOUR_API_KEY"
    Private endpointUrl As String = "https://api.openai.com/v1/engines/gpt-4-8k/completions"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim prompt As String = TextBox1.Text

        Dim client As HttpClient = New HttpClient()
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " & apiKey)

        Dim requestData As New JObject()
        requestData("prompt") = prompt
        requestData("max_tokens") = 50 ' Променете това според вашите нужди

        Dim httpContent As HttpContent = New StringContent(requestData.ToString(), Encoding.UTF8, "application/json")
        Dim response = client.PostAsync(endpointUrl, httpContent).Result

        Dim jsonResponse = response.Content.ReadAsStringAsync().Result

        ' Parsing the response
        Dim responseObject = JObject.Parse(jsonResponse)
        Dim choices As JArray = responseObject("choices")

        If choices IsNot Nothing AndAlso choices.Count > 0 Then
            Dim reply As String = choices(0)("text").ToString()
            TextBox2.Text = reply
        Else
            TextBox2.Text = "The API response is missing or empty."
        End If
    End Sub
End Class
vb.net openai-api