Unity使用豆包语言模型

环境

使用日期:2026.3.23

OS:Windows10

引擎:Unity2021.3.45

豆包语言模型开通

  1. https://www.volcengine.com
    手机号注册,身份证进行实名认证。
  2. https://www.volcengine.com/product/ark
    点击控制台。
  3. 语言模型开通服务。
  4. API Key 管理,创建API Key。
  5. 接入文档:https://www.volcengine.com/docs/82379/1494384?lang=zh

示例

数据类

csharp 复制代码
[Serializable]
public class RequestChat
{
    public string model;
    public List<Message> messages = new List<Message>();
}

[Serializable]
public class Message
{
    public string role;
    public string content;
}

[Serializable]
public class ResponeChatMessage
{
    public Choices[] choices;
}

[Serializable]
public class Choices
{
    public string finish_reason;
    public int index;
    public Message message;
}

请求示例

基础参数(必须)

  1. baseurl
    https://ark.cn-beijing.volces.com/api/v3/chat/completions
  2. model:
    接入文档中打开 API Explorer,找到model参数,选择开通的语言模型。
    示例:doubao-seed-2-0-code-preview-260215
  3. apiKey:
    创建的Key。

提问

RequestChat类设置使用的语言模型以及问题列表。

Message类中设置提问角色以及问题。

使用UnityWebRequest发送请求,接收响应信息。

使用过程中响应时间比较漫长,大概30秒收到响应。

csharp 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class DoubaoChatAPI : MonoBehaviour
{
    [SerializeField] string baseUrl;
    [SerializeField] string model;
    [SerializeField] string apiKey;

    bool isRequesting;//请求中

    float requesetTime;

    string question = string.Empty;
    string answer = string.Empty;

    void OnGUI()
    {
        GUILayout.Label("问题:");
        question = GUILayout.TextField(question);

        GUILayout.Label("回答:");
        GUILayout.Label(answer);

        if (isRequesting == false)
        {
            if (GUILayout.Button("开始对话"))
            {
                isRequesting = true;
                requesetTime = 0;

                RequestChat requestMessages = new RequestChat();
                requestMessages.model = model;
                requestMessages.messages.Add(new Message()
                {
                    role = "user",
                    content = question
                });
                string messages = JsonUtility.ToJson(requestMessages);

                StartCoroutine(PostChat(baseUrl, messages, (result, value) =>
                {
                    Debug.Log(value);
                    if (result)
                    {
                        var info = JsonUtility.FromJson<ResponeChatMessage>(value);
                        answer = info.choices[0].message.content;
                    }
                    else
                    {
                        answer = "回答失败,请查看日志";
                    }
                    isRequesting = false;
                }));
            }
        }
        else
        {
            requesetTime += Time.deltaTime;
            GUILayout.Label("请求时间:" + requesetTime);
        }
    }

    IEnumerator PostChat(string url, string jsonData, Action<bool, string> callback)
    {
        using (UnityWebRequest request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST))
        {
            byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
            request.uploadHandler = new UploadHandlerRaw(bodyRaw);
            request.downloadHandler = new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/json");
            request.SetRequestHeader("Authorization", "Bearer " + apiKey);

            yield return request.SendWebRequest();

            if (request.result == UnityWebRequest.Result.Success)
            {
                callback?.Invoke(true, request.downloadHandler.text);
            }
            else
            {
                Debug.LogError("请求失败:" + request.error);
                callback?.Invoke(false, request.downloadHandler.text);
            }
        }
    }
}
相关推荐
大鱼>9 天前
大语言模型+物联网:LLM理解物理世界
物联网·struts·语言模型·多模态·aiot
AndrewHZ9 天前
【LLM技术全景】大模型能力探秘:In-Context Learning与思维链(CoT)
人工智能·语言模型·大模型·llm·cot·思维链·icl
生成论实验室9 天前
机器人:一个自主运动的系统
人工智能·算法·语言模型·机器人·自动驾驶·agi·安全架构
Data-Miner9 天前
大语言模型+智能体AI,122页PPT详解落地应用培训!
人工智能·microsoft·语言模型
叶帆9 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
枫叶林FYL9 天前
BRIDGE:多模态查询的强化学习对齐与文本检索重构
人工智能·语言模型
小小工匠9 天前
拆解大语言模型:从词向量到注意力机制的内部运行原理
人工智能·语言模型·自然语言处理
暮云星影9 天前
瑞芯微rk3588利用Rockchip NPU运行大语言模型(LLM)
arm开发·人工智能·语言模型·自然语言处理
久数君9 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
生成论实验室9 天前
自动驾驶:一个自主运动的系统
人工智能·算法·机器学习·语言模型·机器人·自动驾驶·安全架构