Skip to content

Commit b60d658

Browse files
authored
Add vector store creation example with manual polling (#682)
Add vector store file batch sample
1 parent 81c4795 commit b60d658

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
using System.Threading.Tasks;
10+
11+
namespace OpenAI.Examples;
12+
13+
// This example uses experimental APIs which are subject to change. To use experimental APIs,
14+
// please acknowledge their experimental status by suppressing the corresponding warning.
15+
#pragma warning disable OPENAI001
16+
17+
public partial class VectorStoreExamples
18+
{
19+
[Test]
20+
public async Task Example01_SimpleVectorStoreCreationAsync()
21+
{
22+
VectorStoreClient client = new(new ApiKeyCredential(Environment.GetEnvironmentVariable("OPENAI_API_KEY")));
23+
24+
IReadOnlyList<OpenAIFile> files = await GetSampleFilesAsync(5);
25+
VectorStoreCreationOptions storeOptions = new VectorStoreCreationOptions();
26+
foreach (var file in files)
27+
{
28+
storeOptions.FileIds.Add(file.Id);
29+
}
30+
VectorStore store = await client.CreateVectorStoreAsync(storeOptions);
31+
32+
store = await PollVectorStoreAsync(client, store.Id);
33+
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 async Task<VectorStore> PollVectorStoreAsync(VectorStoreClient client, string vectorStoreId)
41+
{
42+
while (true)
43+
{
44+
ClientResult<VectorStore> result = await client.GetVectorStoreAsync(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 async Task<IReadOnlyList<OpenAIFile>> GetSampleFilesAsync(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 = await fileClient.UploadFileAsync(
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

Comments
 (0)