main
bicijinlian 4 days ago
parent bdb050c9d6
commit c415864fab

@ -20,7 +20,6 @@ public class OpenAISdkTest
//使用了FromKeyedServices特性所以需要使用IKeyedServiceCollection注册服务 //使用了FromKeyedServices特性所以需要使用IKeyedServiceCollection注册服务
[FromKeyedServices("OpenAIChatClient")]ChatClient singtonChatClient, [FromKeyedServices("OpenAIChatClient")]ChatClient singtonChatClient,
[FromKeyedServices("UiUiAPIClient")]OpenAIClient uiUiApiClient, [FromKeyedServices("UiUiAPIClient")]OpenAIClient uiUiApiClient,
[FromKeyedServices("ZipuAPIClient")]OpenAIClient zipuApiClient [FromKeyedServices("ZipuAPIClient")]OpenAIClient zipuApiClient
) )
@ -176,6 +175,21 @@ public class OpenAISdkTest
_output.WriteLine(responseText); _output.WriteLine(responseText);
Assert.NotNull(result); Assert.NotNull(result);
} }
/// <summary>
/// 键控服务的
/// 使用[FromKeyedServices("ZipuAPIClient")]特性(具体看构造函数)
/// </summary>
[Fact]
public void KeyedService_Singleton_ChatClient_Test()
{
ChatClient chat = _zipuApiClient.GetChatClient("GLM-4.5-Flash");
ClientResult<ChatCompletion> clientResult = chat.CompleteChat("你好,你是谁?");
var responseText = clientResult.Value.Content.First().Text;
_output.WriteLine(responseText);
Assert.NotNull(clientResult);
}
#endregion #endregion
#region 如何将聊天完成与流式处理一起使用 #region 如何将聊天完成与流式处理一起使用
@ -410,17 +424,11 @@ public class OpenAISdkTest
/// <summary> /// <summary>
/// 生成语音 /// 生成语音
/// </summary> /// </summary>
//[Fact] [Fact]
[Fact(Skip ="因本地Ollama测试环境不支持OpenAI音频接口忽略测试")] //[Fact(Skip ="因本地Ollama测试环境不支持OpenAI音频接口忽略测试")]
//[Fact]
public void GenerateSpeech_AudioClient_Test() public void GenerateSpeech_AudioClient_Test()
{ {
var aiClientOption = new OpenAIClientOptions() AudioClient client = _uiUiApiClient.GetAudioClient("tts-1-1106");
{
Endpoint = new Uri("https://sg.uiuiapi.com/v1")
};
AudioClient client = new("tts-1-1106", new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), aiClientOption);
string input = """ string input = """
@ -439,16 +447,11 @@ public class OpenAISdkTest
/// <summary> /// <summary>
/// 语音转文本 /// 语音转文本
/// </summary> /// </summary>
[Fact(Skip ="因本地Ollama测试环境不支持OpenAI音频接口忽略测试")]
//[Fact] //[Fact]
[Fact(Skip ="因本地Ollama测试环境不支持OpenAI音频接口忽略测试")]
public void AudioToText_AudioClient_Test() public void AudioToText_AudioClient_Test()
{ {
var aiClientOption = new OpenAIClientOptions() AudioClient client = _uiUiApiClient.GetAudioClient("whisper-1");
{
Endpoint = new Uri("https://sg.uiuiapi.com/v1")
};
AudioClient client = new("whisper-1", new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), aiClientOption);
string audioFilePath = Path.Combine(Environment.CurrentDirectory, "Assets", "yuxia.mp3"); string audioFilePath = Path.Combine(Environment.CurrentDirectory, "Assets", "yuxia.mp3");
@ -472,15 +475,7 @@ public class OpenAISdkTest
public async Task Respones_With_FileSearch_Test() public async Task Respones_With_FileSearch_Test()
{ {
#pragma warning disable OPENAI001 #pragma warning disable OPENAI001
OpenAIResponseClient client = new OpenAIResponseClient client = _uiUiApiClient.GetOpenAIResponseClient("gpt-4o-mini");
(
"gpt-4o-mini",
new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"),
new OpenAIClientOptions()
{
Endpoint = new Uri("https://sg.uiuiapi.com/v1")
}
);
//已经存在的文件向量IDS(提前将文件向量化) //已经存在的文件向量IDS(提前将文件向量化)
ResponseTool fileSearchTool = ResponseTool.CreateFileSearchTool(vectorStoreIds: ["sssssssss"]); ResponseTool fileSearchTool = ResponseTool.CreateFileSearchTool(vectorStoreIds: ["sssssssss"]);
@ -519,15 +514,7 @@ public class OpenAISdkTest
public async Task WebSearch_ChatClient_Test() public async Task WebSearch_ChatClient_Test()
{ {
#pragma warning disable OPENAI001 #pragma warning disable OPENAI001
OpenAIResponseClient client = new OpenAIResponseClient client = _uiUiApiClient.GetOpenAIResponseClient("gpt-4o-mini");
(
"gpt-4o-mini",
new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"),
new OpenAIClientOptions()
{
Endpoint = new Uri("https://sg.uiuiapi.com/v1")
}
);
OpenAIResponse response = await client.CreateResponseAsync OpenAIResponse response = await client.CreateResponseAsync
( (
@ -575,15 +562,7 @@ public class OpenAISdkTest
//[Fact] //[Fact]
public void Image_ChatClient_Test() public void Image_ChatClient_Test()
{ {
ImageClient imageClient = new ImageClient imageClient = _uiUiApiClient.GetImageClient("dall-e-3");
(
"dall-e-3",
new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"),
new OpenAIClientOptions()
{
Endpoint = new Uri("https://sg.uiuiapi.com/v1")
}
);
string prompt = """ string prompt = """
The concept for a living room that blends Scandinavian simplicity with Japanese minimalism for a serene and cozy atmosphere. The concept for a living room that blends Scandinavian simplicity with Japanese minimalism for a serene and cozy atmosphere.
@ -593,9 +572,6 @@ public class OpenAISdkTest
They can serve as focal points, creating a connection with nature. Soft textiles and cushions in organic fabrics adding comfort and softness to a space. They can serve as focal points, creating a connection with nature. Soft textiles and cushions in organic fabrics adding comfort and softness to a space.
They can serve as accents, adding contrast and texture. They can serve as accents, adding contrast and texture.
"""; """;
prompt = """
""";
ImageGenerationOptions options = new() ImageGenerationOptions options = new()
{ {
@ -618,16 +594,7 @@ public class OpenAISdkTest
//[Fact] //[Fact]
public void Tranacribe_Audio_AudioClient_Test() public void Tranacribe_Audio_AudioClient_Test()
{ {
AudioClient client = new AudioClient client = _uiUiApiClient.GetAudioClient("whisper-1");
(
"whisper-1",
new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"),
new OpenAIClientOptions()
{
Endpoint = new Uri("https://sg.uiuiapi.com/v1")
}
);
string audioFilePath = Path.Combine("Assets", "dongdong.mp3"); string audioFilePath = Path.Combine("Assets", "dongdong.mp3");
AudioTranscriptionOptions options = new() AudioTranscriptionOptions options = new()
@ -673,9 +640,8 @@ public class OpenAISdkTest
Endpoint = new Uri("https://sg.uiuiapi.com/v1"), Endpoint = new Uri("https://sg.uiuiapi.com/v1"),
}; };
OpenAIClient openAIClient = new(new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L")); OpenAIFileClient fileClient = _uiUiApiClient.GetOpenAIFileClient();
OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient(); AssistantClient assistantClient = _uiUiApiClient.GetAssistantClient();
AssistantClient assistantClient = openAIClient.GetAssistantClient();
using Stream document = BinaryData.FromBytes(""" using Stream document = BinaryData.FromBytes("""
{ {
@ -833,14 +799,8 @@ public class OpenAISdkTest
public void Streaming_Vision_AssistantsClient() public void Streaming_Vision_AssistantsClient()
{ {
#pragma warning disable OPENAI001 #pragma warning disable OPENAI001
OpenAIClientOptions clientOptions = new() OpenAIFileClient fileClient = _uiUiApiClient.GetOpenAIFileClient();
{ AssistantClient assistantClient = _uiUiApiClient.GetAssistantClient();
Endpoint = new Uri("https://sg.uiuiapi.com/v1"),
};
OpenAIClient openAIClient = new(new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"));
OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient();
AssistantClient assistantClient = openAIClient.GetAssistantClient();
OpenAIFile pictureOfAppleFile = fileClient.UploadFile OpenAIFile pictureOfAppleFile = fileClient.UploadFile
( (

Loading…
Cancel
Save