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 6 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
10 changes: 3 additions & 7 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"
}
new(AssistantToolType.Retrieval)
},
FileIds = new List<string> { assistantFileId }
};
Expand Down
150 changes: 150 additions & 0 deletions OpenAI-DotNet-Tests/TestFixture_14_ThreadMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
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(ThreadRole.User, created.Role);
Assert.AreEqual(thread.Id, created.ThreadId);

Assert.IsNotNull(created.Content);
Assert.AreEqual(1, created.Content.Length);
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.Length);
Assert.AreEqual(file.Id, created.FileIds[0]);

Assert.IsNotNull(created.Metadata);
Assert.Contains("test", created.Metadata.Keys);
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.Messages)
{
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);
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.Files.Count);

foreach (var file in list.Files)
{
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