Skip to content

Commit

Permalink
update models
Browse files Browse the repository at this point in the history
fixed bugs
  • Loading branch information
DeveloperZelentsov committed Nov 17, 2023
1 parent 801cc8a commit 900b8b3
Show file tree
Hide file tree
Showing 19 changed files with 168 additions and 23 deletions.
8 changes: 4 additions & 4 deletions AISwiftAssist/Sources/AISwiftAssist/APIs/ModelsAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ 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 -> [ASAModel]
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.
Expand Down Expand Up @@ -45,9 +45,9 @@ public final class ModelsAPI: HTTPClient, IModelsAPI {
self.urlSession = urlSession
}

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

public func retrieve(by modelId: String) async throws -> ASAModel {
Expand Down
4 changes: 2 additions & 2 deletions AISwiftAssist/Sources/AISwiftAssist/APIs/ThreadsAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol IThreadsAPI: AnyObject {
/// Create a thread.
/// - Parameter createThreads: Object with parameters for creating a thread.
/// - Returns: A thread object.
func create(by createThreads: ASACreateThreadRequest) async throws -> ASAThread
func create(by createThreads: ASACreateThreadRequest?) async throws -> ASAThread

/// Retrieves a thread by its ID.
/// - Parameter threadId: The ID of the thread to retrieve.
Expand Down Expand Up @@ -53,7 +53,7 @@ public final class ThreadsAPI: HTTPClient, IThreadsAPI {
self.urlSession = urlSession
}

public func create(by createThreads: ASACreateThreadRequest) async throws -> ASAThread {
public func create(by createThreads: ASACreateThreadRequest?) async throws -> ASAThread {
let endpoint = ThreadsEndpoint.createThread(createThreads)
return try await sendRequest(endpoint: endpoint, responseModel: ASAThread.self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,30 @@ public final class AISwiftAssistClient {
self.runsApi = RunsAPI(urlSession: .shared)
self.threadsApi = ThreadsAPI(urlSession: .shared)
}

}

extension AISwiftAssistClient {
/// Creates an assistant and thread based on the provided parameters.
func createAssistantAndThread(with params: AssistantCreationParams) async throws -> AssistantAndThreadConfig {
let modelsResponse = try await modelsApi.get()
guard let model = modelsResponse.data.first(where: { $0.id == params.modelName }) else {
throw NSError(domain: "AISwiftAssistClient", code: 0, userInfo: [NSLocalizedDescriptionKey: "Model not found"])
}

let createAssistantRequest = ASACreateAssistantRequest(asaModel: model,
name: params.name,
description: params.description,
instructions: params.instructions,
tools: params.tools,
fileIds: params.fileIds,
metadata: params.metadata)
let assistant = try await assistantsApi.create(by: createAssistantRequest)

let threadRequest = ASACreateThreadRequest(messages: nil)
let thread = try await threadsApi.create(by: threadRequest)

return AssistantAndThreadConfig(assistant: assistant, thread: thread)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File.swift
//
//
// Created by Alexey on 11/17/23.
//

import Foundation

public struct AssistantAndThreadConfig {
public let assistant: ASAAssistant
public let thread: ASAThread
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// File.swift
//
//
// Created by Alexey on 11/17/23.
//

import Foundation

public struct AssistantCreationParams {

public let modelName: String
public let name: String
public let description: String
public let instructions: String
public let tools: [ASACreateAssistantRequest.Tool]?
public let fileIds: [String]?
public let metadata: [String: String]?

public init(modelName: String, name: String, description: String, instructions: String, tools: [ASACreateAssistantRequest.Tool]? = nil, fileIds: [String]? = nil, metadata: [String : String]? = nil) {
self.modelName = modelName
self.name = name
self.description = description
self.instructions = instructions
self.tools = tools
self.fileIds = fileIds
self.metadata = metadata
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

enum ThreadsEndpoint {
case createThread(ASACreateThreadRequest)
case createThread(ASACreateThreadRequest?)
case retrieveThread(String)
case modifyThread(String, ASAModifyThreadRequest)
case deleteThread(String)
Expand Down Expand Up @@ -61,9 +61,6 @@ extension ThreadsEndpoint: CustomEndpoint {
case .retrieveThread: return nil
case .modifyThread(_, let request): return .init(object: request)
case .deleteThread: return nil



}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension URLComponents {
static var `default`: Self {
var components: Self = .init()
components.scheme = Constants.baseScheme
components.host = Constants.baseHost + Constants.path
components.host = Constants.baseHost
return components
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct ASACreateAssistantRequest: Codable {
case metadata
}

public init(model: String, name: String?, description: String?, instructions: String?, tools: [Tool]?, fileIds: [String]?, metadata: [String : String]?) {
public init(model: String, name: String? = nil, description: String? = nil, instructions: String? = nil, tools: [Tool]? = nil, fileIds: [String]? = nil, metadata: [String : String]? = nil) {
self.model = model
self.name = name
self.description = description
Expand All @@ -48,7 +48,7 @@ public struct ASACreateAssistantRequest: Codable {
self.metadata = metadata
}

public init(asaModel: ASAModel, name: String?, description: String?, instructions: String?, tools: [Tool]?, fileIds: [String]?, metadata: [String : String]?) {
public init(asaModel: ASAModel, name: String? = nil, description: String? = nil, instructions: String? = nil, tools: [Tool]? = nil, fileIds: [String]? = nil, metadata: [String : String]? = nil) {
self.model = asaModel.id
self.name = name
self.description = description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation

/// A request structure for creating a message in a thread.
public struct ASACreateMessageRequest: Codable {

/// The role of the entity that is creating the message. Currently only 'user' is supported.
public let role: String

Expand All @@ -26,4 +27,11 @@ public struct ASACreateMessageRequest: Codable {
case fileIds = "file_ids"
case metadata
}

public init(role: String, content: String, fileIds: [String]? = nil, metadata: [String : String]? = nil) {
self.role = role
self.content = content
self.fileIds = fileIds
self.metadata = metadata
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation

/// A request structure for creating a run in a thread.
public struct ASACreateRunRequest: Codable {

/// The ID of the assistant to use to execute this run.
public let assistantId: String

Expand Down Expand Up @@ -57,4 +58,12 @@ public struct ASACreateRunRequest: Codable {
case assistantId = "assistant_id"
case model, instructions, tools, metadata
}

public init(assistantId: String, model: String? = nil, instructions: String? = nil, tools: [ASACreateRunRequest.Tool]? = nil, metadata: [String : String]? = nil) {
self.assistantId = assistantId
self.model = model
self.instructions = instructions
self.tools = tools
self.metadata = metadata
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public struct ASACreateThreadRequest: Codable {
case metadata
}
}

public init(messages: [Message]? = nil) {
self.messages = messages
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation

/// Parameters for listing assistants.
public struct ASAListAssistantsParameters: Encodable {

/// Optional: A limit on the number of objects to be returned. Can range between 1 and 100. Defaults to 20.
public let limit: Int?

Expand All @@ -20,4 +21,11 @@ public struct ASAListAssistantsParameters: Encodable {

/// Optional: A cursor for use in pagination. 'before' is an object ID that defines your place in the list, to fetch the previous page of the list.
public let before: String?

public init(limit: Int? = nil, order: String? = nil, after: String? = nil, before: String? = nil) {
self.limit = limit
self.order = order
self.after = after
self.before = before
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ import Foundation

/// Parameters for listing messages in a thread.
public struct ASAListMessagesParameters: Codable {

/// Optional: A limit on the number of objects to be returned. Limit can range between 1 and 100.
let limit: Int?
public let limit: Int?

/// Optional: Sort order by the created_at timestamp of the objects. 'asc' for ascending order and 'desc' for descending order.
let order: String?
public let order: String?

/// Optional: A cursor for use in pagination. 'after' is an object ID that defines your place in the list.
let after: String?
public let after: String?

/// Optional: A cursor for use in pagination. 'before' is an object ID that defines your place in the list.
let before: String?

public let before: String?

public init(limit: Int? = nil, order: String? = nil, after: String? = nil, before: String? = nil) {
self.limit = limit
self.order = order
self.after = after
self.before = before
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ import Foundation

/// Parameters for listing runs in a thread.
public struct ASAListRunsParameters: Codable {

/// Optional: A limit on the number of objects to be returned.
let limit: Int?
public let limit: Int?

/// Optional: Sort order by the created_at timestamp of the objects.
let order: String?
public let order: String?

/// Optional: A cursor for use in pagination.
let after: String?
public let after: String?

/// Optional: A cursor for use in pagination.
let before: String?

public let before: String?

public init(limit: Int? = nil, order: String? = nil, after: String? = nil, before: String? = nil) {
self.limit = limit
self.order = order
self.after = after
self.before = before
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
/// A request structure for modifying an existing assistant.
public struct ASAModifyAssistantRequest: Codable {

/// Optional: ID of the model to use.
public let model: String?

Expand All @@ -34,4 +35,14 @@ public struct ASAModifyAssistantRequest: Codable {
case fileIds = "file_ids"
case metadata
}

public init(model: String? = nil, name: String? = nil, description: String? = nil, instructions: String? = nil, tools: [ASAAssistant.Tool]? = nil, fileIds: [String]? = nil, metadata: [String : String]? = nil) {
self.model = model
self.name = name
self.description = description
self.instructions = instructions
self.tools = tools
self.fileIds = fileIds
self.metadata = metadata
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import Foundation

/// A request structure for modifying a message in a thread.
public struct ASAModifyMessageRequest: Codable {

/// Optional: Set of 16 key-value pairs that can be attached to the message.
public let metadata: [String: String]?

enum CodingKeys: String, CodingKey {
case metadata
}

public init(metadata: [String : String]? = nil) {
self.metadata = metadata
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// File.swift
//
//
// Created by Alexey on 11/17/23.
//

import Foundation

public struct ASAListModelsResponse: Codable {

/// The object type, which is always "list".
public let object: String

/// The deletion status of the model.
public let data: [ASAModel]

enum CodingKeys: String, CodingKey {
case object, data
}
}

0 comments on commit 900b8b3

Please sign in to comment.