|
| 1 | +using NUnit.Framework; |
| 2 | +using NUnit.Framework.Internal; |
| 3 | +using OpenAI.Files; |
| 4 | +using OpenAI.VectorStores; |
| 5 | +using System; |
| 6 | +using System.ClientModel; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace OpenAI.Examples; |
| 11 | + |
| 12 | +// This example uses experimental APIs which are subject to change. To use experimental APIs, |
| 13 | +// please acknowledge their experimental status by suppressing the corresponding warning. |
| 14 | +#pragma warning disable OPENAI001 |
| 15 | + |
| 16 | +public partial class VectorStoreExamples |
| 17 | +{ |
| 18 | + [Test] |
| 19 | + public void Example01_SimpleVectorStoreCreation() |
| 20 | + { |
| 21 | + VectorStoreClient client = new(new ApiKeyCredential(Environment.GetEnvironmentVariable("OPENAI_API_KEY"))); |
| 22 | + |
| 23 | + IReadOnlyList<OpenAIFile> files = GetSampleFiles(5); |
| 24 | + VectorStoreCreationOptions storeOptions = new VectorStoreCreationOptions(); |
| 25 | + foreach (var file in files) |
| 26 | + { |
| 27 | + storeOptions.FileIds.Add(file.Id); |
| 28 | + } |
| 29 | + VectorStore store = client.CreateVectorStore(storeOptions); |
| 30 | + |
| 31 | + store = PollVectorStore(client, store.Id); |
| 32 | + |
| 33 | + Assert.AreEqual(VectorStoreStatus.Completed, store.Status); |
| 34 | + Console.WriteLine($"Vector store {store.Id} is ready (status: {store.Status})"); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Wait for the vector store to finish processing. |
| 39 | + /// </summary> |
| 40 | + private static VectorStore PollVectorStore(VectorStoreClient client, string vectorStoreId) |
| 41 | + { |
| 42 | + while (true) |
| 43 | + { |
| 44 | + ClientResult<VectorStore> result = client.GetVectorStore(vectorStoreId); |
| 45 | + var store = result.Value; |
| 46 | + var response = result.GetRawResponse(); |
| 47 | + |
| 48 | + if (store.Status == VectorStoreStatus.InProgress) |
| 49 | + { |
| 50 | + // Default 1s; override if server hints are present |
| 51 | + TimeSpan pollDelay = TimeSpan.FromSeconds(1); |
| 52 | + if (response.Headers.TryGetValue("Retry-After", out var retryAfter)) |
| 53 | + { |
| 54 | + if (int.TryParse(retryAfter, out var delaySeconds)) |
| 55 | + { |
| 56 | + pollDelay = TimeSpan.FromSeconds(delaySeconds); |
| 57 | + } |
| 58 | + else if (DateTimeOffset.TryParse(retryAfter, out var retryAfterDate)) |
| 59 | + { |
| 60 | + pollDelay = retryAfterDate - DateTimeOffset.Now; |
| 61 | + } |
| 62 | + } |
| 63 | + else if (response.Headers.TryGetValue("openai-poll-after-ms", out var msStr) && |
| 64 | + int.TryParse(msStr, out var ms)) |
| 65 | + { |
| 66 | + pollDelay = TimeSpan.FromMilliseconds(ms); |
| 67 | + } |
| 68 | + |
| 69 | + Thread.Sleep(pollDelay); |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + return store; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static IReadOnlyList<OpenAIFile> GetSampleFiles(int count) |
| 79 | + { |
| 80 | + var files = new List<OpenAIFile>(); |
| 81 | + var fileClient = new OpenAIFileClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 82 | + |
| 83 | + for (int i = 0; i < count; i++) |
| 84 | + { |
| 85 | + using var stream = BinaryData.FromString($"This is a sample file {i}").ToStream(); |
| 86 | + var file = fileClient.UploadFile( |
| 87 | + stream, |
| 88 | + $"sample_file_{i:000}.txt", |
| 89 | + FileUploadPurpose.Assistants); |
| 90 | + files.Add(file); |
| 91 | + } |
| 92 | + |
| 93 | + return files; |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#pragma warning restore OPENAI001 |
0 commit comments