A Swift Package Manager (SPM) library for iOS 13 and above, designed to simplify the integration with OpenAI's Assistants API in iOS applications. This library allows you to build AI-powered assistants efficiently and interactively.
- Seamless integration with OpenAI's Assistants API.
- Easily create and manage AI assistants with various models and tools.
- Handle conversations through threads and messages.
- Utilize Code Interpreter, Retrieval, and Function calling tools.
- Optimized for iOS 13+ with Swift.
To add AISwiftAssist
to your Xcode project, follow these steps:
- Open your project in Xcode.
- Click on "File" in the menu bar, then select "Swift Packages" and then "Add Package Dependency..."
- In the dialog that appears, enter the package repository URL:
https://github.com/DeveloperZelentsov/AiSwiftAssist.git
. - Choose the last version to integrate and click "Next".
- Confirm the package products and targets and click "Finish".
Import AISwiftAssist
into your Swift file where you wish to use it:
import AISwiftAssist
Create an instance of AISwiftAssistClient using your API key and organization ID:
let config = AISwiftAssistConfig(apiKey: "your-api-key", organizationId: "your-org-id")
let aiSwiftAssistClient = AISwiftAssistClient(config: config)
To create an AI assistant, define its parameters such as model, name, description, and instructions. You can also specify tools and file IDs if required:
let assistantParams = AssistantCreationParams(
modelName: "gpt-4-1106-preview",
name: "Math Tutor",
description: "Your personal math tutor.",
instructions: "Solve math problems and explain solutions."
)
let creationAssistantParams = try await aiSwiftAssistClient.createAssistantAndThread(with: assistantParams)
let assistantId = creationParams.assistant.id
let threadId = creationParams.thread.id
Sending Messages
Send a message to your assistant through a created thread:
let messageRequest: ASACreateMessageRequest = .init(role: "user", content: content)
try await aiSwiftAssistClient.messagesApi.create(by: threadId, createMessage: messageRequest)
Initiating a Run
To process the message and receive a response, initiate a run:
let runRequest = ASACreateRunRequest(assistantId: assistantId)
try await aiSwiftAssistClient.runsApi.create(by: threadId, createRun: runRequest)
Fetching Responses
Retrieve the assistant's response:
let messages = try await aiSwiftAssistClient.messagesApi.list(threadId: threadId)
// Process and display these messages, including the assistant's response.
Continue the conversation by sending additional messages, initiating runs, and fetching responses. This creates a dynamic and interactive communication flow with the assistant.
This is an example of the most basic usage of the Assistants API. There is potential for much more complex and interesting assistants, and I will continue to evolve this Swift API to enable you to leverage these advanced capabilities.
We welcome your feedback and contributions to enhance AISwiftAssist
. Please feel free to report issues or submit pull requests on our GitHub repository.
This library is distributed under the MIT License.