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
- Asynchronous API calls using Swift's native
async/await
. - Strongly-typed models for requests and responses.
- Supports macOS 12.0 and above.
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"]),
]
Firstly, import the package in your file:
import GPTChat
let gptChatAPI = GPTChatAPI(apiKey: "YOUR_OPENAI_API_KEY", model: "gpt-3.5-turbo")
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)
}
}
Feel free to open issues or pull requests with improvements or bug fixes.