Ohh.ChatAi.TotalApi
1.0.6
See the version list below for details.
dotnet add package Ohh.ChatAi.TotalApi --version 1.0.6
NuGet\Install-Package Ohh.ChatAi.TotalApi -Version 1.0.6
<PackageReference Include="Ohh.ChatAi.TotalApi" Version="1.0.6" />
paket add Ohh.ChatAi.TotalApi --version 1.0.6
#r "nuget: Ohh.ChatAi.TotalApi, 1.0.6"
// Install Ohh.ChatAi.TotalApi as a Cake Addin #addin nuget:?package=Ohh.ChatAi.TotalApi&version=1.0.6 // Install Ohh.ChatAi.TotalApi as a Cake Tool #tool nuget:?package=Ohh.ChatAi.TotalApi&version=1.0.6
Ohh.ChatAi.TotalApi:国内AI-API的终极整合SDK,为你开启智能交互新纪元!
公众号:办公魔盒
博客:https://bgmh.work
微信:vbee_club
简介
本项目旨在为开发者提供一个简洁高效的途径,以便轻松接入国内领先的AI对话大模型整合SDK。开发者仅需在代码中输入接口KEY,即可实现快速接入。目前,本项目支持ASP.NET、Winform、WPF和Web等多种平台。未来,我们计划整合国内主流的chatAI聊天大模型,如Moonshot-Kimi、文心一言、通义千问、DeepSeek、腾讯混元和智谱清言等,以满足更广泛的需求。加入我们,体验无缝接入的便捷与强大功能!
集成情况
我们已成功整合了业界领先的AI对话模型,包括Moonshot-Kimi、DeepSeek、智谱清言、阿里通义千问以及百度文心一言,为您提供一站式接入体验。立即加入,探索智能对话的无限可能!
框架支持
框架 | 版本 |
---|---|
.NET Framework | >=net461(推荐使用VS2022) |
.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 |
AI ApiKey申请链接
Moonshot-Kimi:https://platform.moonshot.cn/
DeepSeek:https://platform.deepseek.com/
智谱清言:https://open.bigmodel.cn/
阿里通义千问:https://dashscope.console.aliyun.com/overview?spm=a2c4g.11186623.0.0.482d3116trywR5
百度文心一言:https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application/create
腾讯混元:https://console.cloud.tencent.com/hunyuan/settings
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);
}
}
}
}
3.已集成阿里通义千问大模型
3.1.支持计算token数量
CalcTokenCountAsync
3.2.普通聊天和流式聊天示例(支持多轮和单论对话)
vb.net示例
Imports Ohh.ChatAi.TotalApi.Dashscope
Imports Ohh.ChatAi.TotalApi.Dashscope.Models
Imports Ohh.ChatAi.TotalApi.Dashscope.Models.Consts
Imports Ohh.ChatAi.TotalApi.Dashscopei.Models.Consts
Imports Ohh.ChatAi.TotalApi.DeepSeek
Public Class DashscopeExample
''' <summary>
''' 通义千问 对象
''' </summary>
Private _tongyi As TongYiClient
''' <summary>
''' 系统角色设定
''' </summary>
Private Const SysContent As String = "你是 阿里通义千问 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 为专有名词,不可翻译成其他语言"
''' <summary>
''' 构造函数
''' </summary>
Public Sub New()
InitializeComponent()
''---------
_tongyi = New TongYiClient("sk-xxxxxxxxxx")
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.QWenPlus,
.input = New InputData With
{
.messages = New List(Of MessagesItem) From {
New MessagesItem With {.role = ChatRoles.System, .content = SysContent},
New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text}
}
},
.parameters = New ParameterData With
{
.max_tokens = 2000, '' 根据不同的模型实际配置
.incremental_output = True '' 增量输出
}
}
'' 订阅事件
RemoveHandler _tongyi.MessageReceivedEventHandler, AddressOf OnMessageReceived
RemoveHandler _tongyi.ErrorEventHandler, AddressOf OnErrorMsg
AddHandler _tongyi.MessageReceivedEventHandler, AddressOf OnMessageReceived
AddHandler _tongyi.ErrorEventHandler, AddressOf OnErrorMsg
'' 开始任务
Await _tongyi.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.output.choices?.FirstOrDefault()?.message?.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.QWenPlus,
.input = New InputData With
{
.messages = New List(Of MessagesItem) From {
New MessagesItem With {.role = ChatRoles.System, .content = SysContent},
New MessagesItem With {.role = ChatRoles.User, .content = textBox1.Text}
}
},
.parameters = New ParameterData With
{
.max_tokens = 2000 '' 根据不同的模型实际配置
}
}
Dim chatRes As ChatResponse = Await _tongyi.ChatAsync(chatReq)
Console.WriteLine("聊天内容:" & chatRes.output.choices?.FirstOrDefault()?.message.content)
richTextBox1.Text = chatRes.output.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.Dashscope;
using Ohh.ChatAi.TotalApi.Dashscope.Models;
using Ohh.ChatAi.TotalApi.Dashscope.Models.Consts;
using Ohh.ChatAi.TotalApi.Dashscopei.Models.Consts;
using Ohh.ChatAi.TotalApi.DeepSeek;
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 System.Windows.Forms;
namespace Ohh.ChatAi.TotalApi.CSharp.Example
{
public partial class DashscopeExample : Form
{
/// <summary>
/// 通义千问 对象
/// </summary>
private TongYiClient _tongyi;
/// <summary>
/// 系统角色设定
/// </summary>
private const string SysContent = "你是 阿里通义千问 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 为专有名词,不可翻译成其他语言";
/// <summary>
/// 构造函数
/// </summary>
public DashscopeExample()
{
InitializeComponent();
//---------
_tongyi = new TongYiClient("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.QWenPlus,
input = new InputData
{
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content =textBox1.Text }
}
},
parameters = new ParameterData
{
max_tokens = 2000, //根据不同的模型实际配置
incremental_output = true //增量输出
}
};
// 订阅事件
_tongyi.MessageReceivedEventHandler -= OnMessageReceived;
_tongyi.ErrorEventHandler -= OnErrorMsg;
_tongyi.MessageReceivedEventHandler += OnMessageReceived;
_tongyi.ErrorEventHandler += OnErrorMsg;
// 开始任务
await _tongyi.ChatStreamAsync(chatReq);
// 局部函数,订阅SSE 消息事件
void OnMessageReceived(object s, ChatResponse m)
{
//Console.WriteLine("聊天内容:" + ee);
Invoke(new Action(() =>
{
var msg = m.output.choices?.FirstOrDefault()?.message?.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.QWenPlus,
input = new InputData
{
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content =textBox1.Text }
}
},
parameters = new ParameterData
{
max_tokens = 2000 //根据不同的模型实际配置
}
};
var chatRes = await _tongyi.ChatAsync(chatReq);
Console.WriteLine("聊天内容:" + chatRes.output.choices?.FirstOrDefault()?.message.content);
richTextBox1.Text = chatRes.output.choices?.FirstOrDefault()?.message.content;
}
catch (Exception ex)
{
Console.WriteLine("聊天失败:" + ex.Message);
}
}
}
}
4.已集成百度文心一言大模型
4.1.支持创建AccessToken访问凭证(一次运行只创建一次,后续将在缓存在中读取)
CreateAccessTokenAsync
4.2.普通聊天和流式聊天示例(支持多轮和单论对话)
vb.net示例
Imports System.IO
Imports Ohh.ChatAi.TotalApi.ErnieTurbo
Imports Ohh.ChatAi.TotalApi.ErnieTurbo.Models
Imports Ohh.ChatAi.TotalApi.ErnieTurbo.Models.Consts
Public Class ErnieWenXinExample
''' <summary>
''' 文心一言对象
''' </summary>
Private _ernieTurboClient As ErnieTurboClient
''' <summary>
''' 系统角色设定
''' </summary>
Private Const SysContent As String = "你是 百度千帆大模型 文心一言 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 为专有名词,不可翻译成其他语言"
''' <summary>
''' 构造函数
''' </summary>
Public Sub New()
InitializeComponent()
''----------
_ernieTurboClient = New ErnieTurboClient("AK-XXXXXXX", "SK-XXXXXXXXXX")
End Sub
Private Sub ErnieWenXinExample_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.Ernie40Turbo8K,
.system = SysContent,
.messages = New List(Of MessagesItem) From {
New MessagesItem With {
.role = ChatRoles.User,
.content = textBox1.Text
}
},
.stream = True
}
'' 订阅事件
RemoveHandler _ernieTurboClient.MessageReceivedEventHandler, AddressOf OnMessageReceived
RemoveHandler _ernieTurboClient.ErrorEventHandler, AddressOf OnErrorMsg
AddHandler _ernieTurboClient.MessageReceivedEventHandler, AddressOf OnMessageReceived
AddHandler _ernieTurboClient.ErrorEventHandler, AddressOf OnErrorMsg
'' 开始任务
Await _ernieTurboClient.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.result
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.Ernie40Turbo8K,
.system = SysContent,
.messages = New List(Of MessagesItem) From {
New MessagesItem With {
.role = ChatRoles.User,
.content = textBox1.Text
}
}
}
''-------------
Dim chatRes As ChatResponse = Await _ernieTurboClient.ChatAsync(chatReq)
Console.WriteLine("聊天内容:" & chatRes.result)
richTextBox1.Text = chatRes.result.Replace("vbLf", vbCrLf)
Catch ex As Exception
Console.WriteLine("聊天失败:" & ex.Message)
End Try
End Sub
End Class
C# 示例
using Ohh.ChatAi.TotalApi.ErnieTurbo;
using Ohh.ChatAi.TotalApi.ErnieTurbo.Models;
using Ohh.ChatAi.TotalApi.ErnieTurbo.Models.Consts;
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 System.Windows.Forms;
namespace Ohh.ChatAi.TotalApi.CSharp.Example
{
public partial class ErnieWenXinExample : Form
{
/// <summary>
/// 文心一言对象
/// </summary>
private ErnieTurboClient _ernieTurboClient;
/// <summary>
/// 系统角色设定
/// </summary>
private const string SysContent = "你是 百度千帆大模型 文心一言 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 为专有名词,不可翻译成其他语言";
/// <summary>
/// 构造函数
/// </summary>
public ErnieWenXinExample()
{
InitializeComponent();
//----------
_ernieTurboClient = new ErnieTurboClient("AK-XXXXXXX", "SK-XXXXXXXXXXXXX");
}
/// <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.Ernie40Turbo8K,
system = SysContent,
messages = new List<MessagesItem> {
new MessagesItem {
role = ChatRoles.User, content = textBox1.Text
}
},
stream = true
};
// 订阅事件
_ernieTurboClient.MessageReceivedEventHandler -= OnMessageReceived;
_ernieTurboClient.ErrorEventHandler -= OnErrorMsg;
_ernieTurboClient.MessageReceivedEventHandler += OnMessageReceived;
_ernieTurboClient.ErrorEventHandler += OnErrorMsg;
// 开始任务
await _ernieTurboClient.ChatStreamAsync(chatReq);
// 局部函数,订阅SSE 消息事件
void OnMessageReceived(object s, ChatResponse m)
{
//Console.WriteLine("聊天内容:" + ee);
Invoke(new Action(() =>
{
var msg = m.result;
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.Ernie40Turbo8K,
system = SysContent,
messages = new List<MessagesItem> {
new MessagesItem {
role = ChatRoles.User,
content = textBox1.Text
}
}
};
var chatRes = await _ernieTurboClient.ChatAsync(chatReq);
Console.WriteLine("聊天内容:" + chatRes.result);
richTextBox1.Text = chatRes.result;
}
catch (Exception ex)
{
Console.WriteLine("聊天失败:" + ex.Message);
}
}
}
}
5.已集成智谱清言大模型
5.1.普通聊天和流式聊天示例(支持多轮和单论对话)
vb.net示例
Imports Ohh.ChatAi.TotalApi.ChatGLM
Imports Ohh.ChatAi.TotalApi.ChatGLM.Models
Imports Ohh.ChatAi.TotalApi.ChatGLM.Models.Consts
Public Class ChatGlmExample
''' <summary>
''' 智谱清言 对象
''' </summary>
Private _chatGlmClient As ChatGlmClient
''' <summary>
''' 系统角色设定
''' </summary>
Private Const SysContent As String = "你是 智谱清言 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言"
''' <summary>
''' 构造函数
''' </summary>
Public Sub New()
InitializeComponent()
''------------------
_chatGlmClient = New ChatGlmClient("apikey-xxxxxxxxxxxx")
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.Glm4,
.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 _chatGlmClient.MessageReceivedEventHandler, AddressOf OnMessageReceived
RemoveHandler _chatGlmClient.ErrorEventHandler, AddressOf OnErrorMsg
AddHandler _chatGlmClient.MessageReceivedEventHandler, AddressOf OnMessageReceived
AddHandler _chatGlmClient.ErrorEventHandler, AddressOf OnErrorMsg
'' 开始任务
Await _chatGlmClient.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("\n\n", 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.Glm4,
.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 _chatGlmClient.ChatAsync(chatReq)
Console.WriteLine("聊天内容:" & chatRes.choices?.FirstOrDefault()?.message.content)
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content.Replace("\n\n", vbCrLf)
Catch ex As Exception
Console.WriteLine("聊天失败:" & ex.Message)
End Try
End Sub
End Class
c# 示例
using Ohh.ChatAi.TotalApi.ChatGLM;
using Ohh.ChatAi.TotalApi.ChatGLM.Models;
using Ohh.ChatAi.TotalApi.ChatGLM.Models.Consts;
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 System.Windows.Forms;
namespace Ohh.ChatAi.TotalApi.CSharp.Example
{
public partial class ChatGlmExample : Form
{
/// <summary>
/// 智谱清言对象
/// </summary>
private ChatGlmClient _chatGlmClient;
/// <summary>
/// 系统角色设定
/// </summary>
private const string SysContent = "你是 智谱清言 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 为专有名词,不可翻译成其他语言";
/// <summary>
/// 构造函数
/// </summary>
public ChatGlmExample()
{
InitializeComponent();
//----------
_chatGlmClient = new ChatGlmClient("apikeyxxxxxxxxxxxxxx");
}
/// <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.Glm4,
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
},
stream = true
};
// 订阅事件
_chatGlmClient.MessageReceivedEventHandler -= OnMessageReceived;
_chatGlmClient.ErrorEventHandler -= OnErrorMsg;
_chatGlmClient.MessageReceivedEventHandler += OnMessageReceived;
_chatGlmClient.ErrorEventHandler += OnErrorMsg;
// 开始任务
await _chatGlmClient.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.Replace("\\n","\r\n"));
}));
}
// 局部函数,订阅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.Glm4,
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
}
};
var chatRes = await _chatGlmClient.ChatAsync(chatReq);
Console.WriteLine("聊天内容:" + chatRes.choices?.FirstOrDefault()?.message.content.Replace("\\n", "\r\n"));
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 is compatible. 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 is compatible. 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 is compatible. 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 is compatible. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 is compatible. net481 is compatible. |
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. |
-
.NETFramework 4.6.1
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
.NETFramework 4.6.2
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
.NETFramework 4.7.2
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
.NETFramework 4.8
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
.NETFramework 4.8.1
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
.NETStandard 2.0
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
net5.0
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
net6.0
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
net7.0
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
-
net8.0
- LaunchDarkly.EventSource (>= 5.1.0)
- Newtonsoft.Json (>= 13.0.3)
- System.Runtime.Caching (>= 8.0.0)
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,阿里 通义千问,百度 文心一言,智谱清言