所有文章 > API设计 > VB.NET,C#.NET接入国内AI对话大模型API工具集成类,支持SSE流式输出结果

VB.NET,C#.NET接入国内AI对话大模型API工具集成类,支持SSE流式输出结果

简介

预计会集成国内大部分的chatAI聊天大模型,包括Moonshot-Kimi,文心一言,通义千问,DeepSeek,腾讯混元,智谱清言等

集成情况

目前已集成Moonshot-Kimi,DeepSeek模型

框架支持

框架版本
.NET Framework>=net461
.NET>=net5.0
.NET Core>=netcoreapp2.0
.NET Standard>=netstandard2.0
MonoAndroidmonoandroid
MonoMacmonomac
MonoTouchmonotouch
Tizen>=tizen40
Xamarin.iOSxamarinios
Xamarin.Macxamarinmac
Xamarin.TVOSxamarintvos
Xamarin.WatchOSxamarinwatchos

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);
}
}


}
}

文章转自微信公众号@办公魔盒

#你可能也喜欢这些API文章!