Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update messages #12

Merged
merged 2 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Sources/AISwiftAssist/Client/AISwiftAssistClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ extension AISwiftAssistClient {
/// Creates an assistant and thread based on the provided parameters.
public 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 {
guard let model = modelsResponse.data.first(where: { $0.id == params.model.rawValue }) else {
throw NSError(domain: "AISwiftAssistClient", code: 0, userInfo: [NSLocalizedDescriptionKey: "Model not found"])
}

let createAssistantRequest = ASACreateAssistantRequest(asaModel: model,
let createAssistantRequest = ASACreateAssistantRequest(asaModel: model,
name: params.name,
description: params.description,
description: params.description,
instructions: params.instructions,
tools: params.tools,
fileIds: params.fileIds,
Expand All @@ -56,4 +56,3 @@ extension AISwiftAssistClient {
return AssistantAndThreadConfig(assistant: assistant, thread: thread)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import Foundation

public struct AssistantCreationParams {

public let modelName: String
public let model: ASAOpenAIModel
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
public init(model: ASAOpenAIModel, name: String, description: String, instructions: String, tools: [ASACreateAssistantRequest.Tool]? = nil, fileIds: [String]? = nil, metadata: [String : String]? = nil) {
self.model = model
self.name = name
self.description = description
self.instructions = instructions
Expand Down
146 changes: 0 additions & 146 deletions Sources/AISwiftAssist/Models/Main/ASAMessage.swift

This file was deleted.

18 changes: 18 additions & 0 deletions Sources/AISwiftAssist/Models/Main/Message/ASAImageContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// File.swift
//
//
// Created by Alexey on 12/13/23.
//

import Foundation

/// Represents an image file in the content of a message.
public struct ASAImageContent: Codable {
/// The File ID of the image in the message content.
public let file_id: String

enum CodingKeys: String, CodingKey {
case file_id
}
}
54 changes: 54 additions & 0 deletions Sources/AISwiftAssist/Models/Main/Message/ASAMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// File.swift
//
//
// Created by Alexey on 11/15/23.
//

import Foundation

/// Represents a message within a thread.
public struct ASAMessage: Codable {
/// The identifier of the message, which can be referenced in API endpoints.
public let id: String

/// The object type, which is always 'thread.message'.
public let object: String

/// The Unix timestamp (in seconds) for when the message was created.
public let createdAt: Int

/// The thread ID that this message belongs to.
public let threadId: String

/// The entity that produced the message. One of 'user' or 'assistant'.
public let role: String

/// The content of the message in array of text and/or images.
public let content: [ASAMessageContent]

/// If applicable, the ID of the assistant that authored this message.
public let assistantId: String?

/// If applicable, the ID of the run associated with the authoring of this message.
public let runId: String?

/// A list of file IDs that the assistant should use. Useful for tools like retrieval and code_interpreter that can access files.
/// A maximum of 10 files can be attached to a message.
public let fileIds: [String]

/// Optional: Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information
/// about the object in a structured format. Keys can be a maximum of 64 characters long, and values can be a maximum of 512 characters long.
public let metadata: [String: String]?

enum CodingKeys: String, CodingKey {
case id, object
case createdAt = "created_at"
case threadId = "thread_id"
case role, content
case assistantId = "assistant_id"
case runId = "run_id"
case fileIds = "file_ids"
case metadata
}
}
48 changes: 48 additions & 0 deletions Sources/AISwiftAssist/Models/Main/Message/ASAMessageContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// File.swift
//
//
// Created by Alexey on 12/13/23.
//

import Foundation

public enum ASAMessageContent: Codable {
case image(ASAImageContent)
case text(ASATextContent)

enum CodingKeys: String, CodingKey {
case type, imageFile = "image_file", text
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let type = try container.decode(String.self, forKey: .type)

switch type {
case "image_file":
let imageContent = try container.decode(ASAImageContent.self, forKey: .imageFile)
self = .image(imageContent)
case "text":
let textContent = try container.decode(ASATextContent.self, forKey: .text)
self = .text(textContent)
default:
throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Invalid type")
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

switch self {
case .image(let imageContent):
try container.encode("image_file", forKey: .type)
try container.encode(imageContent, forKey: .imageFile)
case .text(let textContent):
try container.encode("text", forKey: .type)
try container.encode(textContent, forKey: .text)
}
}
}


47 changes: 47 additions & 0 deletions Sources/AISwiftAssist/Models/Main/Message/ASATextContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// File.swift
//
//
// Created by Alexey on 12/13/23.
//

import Foundation

/// Represents the text content of a message.
public struct ASATextContent: Codable {
/// The value of the text.
public let value: String

/// Optional: Annotations
public let annotations: [ASAAnnotation]?
}

public struct ASAAnnotation: Codable {
let type: String
let text: String
let startIndex: Int
let endIndex: Int
let fileCitation: ASAFileCitation?
let filePath: ASAFilePath?

enum CodingKeys: String, CodingKey {
case type, text, startIndex = "start_index", endIndex = "end_index", fileCitation = "file_citation", filePath = "file_path"
}
}

public struct ASAFileCitation: Codable {
public let fileId: String
public let quote: String

enum CodingKeys: String, CodingKey {
case fileId = "file_id", quote
}
}

public struct ASAFilePath: Codable {
public let fileId: String

enum CodingKeys: String, CodingKey {
case fileId = "file_id"
}
}
Loading