Skip to content

Commit

Permalink
Merge pull request #551 from betalgo/feature/Assistans-Part-3
Browse files Browse the repository at this point in the history
Assistant Vector Support, Message Delete Support, Documentation support.
  • Loading branch information
kayhantolga authored May 17, 2024
2 parents 92f1745 + 1c4ada8 commit ce31b17
Show file tree
Hide file tree
Showing 72 changed files with 3,023 additions and 520 deletions.
13 changes: 12 additions & 1 deletion OpenAI.Playground/ExtensionsAndHelpers/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenAI.Playground.ExtensionsAndHelpers;
using OpenAI.ObjectModels.ResponseModels;

namespace OpenAI.Playground.ExtensionsAndHelpers;

public static class ConsoleExtensions
{
Expand All @@ -9,4 +11,13 @@ public static void WriteLine(string? value, ConsoleColor color = ConsoleColor.Gr
Console.WriteLine(value);
Console.ForegroundColor = defaultColor;
}

public static void WriteError(Error? error)
{
if (error == null)
{
throw new Exception("Unknown Error");
}
WriteLine($"{error.Code}: {error.Message}", ConsoleColor.Red);
}
}
9 changes: 9 additions & 0 deletions OpenAI.Playground/OpenAI.Playground.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<None Update="ApiSettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="SampleData\Assistants-overview.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="SampleData\betterway_corp.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand All @@ -74,6 +77,12 @@
<None Update="SampleData\FineTuningJobSample2.jsonl">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="SampleData\How-Assistants-work.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="SampleData\HowAssistantsWork.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="SampleData\image_edit_mask.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
13 changes: 10 additions & 3 deletions OpenAI.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using OpenAI.Interfaces;
using OpenAI.Playground.TestHelpers;
using LaserCatEyes.HttpClientListener;
using OpenAI.ObjectModels.RequestModels;
using OpenAI.Playground.TestHelpers.AssistantHelpers;

var builder = new ConfigurationBuilder()
.AddJsonFile("ApiSettings.json")
Expand Down Expand Up @@ -40,14 +42,19 @@
// | /|\ | /\ ___\o \o | o/ o/__ /\ | /|\ |
// | / \ / \ | \ /) | ( \ /o\ / ) | (\ / | / \ / \ |
// |-----------------------------------------------------------------------|
//await AssistantTestHelper.BasicsTestHelper.RunTests(sdk);
//await AssistantTestHelper.ThreadsTestHelper.RunTests(sdk);
//await AssistantTestHelper.MessagesTestHelper.RunTests(sdk);
//await AssistantTestHelper.RunTestHelper.RunTests(sdk);
await AssistantTestHelper.VectorTestHelper.RunTests(sdk);

await ChatCompletionTestHelper.RunSimpleChatCompletionTest(sdk);
//await ChatCompletionTestHelper.RunSimpleChatCompletionTest(sdk);
//await ChatCompletionTestHelper.RunSimpleCompletionStreamTest(sdk);


//Assistants - BETA
//await AssistantTestHelper.RunAssistantApiTest(sdk);
//await AssistantTestHelper.RunHowAssistantsWorkTest(sdk);
//await AssistantTestHelper3.RunAssistantApiTest(sdk);
//await AssistantTestHelper3.RunHowAssistantsWorkTest(sdk);

//await MessageTestHelper.RunMessageCreateTest(sdk);

Expand Down
329 changes: 329 additions & 0 deletions OpenAI.Playground/SampleData/Assistants-overview.html

Large diffs are not rendered by default.

366 changes: 366 additions & 0 deletions OpenAI.Playground/SampleData/How-Assistants-work.html

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions OpenAI.Playground/SampleData/HowAssistantsWork.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
How Assistants work Beta
The Assistants API is designed to help developers build powerful AI assistants capable of performing a variety of tasks.

The Assistants API is in beta and we are actively working on adding more functionality. Share your feedback in our Developer Forum!
Assistants can call OpenAI�s models with specific instructions to tune their personality and capabilities.
Assistants can access multiple tools in parallel. These can be both OpenAI-hosted tools � like code_interpreter and file_search � or tools you build / host (via function calling).
Assistants can access persistent Threads. Threads simplify AI application development by storing message history and truncating it when the conversation gets too long for the model�s context length. You create a Thread once, and simply append Messages to it as your users reply.
Assistants can access files in several formats � either as part of their creation or as part of Threads between Assistants and users. When using tools, Assistants can also create files (e.g., images, spreadsheets, etc) and cite files they reference in the Messages they create.
Objects
Assistants object architecture diagram

OBJECT WHAT IT REPRESENTS
Assistant Purpose-built AI that uses OpenAI�s models and calls tools
Thread A conversation session between an Assistant and a user. Threads store Messages and automatically handle truncation to fit content into a model�s context.
Message A message created by an Assistant or a user. Messages can include text, images, and other files. Messages stored as a list on the Thread.
Run An invocation of an Assistant on a Thread. The Assistant uses its configuration and the Thread�s Messages to perform tasks by calling models and tools. As part of a Run, the Assistant appends Messages to the Thread.
Run Step A detailed list of steps the Assistant took as part of a Run. An Assistant can call tools or create Messages during its run. Examining Run Steps allows you to introspect how the Assistant is getting to its final results.
175 changes: 175 additions & 0 deletions OpenAI.Playground/TestHelpers/AssistantHelpers/AssistantTestHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using OpenAI.Interfaces;
using OpenAI.ObjectModels;
using OpenAI.ObjectModels.RequestModels;
using OpenAI.ObjectModels.SharedModels;
using OpenAI.Playground.ExtensionsAndHelpers;

namespace OpenAI.Playground.TestHelpers.AssistantHelpers;
internal static partial class AssistantTestHelper
{
internal static class BasicsTestHelper
{
private const string Instruction = "You are a personal math tutor. When asked a question, write and run Python code to answer the question.";
private const string Name = "Math Tutor";
private static string? CreatedAssistantId { get; set; }

public static async Task RunTests(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Assistant Basics Testing is starting:", ConsoleColor.Blue);
await CreateAssistant(openAI);
await ListAssistants(openAI);
await RetrieveAssistant(openAI);
await ModifyAssistantTask(openAI);
await DeleteAssistant(openAI);
}

public static async Task CreateAssistant(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Create Assistant Testing is starting:", ConsoleColor.Cyan);

var result = await openAI.Beta.Assistants.AssistantCreate(new()
{
Instructions = Instruction,
Name = Name,
Tools = [ToolDefinition.DefineCodeInterpreter()],
Model = Models.Gpt_4_turbo
});

if (result.Successful)
{
CreatedAssistantId = result.Id;
ConsoleExtensions.WriteLine($"Assistant Created Successfully with ID: {result.Id}", ConsoleColor.Green);
}
else
{
ConsoleExtensions.WriteError(result.Error);
}
}

public static async Task ListAssistants(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("List Assistants Testing is starting:", ConsoleColor.Cyan);
var allAssistants = new List<AssistantResponse>();
var hasMore = true;
var lastId = string.Empty;
while (hasMore)
{
var result = await openAI.Beta.Assistants.AssistantList(new() { After = lastId, Limit = 5 });
if (result.Successful)
{
if (result.Data != null)
{
allAssistants.AddRange(result.Data);
}

hasMore = result.HasMore;
lastId = result.LastId;
}
else
{
ConsoleExtensions.WriteError(result.Error);
return;
}
}

foreach (var assistant in allAssistants)
{
Console.WriteLine($"ID: {assistant.Id}, Name: {assistant.Name}");
}

if (allAssistants.FirstOrDefault(r => r.Id == CreatedAssistantId) != null)
{
ConsoleExtensions.WriteLine("List Assistants Test is successful.", ConsoleColor.Green);
}
else
{
ConsoleExtensions.WriteLine("List Assistants Test is failed.", ConsoleColor.Red);
}
}

public static async Task RetrieveAssistant(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Retrieve Assistant Testing is starting:", ConsoleColor.Cyan);
if (string.IsNullOrWhiteSpace(CreatedAssistantId))
{
ConsoleExtensions.WriteLine("Assistant Id is not found. Please create an assistant first.", ConsoleColor.Red);
return;
}

var result = await openAI.Beta.Assistants.AssistantRetrieve(CreatedAssistantId);
if (result.Successful)
{
ConsoleExtensions.WriteLine("Retrieve Assistant Test is successful.", ConsoleColor.Green);
}
else
{
ConsoleExtensions.WriteError(result.Error);
}
}

public static async Task ModifyAssistantTask(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Modify Assistant Testing is starting:", ConsoleColor.Cyan);
if (string.IsNullOrWhiteSpace(CreatedAssistantId))
{
ConsoleExtensions.WriteLine("Assistant Id is not found. Please create an assistant first.", ConsoleColor.Red);
return;
}

const string newName = Name + "2";
const string newInstructions = "You are an HR bot, and you have access to files to answer employee questions about company policies. Always response with info from either of the files.";
var result = await openAI.Beta.Assistants.AssistantModify(CreatedAssistantId, new()
{
Instructions = newInstructions,
Name = newName,
Tools = [ToolDefinition.DefineFileSearch()],
Model = Models.Gpt_4_turbo
});

if (result.Successful)
{
if (result is { Name: newName, Instructions: newInstructions } && result.Tools.First()
.Type == ToolDefinition.DefineFileSearch()
.Type)
{
ConsoleExtensions.WriteLine("Modify Assistant Test is successful.", ConsoleColor.Green);
}
else
{
ConsoleExtensions.WriteLine("Modify Assistant Test is failed.", ConsoleColor.Red);
}
}
else
{
ConsoleExtensions.WriteError(result.Error);
}
}

public static async Task DeleteAssistant(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Delete Assistant Testing is starting:", ConsoleColor.Cyan);
if (string.IsNullOrWhiteSpace(CreatedAssistantId))
{
ConsoleExtensions.WriteLine("Assistant Id is not found. Please create an assistant first.", ConsoleColor.Red);
return;
}

var result = await openAI.Beta.Assistants.AssistantDelete(CreatedAssistantId);
if (result.Successful)
{
if (result.IsDeleted)
{
ConsoleExtensions.WriteLine("Delete Assistant Test is successful.", ConsoleColor.Green);
}
else
{
ConsoleExtensions.WriteLine("Delete Assistant Test is failed.", ConsoleColor.Red);
}
}
else
{
ConsoleExtensions.WriteError(result.Error);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using OpenAI.ObjectModels.SharedModels;
using OpenAI.Playground.ExtensionsAndHelpers;

namespace OpenAI.Playground.TestHelpers;
namespace OpenAI.Playground.TestHelpers.AssistantHelpers;

internal static class AssistantTestHelper
internal static class AssistantTestHelper3
{
/// <summary>
/// Test Assistant api
Expand Down Expand Up @@ -203,11 +203,11 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
#region //create thread message

ConsoleExtensions.WriteLine("Message Create Test:", ConsoleColor.DarkCyan);
var messageResult = await sdk.Beta.Messages.MessageCreate(threadId, new MessageCreateRequest
var messageResult = await sdk.Beta.Messages.CreateMessage(threadId, new MessageCreateRequest
{
Role = StaticValues.AssistantsStatics.MessageStatics.Roles.User,
Content = "Where is Zhejiang Jiacheng Supply Chain Co., LTD.",
FileIds = new List<string>() { uplaodFileId }
Content =new("Where is Zhejiang Jiacheng Supply Chain Co., LTD."),
Attachments = [new() { FileId = uplaodFileId }]
});

if (messageResult.Successful)
Expand Down Expand Up @@ -314,7 +314,7 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)

//Get the final message result
ConsoleExtensions.WriteLine("Message list:", ConsoleColor.DarkCyan);
var messageListResult = await sdk.Beta.Messages.MessageList(threadId);
var messageListResult = await sdk.Beta.Messages.ListMessages(threadId);
if (messageListResult.Successful)
{
var msgRespList = messageListResult.Data;
Expand Down
Loading

0 comments on commit ce31b17

Please sign in to comment.