peerasak-u / GPTChat

This package is a proof-of-concept project aimed at demonstrating the use of ChatGPT-4 to generate a Swift Package for interacting with OpenAI's GPT models

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GPTChat

This package is a proof-of-concept project aimed at demonstrating the use of ChatGPT-4 to generate a Swift Package for interacting with OpenAI's GPT models

Features

  • Asynchronous API calls using Swift's native async/await.
  • Strongly-typed models for requests and responses.
  • Supports macOS 12.0 and above.

Installation

Swift Package Manager

You can install GPTChat via Swift Package Manager by adding the package to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/peerasak-u/GPTChat.git", from: "1.0.0"),
]

Then, add GPTChat to your targets:

targets: [
    .target(
        name: "YourTarget",
        dependencies: ["GPTChat"]),
]

Usage

Firstly, import the package in your file:

import GPTChat

Initialize the API Client

let gptChatAPI = GPTChatAPI(apiKey: "YOUR_OPENAI_API_KEY", model: "gpt-3.5-turbo")

Making a Chat Request

let systemMessage = GPTMessage(role: "system", content: "You are a helpful assistant.")
let userMessage = GPTMessage(role: "user", content: "Who won the world series in 2020?")

Task {
    do {
        let response = try await gptChatAPI.chat(withMessages: [systemMessage, userMessage])
        print(response.choices.first?.message.content ?? "No response")
    } catch {
        print("Error:", error.localizedDescription)
    }
}

Contributing

Feel free to open issues or pull requests with improvements or bug fixes.

About

This package is a proof-of-concept project aimed at demonstrating the use of ChatGPT-4 to generate a Swift Package for interacting with OpenAI's GPT models


Languages

Language:Swift 100.0%