Skip to content

Commit

Permalink
Merge pull request #590 from betalgo/dev
Browse files Browse the repository at this point in the history
8.4.0
  • Loading branch information
kayhantolga authored Jun 11, 2024
2 parents 7772cf3 + 85bfe12 commit 025f684
Show file tree
Hide file tree
Showing 11 changed files with 542 additions and 223 deletions.
1 change: 1 addition & 0 deletions OpenAI.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OpenAI.Extensions;
using OpenAI.Interfaces;
using OpenAI.Playground.TestHelpers;
using OpenAI.Playground.TestHelpers.AssistantHelpers;

var builder = new ConfigurationBuilder().AddJsonFile("ApiSettings.json")
.AddUserSecrets<Program>();
Expand Down
178 changes: 178 additions & 0 deletions OpenAI.Playground/TestHelpers/AssistantHelpers/RunTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static async Task RunTests(IOpenAIService openAI)
await RunCancelTests(openAI);
await RunToolTests(openAI);
await RunThreadAndRunTests(openAI);
await RunStreamTests(openAI);
await Cleanup(openAI);
}

Expand Down Expand Up @@ -56,11 +57,28 @@ public static async Task RunToolTests(IOpenAIService openAI)
await Cleanup(openAI);
}

public static async Task RunStreamTests(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Run Stream Testing is starting:", ConsoleColor.Blue);
await CreateRunAsStreamTest(openAI);
await Cleanup(openAI);
await CreateThreadAndRunAsStream(openAI);
await Cleanup(openAI);
await CreateToolRunTest(openAI);
await ListRunsTest(openAI);
await RetrieveRunTest(openAI);
await ModifyRunTest(openAI);
await WaitUntil(openAI, "requires_action");
await SubmitToolOutputsAsStreamToRunTest(openAI);
await Cleanup(openAI);
}

public static async Task RunThreadAndRunTests(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Run Thread and Run Testing is starting:", ConsoleColor.Blue);
await CreateThreadAndRun(openAI);
}


public static async Task CreateRunTest(IOpenAIService openAI)
{
Expand Down Expand Up @@ -111,6 +129,65 @@ public static async Task CreateRunTest(IOpenAIService openAI)
}
}

public static async Task CreateRunAsStreamTest(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Run Create As Stream Testing is starting:", ConsoleColor.Cyan);
var assistantResult = await openAI.Beta.Assistants.AssistantCreate(new()
{
Instructions = "You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
Name = "Math Tutor",
Tools = [ToolDefinition.DefineCodeInterpreter()],
Model = Models.Gpt_4_turbo
});
if (assistantResult.Successful)
{
CreatedAssistantId = assistantResult.Id;
ConsoleExtensions.WriteLine($"Assistant Created Successfully with ID: {assistantResult.Id}", ConsoleColor.Green);
}
else
{
ConsoleExtensions.WriteError(assistantResult.Error);
return;
}

var threadResult = await openAI.Beta.Threads.ThreadCreate();
if (threadResult.Successful)
{
CreatedThreadId = threadResult.Id;
ConsoleExtensions.WriteLine($"Thread Created Successfully with ID: {threadResult.Id}", ConsoleColor.Green);
}
else
{
ConsoleExtensions.WriteError(threadResult.Error);
return;
}

var result = openAI.Beta.Runs.RunCreateAsStream(CreatedThreadId, new()
{
AssistantId = assistantResult.Id
});

await foreach (var run in result)
{
if (run.Successful)
{
if (string.IsNullOrEmpty(run.Status))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
}
}
else
{
ConsoleExtensions.WriteError(run.Error);
}
}

}

public static async Task CreateToolRunTest(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Run Create Tool Testing is starting:", ConsoleColor.Cyan);
Expand Down Expand Up @@ -343,6 +420,55 @@ public static async Task SubmitToolOutputsToRunTest(IOpenAIService openAI)
}
}

public static async Task SubmitToolOutputsAsStreamToRunTest(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Submit Tool Outputs To Run Testing is starting:", ConsoleColor.Cyan);
if (string.IsNullOrWhiteSpace(CreatedRunId))
{
ConsoleExtensions.WriteLine("Run Id is not found. Please create a run first.", ConsoleColor.Red);
return;
}

if (string.IsNullOrWhiteSpace(CreatedThreadId))
{
ConsoleExtensions.WriteLine("Thread Id is not found. Please create a thread first.", ConsoleColor.Red);
return;
}

var retrieveResult = await openAI.Beta.Runs.RunRetrieve(CreatedThreadId, CreatedRunId);
var result = openAI.Beta.Runs.RunSubmitToolOutputsAsStream(CreatedThreadId, CreatedRunId, new()
{
ToolOutputs =
[
new()
{
ToolCallId = retrieveResult.RequiredAction!.SubmitToolOutputs.ToolCalls.First()
.Id,
Output = "70 degrees and sunny."
}
]
});

await foreach (var run in result)
{
if (run.Successful)
{
if (string.IsNullOrEmpty(run.Status))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
}
}
else
{
ConsoleExtensions.WriteError(run.Error);
}
}
}

public static async Task CancelRunTest(IOpenAIService openAI)
{
ConsoleExtensions.WriteLine("Run Cancel Testing is starting:", ConsoleColor.Cyan);
Expand Down Expand Up @@ -488,6 +614,56 @@ public static async Task CreateThreadAndRun(IOpenAIService sdk)
}
}

public static async Task CreateThreadAndRunAsStream(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Create Thread and Run As Stream Testing is starting:", ConsoleColor.Cyan);
var assistantResult = await sdk.Beta.Assistants.AssistantCreate(new()
{
Instructions = "You are a personal math tutor. When asked a question, write and run Python code to answer the question.",
Name = "Math Tutor",
Tools = [ToolDefinition.DefineCodeInterpreter()],
Model = Models.Gpt_4_turbo
});
CreatedAssistantId = assistantResult.Id;
var runResult = sdk.Beta.Runs.CreateThreadAndRunAsStream(new()
{
AssistantId = assistantResult.Id,
Thread = new()
{
Messages =
[
new()
{
Role = StaticValues.AssistantsStatics.MessageStatics.Roles.User,
Content = new("Explain deep learning to a 5 year old.")
}
]
}
});

await foreach (var run in runResult)
{
if (run.Successful)
{
if (string.IsNullOrEmpty(run.Status))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
}
}
else
{
ConsoleExtensions.WriteError(run.Error);
}
}
ConsoleExtensions.WriteLine("Create Thread and Run As Stream Test is successful.", ConsoleColor.Green);


}

public static async Task Cleanup(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Cleanup Testing is starting:", ConsoleColor.Cyan);
Expand All @@ -496,6 +672,7 @@ public static async Task Cleanup(IOpenAIService sdk)
var threadResult = await sdk.Beta.Threads.ThreadDelete(CreatedThreadId);
if (threadResult.Successful)
{
CreatedThreadId = null;
ConsoleExtensions.WriteLine("Thread Deleted Successfully.", ConsoleColor.Green);
}
else
Expand All @@ -509,6 +686,7 @@ public static async Task Cleanup(IOpenAIService sdk)
var assistantResult = await sdk.Beta.Assistants.AssistantDelete(CreatedAssistantId);
if (assistantResult.Successful)
{
CreatedAssistantId = null;
ConsoleExtensions.WriteLine("Assistant Deleted Successfully.", ConsoleColor.Green);
}
else
Expand Down
Loading

0 comments on commit 025f684

Please sign in to comment.