Introduction:
In this tutorial, you will learn how to create a simple command-line application that uses the CleverBird library to interact with OpenAI's GPT-4 model.
Prerequisites
- Swift 5.5 or later
- Xcode 13 or later
- An OpenAI API key
Step 1: Create a new Swift command-line application
- Open Xcode and create a new project.
- Choose the "macOS" tab and select "Command Line Tool."
- Set the Product Name to "CleverBirdExample," and make sure the Language is set to "Swift."
- Choose a location to save your project and click "Create."
Step 2: Add the CleverBird library
Add the CleverBird library using the Swift Package Manager:
- In Xcode, go to "File" > "Add Packages."
- Enter the URL
https://github.com/btfranklin/CleverBird.git
and click "Add Package." - Select the latest version of CleverBird and click "Add Package."
Step 3: Import CleverBird and define main function
In the main.swift
file, import the CleverBird library and define the main
function:
import Foundation
import CleverBird
func main() async {
// Your code will go here
}
await main()
Step 4: Set up your OpenAI API connection
Inside the main
function, create a connection to the OpenAI API using your API key:
let apiKey = "your_api_key_here"
let connection = OpenAIAPIConnection(apiKey: apiKey)
Replace "your_api_key_here"
with your actual OpenAI API key.
Step 5: Create a chat thread with system and user messages
Initialize a chat thread and add a system message with instructions for the AI and a user message with a query:
let chatThread = OpenAIChatThread(connection: connection)
.addSystemMessage("You are a helpful assistant.")
.addUserMessage("Who won the world series in 2020?")
Step 6: Complete the chat and print the assistant's response
Use the complete()
function to get the AI's response and print it:
if let response = await chatThread.complete() {
print("Assistant: \(response.content)")
} else {
print("Error: Unable to get a response from the AI.")
}
Step 7: Count and print the tokens used in the chat thread
Use the tokenCount()
function to count the tokens used in the chat thread and print the result:
let tokenCount = chatThread.tokenCount()
print("Token count: \(tokenCount)")
Now your complete main.swift
file should look like this:
import Foundation
import CleverBird
func main() async {
let apiKey = "your_api_key_here"
let connection = OpenAIAPIConnection(apiKey: apiKey)
let chatThread = OpenAIChatThread(connection: connection)
.addSystemMessage("You are a helpful assistant.")
.addUserMessage("Who won the world series in 2020?")
if let response = await chatThread.complete() {
print("Assistant: \(response.content)")
} else {
print("Error: Unable to get a response from the AI.")
}
let tokenCount = chatThread.tokenCount()
print("Token count: \(tokenCount)")
}
await main()
Step 8: Run the command-line application
Now that your command-line application is complete, it's time to run it.
- In Xcode, select the "CleverBirdExample" scheme from the toolbar at the top.
- Click the "Run" button (the play button icon) or press
Cmd + R
to run the application. - The application will run, and the output will be displayed in the "Debug area" at the bottom of the Xcode window.
You should see the assistant's response to the user message and the token count used in the chat thread:
Assistant: The Los Angeles Dodgers won the World Series in 2020.
Token count: 25
Congratulations! You have successfully built a command-line application using the CleverBird library to interact with OpenAI's GPT-4 model. You can now modify the user messages or add more system and user messages to create more complex interactions with the AI.
Top comments (0)