Skip to content

Examples

Andrew Lambert edited this page Jul 21, 2024 · 37 revisions

Chatting with an AI assistant

This example uses the OpenAI.ChatCompletion class to chat with an AI assistant.

  OpenAI.APIKey = "YOUR API KEY"
  Dim reply As OpenAI.ChatCompletion = OpenAI.ChatCompletion.Create("user", "Hello, I've come here looking for an argument.")
  Dim chatresult As String = reply.GetResult() ' assistant: No you haven't!
  
  reply = reply.GenerateNext("user", "Yes I have!")
  chatresult = reply.GetResult() ' assistant: Sorry, is this the 5 minute argument, or the whole half hour?
  
  reply = reply.GenerateNext("user", "What?")
  chatresult = reply.GetResult() ' assistant: Are you here for the whole half hour?
  
  'etc.

Computer vision

This example uses the OpenAI.ImageRecognition class to ask an AI assistant to identify an image:

  OpenAI.APIKey = "YOUR API KEY"
  Dim url As String = "https://upload.wikimedia.org/wikipedia/commons/9/99/Aerial_view_of_the_White_House.jpg"  
  Dim response As OpenAI.Response = OpenAI.ImageRecognition.Create("What is this a photo of?", url)
  Dim answer As String = response.GetResult() ' This is an aerial photo of the the White House in Washington, DC.

Speech synthesis

This example uses the OpenAI.AudioGeneration class to generate an MP3 of the AI speaking provided text.

  OpenAI.APIKey = "YOUR API KEY"
  Dim txt As String = "This is a test of the OpenAI API. This is only a test. Had this been an actual API call, your API key would have been used instead of the example filler."
  Dim model As OpenAI.Model = "tts-1"
  Dim voice As String = "alloy"
  Dim mp3audio As MemoryBlock = OpenAI.AudioGeneration.CreateRaw(txt, model, voice)

Generate subtitles for a video

This example uses the OpenAI.AudioTranscription class to upload an MP4 file for transcription into SRT (subtitles) format.

  OpenAI.APIKey = "YOUR API KEY"
  Dim file As FolderItem = SpecialFolder.Desktop.Child("speech.mp4")
  Dim subtitles As String = OpenAI.AudioTranscription.CreateRaw(file, "srt")

Translate text

This example uses the OpenAI.ChatCompletion class to translate an English sentence into French.

  OpenAI.APIKey = "YOUR API KEY"
  Dim chatlog As New OpenAI.ChatCompletionData()
  chatlog.AddMessage("system", "You will be provided with English sentences. Your task is to translate them into French.")
  Dim reply As OpenAI.ChatCompletion = OpenAI.ChatCompletion.Create(chatlog, "user", "Hello, I would like to purchase some matches.")
  Dim translation As String = reply.GetResult()

Translate audio

This example uses the OpenAI.AudioTranslation class to upload an MP3 file for transcription into English text:

  OpenAI.APIKey = "YOUR API KEY"
  Dim file As FolderItem = SpecialFolder.Desktop.Child("speech.mp3")
  Dim reply As OpenAI.Response = OpenAI.AudioTranslation.Create(file)
  Dim translation As String = reply.GetResult()

Generate picture

This example uses the OpenAI.Image class to generate a picture from a natural language prompt.

  OpenAI.APIKey = "YOUR API KEY"
  Dim result As OpenAI.Response = OpenAI.Image.Create("A rodent of unusual size")
  Dim p As Picture = result.GetResult()

Edit a picture

This example uses the OpenAI.Image class to edit a picture from a natural language prompt and a mask picture. The mask is a picture of the same dimensions as the original whose fully transparent areas (i.e. where alpha is zero) indicates where the original should be edited. If the original is already transparent in the target areas then the mask can be omitted.

  OpenAI.APIKey = "YOUR API KEY"
  Dim src As FolderItem = SpecialFolder.Desktop.Child("my portrait.png")
  Dim original As Picture = Picture.Open(src)
  src = SpecialFolder.Desktop.Child("my portrait mask.png")
  Dim mask As Picture = Picture.Open(src)
  ' let's add a NYC skyline as a backdrop to our portrait
  Dim img As OpenAI.Response = OpenAI.Image.Edit(original, "The New York City skyline", mask)
  Dim output As Picture = img.GetResult()

Check text for offensive content

This example uses the OpenAI.Moderation class to check a comment for offensive content.

  OpenAI.APIKey = "YOUR API KEY"
  Dim result As OpenAI.Response = OpenAI.Moderation.Create("I will kill that bitch")
  Dim response As JSONItem = result.GetResult()
  If response.Value("flagged") = True Then
    Dim categories As JSONItem = response.Value("categories")
    Dim scores As JSONItem = response.Value("category_scores")
    ' etc
  End If

Sending a custom request

This example uses the OpenAI.Request class to set the parameters of a custom request.

See also the OpenAI documentation examples page.

  OpenAI.APIKey = "YOUR API KEY"
  Dim chatlog As New OpenAI.ChatCompletionData
  chatlog.AddMessage("user", "What is the airspeed velocity of an unladen European swallow?")
  Dim request As New OpenAI.Request
  request.Model = "gpt-4"
  request.Messages = chatlog
  Dim result As OpenAI.Response = OpenAI.ChatCompletion.Create(request)
  Dim answer As String = result.GetResult()

Training a fine-tuned AI model

This example creates a local training file, uploads it to OpenAI, creates a fine tuning job with the file and one of the base AI models, and then uses the resulting fine-tuned model to perform a chat request.

  OpenAI.APIKey = "YOUR API KEY"
  
  ' first, create a local file containing your training lines
  Dim datafile As FolderItem = SpecialFolder.Desktop.Child("datafile.jsonl")
  Dim trainer As OpenAI.FineTuneData = OpenAI.FineTuneData.Create(datafile)
  trainer.AddLine("You are anwsering riddles.", "What has to be broken before you can use it?", "An egg")
  trainer.AddLine("You are anwsering riddles.", "I'm tall when I'm young, and I'm short when I'm old. What am I?", "A candle")
  trainer.AddLine("You are anwsering riddles.", "What month of the year has 28 days?", "All of them")
  ' etc.
  trainer.Save(datafile, True)
  
  ' then upload the training file to OpenAI
  Dim file As OpenAI.File = OpenAI.File.Create(datafile, "fine-tune")
  
  ' then create the fine-tuning job
  Dim basemodel As OpenAI.Model = "gpt-3.5-turbo-1106"
  Dim job As OpenAI.FineTuneJob = OpenAI.FineTuneJob.Create(file, basemodel)
  ' the fine-tune job is now pending. It may take a long time to complete.
  
  ' periodically refresh the job to retrieve the current status
  job.Refresh()
  If job.Status = OpenAI.JobStatus.Succeeded Then
    ' once the training job has succeeded you can use the trained model in your requests
    Dim mymodel As OpenAI.Model = job.FineTunedModel
    Dim chat As OpenAI.ChatCompletion = OpenAI.ChatCompletion.Create("user", "What month has 28 days?", mymodel)
    Dim answer As String = chat.GetResult() ' All of them.
  End If
Clone this wiki locally