Skip to content

Pocket Assistant is a SwiftUI-based iOS app that seamlessly integrates OpenAI's Assistant API for interactive AI conversations. Experience a real-time chat interface where users can engage with an AI assistant, leveraging the latest in AI technology for a unique messaging experience.

WhatsMusic/Pocket-Assistant

Repository files navigation

Openai Assistant API with Swift UI on iOS

Overview

Pocket Assistant is a SwiftUI-based iOS application designed to interact with an AI assistant through text messages. The app allows users to send messages to the AI assistant and receive responses, creating a conversational experience. It leverages the SwiftOpenAI package to communicate with the OpenAI API, which powers the AI assistant's capabilities.

Features

  • Messaging Interface: Users can send and receive messages from the AI assistant in a chat-like interface.
  • AI Integration: The app integrates with OpenAI's API to process user messages and generate AI responses.
  • Thread Management: The app manages conversation threads, ensuring continuity in the dialogue with the AI assistant.
  • Asynchronous Operations: The app performs network requests and processes AI responses asynchronously for a smooth user experience.

Code Structure

The codebase is organized into several directories and files, each serving a specific purpose in the application:

Models

  • AssistantModels.swift: Defines the data structures used to represent messages, threads, and responses from the AI assistant. It includes structures such as Message, MessageParameter, MessageCreationResponse, and RunResponse.

ViewModels

  • AssistantViewModel.swift: Contains the AssistantViewModel class, which serves as the intermediary between the views and the model. It handles the business logic, including sending messages, creating threads, and fetching responses from the AI assistant.

Views

  • MessageView.swift: Defines the MessageView view, which is responsible for displaying individual messages in the chat interface. It adjusts the layout based on whether the message is from the user or the assistant.
  • AssistantView.swift: The main view of the app, which includes the messaging interface and the input area for sending new messages. It uses AssistantViewModel to handle user interactions.
  • ContentView.swift: A placeholder view that is part of the default SwiftUI template.

Assets

  • Assets.xcassets: Contains the asset catalog for the app, including app icons, color sets, and image assets.
  • AccentColor.colorset: Defines the accent color used throughout the app.
  • AppIcon.appiconset: Contains the app icon in various sizes for different devices.
  • Preview Content/Preview Assets.xcassets: Contains assets used for previewing the app in Xcode.

App Entry Point

  • Pocket_AssistantApp.swift: The main entry point of the app, which sets up the SwiftUI app lifecycle and initializes the AssistantViewModel.

Usage

Upon launching the app, users are presented with the AssistantView, where they can type messages to the AI assistant. The app maintains a thread ID to keep track of the conversation. Users can view their message history and the AI's responses in a scrollable list.

The app's asynchronous nature allows for a responsive interface, even while waiting for the AI to process and respond to messages. The AssistantViewModel takes care of updating the message list and handling the state of the conversation.

Preview

The app includes preview providers for SwiftUI previews during development, allowing developers to see UI components in Xcode's canvas.

Dependencies

The app relies on the SwiftOpenAI package to interact with the OpenAI API. This dependency is used within the AssistantViewModel to create and manage runs, as well as to fetch messages from the AI assistant.

Conclusion

Pocket Assistant is a simple yet powerful example of integrating AI into a mobile app to create an interactive messaging experience. The app's structure and code organization facilitate easy maintenance and potential future enhancements.

About

Pocket Assistant is a SwiftUI-based iOS app that seamlessly integrates OpenAI's Assistant API for interactive AI conversations. Experience a real-time chat interface where users can engage with an AI assistant, leveraging the latest in AI technology for a unique messaging experience.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages