From c415864fab61f6bd924dd5ba2e6ac85473816081 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Wed, 13 Aug 2025 21:02:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OllamaStudy.UseExtensionsAI/OpenAISdkTest.cs | 96 ++++++-------------- 1 file changed, 28 insertions(+), 68 deletions(-) diff --git a/OllamaStudy.UseExtensionsAI/OpenAISdkTest.cs b/OllamaStudy.UseExtensionsAI/OpenAISdkTest.cs index bb9ec0e..cbadcd2 100644 --- a/OllamaStudy.UseExtensionsAI/OpenAISdkTest.cs +++ b/OllamaStudy.UseExtensionsAI/OpenAISdkTest.cs @@ -20,7 +20,6 @@ public class OpenAISdkTest //使用了FromKeyedServices特性,所以需要使用IKeyedServiceCollection注册服务 [FromKeyedServices("OpenAIChatClient")]ChatClient singtonChatClient, - [FromKeyedServices("UiUiAPIClient")]OpenAIClient uiUiApiClient, [FromKeyedServices("ZipuAPIClient")]OpenAIClient zipuApiClient ) @@ -176,6 +175,21 @@ public class OpenAISdkTest _output.WriteLine(responseText); Assert.NotNull(result); } + + /// + /// 键控服务的 + /// 使用[FromKeyedServices("ZipuAPIClient")]特性(具体看构造函数) + /// + [Fact] + public void KeyedService_Singleton_ChatClient_Test() + { + ChatClient chat = _zipuApiClient.GetChatClient("GLM-4.5-Flash"); + ClientResult clientResult = chat.CompleteChat("你好,你是谁?"); + + var responseText = clientResult.Value.Content.First().Text; + _output.WriteLine(responseText); + Assert.NotNull(clientResult); + } #endregion #region 如何将聊天完成与流式处理一起使用 @@ -410,17 +424,11 @@ public class OpenAISdkTest /// /// 生成语音 /// - //[Fact] - [Fact(Skip ="因本地Ollama测试环境,不支持OpenAI音频接口,忽略测试")] - //[Fact] + [Fact] + //[Fact(Skip ="因本地Ollama测试环境,不支持OpenAI音频接口,忽略测试")] public void GenerateSpeech_AudioClient_Test() { - var aiClientOption = new OpenAIClientOptions() - { - Endpoint = new Uri("https://sg.uiuiapi.com/v1") - }; - - AudioClient client = new("tts-1-1106", new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), aiClientOption); + AudioClient client = _uiUiApiClient.GetAudioClient("tts-1-1106"); string input = """ 对于那些照顾室内植物的人来说,过度浇水是一个常见的问题。 @@ -439,16 +447,11 @@ public class OpenAISdkTest /// /// 语音转文本 /// - [Fact(Skip ="因本地Ollama测试环境,不支持OpenAI音频接口,忽略测试")] //[Fact] + [Fact(Skip ="因本地Ollama测试环境,不支持OpenAI音频接口,忽略测试")] public void AudioToText_AudioClient_Test() { - var aiClientOption = new OpenAIClientOptions() - { - Endpoint = new Uri("https://sg.uiuiapi.com/v1") - }; - - AudioClient client = new("whisper-1", new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), aiClientOption); + AudioClient client = _uiUiApiClient.GetAudioClient("whisper-1"); string audioFilePath = Path.Combine(Environment.CurrentDirectory, "Assets", "yuxia.mp3"); @@ -472,15 +475,7 @@ public class OpenAISdkTest public async Task Respones_With_FileSearch_Test() { #pragma warning disable OPENAI001 - OpenAIResponseClient client = new - ( - "gpt-4o-mini", - new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), - new OpenAIClientOptions() - { - Endpoint = new Uri("https://sg.uiuiapi.com/v1") - } - ); + OpenAIResponseClient client = _uiUiApiClient.GetOpenAIResponseClient("gpt-4o-mini"); //已经存在的文件向量IDS(提前将文件向量化) ResponseTool fileSearchTool = ResponseTool.CreateFileSearchTool(vectorStoreIds: ["sssssssss"]); @@ -519,15 +514,7 @@ public class OpenAISdkTest public async Task WebSearch_ChatClient_Test() { #pragma warning disable OPENAI001 - OpenAIResponseClient client = new - ( - "gpt-4o-mini", - new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), - new OpenAIClientOptions() - { - Endpoint = new Uri("https://sg.uiuiapi.com/v1") - } - ); + OpenAIResponseClient client = _uiUiApiClient.GetOpenAIResponseClient("gpt-4o-mini"); OpenAIResponse response = await client.CreateResponseAsync ( @@ -575,15 +562,7 @@ public class OpenAISdkTest //[Fact] public void Image_ChatClient_Test() { - ImageClient imageClient = new - ( - "dall-e-3", - new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), - new OpenAIClientOptions() - { - Endpoint = new Uri("https://sg.uiuiapi.com/v1") - } - ); + ImageClient imageClient = _uiUiApiClient.GetImageClient("dall-e-3"); string prompt = """ 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 accents, adding contrast and texture. """; - prompt = """ - 画一个中国古代的将军。身披铠甲,手持长剑,眼神坚定,表情严肃,看起来很威严。国字脸,在战场上指挥作战! - """; ImageGenerationOptions options = new() { @@ -618,16 +594,7 @@ public class OpenAISdkTest //[Fact] public void Tranacribe_Audio_AudioClient_Test() { - AudioClient client = new - ( - "whisper-1", - new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L"), - new OpenAIClientOptions() - { - Endpoint = new Uri("https://sg.uiuiapi.com/v1") - } - ); - + AudioClient client = _uiUiApiClient.GetAudioClient("whisper-1"); string audioFilePath = Path.Combine("Assets", "dongdong.mp3"); AudioTranscriptionOptions options = new() @@ -673,9 +640,8 @@ public class OpenAISdkTest Endpoint = new Uri("https://sg.uiuiapi.com/v1"), }; - OpenAIClient openAIClient = new(new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L")); - OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient(); - AssistantClient assistantClient = openAIClient.GetAssistantClient(); + OpenAIFileClient fileClient = _uiUiApiClient.GetOpenAIFileClient(); + AssistantClient assistantClient = _uiUiApiClient.GetAssistantClient(); using Stream document = BinaryData.FromBytes(""" { @@ -833,14 +799,8 @@ public class OpenAISdkTest public void Streaming_Vision_AssistantsClient() { #pragma warning disable OPENAI001 - OpenAIClientOptions clientOptions = new() - { - Endpoint = new Uri("https://sg.uiuiapi.com/v1"), - }; - - OpenAIClient openAIClient = new(new ApiKeyCredential("sk-4azuOUkbzNGP22pQkND8ad1vZl7ladwBQyqGKlWWZyxYgX1L")); - OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient(); - AssistantClient assistantClient = openAIClient.GetAssistantClient(); + OpenAIFileClient fileClient = _uiUiApiClient.GetOpenAIFileClient(); + AssistantClient assistantClient = _uiUiApiClient.GetAssistantClient(); OpenAIFile pictureOfAppleFile = fileClient.UploadFile (