Git Product home page Git Product logo

chatgptkit's Introduction

ChatGPTKit

A very simple Swifty API to use ChatGPT from OpenAI. Please let me know if you find any errors.

Installation

  1. In your Xcode project, go to File -> Add Packages
  2. Enter the package URL: https://https://github.com/heysaik/ChatGPTKit
  3. Click on Add Package and let Xcode install the Swift Package

Usage

let chattyGPT = ChatGPTKit(apiKey: "YOUR-API-KEY")
var history = [Message(role: .system, content: "You are a bot designed to aid mental health."), Message(role: .user, content: "Hello! I'm feeling rather sad today.")]

switch try await chattyGPT.performCompletions(messages: history) {
case .success(let response):
    if let firstResponse = response.choices?.first {
        history.append(firstResponse.message)
        print(firstResponse.message.content)
    }
case .failure(let error):
    print(error.message)
}

Types

This can help you understand how to use the package better. This is an exact Swift copy of the response object from the OpenAI API.

Model

enum Model {
    case turbo
    case turbo31
}

Role

Typically, a conversation is formatted with a system message first, followed by alternating user and assistant messages.

  • The system message helps set the behavior of the assistant.
  • The user messages help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction.
  • The assistant messages help store prior responses. They can also be written by a developer to help give examples of desired behavior.
enum Role {
    case system
    case user
    case assistant
}

Message

Messages must be an array of message objects, where each object has a role (either “system”, “user”, or “assistant”) and content (the content of the message). Conversations can be as short as 1 message or fill many pages.

struct Message {
    var role: Role
    var content: String
}

APIUsage

struct APIUsage {
    var prompt_tokens: Int
    var completion_tokens: Int
    var total_tokens: Int
}

ResponseChoice

struct ResponseChoice {
    var index: Int
    var message: Message
    var finish_reason: String
}

Response

struct Response {
    var id: String
    var object: String
    var created: Int
    var choices: [ResponseChoice]
    var usage: APIUsage
}

APIError

APIError is the default Error type this package returns. If the OpenAI API returns an error, it will be of this type and you can get the code, message, and type. If there is another error from the client side, you can read the error property to understand the error.

struct APIError: Error {
    public var code: Int?
    public var message: String
    public var type: String?
    public var error: Error?
    
    public init(code: Int? = nil, message: String, type: String? = nil, error: Error? = nil) {
        self.code = code
        self.message = message
        self.type = type
        self.error = error
    }
}

ErrorResponse

struct ErrorResponse {
    var error: APIError
}

Compatibility

  • iOS 13.0+
  • macOS 13.0+
  • watchOS 9.0+
  • tvOS 16.0+

chatgptkit's People

Contributors

heysaik avatar

Watchers

 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.