-
-
Notifications
You must be signed in to change notification settings - Fork 7
Examples
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.
This example uses the OpenAI.ImageRecognition class to ask an AI assistant to identify an image:
OpenAI.APIKey = "YOUR KEY HERE"
Dim url As String = "https://upload.wikimedia.org/wikipedia/commons/9/99/Aerial_view_of_the_White_House.jpg"
Dim response As OpenAI.ImageRecognition = 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.
response = response.GenerateNext("user", "What direction is the camera facing?")
answer = response.GetResult() ' The camera is facing northward.
'etc.
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)
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")
This example uses the OpenAI.ChatCompletion 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.ChatCompletion.Create("user", instruction + prompt)
Dim translation As String = result.GetResult()
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()
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()
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()
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
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()
Wiki home | Project page | Bugs | Become a sponsor
Text and code examples are Copyright ©2023-24 Andrew Lambert, offered under the CC BY-SA 3.0 License.