Skip to content

Examples

Andrew Lambert edited this page Feb 1, 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(0)

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(0)

Explain source code

This example asks the AI to explain a snippet of Python source code.

  OpenAI.APIKey = "YOUR API KEY"
  Dim instruction As String = """""""" + EndOfLine.UNIX + "Here's what the above class is doing:" + EndOfLine.UNIX + "1." + EndOfLine.UNIX + EndOfLine.UNIX
  Dim prompt As String = "print('Hello, world!')" + EndOfLine.UNIX + EndOfLine.UNIX
  Dim result As OpenAI.Response = OpenAI.Completion.Create(prompt + instruction, 1, OpenAI.Model.Lookup("code-davinci-002"))
  Dim explanation As String = result.GetResult(0)

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(0)

Check text for offensive content

This example asks the AI to check a comment for offensive content.

Dim result As OpenAI.Response = OpenAI.Moderation.Create("I will kill that bitch")
Dim response As JSONItem = result.GetResult(0)
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 creates a new OpenAI.Request object and uses it to set the parameters of a custom request.

  OpenAI.APIKey = "YOUR API KEY"
  Dim request As New OpenAI.Request
  request.Model = OpenAI.Model.Lookup("text-davinci-003")
  request.MaxTokens = 60
  request.Prompt = "What is the airspeed velocity of an unladen European swallow?"
  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