Skip to content

Commit

Permalink
Merge pull request #1 from DeveloperZelentsov/feature/integrateMainFe…
Browse files Browse the repository at this point in the history
…aturies

Integrate main featuries
  • Loading branch information
DeveloperZelentsov authored Nov 17, 2023
2 parents b363160 + 7f62816 commit e7845f4
Show file tree
Hide file tree
Showing 50 changed files with 2,384 additions and 0 deletions.
8 changes: 8 additions & 0 deletions AISwiftAssist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
27 changes: 27 additions & 0 deletions AISwiftAssist/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "AISwiftAssist",
platforms: [
.iOS(.v13),
.watchOS(.v8)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "AISwiftAssist",
targets: ["AISwiftAssist"]),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "AISwiftAssist"),
.testTarget(
name: "AISwiftAssistTests",
dependencies: ["AISwiftAssist"]),
]
)
87 changes: 87 additions & 0 deletions AISwiftAssist/Sources/AISwiftAssist/APIs/AssistantsAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// File.swift
//
//
// Created by Alexey on 11/15/23.
//

import Foundation

/// Build assistants that can call models and use tools to perform tasks. [Link for Assistants](https://platform.openai.com/docs/api-reference/assistants)
public protocol IAssistantsAPI: AnyObject {

/// Returns a list of assistants.
/// - Parameter parameters: Parameters for the list of assistants.
/// - Returns: A list of assistant objects.
func get(with parameters: ASAListAssistantsParameters?) async throws -> [ASAAssistant]

/// Create an assistant with a model and instructions.
/// - Parameter createAssistant: The create assistant model.
/// - Returns: An assistant object.
func create(by createAssistant: ASACreateAssistantRequest) async throws -> ASAAssistant

/// Retrieves an assistant.
/// - Parameter assistantId: The ID of the assistant to retrieve.
/// - Returns: The assistant object matching the specified ID.
func retrieve(by assistantId: String) async throws -> ASAAssistant

/// Modifies an assistant.
/// - Parameters:
/// - assistantId: The ID of the assistant to modify.
/// - modifyAssistant: Object containing the properties to update.
/// - Returns: The modified assistant object.
func modify(by assistantId: String, modifyAssistant: ASAModifyAssistantRequest) async throws -> ASAAssistant

/// Delete an assistant.
/// - Parameter assistantId: The ID of the assistant to delete.
/// - Returns: Deletion status
func delete(by assistantId: String) async throws -> ASADeleteModelResponse
}

public final class AssistantsAPI: HTTPClient, IAssistantsAPI {

let urlSession: URLSession

public init(apiKey: String,
baseScheme: String = Constants.baseScheme,
baseHost: String = Constants.baseHost,
path: String = Constants.path,
urlSession: URLSession = .shared) {
Constants.apiKey = apiKey
Constants.baseScheme = baseScheme
Constants.baseHost = baseHost
Constants.path = path
self.urlSession = urlSession
}

public init(urlSession: URLSession = .shared) {
self.urlSession = urlSession
}

public func get(with parameters: ASAListAssistantsParameters? = nil) async throws -> [ASAAssistant] {
let endpoint = AssistantEndpoint.getAssistants(parameters)
return try await sendRequest(endpoint: endpoint, responseModel: [ASAAssistant].self)
}

public func create(by createAssistant: ASACreateAssistantRequest) async throws -> ASAAssistant {
let endpoint = AssistantEndpoint.createAssistant(createAssistant)
return try await sendRequest(endpoint: endpoint, responseModel: ASAAssistant.self)
}

public func retrieve(by assistantId: String) async throws -> ASAAssistant {
let endpoint = AssistantEndpoint.retrieveAssistant(assistantId)
return try await sendRequest(endpoint: endpoint, responseModel: ASAAssistant.self)
}

public func modify(by assistantId: String, modifyAssistant: ASAModifyAssistantRequest) async throws -> ASAAssistant {
let endpoint = AssistantEndpoint.modifyAssistant(assistantId, modifyAssistant)
return try await sendRequest(endpoint: endpoint, responseModel: ASAAssistant.self)
}

public func delete(by assistantId: String) async throws -> ASADeleteModelResponse {
let endpoint = AssistantEndpoint.deleteAssistant(assistantId)
return try await sendRequest(endpoint: endpoint, responseModel: ASADeleteModelResponse.self)
}

}

82 changes: 82 additions & 0 deletions AISwiftAssist/Sources/AISwiftAssist/APIs/MessagesAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// File.swift
//
//
// Created by Alexey on 11/15/23.
//

import Foundation

/// Create messages within threads [Link for Messages](https://platform.openai.com/docs/api-reference/messages)
public protocol IMessagesAPI: AnyObject {

/// Create a message.
/// - Parameters:
/// - threadId: The ID of the thread to create a message for.
/// - createMessage: Object with parameters for creating a message.
/// - Returns: A message object.
func create(by threadId: String, createMessage: ASACreateMessageRequest) async throws -> ASAMessage

/// Retrieve a message.
/// - Parameters:
/// - threadId: The ID of the thread to which this message belongs.
/// - messageId: The ID of the message to retrieve.
/// - Returns: The message object matching the specified ID.
func retrieve(by threadId: String, messageId: String) async throws -> ASAMessage

/// Modifies a message.
/// - Parameters:
/// - threadId: The ID of the thread to which this message belongs.
/// - messageId: The ID of the message to modify.
/// - modifyMessage: Object with parameters for modifying a message.
/// - Returns: The modified message object.
func modify(by threadId: String, messageId: String, modifyMessage: ASAModifyMessageRequest) async throws -> ASAMessage

/// Returns a list of messages for a given thread.
/// - Parameters:
/// - threadId: The ID of the thread the messages belong to.
/// - parameters: Parameters for the list of messages.
/// - Returns: A list of message objects.
func getMessages(by threadId: String, parameters: ASAListMessagesParameters?) async throws -> ASAMessagesListResponse
}

public final class MessagesAPI: HTTPClient, IMessagesAPI {

let urlSession: URLSession

public init(apiKey: String,
baseScheme: String = Constants.baseScheme,
baseHost: String = Constants.baseHost,
path: String = Constants.path,
urlSession: URLSession = .shared) {
Constants.apiKey = apiKey
Constants.baseScheme = baseScheme
Constants.baseHost = baseHost
Constants.path = path
self.urlSession = urlSession
}

public init(urlSession: URLSession = .shared) {
self.urlSession = urlSession
}

public func create(by threadId: String, createMessage: ASACreateMessageRequest) async throws -> ASAMessage {
let endpoint = MessagesEndpoint.createMessage(threadId, createMessage)
return try await sendRequest(endpoint: endpoint, responseModel: ASAMessage.self)
}

public func retrieve(by threadId: String, messageId: String) async throws -> ASAMessage {
let endpoint = MessagesEndpoint.retrieveMessage(threadId, messageId)
return try await sendRequest(endpoint: endpoint, responseModel: ASAMessage.self)
}

public func modify(by threadId: String, messageId: String, modifyMessage: ASAModifyMessageRequest) async throws -> ASAMessage {
let endpoint = MessagesEndpoint.modifyMessage(threadId, messageId, modifyMessage)
return try await sendRequest(endpoint: endpoint, responseModel: ASAMessage.self)
}

public func getMessages(by threadId: String, parameters: ASAListMessagesParameters?) async throws -> ASAMessagesListResponse {
let endpoint = MessagesEndpoint.listMessages(threadId, parameters)
return try await sendRequest(endpoint: endpoint, responseModel: ASAMessagesListResponse.self)
}
}
62 changes: 62 additions & 0 deletions AISwiftAssist/Sources/AISwiftAssist/APIs/ModelsAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// File.swift
//
//
// Created by Alexey on 11/15/23.
//

import Foundation

/// Describes an OpenAI model offering that can be used with the API. [Link for Models](https://platform.openai.com/docs/api-reference/models)
public protocol IModelsAPI: AnyObject {

/// Lists the currently available models, and provides basic information about each one such as the owner and availability.
/// - Returns: A list of model objects.
func get() async throws -> ASAListModelsResponse

/// Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
/// - Parameter modelId: The ID of the model to use for this request
/// - Returns: The model object matching the specified ID.
func retrieve(by modelId: String) async throws -> ASAModel

/// Delete a fine-tuned model. You must have the Owner role in your organization to delete a model.
/// - Parameter modelId: The model to delete
/// - Returns: Deletion status.
func delete(by modelId: String) async throws -> ASADeleteModelResponse
}

public final class ModelsAPI: HTTPClient, IModelsAPI {

let urlSession: URLSession

public init(apiKey: String,
baseScheme: String = Constants.baseScheme,
baseHost: String = Constants.baseHost,
path: String = Constants.path,
urlSession: URLSession = .shared) {
Constants.apiKey = apiKey
Constants.baseScheme = baseScheme
Constants.baseHost = baseHost
Constants.path = path
self.urlSession = urlSession
}

public init(urlSession: URLSession = .shared) {
self.urlSession = urlSession
}

public func get() async throws -> ASAListModelsResponse {
let endpoint = ModelsEndpoint.getModels
return try await sendRequest(endpoint: endpoint, responseModel: ASAListModelsResponse.self)
}

public func retrieve(by modelId: String) async throws -> ASAModel {
let endpoint = ModelsEndpoint.retrieveModel(modelId)
return try await sendRequest(endpoint: endpoint, responseModel: ASAModel.self)
}

public func delete(by modelId: String) async throws -> ASADeleteModelResponse {
let endpoint = ModelsEndpoint.deleteModel(modelId)
return try await sendRequest(endpoint: endpoint, responseModel: ASADeleteModelResponse.self)
}
}
46 changes: 46 additions & 0 deletions AISwiftAssist/Sources/AISwiftAssist/APIs/RunsAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// File.swift
//
//
// Created by Alexey on 11/15/23.
//

import Foundation

/// Represents an execution run on a thread. [Link for Runs](https://platform.openai.com/docs/api-reference/runs)
public protocol IRunsAPI: AnyObject {

/// Create a run.
/// - Parameters:
/// - threadId: The ID of the thread to run.
/// - createRun: Object with parameters for creating a run.
/// - Returns: A run object.
func create(by threadId: String, createRun: ASACreateRunRequest) async throws -> ASARun
}

public final class RunsAPI: HTTPClient, IRunsAPI {

let urlSession: URLSession

public init(apiKey: String,
baseScheme: String = Constants.baseScheme,
baseHost: String = Constants.baseHost,
path: String = Constants.path,
urlSession: URLSession = .shared) {
Constants.apiKey = apiKey
Constants.baseScheme = baseScheme
Constants.baseHost = baseHost
Constants.path = path
self.urlSession = urlSession
}

public init(urlSession: URLSession = .shared) {
self.urlSession = urlSession
}

public func create(by threadId: String, createRun: ASACreateRunRequest) async throws -> ASARun {
let endpoint = RunsEndpoint.createRun(threadId, createRun)
return try await sendRequest(endpoint: endpoint, responseModel: ASARun.self)
}

}
Loading

0 comments on commit e7845f4

Please sign in to comment.