Skip to content

Latest commit

 

History

History
100 lines (77 loc) · 8.23 KB

README.md

File metadata and controls

100 lines (77 loc) · 8.23 KB

Introduction

OpenAI is an AI research and deployment company. Xojo-OpenAI is a Xojo and RealStudio wrapper for the OpenAI public API.

Example

This example chats with an AI assistant. More examples.

  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.

Highlights

  • Issue natural-language instructions to the AI
  • Generate images based on a description
  • Translate and transcribe digital audio
  • Speech synthesis
  • Analyze text for hate, threats, self-harm, sexual content, child abuse, and violence
  • Can use the RB-libcURL wrapper, the MonkeyBread curl plugin, the Xojo URLConnection, or the Xojo HTTPSecureSocket to make API requests.

XojoOpenAI1

This screen shot depicts the Xojo-OpenAI demo showing the result of an image generation request.

Become a sponsor

If you use this code in a commercial project, or just want to show your appreciation, please consider sponsoring me through GitHub. https://github.com/sponsors/charonn0

Synopsis

OpenAI API endpoints are exposed through several object classes:

Endpoint Object Class Comment
/v1/chat/completions ChatCompletion An AI response to a chat conversation prompt.
/v1/chat/completions ImageRecognition An AI response to a chat-based image recognition session.
/v1/images/generations and /v1/images/edits Image An image that was generated or modified.
/v1/audio/speech AudioGeneration A digital audio file containing synthesized speech from provided text.
/v1/audio/transcriptions AudioTranscription A transcript of an English language audio file.
/v1/audio/translations AudioTranslation An English translation of an audio file.
/v1/moderations Moderation An analysis of offensive content in a given text. (i.e. "content moderation")
/v1/models Model List and select from the available AI models
/v1/files File A fine-tuning file that has been, is being, or will be uploaded.

To make a request of the API, call the appropriate factory method on the corresponding object class. For example, to make a request of the /v1/images/generations endpoint you would use the OpenAI.Image.Create factory method, whereas the /v1/images/edits endpoint is accessed using the OpenAI.Image.Edit factory method. Factory methods perform the request and return a OpenAI.Response object (or one of its subclasses) containing the response.

Responses may have zero, one, or several choices, depending on the number of results you asked for. You can see how many choices are available by reading the Response.ResultCount property, and retrieve each result by calling the Response.GetResult method.

Factory methods generally come in two flavors: basic and advanced. The basic versions accept a few common parameters as direct arguments while the advanced versions accept a OpenAI.Request object with the correct properties set.

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

How to incorporate OpenAI into your Xojo project

Copy the OpenAI module

  1. Download the Xojo-OpenAI project either in ZIP archive format or by cloning the repository with your Git client.
  2. Open the Xojo-OpenAI project in REALstudio or Xojo. Open your project in a separate window.
  3. Copy the Xojo-OpenAI module into your project and save. Do not use the "Import" feature of the Xojo IDE.

Using RB-libcURL or MBS

The OpenAI module may optionally use my open source RB-libcURL wrapper or the commercial MBS libcurl plugin. These should be preferred in order to take advantage of HTTP/2. If neither of these are available, the built-in URLConnection class is used if it is available. In versions of Xojo that do not have the URLConnection class (<=2018r2) the built-in HTTPSecureSocket class is used unless it's too old (<=2014r2), in which case you will need to use one of the curl options.

To enable RB-libcURL, copy (not Import) the libcURL module from the RB-libcURL project into your project and set the OpenAI.USE_RBLIBCURL constant to True.

To enable the MBS plugin, ensure the plugin is installed and then set OpenAI.USE_MBS constant to True.

If both USE_RBLIBCURL and USE_MBS are True then USE_RBLIBCURL takes precedence.

If you are using neither RB-libcURL nor MBS then set both USE_RBLIBCURL and USE_MBS to False to avoid compiler errors.