GithubHelp home page GithubHelp logo

duanhai / openai-kit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dylanshine/openai-kit

0.0 0.0 0.0 92 KB

A community Swift package used to interact with the OpenAI API

License: MIT License

Swift 100.00%

openai-kit's Introduction

OpenAIKit

Swift

OpenAIKit is a Swift package used to communicate with the OpenAI API.

Setup

Add the dependency to Package.swift:

dependencies: [
    ...
    .package(url: "https://github.com/dylanshine/openai-kit.git", from: "1.0.0")
],
targets: [
    .target(name: "App", dependencies: [
        .product(name: "OpenAIKit", package: "openai-kit"),
    ]),

It is encouraged to use environment variables to inject the OpenAI API key, instead of hardcoding it in the source code.

# .env

OPENAI_API_KEY="YOUR-API-KEY"
OPENAI_ORGANIZATION="YOUR-ORGANIZATION"

โš ๏ธ OpenAI strongly recommends developers of client-side applications proxy requests through a separate backend service to keep their API key safe. API keys can access and manipulate customer billing, usage, and organizational data, so it's a significant risk to expose them.

Create a OpenAIKit.Client using a httpClient and configuration.

var apiKey: String {
    ProcessInfo.processInfo.environment["OPENAI_API_KEY"]!
}

var organization: String {
    ProcessInfo.processInfo.environment["OPENAI_ORGANIZATION"]!
}

...

let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
let configuration = Configuration(apiKey: apiKey, organization: organization)

let openAIClient = OpenAIKit.Client(httpClient: httpClient, configuration: configuration)

Using the API

The OpenAIKit.Client implements a handful of methods to interact with the OpenAI API:

import OpenAIKit

let completion = try await openAIClient.completions.create(
    model: Model.GPT3.davinci,
    prompts: ["Write a haiku"]
)

What's Implemented

Error handling

If the request to the API failed for any reason an OpenAIKit.APIErrorResponse is thrown. Simply ensure you catch errors thrown like any other throwing function

do {
   ...
} catch let error as APIErrorResponse {
    print(error)
}

openai-kit's People

Contributors

dylanshine avatar ronaldmannak avatar tonyduan2023 avatar arthurgarzajr avatar pabraiventures avatar ryu0118 avatar hyouuu avatar

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.