Skip to content

Examples

Andrew Lambert edited this page Mar 6, 2023 · 37 revisions

Correct text

This example asks the AI 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()

Translate text

This example asks the AI 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 uploads an MP3 file of a speech for the AI to translate into English text:

  OpenAI.APIKey = "YOUR KEY HERE"
  Dim file As FolderItem = SpecialFolder.Desktop.Child("speech.mp3")
  Dim reply As OpenAI.Response = OpenAI.AudioTranscription.Create(file)
  Dim transcript As String = reply.GetResult()

Answer questions based on supplied text

This example asks the AI to answer questions using 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()

Generate picture

This example asks the AI 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 asks the AI 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 asks the AI 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

Chatting with an AI assistant

This example chats 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.

Generate subtitles for a video

This example uploads an MP4 video file of a speech and asks the AI to generate a SRT (subtitles) file for it.

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

Sending a custom request

This example creates a new OpenAI.Request object and uses it 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
Clone this wiki locally