Ohh.ChatAi.TotalApi
1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Ohh.ChatAi.TotalApi --version 1.0.2
NuGet\Install-Package Ohh.ChatAi.TotalApi -Version 1.0.2
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="Ohh.ChatAi.TotalApi" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ohh.ChatAi.TotalApi --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Ohh.ChatAi.TotalApi, 1.0.2"
#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 Ohh.ChatAi.TotalApi as a Cake Addin #addin nuget:?package=Ohh.ChatAi.TotalApi&version=1.0.2 // Install Ohh.ChatAi.TotalApi as a Cake Tool #tool nuget:?package=Ohh.ChatAi.TotalApi&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Ohh.ChatAi.TotalApi国内AI-API集大成者
公众号:办公魔盒
博客:https://bgmh.work
微信:vbee_club
简介
预计会集成国内大部分的chatAI聊天大模型,包括Moonshot-Kimi,文心一言,通义千问,DeepSeek,腾讯混元,智谱清言等
集成情况
目前已集成Moonshot-Kimi,DeepSeek模型
框架支持
框架 | 版本 |
---|---|
.NET Framework | >=net461 |
.NET | >=net5.0 |
.NET Core | >=netcoreapp2.0 |
.NET Standard | >=netstandard2.0 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | >=tizen40 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
1.已集成Kimi大模型
1.1.支持查询模型列表API
GetModelsListAsync
1.2.支持计算当前请求token数据
CalcTokenCountAsync
1.3.支持查询账户余额
GetBalanceAsync
1.4.普通聊天和流式聊天示例(支持多轮和单论对话)
vb.net示例
Imports System.IO
Imports System.Reflection
Imports System.Runtime.Remoting.Messaging
Imports Ohh.ChatAi.TotalApi.MoonshotKimi
Imports Ohh.ChatAi.TotalApi.MoonshotKimi.Models
Imports Ohh.ChatAi.TotalApi.MoonshotKimi.Models.Consts
Public Class Form1
''' <summary>
''' kimi对象
''' </summary>
Private _kimi As MoonshotClient
''' <summary>
''' 系统角色设定
''' </summary>
Private Const SysContent As String = "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言"
Public Sub New()
InitializeComponent()
''------------------
_kimi = New MoonshotClient("sk-xxxxxxxxxxxxxxxx")
End Sub
''' <summary>
''' 单论流式对话
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Async Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Try
richTextBox1.Clear()
''------
Dim chatReq As New ChatRequest With
{
.model = ChatModels.MoonshotV1By8K,
.messages = New List(Of MessagesItem) From {
New MessagesItem With {.role = ChatRoles.System, .content = SysContent},
New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text}
},
.stream = True
}
'' 订阅事件
RemoveHandler _kimi.MessageReceivedEventHandler, AddressOf OnMessageReceived
RemoveHandler _kimi.ErrorEventHandler, AddressOf OnErrorMsg
AddHandler _kimi.MessageReceivedEventHandler, AddressOf OnMessageReceived
AddHandler _kimi.ErrorEventHandler, AddressOf OnErrorMsg
'' 开始任务
Await _kimi.ChatStreamAsync(chatReq)
Catch ex As Exception
Console.WriteLine("流式聊天失败:" + ex.Message)
End Try
End Sub
''' <summary>
''' 订阅SSE事件
''' </summary>
''' <param name="s"></param>
''' <param name="m"></param>
Sub OnMessageReceived(s As Object, m As ChatResponse)
''Console.WriteLine("聊天内容:" + ee);
Invoke(Sub()
Dim msg As String = m.choices?.FirstOrDefault()?.delta.content
If Not String.IsNullOrWhiteSpace(msg) Then richTextBox1.AppendText(msg)
End Sub)
End Sub
''' <summary>
''' 局部函数,订阅SSE 错误事件
''' </summary>
Sub OnErrorMsg(s As Object, ex As Exception)
MessageBox.Show(ex.Message)
End Sub
''' <summary>
''' 单论普通对话
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Async Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
Try
Dim chatReq As New ChatRequest With
{
.model = ChatModels.MoonshotV1By8K,
.messages = New List(Of MessagesItem) From {
New MessagesItem With {.role = ChatRoles.System, .content = SysContent},
New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text}
}
}
Dim chatRes As ChatResponse = Await _kimi.ChatAsync(chatReq)
Console.WriteLine("聊天内容:" & chatRes.choices?.FirstOrDefault()?.message.content)
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content
Catch ex As Exception
Console.WriteLine("聊天失败:" & ex.Message)
End Try
End Sub
''' <summary>
''' 多轮对话列表
''' </summary>
Private HistoryList As New List(Of MessagesItem) From {New MessagesItem With {.role = ChatRoles.System, .content = SysContent}}
''' <summary>
''' 多轮流式对话
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Async Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
Try
richTextBox1.Clear()
''------当前要说的内容
HistoryList.Add(New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text})
''------
Dim chatReq As New ChatRequest With
{
.model = ChatModels.MoonshotV1By8K,
.messages = HistoryList,
.stream = True
}
'' 订阅事件
RemoveHandler _kimi.MessageReceivedEventHandler, AddressOf OnMessageReceived
RemoveHandler _kimi.ErrorEventHandler, AddressOf OnErrorMsg
RemoveHandler _kimi.CloseedEventHandler, AddressOf OnCloseed
AddHandler _kimi.MessageReceivedEventHandler, AddressOf OnMessageReceived
AddHandler _kimi.ErrorEventHandler, AddressOf OnErrorMsg
AddHandler _kimi.CloseedEventHandler, AddressOf OnCloseed
'' 开始任务
Await _kimi.ChatStreamAsync(chatReq)
Catch ex As Exception
Console.WriteLine("流式聊天失败:" + ex.Message)
End Try
End Sub
''' <summary>
''' 订阅SSE 关闭事件
''' </summary>
''' <param name="s"></param>
''' <param name="ex"></param>
Sub OnCloseed(s As Object, ex As String)
HistoryList.Add(New MessagesItem With {.role = ChatRoles.Assistant, .content = textBox1.Text})
End Sub
''' <summary>
''' 多轮普通对话
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Async Sub button4_Click(sender As Object, e As EventArgs) Handles button4.Click
Try
''------当前要说的内容
HistoryList.Add(New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text})
''---------------
Dim chatReq As New ChatRequest With
{
.model = ChatModels.MoonshotV1By8K,
.messages = HistoryList
}
Dim chatRes As ChatResponse = Await _kimi.ChatAsync(chatReq)
Console.WriteLine("聊天内容:" & chatRes.choices?.FirstOrDefault()?.message.content)
HistoryList.Add(New MessagesItem With {.role = ChatRoles.Assistant, .content = textBox1.Text})
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content
Catch ex As Exception
Console.WriteLine("聊天失败:" & ex.Message)
End Try
End Sub
End Class
c# 示例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ohh.ChatAi.TotalApi.MoonshotKimi;
using Ohh.ChatAi.TotalApi.MoonshotKimi.Models;
using Ohh.ChatAi.TotalApi.MoonshotKimi.Models.Consts;
using System.Windows.Forms;
namespace Ohh.ChatAi.TotalApi.CSharp.Example
{
public partial class Form1 : Form
{
/// <summary>
/// kimi对象
/// </summary>
private MoonshotClient _kimi;
/// <summary>
/// 系统角色设定
/// </summary>
private const string SysContent = "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言";
public Form1()
{
InitializeComponent();
//---------
_kimi = new MoonshotClient("sk-xxxxxxxxxxxxxxxxxxx");
}
/// <summary>
/// 单轮流式聊天
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button1_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Clear();
//------
var chatReq = new ChatRequest
{
model = ChatModels.MoonshotV1By8K,
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
},
stream = true
};
// 订阅事件
_kimi.MessageReceivedEventHandler -= OnMessageReceived;
_kimi.ErrorEventHandler -= OnErrorMsg;
_kimi.MessageReceivedEventHandler += OnMessageReceived;
_kimi.ErrorEventHandler += OnErrorMsg;
// 开始任务
await _kimi.ChatStreamAsync(chatReq);
// 局部函数,订阅SSE 消息事件
void OnMessageReceived(object s, ChatResponse m)
{
//Console.WriteLine("聊天内容:" + ee);
Invoke(new Action(() =>
{
var msg = m.choices?.FirstOrDefault()?.delta.content;
if (msg != null) richTextBox1.AppendText(msg);
}));
}
// 局部函数,订阅SSE 错误事件
void OnErrorMsg(object s, Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine("流式聊天失败:" + ex.Message);
}
}
/// <summary>
/// 单轮普通聊天
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button2_Click(object sender, EventArgs e)
{
try
{
var chatReq = new ChatRequest
{
model = ChatModels.MoonshotV1By8K,
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
}
};
var chatRes = await _kimi.ChatAsync(chatReq);
Console.WriteLine("聊天内容:" + chatRes.choices?.FirstOrDefault()?.message.content);
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content;
}
catch (Exception ex)
{
Console.WriteLine("聊天失败:" + ex.Message);
}
}
/// <summary>
/// 多轮对话列表
/// </summary>
private List<MessagesItem> HistoryList = new List<MessagesItem> { new MessagesItem { role = ChatRoles.System, content = SysContent } };
/// <summary>
/// 多轮流式对话
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button3_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Clear();
//------当前要说的内容
HistoryList.Add(new MessagesItem { role = ChatRoles.User, content = textBox1.Text });
//------
var chatReq = new ChatRequest
{
model = ChatModels.MoonshotV1By8K,
messages = HistoryList,
stream = true
};
// 订阅事件
_kimi.MessageReceivedEventHandler -= OnMessageReceived;
_kimi.ErrorEventHandler -= OnErrorMsg;
_kimi.CloseedEventHandler -= OnCloseed;
_kimi.MessageReceivedEventHandler += OnMessageReceived;
_kimi.ErrorEventHandler += OnErrorMsg;
_kimi.CloseedEventHandler += OnCloseed;
// 开始任务
await _kimi.ChatStreamAsync(chatReq);
// 局部函数,订阅SSE 消息事件
void OnMessageReceived(object s, ChatResponse m)
{
//Console.WriteLine("聊天内容:" + ee);
Invoke(new Action(() =>
{
var msg = m.choices?.FirstOrDefault()?.delta.content;
if (msg != null)
{
richTextBox1.AppendText(msg);
}
}));
}
// 局部函数,订阅SSE 错误事件
void OnErrorMsg(object s, Exception ex)
{
MessageBox.Show(ex.Message);
}
// 局部函数,订阅SSE 关闭事件
void OnCloseed(object s, string ex)
{
HistoryList.Add(new MessagesItem { role = ChatRoles.Assistant, content = textBox1.Text });
}
}
catch (Exception ex)
{
Console.WriteLine("流式聊天失败:" + ex.Message);
}
}
/// <summary>
/// 多轮普通聊天
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button4_Click(object sender, EventArgs e)
{
try
{
//------当前要说的内容
HistoryList.Add(new MessagesItem { role = ChatRoles.User, content = textBox1.Text });
//-------------------
var chatReq = new ChatRequest
{
model = ChatModels.MoonshotV1By8K,
messages = HistoryList
};
var chatRes = await _kimi.ChatAsync(chatReq);
HistoryList.Add(new MessagesItem { role = ChatRoles.Assistant, content = textBox1.Text });
Console.WriteLine("聊天内容:" + chatRes.choices?.FirstOrDefault()?.message.content);
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content;
}
catch (Exception ex)
{
Console.WriteLine("聊天失败:" + ex.Message);
}
}
}
}
2.已集成DeepSeek大模型
2.1.支持查询模型列表API
GetModelsListAsync
2.2.支持查询账户余额
GetBalanceAsync
2.3.普通聊天和流式聊天示例(支持多轮和单论对话)
vb.net示例
Imports Ohh.ChatAi.TotalApi.DeepSeek
Imports Ohh.ChatAi.TotalApi.DeepSeek.Models
Imports Ohh.ChatAi.TotalApi.DeepSeek.Models.Consts
Imports Ohh.ChatAi.TotalApi.MoonshotKimi
Public Class DeepSeekExample
''' <summary>
''' DeepSeek对象
''' </summary>
Private _deepSeekClient As DeepSeekClient
''' <summary>
''' 系统角色设定
''' </summary>
Private Const SysContent As String = "你是 DeepSeek AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言"
Public Sub New()
InitializeComponent()
''------------------
_deepSeekClient = New DeepSeekClient("sk-xxxxxxxxxxxxxxx")
End Sub
Private Sub DeepSeekExample_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
''' <summary>
''' 单论流式对话
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Async Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Try
richTextBox1.Clear()
''------
Dim chatReq As New ChatRequest With
{
.model = ChatModels.DeepseekChat,
.messages = New List(Of MessagesItem) From {
New MessagesItem With {.role = ChatRoles.System, .content = SysContent},
New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text}
},
.stream = True
}
'' 订阅事件
RemoveHandler _deepSeekClient.MessageReceivedEventHandler, AddressOf OnMessageReceived
RemoveHandler _deepSeekClient.ErrorEventHandler, AddressOf OnErrorMsg
AddHandler _deepSeekClient.MessageReceivedEventHandler, AddressOf OnMessageReceived
AddHandler _deepSeekClient.ErrorEventHandler, AddressOf OnErrorMsg
'' 开始任务
Await _deepSeekClient.ChatStreamAsync(chatReq)
Catch ex As Exception
Console.WriteLine("流式聊天失败:" + ex.Message)
End Try
End Sub
''' <summary>
''' 订阅SSE事件
''' </summary>
''' <param name="s"></param>
''' <param name="m"></param>
Sub OnMessageReceived(s As Object, m As ChatResponse)
''Console.WriteLine("聊天内容:" + ee);
Invoke(Sub()
Dim msg As String = m.choices?.FirstOrDefault()?.delta.content
If Not String.IsNullOrWhiteSpace(msg) Then richTextBox1.AppendText(msg.Replace("vbLf", vbCrLf))
End Sub)
End Sub
''' <summary>
''' 局部函数,订阅SSE 错误事件
''' </summary>
Sub OnErrorMsg(s As Object, ex As Exception)
MessageBox.Show(ex.Message)
End Sub
''' <summary>
''' 单论普通对话
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Async Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
Try
Dim chatReq As New ChatRequest With
{
.model = ChatModels.DeepseekChat,
.messages = New List(Of MessagesItem) From {
New MessagesItem With {.role = ChatRoles.System, .content = SysContent},
New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text}
}
}
Dim chatRes As ChatResponse = Await _deepSeekClient.ChatAsync(chatReq)
Console.WriteLine("聊天内容:" & chatRes.choices?.FirstOrDefault()?.message.content)
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content.Replace("vbLf", vbCrLf)
Catch ex As Exception
Console.WriteLine("聊天失败:" & ex.Message)
End Try
End Sub
''' <summary>
''' 多轮对话列表
''' </summary>
Private HistoryList As New List(Of MessagesItem) From {New MessagesItem With {.role = ChatRoles.System, .content = SysContent}}
''' <summary>
''' 多轮流式对话
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Async Sub button3_Click(sender As Object, e As EventArgs) Handles button3.Click
Try
richTextBox1.Clear()
''------当前要说的内容
HistoryList.Add(New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text})
''------
Dim chatReq As New ChatRequest With
{
.model = ChatModels.DeepseekChat,
.messages = HistoryList,
.stream = True
}
'' 订阅事件
RemoveHandler _deepSeekClient.MessageReceivedEventHandler, AddressOf OnMessageReceived
RemoveHandler _deepSeekClient.ErrorEventHandler, AddressOf OnErrorMsg
RemoveHandler _deepSeekClient.CloseedEventHandler, AddressOf OnCloseed
AddHandler _deepSeekClient.MessageReceivedEventHandler, AddressOf OnMessageReceived
AddHandler _deepSeekClient.ErrorEventHandler, AddressOf OnErrorMsg
AddHandler _deepSeekClient.CloseedEventHandler, AddressOf OnCloseed
'' 开始任务
Await _deepSeekClient.ChatStreamAsync(chatReq)
Catch ex As Exception
Console.WriteLine("流式聊天失败:" + ex.Message)
End Try
End Sub
''' <summary>
''' 订阅SSE 关闭事件
''' </summary>
''' <param name="s"></param>
''' <param name="ex"></param>
Sub OnCloseed(s As Object, ex As String)
HistoryList.Add(New MessagesItem With {.role = ChatRoles.Assistant, .content = textBox1.Text})
End Sub
''' <summary>
''' 多轮普通对话
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Async Sub button4_Click(sender As Object, e As EventArgs) Handles button4.Click
Try
''------当前要说的内容
HistoryList.Add(New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text})
''---------------
Dim chatReq As New ChatRequest With
{
.model = ChatModels.DeepseekChat,
.messages = HistoryList
}
Dim chatRes As ChatResponse = Await _deepSeekClient.ChatAsync(chatReq)
Console.WriteLine("聊天内容:" & chatRes.choices?.FirstOrDefault()?.message.content)
HistoryList.Add(New MessagesItem With {.role = ChatRoles.Assistant, .content = textBox1.Text})
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content.Replace("vbLf", vbCrLf)
Catch ex As Exception
Console.WriteLine("聊天失败:" & ex.Message)
End Try
End Sub
End Class
c# 示例
using Ohh.ChatAi.TotalApi.DeepSeek;
using Ohh.ChatAi.TotalApi.DeepSeek.Models;
using Ohh.ChatAi.TotalApi.DeepSeek.Models.Consts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Ohh.ChatAi.TotalApi.CSharp.Example
{
public partial class DeepSeekExample : Form
{
/// <summary>
/// DeepSeek对象
/// </summary>
private DeepSeekClient _deepSeekClient;
/// <summary>
/// 系统角色设定
/// </summary>
private const string SysContent = "你是 DeepSeek AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言";
public DeepSeekExample()
{
InitializeComponent();
//---------
_deepSeekClient = new DeepSeekClient("sk-xxxxxxxxxxxxx");
}
private void DeepSeekExample_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 单轮流式聊天
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button1_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Clear();
//------
var chatReq = new ChatRequest
{
model = ChatModels.DeepseekChat,
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
},
stream = true
};
// 订阅事件
_deepSeekClient.MessageReceivedEventHandler -= OnMessageReceived;
_deepSeekClient.ErrorEventHandler -= OnErrorMsg;
_deepSeekClient.MessageReceivedEventHandler += OnMessageReceived;
_deepSeekClient.ErrorEventHandler += OnErrorMsg;
// 开始任务
await _deepSeekClient.ChatStreamAsync(chatReq);
// 局部函数,订阅SSE 消息事件
void OnMessageReceived(object s, ChatResponse m)
{
//Console.WriteLine("聊天内容:" + ee);
Invoke(new Action(() =>
{
var msg = m.choices?.FirstOrDefault()?.delta.content;
if (msg != null) richTextBox1.AppendText(msg);
}));
}
// 局部函数,订阅SSE 错误事件
void OnErrorMsg(object s, Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
Console.WriteLine("流式聊天失败:" + ex.Message);
}
}
/// <summary>
/// 单轮普通聊天
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button2_Click(object sender, EventArgs e)
{
try
{
var chatReq = new ChatRequest
{
model = ChatModels.DeepseekChat,
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
}
};
var chatRes = await _deepSeekClient.ChatAsync(chatReq);
Console.WriteLine("聊天内容:" + chatRes.choices?.FirstOrDefault()?.message.content);
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content;
}
catch (Exception ex)
{
Console.WriteLine("聊天失败:" + ex.Message);
}
}
/// <summary>
/// 多轮对话列表
/// </summary>
private List<MessagesItem> HistoryList = new List<MessagesItem> { new MessagesItem { role = ChatRoles.System, content = SysContent } };
/// <summary>
/// 多轮流式对话
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button3_Click(object sender, EventArgs e)
{
try
{
richTextBox1.Clear();
//------当前要说的内容
HistoryList.Add(new MessagesItem { role = ChatRoles.User, content = textBox1.Text });
//------
var chatReq = new ChatRequest
{
model = ChatModels.DeepseekChat,
messages = HistoryList,
stream = true
};
// 订阅事件
_deepSeekClient.MessageReceivedEventHandler -= OnMessageReceived;
_deepSeekClient.ErrorEventHandler -= OnErrorMsg;
_deepSeekClient.CloseedEventHandler -= OnCloseed;
_deepSeekClient.MessageReceivedEventHandler += OnMessageReceived;
_deepSeekClient.ErrorEventHandler += OnErrorMsg;
_deepSeekClient.CloseedEventHandler += OnCloseed;
// 开始任务
await _deepSeekClient.ChatStreamAsync(chatReq);
// 局部函数,订阅SSE 消息事件
void OnMessageReceived(object s, ChatResponse m)
{
//Console.WriteLine("聊天内容:" + ee);
Invoke(new Action(() =>
{
var msg = m.choices?.FirstOrDefault()?.delta.content;
if (msg != null)
{
richTextBox1.AppendText(msg);
}
}));
}
// 局部函数,订阅SSE 错误事件
void OnErrorMsg(object s, Exception ex)
{
MessageBox.Show(ex.Message);
}
// 局部函数,订阅SSE 关闭事件
void OnCloseed(object s, string ex)
{
HistoryList.Add(new MessagesItem { role = ChatRoles.Assistant, content = textBox1.Text });
}
}
catch (Exception ex)
{
Console.WriteLine("流式聊天失败:" + ex.Message);
}
}
/// <summary>
/// 多轮普通聊天
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void button4_Click(object sender, EventArgs e)
{
try
{
//------当前要说的内容
HistoryList.Add(new MessagesItem { role = ChatRoles.User, content = textBox1.Text });
//-------------------
var chatReq = new ChatRequest
{
model = ChatModels.DeepseekChat,
messages = HistoryList
};
var chatRes = await _deepSeekClient.ChatAsync(chatReq);
HistoryList.Add(new MessagesItem { role = ChatRoles.Assistant, content = textBox1.Text });
Console.WriteLine("聊天内容:" + chatRes.choices?.FirstOrDefault()?.message.content);
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content;
}
catch (Exception ex)
{
Console.WriteLine("聊天失败:" + ex.Message);
}
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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.
-
.NETStandard 2.0
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
目前支持Kimi MoonshotAI,DeepSeek AI