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

thread messages and runs #171

Merged
merged 10 commits into from
Nov 16, 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
42 changes: 19 additions & 23 deletions OpenAI-DotNet-Tests/TestFixture_11_Assistants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,19 @@ public async Task Test_01_CreateAssistant()

var assistantFileId = file.Id;

var request = new CreateAssistantRequest
var request = new CreateAssistantRequest("gpt-3.5-turbo-1106")
{
Name = "Test",
Description = "Test description",
Instructions = "You are test assistant",
Model = "gpt-3.5-turbo-1106",
Metadata = new Dictionary<string, object>
{
["int"] = 1,
["int"] = "1",
["text"] = "test"
},
Tools = new List<AssistantTool>
{
new()
{
Type = "retrieval"
}
AssistantTool.Retrieval
},
FileIds = new List<string> { assistantFileId }
};
Expand All @@ -60,9 +56,9 @@ public async Task Test_02_ListAssistants()
var assistantsList = await OpenAIClient.AssistantsEndpoint.ListAssistantsAsync();

Assert.IsNotNull(assistantsList);
Assert.IsNotEmpty(assistantsList.Assistants);
Assert.IsNotEmpty(assistantsList.Items);

foreach (var assistant in assistantsList.Assistants)
foreach (var assistant in assistantsList.Items)
{
var retrieved = await OpenAIClient.AssistantsEndpoint.RetrieveAssistantAsync(assistant.Id);
Assert.NotNull(retrieved);
Expand All @@ -78,16 +74,16 @@ public async Task Test_03_ListAssistantFiles()
var assistantsList = await OpenAIClient.AssistantsEndpoint.ListAssistantsAsync();

Assert.IsNotNull(assistantsList);
Assert.IsNotEmpty(assistantsList.Assistants);
Assert.IsNotEmpty(assistantsList.Items);

foreach (var assistant in assistantsList.Assistants)
foreach (var assistant in assistantsList.Items)
{
var filesList = await OpenAIClient.AssistantsEndpoint.ListAssistantFilesAsync(assistant.Id);

Assert.IsNotNull(assistantsList);
Assert.IsNotEmpty(assistantsList.Assistants);
Assert.IsNotEmpty(assistantsList.Items);

foreach (var file in filesList.Files)
foreach (var file in filesList.Items)
{
Assert.IsNotNull(file);

Expand All @@ -107,16 +103,16 @@ public async Task Test_04_DeleteAssistantFile()
var assistantsList = await OpenAIClient.AssistantsEndpoint.ListAssistantsAsync();

Assert.IsNotNull(assistantsList);
Assert.IsNotEmpty(assistantsList.Assistants);
Assert.IsNotEmpty(assistantsList.Items);

foreach (var assistant in assistantsList.Assistants)
foreach (var assistant in assistantsList.Items)
{
var filesList = await OpenAIClient.AssistantsEndpoint.ListAssistantFilesAsync(assistant.Id);

Assert.IsNotNull(filesList);
Assert.IsNotEmpty(filesList.Files);
Assert.IsNotEmpty(filesList.Items);

foreach (var file in filesList.Files)
foreach (var file in filesList.Items)
{
Assert.IsNotNull(file);

Expand All @@ -129,7 +125,7 @@ public async Task Test_04_DeleteAssistantFile()
filesList = await OpenAIClient.AssistantsEndpoint.ListAssistantFilesAsync(assistant.Id);

Assert.IsNotNull(filesList);
Assert.IsEmpty(filesList.Files);
Assert.IsEmpty(filesList.Items);
}
}

Expand All @@ -140,9 +136,9 @@ public async Task Test_05_ModifyAssistants()
var assistantsList = await OpenAIClient.AssistantsEndpoint.ListAssistantsAsync();

Assert.IsNotNull(assistantsList);
Assert.IsNotEmpty(assistantsList.Assistants);
Assert.IsNotEmpty(assistantsList.Items);

foreach (var assistant in assistantsList.Assistants)
foreach (var assistant in assistantsList.Items)
{
var request = new ModifyAssistantRequest
{
Expand Down Expand Up @@ -171,9 +167,9 @@ public async Task Test_06_DeleteAssistant()
var assistantsList = await OpenAIClient.AssistantsEndpoint.ListAssistantsAsync();

Assert.IsNotNull(assistantsList);
Assert.IsNotEmpty(assistantsList.Assistants);
Assert.IsNotEmpty(assistantsList.Items);

foreach (var assistant in assistantsList.Assistants)
foreach (var assistant in assistantsList.Items)
{
var result = await OpenAIClient.AssistantsEndpoint.DeleteAssistantAsync(assistant.Id);
Assert.IsTrue(result);
Expand All @@ -182,7 +178,7 @@ public async Task Test_06_DeleteAssistant()

assistantsList = await OpenAIClient.AssistantsEndpoint.ListAssistantsAsync();
Assert.IsNotNull(assistantsList);
Assert.IsEmpty(assistantsList.Assistants);
Assert.IsEmpty(assistantsList.Items);
}
}
}
151 changes: 151 additions & 0 deletions OpenAI-DotNet-Tests/TestFixture_14_ThreadMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using OpenAI.Chat;
using OpenAI.Files;
using OpenAI.Threads;
using Message = OpenAI.Threads.Message;

namespace OpenAI.Tests
{
internal class TestFixture_14_TheadMessages : AbstractTestFixture
{
private static CreateThreadRequest TestThread { get; } = new CreateThreadRequest(
new List<Message>
{
new Message("Test message")
},
new Dictionary<string, string>
{
["text"] = "test"
}
);

[Test]
public async Task Test_01_CreateThreadMessage()
{
Assert.IsNotNull(OpenAIClient.ThreadsEndpoint);

var thread = await OpenAIClient.ThreadsEndpoint.CreateThreadAsync(TestThread);
var file = await CreateFileForAssistant();

var request = new CreateThreadMessageRequest("Test content")
{
FileIds = new[] { file.Id },
Metadata = new Dictionary<string, string>
{
["test"] = "value"
}
};

var created = await OpenAIClient.ThreadsEndpoint.CreateThreadMessageAsync(thread.Id, request);

Assert.IsNotNull(created);
Assert.AreEqual("thread.message", created.Object);
Assert.AreEqual(Role.User, created.Role);
Assert.AreEqual(thread.Id, created.ThreadId);

Assert.IsNotNull(created.Content);
Assert.AreEqual(1, created.Content.Count);
Assert.AreEqual(ContentType.Text, created.Content[0].Type);
Assert.AreEqual("Test content", created.Content[0].Text.Value);

Assert.IsNotEmpty(created.FileIds);
Assert.AreEqual(1, created.FileIds.Count);
Assert.AreEqual(file.Id, created.FileIds[0]);

Assert.IsNotNull(created.Metadata);
Assert.Contains("test", created.Metadata.Keys.ToList());
Assert.AreEqual("value", created.Metadata["test"]);
}

[Test]
public async Task Test_02_ListThreadMessages()
{
var thread = await OpenAIClient.ThreadsEndpoint.CreateThreadAsync(TestThread);

var message1 = await OpenAIClient.ThreadsEndpoint.CreateThreadMessageAsync(
thread.Id, new CreateThreadMessageRequest("Test content"));

var message2 = await OpenAIClient.ThreadsEndpoint.CreateThreadMessageAsync(
thread.Id, new CreateThreadMessageRequest("Test content 2"));

var list = await OpenAIClient.ThreadsEndpoint.ListThreadMessagesAsync(thread.Id);

Assert.IsNotNull(list);

foreach (var message in list.Items)
{
var retrieved = await OpenAIClient.ThreadsEndpoint.RetrieveThreadMessageAsync(message.ThreadId, message.Id);
Assert.NotNull(retrieved);

Console.WriteLine($"[{retrieved.Id}] {retrieved.Content}");
}
}

[Test]
public async Task Test_03_ModifyThreadMessage()
{
var thread = await OpenAIClient.ThreadsEndpoint.CreateThreadAsync(TestThread);

var message = await OpenAIClient.ThreadsEndpoint.CreateThreadMessageAsync(
thread.Id, new CreateThreadMessageRequest("Test content"));

var modified = await OpenAIClient.ThreadsEndpoint.ModifyThreadMessageAsync(
message.ThreadId,
message.Id,
new Dictionary<string, string>
{
["test"] = "value"
});

Assert.IsNotNull(modified);
Assert.IsNotNull(modified.Metadata);
Assert.Contains("test", modified.Metadata.Keys.ToList());
Assert.AreEqual("value", modified.Metadata["test"]);
}

[Test]
public async Task Test_04_ListMessageFiles()
{
var thread = await OpenAIClient.ThreadsEndpoint.CreateThreadAsync(TestThread);
var file1 = await CreateFileForAssistant();
var file2 = await CreateFileForAssistant();

var createRequest = new CreateThreadMessageRequest("Test content")
{
FileIds = new[] { file1.Id, file2.Id }
};

var message = await OpenAIClient.ThreadsEndpoint.CreateThreadMessageAsync(thread.Id, createRequest);

var list = await OpenAIClient.ThreadsEndpoint.ListMessageFilesAsync(message.ThreadId, message.Id);

Assert.IsNotNull(list);
Assert.AreEqual(2, list.Items.Count);

foreach (var file in list.Items)
{
var retrieved =
await OpenAIClient.ThreadsEndpoint.RetrieveMessageFile(thread.Id, message.Id, file.Id);

Assert.IsNotNull(retrieved);

Console.WriteLine($"[{retrieved.MessageId}] -> {retrieved.Id}");
}
}

private async Task<FileData> CreateFileForAssistant()
{
var testData = "Some useful knowledge";
var fileName = "test.txt";
await File.WriteAllTextAsync(fileName, testData);
Assert.IsTrue(File.Exists(fileName));
var file = await OpenAIClient.FilesEndpoint.UploadFileAsync(fileName, "assistants");
return file;
}
}
}
Loading