所有文章 > API开发 > VB.NET,C#.NET接入文心一言4.0AI对话大模型

VB.NET,C#.NET接入文心一言4.0AI对话大模型

1.新增文心一言3.5-4.0大模型

Nuget直接搜索Ohh.ChatAi.TotalApi

https://www.nuget.org/packages/Ohh.ChatAi.TotalApi

2.代码示例

2.1.支持创建AccessToken访问凭证(一次运行只创建一次,后续将在缓存在中读取)

CreateAccessTokenAsync

2.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>
''' DeepSeek对象
''' </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>
/// DeepSeek对象
/// </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);
}
}
}
}

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

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