Skip to content

Commit 8c60e8a

Browse files
authored
Merge pull request #106 from cnblogs/support-cosy-voice
feat: support cosy voice
2 parents 3693cbb + 2ac8372 commit 8c60e8a

File tree

73 files changed

+2622
-118
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2622
-118
lines changed

Cnblogs.DashScope.Sdk.sln.DotSettings

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002EAI_002EUnitTests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
23
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002ESample_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
34
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002ESdk_002ESnapshotGenerator_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
4-
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002ESdk_002EUnitTests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
5+
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002ESdk_002EUnitTests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
6+
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002ETests_002EShared_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
7+
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002EWebSample_002EClient_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
8+
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Cnblogs_002EDashScope_002EWebSample_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
<IsPackable>false</IsPackable>
9-
</PropertyGroup>
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
1010

11-
<ItemGroup>
12-
<ProjectReference Include="..\..\src\Cnblogs.DashScope.Sdk\Cnblogs.DashScope.Sdk.csproj" />
13-
<ProjectReference Include="..\..\src\Cnblogs.DashScope.AI\Cnblogs.DashScope.AI.csproj" />
14-
</ItemGroup>
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\Cnblogs.DashScope.Sdk\Cnblogs.DashScope.Sdk.csproj"/>
13+
<ProjectReference Include="..\..\src\Cnblogs.DashScope.AI\Cnblogs.DashScope.AI.csproj"/>
14+
</ItemGroup>
1515

16-
<ItemGroup>
17-
<None Update="test.txt">
18-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19-
</None>
20-
</ItemGroup>
16+
<ItemGroup>
17+
<None Update="test.txt">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
2121

22-
<ItemGroup>
23-
<PackageReference Include="Microsoft.Extensions.AI" Version="9.5.0" />
24-
</ItemGroup>
22+
<ItemGroup>
23+
<PackageReference Include="Microsoft.Extensions.AI" Version="9.5.0"/>
24+
</ItemGroup>
2525

2626
</Project>

sample/Cnblogs.DashScope.Sample/Program.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
using Microsoft.Extensions.AI;
1010

1111
Console.WriteLine("Reading key from environment variable DASHSCOPE_KEY");
12-
var apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_API_KEY");
12+
var apiKey = Environment.GetEnvironmentVariable("DASHSCOPE_KEY", EnvironmentVariableTarget.Process)
13+
?? Environment.GetEnvironmentVariable("DASHSCOPE_KEY", EnvironmentVariableTarget.User);
1314
if (string.IsNullOrEmpty(apiKey))
1415
{
1516
Console.Write("ApiKey > ");
@@ -63,6 +64,35 @@
6364
userInput = Console.ReadLine()!;
6465
await ApplicationCallAsync(applicationId, userInput);
6566
break;
67+
case SampleType.TextToSpeech:
68+
{
69+
using var tts = await dashScopeClient.CreateSpeechSynthesizerSocketSessionAsync("cosyvoice-v2");
70+
var taskId = await tts.RunTaskAsync(
71+
new SpeechSynthesizerParameters { Voice = "longxiaochun_v2", Format = "mp3" });
72+
await tts.ContinueTaskAsync(taskId, "博客园");
73+
await tts.ContinueTaskAsync(taskId, "代码改变世界");
74+
await tts.FinishTaskAsync(taskId);
75+
var file = new FileInfo("tts.mp3");
76+
var writer = file.OpenWrite();
77+
await foreach (var b in tts.GetAudioAsync())
78+
{
79+
writer.WriteByte(b);
80+
}
81+
82+
writer.Close();
83+
84+
var tokenUsage = 0;
85+
await foreach (var message in tts.GetMessagesAsync())
86+
{
87+
if (message.Payload.Usage?.Characters > tokenUsage)
88+
{
89+
tokenUsage = message.Payload.Usage.Characters;
90+
}
91+
}
92+
93+
Console.WriteLine($"audio saved to {file.FullName}, token usage: {tokenUsage}");
94+
break;
95+
}
6696
}
6797

6898
return;

sample/Cnblogs.DashScope.Sample/SampleType.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ public enum SampleType
1616

1717
MicrosoftExtensionsAiToolCall,
1818

19-
ApplicationCall
19+
ApplicationCall,
20+
21+
TextToSpeech,
2022
}

sample/Cnblogs.DashScope.Sample/SampleTypeDescriptor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static string GetDescription(this SampleType sampleType)
1414
SampleType.MicrosoftExtensionsAi => "Use with Microsoft.Extensions.AI",
1515
SampleType.MicrosoftExtensionsAiToolCall => "Use tool call with Microsoft.Extensions.AI interfaces",
1616
SampleType.ApplicationCall => "Call pre-defined application",
17+
SampleType.TextToSpeech => "TTS task",
1718
_ => throw new ArgumentOutOfRangeException(nameof(sampleType), sampleType, "Unsupported sample option")
1819
};
1920
}

src/Cnblogs.DashScope.AI/Cnblogs.DashScope.AI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="JsonSchema.Net.Generation" Version="5.0.2" />
14+
<PackageReference Include="JsonSchema.Net.Generation" Version="5.0.3" />
1515
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.5.0" />
1616
</ItemGroup>
1717

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly:InternalsVisibleTo("Cnblogs.DashScope.Sdk.UnitTests")]
4+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

src/Cnblogs.DashScope.AspNetCore/Cnblogs.DashScope.AspNetCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<PropertyGroup>
44
<Product>Cnblogs.DashScopeSDK</Product>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6-
<PackageTags>Cnblogs;Dashscope;AI;Sdk;Embedding;AspNetCore</PackageTags>
6+
<PackageTags>Cnblogs;Dashscope;AI;Sdk;Embedding;AspNetCore;Bailian</PackageTags>
7+
<RootNamespace>Cnblogs.DashScope.AspNetCore</RootNamespace>
78
</PropertyGroup>
89

910
<ItemGroup>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Cnblogs.DashScope.AspNetCore;
2+
3+
internal static class DashScopeAspNetCoreDefaults
4+
{
5+
public const string DefaultHttpClientName = "Cnblogs.DashScope.Http";
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Cnblogs.DashScope.Core;
2+
3+
namespace Cnblogs.DashScope.AspNetCore;
4+
5+
/// <summary>
6+
/// The <see cref="DashScopeClientCore"/> with DI and options pattern support.
7+
/// </summary>
8+
public class DashScopeClientAspNetCore
9+
: DashScopeClientCore
10+
{
11+
/// <summary>
12+
/// The <see cref="DashScopeClientCore"/> with DI and options pattern support.
13+
/// </summary>
14+
/// <param name="factory">The factory to create <see cref="HttpClient"/>.</param>
15+
/// <param name="pool">The socket pool for WebSocket API calls.</param>
16+
public DashScopeClientAspNetCore(IHttpClientFactory factory, DashScopeClientWebSocketPool pool)
17+
: base(factory.CreateClient(DashScopeAspNetCoreDefaults.DefaultHttpClientName), pool)
18+
{
19+
}
20+
}

0 commit comments

Comments
 (0)