From 10bcd9dd4a27e3f3a6c6972cd37a2850bd0650fc Mon Sep 17 00:00:00 2001 From: Tolga Kayhan Date: Sat, 21 Sep 2024 18:43:18 +0100 Subject: [PATCH 1/2] Added support for Vector Stores Static and file search tool parameters. --- .../AssistantHelpers/VectorTestHelper.cs | 28 +++++++++ .../RequestModels/CreateVectorStoreRequest.cs | 39 ++++++++++++ .../RequestModels/ToolDefinition.cs | 59 +++++++++++++++++-- OpenAI.SDK/ObjectModels/StaticValueHelper.cs | 9 +++ 4 files changed, 131 insertions(+), 4 deletions(-) diff --git a/OpenAI.Playground/TestHelpers/AssistantHelpers/VectorTestHelper.cs b/OpenAI.Playground/TestHelpers/AssistantHelpers/VectorTestHelper.cs index 0448b171..540ada64 100644 --- a/OpenAI.Playground/TestHelpers/AssistantHelpers/VectorTestHelper.cs +++ b/OpenAI.Playground/TestHelpers/AssistantHelpers/VectorTestHelper.cs @@ -73,6 +73,34 @@ public static async Task CreateVector(IOpenAIService openAI) } } + public static async Task CreateVectorWithChunkingStrategy(IOpenAIService openAI) + { + ConsoleExtensions.WriteLine("Create Vector Testing is starting:", ConsoleColor.Cyan); + var result = await openAI.Beta.VectorStores.CreateVectorStore(new() + { + Name = "Support FAQ", + ChunkingStrategy = new() + { + Type = StaticValues.VectorStoreStatics.ChunkingStrategyType.Static, + StaticParameters = new() + { + ChunkOverlapTokens = 400, + MaxChunkSizeTokens = 800 + } + } + }); + + if (result.Successful) + { + CreatedVectorId = result.Id; + ConsoleExtensions.WriteLine($"Vector Created Successfully with ID: {result.Id}", ConsoleColor.Green); + } + else + { + ConsoleExtensions.WriteError(result.Error); + } + } + public static async Task ListVectors(IOpenAIService openAI) { ConsoleExtensions.WriteLine("List Vectors Testing is starting:", ConsoleColor.Cyan); diff --git a/OpenAI.SDK/ObjectModels/RequestModels/CreateVectorStoreRequest.cs b/OpenAI.SDK/ObjectModels/RequestModels/CreateVectorStoreRequest.cs index 4ac72e88..7f57b359 100644 --- a/OpenAI.SDK/ObjectModels/RequestModels/CreateVectorStoreRequest.cs +++ b/OpenAI.SDK/ObjectModels/RequestModels/CreateVectorStoreRequest.cs @@ -31,4 +31,43 @@ public class CreateVectorStoreRequest /// [JsonPropertyName("metadata")] public Dictionary? Metadata { get; set; } + + /// + /// The chunking strategy used to chunk the file(s). If not set, will use the auto strategy. + /// Only applicable if file_ids is non-empty. + /// + [JsonPropertyName("chunking_strategy")] + public ChunkingStrategy? ChunkingStrategy { get; set; } +} + +public class ChunkingStrategy +{ + /// + /// The type of chunking strategy. Must be either "auto" or "static". + /// + [JsonPropertyName("type")] + public string Type { get; set; } + + /// + /// The static chunking parameters. Required if type is "static". + /// + [JsonPropertyName("static")] + public StaticChunkingParameters? StaticParameters { get; set; } +} + +public class StaticChunkingParameters +{ + /// + /// The maximum number of tokens in each chunk. The default value is 800. + /// The minimum value is 100 and the maximum value is 4096. + /// + [JsonPropertyName("max_chunk_size_tokens")] + public int MaxChunkSizeTokens { get; set; } + + /// + /// 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. + /// + [JsonPropertyName("chunk_overlap_tokens")] + public int ChunkOverlapTokens { get; set; } } \ No newline at end of file diff --git a/OpenAI.SDK/ObjectModels/RequestModels/ToolDefinition.cs b/OpenAI.SDK/ObjectModels/RequestModels/ToolDefinition.cs index 6aca828d..7ca8526c 100644 --- a/OpenAI.SDK/ObjectModels/RequestModels/ToolDefinition.cs +++ b/OpenAI.SDK/ObjectModels/RequestModels/ToolDefinition.cs @@ -15,12 +15,18 @@ public class ToolDefinition public string Type { get; set; } /// - /// Structured Outputs for function calling can be enabled with a single parameter, just by supplying strict: true. - /// Please note: This field is not mentioned in the API documentation but is referenced in other documents. + /// Structured Outputs for function calling can be enabled with a single parameter, just by supplying strict: true. + /// Please note: This field is not mentioned in the API documentation but is referenced in other documents. /// [JsonPropertyName("strict")] public bool? Strict { get; set; } + /// + /// Overrides for the file search tool. + /// + [JsonPropertyName("file_search")] + public FileSearchTool? FileSearchTool { get; set; } + /// /// A list of functions the model may generate JSON inputs for. /// @@ -73,11 +79,56 @@ public static ToolDefinition DefineRetrieval() }; } - public static ToolDefinition DefineFileSearch() + public static ToolDefinition DefineFileSearch(FileSearchTool? fileSearchTool = null) { return new() { - Type = StaticValues.AssistantsStatics.ToolCallTypes.FileSearch + Type = StaticValues.AssistantsStatics.ToolCallTypes.FileSearch, + FileSearchTool = fileSearchTool }; } +} + +public class FileSearchTool +{ + /// + /// The maximum number of results the file search tool should output. The default is 20 for gpt-4* models and 5 for + /// gpt-3.5-turbo. This number should be between 1 and 50 inclusive. + /// Note that the file search tool may output fewer than max_num_results results. + /// + /// See the + /// file search tool documentation + /// + /// for more information. + /// + [JsonPropertyName("max_num_results")] + public int? MaxNumberResults { get; set; } + + /// + /// The ranking options for the file search. If not specified, the file search tool will use the auto ranker and a + /// score_threshold of 0. + /// See the + /// + /// file + /// search tool documentation + /// + /// for more information. + /// + [JsonPropertyName("ranking_options")] + public RankingOptions? RankingOptions { get; set; } +} + +public class RankingOptions +{ + /// + /// The ranker to use for the file search. If not specified will use the auto ranker. + /// + [JsonPropertyName("ranker")] + public string? Ranker { get; set; } + + /// + /// The score threshold for the file search. All values must be a floating point number between 0 and 1. + /// + [JsonPropertyName("score_threshold")] + public int ScoreThreshold { get; set; } } \ No newline at end of file diff --git a/OpenAI.SDK/ObjectModels/StaticValueHelper.cs b/OpenAI.SDK/ObjectModels/StaticValueHelper.cs index fb5bface..9821ce6f 100644 --- a/OpenAI.SDK/ObjectModels/StaticValueHelper.cs +++ b/OpenAI.SDK/ObjectModels/StaticValueHelper.cs @@ -174,4 +174,13 @@ public static class RequiredActionTypes public static string SubmitToolOutputs => "submit_tool_outputs"; } } + + public static class VectorStoreStatics + { + public static class ChunkingStrategyType + { + public static string Auto => "auto"; + public static string Static => "static"; + } + } } From f0867328689556bb074679715db36caafcb039f8 Mon Sep 17 00:00:00 2001 From: Tolga Kayhan Date: Sat, 21 Sep 2024 18:53:59 +0100 Subject: [PATCH 2/2] Chunking strategy was added to vector store responses and vector store file requests and responses. --- .../RequestModels/CreateVectorStoreFileBatchRequest.cs | 7 +++++++ .../VectorStoreResponseModels/VectorStoreFileObject.cs | 7 +++++++ .../VectorStoreObjectResponse.cs | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/OpenAI.SDK/ObjectModels/RequestModels/CreateVectorStoreFileBatchRequest.cs b/OpenAI.SDK/ObjectModels/RequestModels/CreateVectorStoreFileBatchRequest.cs index f8d6f076..faf62117 100644 --- a/OpenAI.SDK/ObjectModels/RequestModels/CreateVectorStoreFileBatchRequest.cs +++ b/OpenAI.SDK/ObjectModels/RequestModels/CreateVectorStoreFileBatchRequest.cs @@ -10,4 +10,11 @@ public class CreateVectorStoreFileBatchRequest /// [JsonPropertyName("file_ids")] public List FileIds { get; set; } + + /// + /// The chunking strategy used to chunk the file(s). If not set, will use the auto strategy. + /// Only applicable if file_ids is non-empty. + /// + [JsonPropertyName("chunking_strategy")] + public ChunkingStrategy? ChunkingStrategy { get; set; } } \ No newline at end of file diff --git a/OpenAI.SDK/ObjectModels/ResponseModels/VectorStoreResponseModels/VectorStoreFileObject.cs b/OpenAI.SDK/ObjectModels/ResponseModels/VectorStoreResponseModels/VectorStoreFileObject.cs index 65780d06..4f32427a 100644 --- a/OpenAI.SDK/ObjectModels/ResponseModels/VectorStoreResponseModels/VectorStoreFileObject.cs +++ b/OpenAI.SDK/ObjectModels/ResponseModels/VectorStoreResponseModels/VectorStoreFileObject.cs @@ -1,4 +1,5 @@ using System.Text.Json.Serialization; +using OpenAI.ObjectModels.RequestModels; namespace OpenAI.ObjectModels.ResponseModels.VectorStoreResponseModels; @@ -46,4 +47,10 @@ public record VectorStoreFileObject : BaseResponse /// [JsonPropertyName("last_error")] public Error? LastError { get; set; } + + /// + /// The strategy used to chunk the file. + /// + [JsonPropertyName("chunking_strategy")] + public ChunkingStrategy? ChunkingStrategy { get; set; } } \ No newline at end of file diff --git a/OpenAI.SDK/ObjectModels/ResponseModels/VectorStoreResponseModels/VectorStoreObjectResponse.cs b/OpenAI.SDK/ObjectModels/ResponseModels/VectorStoreResponseModels/VectorStoreObjectResponse.cs index 49c414e7..41a2c3ef 100644 --- a/OpenAI.SDK/ObjectModels/ResponseModels/VectorStoreResponseModels/VectorStoreObjectResponse.cs +++ b/OpenAI.SDK/ObjectModels/ResponseModels/VectorStoreResponseModels/VectorStoreObjectResponse.cs @@ -1,4 +1,5 @@ using System.Text.Json.Serialization; +using OpenAI.ObjectModels.RequestModels; namespace OpenAI.ObjectModels.ResponseModels.VectorStoreResponseModels; @@ -63,4 +64,11 @@ public record VectorStoreObjectResponse : BaseResponse /// [JsonPropertyName("metadata")] public Dictionary? Metadata { get; set; } + + /// + /// The chunking strategy used to chunk the file(s). If not set, will use the auto strategy. + /// Only applicable if file_ids is non-empty. + /// + [JsonPropertyName("chunking_strategy")] + public ChunkingStrategy? ChunkingStrategy { get; set; } } \ No newline at end of file