Skip to content

Commit

Permalink
chunk strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrochabrun committed Jun 21, 2024
1 parent 3dddc14 commit 1d535a2
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 12 deletions.
45 changes: 35 additions & 10 deletions Sources/OpenAI/Public/ResponseModels/Runs/RunStepDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public enum RunStepToolCall: Codable {

public struct CodeInterpreterToolCall: Codable {
public var input: String?
public let outputs: [CodeInterpreterOutput]?
public var outputs: [CodeInterpreterOutput]?

enum CodingKeys: String, CodingKey {
case input, outputs
Expand All @@ -150,7 +150,7 @@ public struct CodeInterpreterToolCall: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
input = try container.decodeIfPresent(String.self, forKey: .input)
// This is neede as the input is retrieved as ""input": "# Calculate the square root of 500900\nmath.sqrt(500900)"
// This is needed as the input is retrieved as ""input": "# Calculate the square root of 500900\nmath.sqrt(500900)"
input = input?.replacingOccurrences(of: "\\n", with: "\n")
outputs = try container.decodeIfPresent([CodeInterpreterOutput].self, forKey: .outputs)
}
Expand All @@ -163,6 +163,11 @@ public struct CodeInterpreterToolCall: Codable {
try container.encode(encodedInput, forKey: .input)
try container.encode(outputs, forKey: .outputs)
}

public init(input: String?, outputs: [CodeInterpreterOutput]?) {
self.input = input
self.outputs = outputs
}
}

public enum CodeInterpreterOutput: Codable {
Expand Down Expand Up @@ -210,23 +215,37 @@ public enum CodeInterpreterOutput: Codable {
public struct CodeInterpreterLogOutput: Codable {

/// Always logs.
public let type: String
public var type: String
/// The text output from the Code Interpreter tool call.
public let logs: String
public var logs: String

public init(type: String, logs: String) {
self.type = type
self.logs = logs
}
}

public struct CodeInterpreterImageOutput: Codable {

public let type: String
public let image: Image
public var type: String
public var image: Image

public struct Image: Codable {
/// The [file](https://platform.openai.com/docs/api-reference/files) ID of the image.
public let fileID: String
public var fileID: String

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

public init(fileID: String) {
self.fileID = fileID
}
}

public init(type: String, image: Image) {
self.type = type
self.image = image
}
}

Expand All @@ -247,9 +266,15 @@ public struct FileSearchToolCall: Codable {
public struct FunctionToolCall: Codable {

/// The name of the function.
public let name: String?
public var name: String?
/// The arguments passed to the function.
public let arguments: String
public var arguments: String
/// The output of the function. This will be null if the outputs have not been [submitted](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) yet.
public let output: String?
public var output: String?

public init(name: String? = nil, arguments: String, output: String? = nil) {
self.name = name
self.arguments = arguments
self.output = output
}
}
90 changes: 88 additions & 2 deletions Sources/OpenAI/Public/Shared/ToolResources.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,108 @@ import Foundation
/// properties
/// vector_store_id (array)
/// The ID of the vector store attached to this assistant. There can be a maximum of 1 vector store attached to the assistant.

// MARK: ToolResources

public struct ToolResources: Codable {

public let fileSearch: FileSearch?
public let codeInterpreter: CodeInterpreter?

// MARK: FileSearch

public struct FileSearch: Codable {

public let vectorStoreIds: [String]
/// The [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant.
public let vectorStoreIds: [String]?

/// A helper to create a [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) with file_ids and attach it to this assistant. There can be a maximum of 1 vector store attached to the assistant.
public let vectorStores: [VectorStore]?

public struct VectorStore: Codable {

/// A list of [file](https://platform.openai.com/docs/api-reference/files) IDs to add to the vector store. There can be a maximum of 10000 files in a vector store.
public let fileIDS: [String]?
/// The chunking strategy used to chunk the file(s). If not set, will use the auto strategy.
public let chunkingStrategy: ChunkingStrategy?
/// Set of 16 key-value pairs that can be attached to a vector store. This can be useful for storing additional information about the vector store in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.
public let metadata: [String: String]?

public enum ChunkingStrategy: Codable {
case auto

/// `maxChunkSizeTokens`: The maximum number of tokens in each chunk. The default value is 800. The minimum value is 100 and the maximum value is 4096.
/// `chunk_overlap_tokens`: The number of tokens that overlap between chunks. The default value is 400.
/// Note that the overlap must not exceed half of max_chunk_size_tokens.
case `static`(maxChunkSizeTokens: Int, chunkOverlapTokens: Int)

enum CodingKeys: String, CodingKey {
case type
case maxChunkSizeTokens = "max_chunk_size_tokens"
case chunkOverlapTokens = "chunk_overlap_tokens"
}

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 "auto":
self = .auto
case "static":
let maxChunkSizeTokens = try container.decode(Int.self, forKey: .maxChunkSizeTokens)
let chunkOverlapTokens = try container.decode(Int.self, forKey: .chunkOverlapTokens)
self = .static(maxChunkSizeTokens: maxChunkSizeTokens, chunkOverlapTokens: chunkOverlapTokens)
default:
throw DecodingError.dataCorruptedError(forKey: CodingKeys.type, in: container, debugDescription: "Invalid type value")
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .auto:
try container.encode("auto", forKey: .type)
case .static(let maxChunkSizeTokens, let chunkOverlapTokens):
try container.encode("static", forKey: .type)
try container.encode(maxChunkSizeTokens, forKey: .maxChunkSizeTokens)
try container.encode(chunkOverlapTokens, forKey: .chunkOverlapTokens)
}
}
}

enum CodingKeys: String, CodingKey {
case fileIDS = "file_ids"
case chunkingStrategy = "chunking_strategy"
case metadata
}

public init(
fileIDS: [String]?,
chunkingStrategy: ChunkingStrategy?,
metadata: [String : String]?)
{
self.fileIDS = fileIDS
self.chunkingStrategy = chunkingStrategy
self.metadata = metadata
}
}

enum CodingKeys: String, CodingKey {
case vectorStoreIds = "vector_store_ids"
case vectorStores = "vector_stores"
}

public init(vectorStoreIds: [String]) {
public init(
vectorStoreIds: [String]?,
vectorStores: [VectorStore]?)
{
self.vectorStoreIds = vectorStoreIds
self.vectorStores = vectorStores
}
}

// MARK: CodeInterpreter

public struct CodeInterpreter: Codable {

public let fileIds: [String]
Expand All @@ -57,6 +141,8 @@ public struct ToolResources: Codable {
case codeInterpreter = "code_interpreter"
}

// MARK: ToolResources+Initializer

public init(
fileSearch: FileSearch? = nil,
codeInterpreter: CodeInterpreter? = nil)
Expand Down

0 comments on commit 1d535a2

Please sign in to comment.