dalu / Xojo-OpenAI

A Xojo API for interacting with OpenAI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 asks the AI to correct a sentence for English spelling and grammar More examples.

  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)

Highlights

  • Issue natural-language instructions to the AI
  • Generate images based on a description
  • Modify, analyze, and parse text or source code according to instructions
  • Analyze text for hate, threats, self-harm, sexual content, child abuse, and violence
  • Can use the RB-libcURL wrapper, the MonkeyBread curl plugin, or the Xojo URLConnection to make API requests.

Synopsis

Caution: This project is still a work in progress and not everything works yet.

OpenAI API endpoints are exposed through several object classes:

Endpoint Object Class Comment
/v1/models Model List and select from the available AI models
/v1/completions and /v1/edits Completion A text or code completion or edit.
/v1/images/generations and /v1/images/edits Image An image that was generated or modified.
/v1/files File A file that was or will be uploaded.
/v1/fine-tunes FineTune TBD.
/v1/moderations Moderation An analysis of offensive content in a given text. (i.e. "content moderation")

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/completions endpoint you would use the OpenAI.Completion.Create or OpenAI.Completion.Edit factory methods. 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 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

How to incorporate OpenAI into your Xojo project

Import 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.

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/RealStudio that do not have the URLConnection class (<=2018r2) you will need to use one of the curl options.

To enable RB-libcURL, copy (not 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.

Examples

About

A Xojo API for interacting with OpenAI

License:MIT License


Languages

Language:REALbasic 100.0%