所有文章 > API开发 > VB.NET,C#.NET接入抖音豆包AI对话大模型

VB.NET,C#.NET接入抖音豆包AI对话大模型

1.新增接入 抖音豆包AI 方舟大模型

Nuget直接搜索Ohh.ChatAi.TotalApi

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

2.已集成抖音豆包方舟大模型

2.1.开通模型计费

https://www.volcengine.com/docs/82379/1099320

2.2.豆包模型比较特殊,model字段使用的是接入点ID,请根据以下文档创建,然后复制ep-xxxxxx-vkfww的接入点ID填入Model字段即可

https://www.volcengine.com/docs/82379/1099522

2.3.普通聊天和流式聊天示例(支持多轮和单论对话)

vb.net示例

using Ohh.ChatAi.TotalApi.DouYinDouBao;
using Ohh.ChatAi.TotalApi.DouYinDouBao.Models;
using Ohh.ChatAi.TotalApi.DouYinDouBao.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 DouYinDouBaoExample : Form
{

/// <summary>
/// DouYinDouBao对象
/// </summary>
private DouYinDouBaoClient _douYinDouBaoClient;

/// <summary>
/// 系统角色设定
/// </summary>
private const string SysContent = "你是 抖音豆包 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言";



/// <summary>
/// 构造函数
/// </summary>
public DouYinDouBaoExample()
{
InitializeComponent();
//---------
_douYinDouBaoClient = new DouYinDouBaoClient("xxxxxxxxxxxxxxxxxxx");
}

private void DouYinDouBaoExample_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 = "ep-20240704033659-vkfww", //以 endpoint_id 索引对应的模型接入点。接入点创建:模型推理->创建模型推理->复制ep-xxxxxxx-vkfww的模型ID
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
},
stream = true
};
// 订阅事件
_douYinDouBaoClient.MessageReceivedEventHandler -= OnMessageReceived;
_douYinDouBaoClient.ErrorEventHandler -= OnErrorMsg;
_douYinDouBaoClient.MessageReceivedEventHandler += OnMessageReceived;
_douYinDouBaoClient.ErrorEventHandler += OnErrorMsg;
// 开始任务
await _douYinDouBaoClient.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 = "ep-20240704033659-vkfww", //以 endpoint_id 索引对应的模型接入点。接入点创建:模型推理->创建模型推理->复制ep-xxxxxxx-vkfww的模型ID
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
}
};
var chatRes = await _douYinDouBaoClient.ChatAsync(chatReq);
Console.WriteLine("聊天内容:" + chatRes.choices?.FirstOrDefault()?.message.content);
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content;
}
catch (Exception ex)
{
Console.WriteLine("聊天失败:" + ex.Message);
}
}
}
}

C#示例

using Ohh.ChatAi.TotalApi.DouYinDouBao;
using Ohh.ChatAi.TotalApi.DouYinDouBao.Models;
using Ohh.ChatAi.TotalApi.DouYinDouBao.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 DouYinDouBaoExample : Form
{

/// <summary>
/// DouYinDouBao对象
/// </summary>
private DouYinDouBaoClient _douYinDouBaoClient;

/// <summary>
/// 系统角色设定
/// </summary>
private const string SysContent = "你是 抖音豆包 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。Moonshot AI 为专有名词,不可翻译成其他语言";



/// <summary>
/// 构造函数
/// </summary>
public DouYinDouBaoExample()
{
InitializeComponent();
//---------
_douYinDouBaoClient = new DouYinDouBaoClient("xxxxxxxxxxxxxxxxxxx");
}

private void DouYinDouBaoExample_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 = "ep-20240704033659-vkfww", //以 endpoint_id 索引对应的模型接入点。接入点创建:模型推理->创建模型推理->复制ep-xxxxxxx-vkfww的模型ID
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
},
stream = true
};
// 订阅事件
_douYinDouBaoClient.MessageReceivedEventHandler -= OnMessageReceived;
_douYinDouBaoClient.ErrorEventHandler -= OnErrorMsg;
_douYinDouBaoClient.MessageReceivedEventHandler += OnMessageReceived;
_douYinDouBaoClient.ErrorEventHandler += OnErrorMsg;
// 开始任务
await _douYinDouBaoClient.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 = "ep-20240704033659-vkfww", //以 endpoint_id 索引对应的模型接入点。接入点创建:模型推理->创建模型推理->复制ep-xxxxxxx-vkfww的模型ID
messages = new List<MessagesItem> {
new MessagesItem { role = ChatRoles.System, content = SysContent },
new MessagesItem { role = ChatRoles.User, content = textBox1.Text }
}
};
var chatRes = await _douYinDouBaoClient.ChatAsync(chatReq);
Console.WriteLine("聊天内容:" + chatRes.choices?.FirstOrDefault()?.message.content);
richTextBox1.Text = chatRes.choices?.FirstOrDefault()?.message.content;
}
catch (Exception ex)
{
Console.WriteLine("聊天失败:" + ex.Message);
}
}
}
}

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

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