Unity之使用火山引擎实现文字提问流式回复

文字提问

设置参数apiKey

调用Request方法,传入对话内容

注册事件,接收回复内容

csharp 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 文本提问
/// </summary>
public class TextQuestions : MonoBehaviour
{
    string baseUrl = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
    string apiKey = "";

    [Header("模型")]
    [SerializeField] string model = "doubao-seed-2-0-code-preview-260215";
    [Header("深度思考")]
    [SerializeField] string thinking = "disabled";
    [Header("模型回答最大长度(单位token)")]
    [SerializeField] int maxTokens = 4096;
    [Header("思考消耗")]
    [SerializeField] string reasoningEffort = "minimal";

    public event Action<string, bool> onReceive;
    public event Action onCompleted;
    public event Action OnError;

    void OnReceive(string content)
    {
        var respone = JsonUtility.FromJson<ResponeChatMessage>(content);
        onReceive?.Invoke(respone.choices[0].delta.content,
        respone.choices[0].finish_reason == "stop");
    }

    void OnCompleted()
    {
        onCompleted?.Invoke();
    }

    public void Request(List<Message> messages)
    {
        RequestChat requestMessages = new RequestChat
        {
            messages = messages,
            model = model,
            thinking = new ThinkingType()
            {
                type = thinking,
            },
            max_tokens = maxTokens,
            reasoning_effort = reasoningEffort,
        };
        string jsonData = JsonUtility.ToJson(requestMessages);
        StartCoroutine(PostChat(jsonData));
    }

    IEnumerator PostChat(string jsonData)
    {
        using (UnityWebRequest request = new UnityWebRequest(baseUrl, UnityWebRequest.kHttpVerbPOST))
        {
            byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
            request.uploadHandler = new UploadHandlerRaw(bodyRaw);

            var tTSDownload = new TTSDownload();
            tTSDownload.OnObjectReceived += OnReceive;
            tTSDownload.OnComplete += OnCompleted;

            request.downloadHandler = tTSDownload;

            request.SetRequestHeader("Content-Type", "application/json");
            request.SetRequestHeader("Authorization", "Bearer " + apiKey);
            yield return request.SendWebRequest();
            if (request.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError("请求失败:" + request.error);
                OnError?.Invoke();
            }

            tTSDownload.OnObjectReceived -= OnReceive;
            tTSDownload.OnComplete -= OnCompleted;
        }
    }
}

[Serializable]
public class RequestChat
{
    public string model;
    public List<Message> messages;
    public ThinkingType thinking;
    public int max_tokens;
    public string reasoning_effort;
    public bool stream = true;
}
[Serializable]
public class ThinkingType
{
    public string type;
}

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

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

[Serializable]
public class Choices
{
    public MessageDelta delta;
    public string finish_reason;
    public int index;
}

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

拓展DownloadHandler

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

public class TTSDownload : DownloadHandlerScript
{
    private StringBuilder buffer = new StringBuilder();
    public event Action<string> OnObjectReceived;
    public event Action OnComplete;

    public TTSDownload() : base(new byte[4096]) { }

    protected override bool ReceiveData(byte[] data, int dataLength)
    {
        string chunk = Encoding.UTF8.GetString(data, 0, dataLength);
        buffer.Append(chunk);
        ProcessBuffer();
        return true;
    }

    private void ProcessBuffer()
    {
        string content = buffer.ToString();
        int splitStartIndex = -1;
        int braceCount = 0;
        int lastProcessed = 0;
        for (int i = 0; i < content.Length; i++)
        {
            if (content[i] == '{')
            {
                if (braceCount == 0) splitStartIndex = i;
                braceCount++;
            }
            else if (content[i] == '}')
            {
                braceCount--;
                if (braceCount == 0 && splitStartIndex != -1)
                {
                    string obj = content.Substring(splitStartIndex, i - splitStartIndex + 1);
                    OnObjectReceived?.Invoke(obj);
                    splitStartIndex = -1;
                    lastProcessed = i + 1;
                }
            }
        }

        if (lastProcessed > 0)
            buffer.Remove(0, lastProcessed);
    }

    protected override void CompleteContent()
    {
        OnComplete?.Invoke();
    }
}
相关推荐
叶帆16 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君16 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子17 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
hai31524754317 天前
九章编程法 · 猜数字游戏 (GW-BASIC 重构版) *
人工智能·microsoft·游戏引擎·游戏程序
心前阳光17 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光17 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯17 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案17 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔17 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
鼎艺创新科技17 天前
三维电子沙盘中OSGB倾斜摄影数据的加载与渲染
游戏引擎·cocos2d