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

8.5.0 #602

Merged
merged 7 commits into from
Jun 15, 2024
Merged

8.5.0 #602

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
3 changes: 2 additions & 1 deletion OpenAI.Playground/ExtensionsAndHelpers/ConsoleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public static void WriteError(Error? error)
{
if (error == null)
{
throw new Exception("Unknown Error");
throw new("Unknown Error");
}

WriteLine($"{error.Code}: {error.Message}", ConsoleColor.Red);
}
}
4 changes: 1 addition & 3 deletions OpenAI.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using OpenAI.Extensions;
using OpenAI.Interfaces;
using OpenAI.Playground.TestHelpers;
using OpenAI.Playground.TestHelpers.AssistantHelpers;

var builder = new ConfigurationBuilder().AddJsonFile("ApiSettings.json")
.AddUserSecrets<Program>();
var builder = new ConfigurationBuilder().AddJsonFile("ApiSettings.json").AddUserSecrets<Program>();

IConfiguration configuration = builder.Build();
var serviceCollection = new ServiceCollection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
using OpenAI.Playground.ExtensionsAndHelpers;

namespace OpenAI.Playground.TestHelpers.AssistantHelpers;
internal static partial class AssistantTestHelper

internal static partial class AssistantTestHelper
{
internal static class BasicsTestHelper
{
Expand Down Expand Up @@ -128,9 +129,7 @@ public static async Task ModifyAssistantTask(IOpenAIService openAI)

if (result.Successful)
{
if (result is { Name: newName, Instructions: newInstructions } && result.Tools.First()
.Type == ToolDefinition.DefineFileSearch()
.Type)
if (result is { Name: newName, Instructions: newInstructions } && result.Tools.First().Type == ToolDefinition.DefineFileSearch().Type)
{
ConsoleExtensions.WriteLine("Modify Assistant Test is successful.", ConsoleColor.Green);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ public static async Task RunAssistantApiTest(IOpenAIService sdk)

#region Create assistant

var func = new FunctionDefinitionBuilder("get_corp_location", "get location of corp").AddParameter("name", PropertyDefinition.DefineString("company name, e.g. Betterway"))
.Validate()
.Build();
var func = new FunctionDefinitionBuilder("get_corp_location", "get location of corp").AddParameter("name", PropertyDefinition.DefineString("company name, e.g. Betterway")).Validate().Build();

ConsoleExtensions.WriteLine("Assistant Create Test:", ConsoleColor.DarkCyan);
var assistantResult = await sdk.Beta.Assistants.AssistantCreate(new AssistantCreateRequest
var assistantResult = await sdk.Beta.Assistants.AssistantCreate(new()
{
Instructions = "You are a professional assistant who provides company information. Company-related data comes from uploaded questions and does not provide vague answers, only clear answers.",
Name = "Qicha",
Tools = new List<ToolDefinition>() { ToolDefinition.DefineCodeInterpreter(), ToolDefinition.DefineFileSearch(), ToolDefinition.DefineFunction(func) },
Tools = new() { ToolDefinition.DefineCodeInterpreter(), ToolDefinition.DefineFileSearch(), ToolDefinition.DefineFunction(func) },
Model = Models.Gpt_3_5_Turbo_1106
});
if (assistantResult.Successful)
Expand Down Expand Up @@ -75,7 +73,7 @@ public static async Task RunAssistantApiTest(IOpenAIService sdk)
#region // Assistant modify

ConsoleExtensions.WriteLine("Assistant modify:", ConsoleColor.DarkCyan);
var asstResult = await sdk.Beta.Assistants.AssistantModify(assistantId, new AssistantModifyRequest()
var asstResult = await sdk.Beta.Assistants.AssistantModify(assistantId, new()
{
Name = "Qicha rename"
});
Expand Down Expand Up @@ -161,17 +159,15 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)

#region //create assistants

var func = new FunctionDefinitionBuilder("get_corp_location", "get location of corp").AddParameter("name", PropertyDefinition.DefineString("company name, e.g. Betterway"))
.Validate()
.Build();
var func = new FunctionDefinitionBuilder("get_corp_location", "get location of corp").AddParameter("name", PropertyDefinition.DefineString("company name, e.g. Betterway")).Validate().Build();

ConsoleExtensions.WriteLine("Assistant Create Test:", ConsoleColor.DarkCyan);
var assistantResult = await sdk.Beta.Assistants.AssistantCreate(new AssistantCreateRequest
var assistantResult = await sdk.Beta.Assistants.AssistantCreate(new()
{
Instructions = "You are a professional assistant who provides company information. Company-related data comes from uploaded questions and does not provide vague answers, only clear answers.",
Name = "Qicha",
Model = Models.Gpt_3_5_Turbo_1106,
Tools = new List<ToolDefinition>() { ToolDefinition.DefineCodeInterpreter(), ToolDefinition.DefineFileSearch(), ToolDefinition.DefineFunction(func) },
Tools = [ToolDefinition.DefineCodeInterpreter(), ToolDefinition.DefineFileSearch(), ToolDefinition.DefineFunction(func)]
});

if (assistantResult.Successful)
Expand Down Expand Up @@ -211,16 +207,19 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
#region //create thread message

ConsoleExtensions.WriteLine("Message Create Test:", ConsoleColor.DarkCyan);
var messageResult = await sdk.Beta.Messages.CreateMessage(threadId, new MessageCreateRequest
var messageResult = await sdk.Beta.Messages.CreateMessage(threadId, new()
{
Role = StaticValues.AssistantsStatics.MessageStatics.Roles.User,
Content =new("Where is Zhejiang Jiacheng Supply Chain Co., LTD."),
Content = new("Where is Zhejiang Jiacheng Supply Chain Co., LTD."),
// Tools must be specified for Attachments
Attachments = [new()
{
FileId = uploadFileId,
Tools = [ ToolDefinition.DefineFileSearch() ]
}]
Attachments =
[
new()
{
FileId = uploadFileId,
Tools = [ToolDefinition.DefineFileSearch()]
}
]
});

if (messageResult.Successful)
Expand All @@ -240,7 +239,7 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
#region //create run

ConsoleExtensions.WriteLine("Run Create Test:", ConsoleColor.DarkCyan);
var runResult = await sdk.Beta.Runs.RunCreate(threadId, new RunCreateRequest()
var runResult = await sdk.Beta.Runs.RunCreate(threadId, new()
{
AssistantId = assistantId
});
Expand Down Expand Up @@ -305,7 +304,7 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
//All outputs must be submitted in a single request.
if (toolOutputs.Any())
{
await sdk.Beta.Runs.RunSubmitToolOutputs(threadId, runId, new SubmitToolOutputsToRunRequest()
await sdk.Beta.Runs.RunSubmitToolOutputs(threadId, runId, new()
{
ToolOutputs = toolOutputs
});
Expand All @@ -332,8 +331,7 @@ public static async Task RunHowAssistantsWorkTest(IOpenAIService sdk)
{
var msgRespList = messageListResult.Data;
var ask = msgRespList?.FirstOrDefault(msg => msg.Role == StaticValues.AssistantsStatics.MessageStatics.Roles.User);
var replys = msgRespList?.Where(msg => msg.CreatedAt > ask?.CreatedAt && msg.Role == StaticValues.AssistantsStatics.MessageStatics.Roles.Assistant)
.ToList() ?? new List<MessageResponse>();
var replys = msgRespList?.Where(msg => msg.CreatedAt > ask?.CreatedAt && msg.Role == StaticValues.AssistantsStatics.MessageStatics.Roles.Assistant).ToList() ?? new List<MessageResponse>();
ConsoleExtensions.WriteLine(replys.ToJson());
}
else
Expand Down
155 changes: 134 additions & 21 deletions OpenAI.Playground/TestHelpers/AssistantHelpers/RunTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ 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 @@ -165,27 +165,65 @@ public static async Task CreateRunAsStreamTest(IOpenAIService openAI)
var result = openAI.Beta.Runs.RunCreateAsStream(CreatedThreadId, new()
{
AssistantId = assistantResult.Id
});
},justDataMode:false);

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

else if (run is RunStepResponse runStepResponse)
{
if (string.IsNullOrEmpty(runStepResponse.Status))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Run Step Id: {runStepResponse.Id}, Status: {runStepResponse.Status}");
}
}

else if (run is MessageResponse messageResponse)
{
if (string.IsNullOrEmpty(messageResponse.Id))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Message Id: {messageResponse.Id}, Message: {messageResponse.Content?.FirstOrDefault()?.Text?.Value}");
}
}
else
{
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
if (run.StreamEvent!=null)
{
Console.WriteLine(run.StreamEvent);
}
else
{
Console.Write(".");
}
}
}
else
{
ConsoleExtensions.WriteError(run.Error);
}
}

}

public static async Task CreateToolRunTest(IOpenAIService openAI)
Expand Down Expand Up @@ -404,8 +442,7 @@ public static async Task SubmitToolOutputsToRunTest(IOpenAIService openAI)
[
new()
{
ToolCallId = retrieveResult.RequiredAction!.SubmitToolOutputs.ToolCalls.First()
.Id,
ToolCallId = retrieveResult.RequiredAction!.SubmitToolOutputs.ToolCalls.First().Id,
Output = "70 degrees and sunny."
}
]
Expand Down Expand Up @@ -442,8 +479,7 @@ public static async Task SubmitToolOutputsAsStreamToRunTest(IOpenAIService openA
[
new()
{
ToolCallId = retrieveResult.RequiredAction!.SubmitToolOutputs.ToolCalls.First()
.Id,
ToolCallId = retrieveResult.RequiredAction!.SubmitToolOutputs.ToolCalls.First().Id,
Output = "70 degrees and sunny."
}
]
Expand All @@ -453,13 +489,52 @@ public static async Task SubmitToolOutputsAsStreamToRunTest(IOpenAIService openA
{
if (run.Successful)
{
if (string.IsNullOrEmpty(run.Status))
Console.WriteLine($"Event:{run.StreamEvent}");
if (run is RunResponse runResponse)
{
if (string.IsNullOrEmpty(runResponse.Status))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Run Id: {runResponse.Id}, Status: {runResponse.Status}");
}
}

else if (run is RunStepResponse runStepResponse)
{
if (string.IsNullOrEmpty(runStepResponse.Status))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Run Step Id: {runStepResponse.Id}, Status: {runStepResponse.Status}");
}
}

else if (run is MessageResponse messageResponse)
{
Console.Write(".");
if (string.IsNullOrEmpty(messageResponse.Id))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Message Id: {messageResponse.Id}, Message: {messageResponse.Content?.FirstOrDefault()?.Text?.Value}");
}
}
else
{
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
if (run.StreamEvent != null)
{
Console.WriteLine(run.StreamEvent);
}
else
{
Console.Write(".");
}
}
}
else
Expand All @@ -485,6 +560,7 @@ public static async Task CancelRunTest(IOpenAIService openAI)
{
CreatedAssistantId = assistantResult.Id;
}

var createRunResult = await openAI.Beta.Runs.RunCreate(createThreadResult.Id, new() { AssistantId = assistantResult.Id });
var result = await openAI.Beta.Runs.RunCancel(createThreadResult.Id, createRunResult.Id);

Expand Down Expand Up @@ -565,8 +641,7 @@ public static async Task RetrieveRunStepTest(IOpenAIService openAI)
}

var resultStepsList = await openAI.Beta.RunSteps.RunStepsList(CreatedThreadId, CreatedRunId);
var result = await openAI.Beta.RunSteps.RunStepRetrieve(CreatedThreadId, CreatedRunId, resultStepsList.Data!.First()
.Id);
var result = await openAI.Beta.RunSteps.RunStepRetrieve(CreatedThreadId, CreatedRunId, resultStepsList.Data!.First().Id);
if (result.Successful)
{
ConsoleExtensions.WriteLine("Retrieve Run Step Test is successful.", ConsoleColor.Green);
Expand Down Expand Up @@ -640,28 +715,66 @@ public static async Task CreateThreadAndRunAsStream(IOpenAIService sdk)
]
}
});

await foreach (var run in runResult)
{
if (run.Successful)
{
if (string.IsNullOrEmpty(run.Status))
Console.WriteLine($"Event:{run.StreamEvent}");
if (run is RunResponse runResponse)
{
Console.Write(".");
if (string.IsNullOrEmpty(runResponse.Status))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Run Id: {runResponse.Id}, Status: {runResponse.Status}");
}
}

else if (run is RunStepResponse runStepResponse)
{
if (string.IsNullOrEmpty(runStepResponse.Status))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Run Step Id: {runStepResponse.Id}, Status: {runStepResponse.Status}");
}
}

else if (run is MessageResponse messageResponse)
{
if (string.IsNullOrEmpty(messageResponse.Id))
{
Console.Write(".");
}
else
{
ConsoleExtensions.WriteLine($"Message Id: {messageResponse.Id}, Message: {messageResponse.Content?.FirstOrDefault()?.Text?.Value}");
}
}
else
{
ConsoleExtensions.WriteLine($"Run Id: {run.Id}, Status: {run.Status}");
if (run.StreamEvent != null)
{
Console.WriteLine(run.StreamEvent);
}
else
{
Console.Write(".");
}
}
}
else
{
ConsoleExtensions.WriteError(run.Error);
}
}
ConsoleExtensions.WriteLine("Create Thread and Run As Stream Test is successful.", ConsoleColor.Green);


ConsoleExtensions.WriteLine("Create Thread and Run As Stream Test is successful.", ConsoleColor.Green);
}

public static async Task Cleanup(IOpenAIService sdk)
Expand Down
Loading
Loading