Skip to content

Examples

Andrew Lambert edited this page Dec 12, 2023 · 37 revisions

Chatting with an AI assistant

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

  OpenAI.APIKey = "YOUR KEY HERE"
  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.

Speech synthesis

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

  OpenAI.APIKey = "YOUR KEY HERE"
  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 KEY HERE"
  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.Completion class to translate an English sentence into French.

  OpenAI.APIKey = "YOUR API KEY"
  Dim instruction As String = "Translate this into French: " + EndOfLine.UNIX + EndOfLine.UNIX
  Dim prompt As String = "Hello, I would like to purchase some matches"
  Dim result As OpenAI.Response = OpenAI.Completion.Create(instruction + prompt)
  Dim translation As String = result.GetResult()

Translate audio

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

  OpenAI.APIKey = "YOUR KEY HERE"
  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.Generate("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 request As New OpenAI.Request
  request.Prompt = "What is the airspeed velocity of an unladen European swallow?"
  request.Model = "text-davinci-003"
  request.Temperature = 0.9
  request.NumberOfResults = 5
  request.BestOf = 10
  request.MaxTokens = 128
  
  Dim result As OpenAI.Response = OpenAI.Completion.Create(request)
  Dim choices() As String
  For i As Integer = 0 To result.ResultCount - 1
    choices.Append(result.GetResult(i))
  Next

Deprecated endpoint examples

The following examples are for endpoints which have been or will be deprecated by OpenAI.

Correct text

This example uses the OpenAI.Completion class to correct an English sentence.

  OpenAI.APIKey = "YOUR API KEY"
  Dim instruction As String = "Correct this to standard English" ' natural language instructions to AI
  Dim prompt As String = "This ez uh test uv teh OpenAI AIP"
  Dim result As OpenAI.Response = OpenAI.Completion.Edit(prompt, instruction)
  Dim correction As String = result.GetResult()

Answer questions based on supplied text

This example uses the OpenAI.Completion class to answer questions based on information provided in a natural language format (such as an encyclopedia article.)

  OpenAI.APIKey = "YOUR API KEY"
  Dim request As New OpenAI.Request()
  request.Model = "text-davinci-003"
  Dim baleeninfo As String ' the first paragraph of the wikipedia article on baleen whales
  request.Prompt = "Based on the following text, what are baleen whales?" + EndOfLine.UNIX + EndOfLine.UNIX + baleeninfo
  Dim result As OpenAI.Response = OpenAI.Completion.Create(request)
  Dim answer As String = result.GetResult()
Clone this wiki locally