DEV Community

B.T. Franklin
B.T. Franklin

Posted on

Tutorial: Building a Command-Line Application with CleverBird and OpenAI GPT-4

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

  1. Open Xcode and create a new project.
  2. Choose the "macOS" tab and select "Command Line Tool."
  3. Set the Product Name to "CleverBirdExample," and make sure the Language is set to "Swift."
  4. 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:

  1. In Xcode, go to "File" > "Add Packages."
  2. Enter the URL https://github.com/btfranklin/CleverBird.git and click "Add Package."
  3. 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()
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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?")
Enter fullscreen mode Exit fullscreen mode

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.")
}
Enter fullscreen mode Exit fullscreen mode

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)")
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Step 8: Run the command-line application

Now that your command-line application is complete, it's time to run it.

  1. In Xcode, select the "CleverBirdExample" scheme from the toolbar at the top.
  2. Click the "Run" button (the play button icon) or press Cmd + R to run the application.
  3. 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
Enter fullscreen mode Exit fullscreen mode

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)