Giter Site home page Giter Site logo

tranq72 / openaiapi Goto Github PK

View Code? Open in Web Editor NEW
12.0 1.0 2.0 26 KB

OpenAI API wrapper for Swift

License: MIT License

Swift 100.00%
gpt-3 openai openai-api swift swift-packages api chatgpt chatgpt-api whisper-ai speech-processing speech-recognition speech-to-text

openaiapi's Introduction

OpenAIAPI

A light wrapper around OpenAI API, written in Swift.

The full OpenAI platform docs are here: https://platform.openai.com/docs/

What's Implemented

Installation

Swift Package Manager

  1. Select File/Add Packages from xcode menu
  2. Paste https://github.com/tranq72/OpenAIAPI.git

To update, select Packages/Update to Latest Package Versions

Usage

import OpenAIAPI
let openAI = OpenAIAPI(OpenAIAPIConfig(secret: "..."))
let config = OpenAIAPICompletionParms(max_tokens: 500, temperature:0.9)

openAI.createCompletion(prompt: "Write a poem in the style of Dante about Steve Jobs", config: config) { (result:Result<OpenAIAPICompletionResponse, WebServiceError>) in
    switch result {
       case .success(let success):
          dump(success)
       case .failure(let failure):
          print("\(failure.localizedDescription)")
    }
}

๐Ÿ’ก OpenAIAPI supports Swift concurrency, e.g.:

Task {
      do {
          let result = try await openAI.retrieveModel("text-davinci-003")
          dump(result)
      } catch {
          print(error.localizedDescription)
      }
}
let config = OpenAIAPIEditParms(n: 2)

openAI.createEdit(instruction:"Fix spelling and grammar", input:"The pens is an the taible", config: config) { (result:Result<OpenAIAPIEditResponse, WebServiceError>) in
    switch result {
       case .success(let success):
          dump(success)
       case .failure(let failure):
          print("\(failure.localizedDescription)")
    }
}
let config = OpenAIAPIAudioParms(prompt: nil, response_format:OpenAIAPIResponseFormat.json.name) //, language: Iso639_1.en.code)

openAI.createTranscription(filedata:audio, filename: "transcript.mp3", config:queryParms) { (result:Result<OpenAIAPIAudioResponse, WebServiceError>) in
    switch result {
       case .success(let success):
          dump(success)
       case .failure(let failure):
          print("\(failure.localizedDescription)")
    }
}

openAI.createTranslation(filedata:audio, filename: "transcript.mp3", config:queryParms) { (result:Result<OpenAIAPIAudioResponse, WebServiceError>) in
    switch result {
       case .success(let success):
          dump(success)
       case .failure(let failure):
          print("\(failure.localizedDescription)")
    }
}
openAI.listModels { (result:Result<OpenAIAPIModelsResponse, WebServiceError>)  in
  switch result {
     case .success(let success):
        dump(success)
     case .failure(let failure):
        print("\(failure.localizedDescription)")
  }
}
openAI.retrieveModel("text-davinci-003") { (result:Result<OpenAIAPIModelResponse, WebServiceError>) in
            switch result {
            case .success(let success):
                dump(success)
            case .failure(let failure):
                dLog("FAILED: \(failure.localizedDescription)")
            }
        }

Query parameters

Default values for most parameters can be overridden for each query using the corresponding configuration objects: OpenAIAPICompletionParms, OpenAIAPIEditParms

For the full list of the supported parameters and their default values see OpenAIAPIQueryParms.swift.
For the supported models see OpenAIAPIModel.swift.
The createTranscription request supports an optional language parameter (string in ISO-639-1 code, see Iso639_1.swift) that can be used as a hint to improve accuracy and latency.

API secret

Keep your API secret secure and away from client apps. Instead of directly calling a third party API (like OpenAI, which is a paid service) you better deploy a reverse-proxy in your backend and set the endpoint and secret parameters accordingly.

Contributing

This is just an initial draft implementation for a side project. Feel free to raise a pull request if you spot a bug or would like to contribute.

License

MIT

openaiapi's People

Contributors

tranq72 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

gfdac smniemi

openaiapi's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.