Sdcb.SparkDesk 2.2.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Sdcb.SparkDesk --version 2.2.0                
NuGet\Install-Package Sdcb.SparkDesk -Version 2.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sdcb.SparkDesk" Version="2.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Sdcb.SparkDesk --version 2.2.0                
#r "nuget: Sdcb.SparkDesk, 2.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Sdcb.SparkDesk as a Cake Addin
#addin nuget:?package=Sdcb.SparkDesk&version=2.2.0

// Install Sdcb.SparkDesk as a Cake Tool
#tool nuget:?package=Sdcb.SparkDesk&version=2.2.0                

Sdcb.SparkDesk NuGet NuGet GitHub

English | 简体中文

Sdcb.SparkDesk是一个非官方的开源项目,提供SparkDesk WebSocket API (https://console.xfyun.cn/services/cbm) 的 .NET 客户端。现在已支持ModelVersion,用户可以选择包括V2模型在内的各种版本模型。上游文档地址:https://www.xfyun.cn/doc/spark/Guide.html

这个项目可以用来开发能够用自然语言与用户交流的聊天机器人和虚拟助手。

功能

  • 为 SparkDesk API 提供 .NET 客户端。
  • 支持同步和异步通信。
  • 实现了流API,以实现实时通信。
  • 为聊天机器人开发提供了简单直观的API。
  • 支持 ModelVersion,允许用户在不同版本的模型中选择。

安装

通过NuGet可以安装 Sdcb.SparkDesk。 在程序包管理器控制台中运行以下命令以安装软件包:

Install-Package Sdcb.SparkDesk

使用方法

要使用 Sdcb.SparkDesk,您需要创建一个 SparkDeskClient 类实例。您可以通过将SparkDesk API凭据传递给构造函数来创建客户端:

SparkDeskClient client = new SparkDeskClient(appId, apiKey, apiSecret);

示例1:与虚拟助手聊天(V1模型)

以下示例显示了如何使用 ChatAsync 方法与虚拟助手聊天:

SparkDeskClient client = new SparkDeskClient(appId, apiKey, apiSecret);
ChatResponse response = await client.ChatAsync(ModelVersion.V1_5, new ChatMessage[] 
{
    ChatMessage.FromUser("系统提示:你叫张三,一名5岁男孩,你在金色摇篮幼儿园上学,你的妈妈叫李四,是一名工程师"),
    ChatMessage.FromUser("你好小朋友,我是周老师,你在哪上学?"),
});
Console.WriteLine(response.Text);

示例2:使用流API与虚拟助手聊天(V2模型)

以下示例显示了如何使用 ChatAsStreamAsync 方法和V2模型以及流API与虚拟助手聊天:

SparkDeskClient client = new SparkDeskClient(appId, apiKey, apiSecret);
await foreach (StreamedChatResponse msg in client.ChatAsStreamAsync(ModelVersion.V2, new ChatMessage[] { ChatMessage.FromUser("湖南的省会在哪?") }, new ChatRequestParameters
{
    ChatId = "test",
    MaxTokens = 20,
    Temperature = 0.5f,
    TopK = 4,
}))
{
    Console.WriteLine(msg);
}

示例3:使用流API和回调与虚拟助手聊天 (V3模型)

以下示例显示了如何使用 ChatAsStreamAsync 方法以及V3模型、流API和回调与虚拟助手聊天:

SparkDeskClient client = new SparkDeskClient(appId, apiKey, apiSecret);
StringBuilder sb = new();
TokensUsage usage = await client.ChatAsStreamAsync(ModelVersion.V3, new ChatMessage[] 
{ 
    ChatMessage.FromUser("1+1=?"),
    ChatMessage.FromAssistant("1+1=3"),
    ChatMessage.FromUser("不对啊,请再想想?")
}, s => sb.Append(s), uid: "zhoujie");

string realResponse = sb.ToString();
Console.WriteLine(realResponse);

示例4:使用流API的控制台聊天机器人(V1.5模型)

以下示例显示了如何跟踪会话历史并使用流API与V1.5模型的虚拟助手聊天:

SparkDeskClient client = new SparkDeskClient(appId, apiKey, apiSecret);

List<ChatMessage> conversation = new List<ChatMessage>();
while (true)
{
    string? prompt = Console.ReadLine();
    if (prompt == null || prompt == "q")
    {
        break;
    }

    conversation.Add(ChatMessage.FromUser(prompt));
    Console.WriteLine($"> {prompt}");
    
    StringBuilder resp = new StringBuilder();
    await foreach (string c in client.ChatAsStreamAsync(ModelVersion.V1_5, conversation.ToArray()))
    {
        resp.Append(resp);
        Console.Write(c);
    }
    Console.WriteLine();
    conversation.Add(ChatMessage.FromAssistant(resp.ToString()));
}

许可证

Sdcb.SparkDesk 遵循 MIT 许可证。 请参阅LICENSE.txt文件以获取更多信息。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Sdcb.SparkDesk:

Package Downloads
AIDotNet.SparkDesk

AI DotNet API

BotSharp.Plugin.SparkDesk

Package Description

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Sdcb.SparkDesk:

Repository Stars
SciSharp/BotSharp
AI Multi-Agent Framework in .NET
maker-community/ElectronBot.DotNet
一个为ElectronBot机器人和瀚文键盘(HelloWorldKeyboard)开发的上位机软件(包含机器人USB操作SDK和瀚文键盘HID操作SDK),采用Windows App SDK框架编写界面。 A host computer software developed for ElectronBot robots (including robot USB operation SDK), using the Windows App SDK framework to write UI.
Version Downloads Last updated
3.1.0 1,843 6/28/2024
3.0.0 3,303 3/9/2024
2.3.0 143 1/30/2024
2.2.0 488 10/26/2023
2.1.0 151 10/24/2023
2.0.0 204 8/19/2023
1.1.0 158 6/20/2023