unity中 使用http调用 openai 3.5

using UnityEngine;
using UnityEditor;
using UnityEngine.Networking;

namespace AICode
{

    static class OpenAIUtil
    {
        public static byte[] CreateRequestBody(string prompt)
        {
            var msg = new OpenAI.RequestMessage();
            msg.role = "user";
            msg.content = prompt;

            var req = new OpenAI.Request();
            req.model = "gpt-3.5-turbo";
            req.messages = new[] { msg };

            return System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(req));
        }

        public static string InvokeChat(string prompt)
        {
            var res = "";

            using (var post = UnityWebRequest.Put(OpenAI.Api.Url, CreateRequestBody(prompt)))
            {
                post.method = "POST";
                post.SetRequestHeader("Content-Type", "application/json");
                post.SetRequestHeader("Authorization", "Bearer " + "你的api key ");
                post.certificateHandler = new CertificateWhore();
                var req = post.SendWebRequest();

                var progress = 0.0f;

                while (true)
                {
                    if (req.isDone)
                    {
                        res = post.downloadHandler.text;
                        break;
                    }

                    EditorUtility.DisplayProgressBar("AICode", "Generating...", progress);
                    System.Threading.Thread.Sleep(100);
                    progress += 0.01f;
                }

                var json = post.downloadHandler.text;
                var data = JsonUtility.FromJson<OpenAI.Response>(json);
                res = data.choices[0].message.content;
            }

            EditorUtility.ClearProgressBar();

            return res;
        }
    }

    public class CertificateWhore : CertificateHandler
    {
        protected override bool ValidateCertificate(byte[] certificateData)
        {
            return true;
        }
    }
} // namespace AICode

OpenAI 数据类

namespace AICode.OpenAI
{
    public static class Api
    {
        public const string Url = "https://api.openai.com/v1/chat/completions";
    }

    [System.Serializable]
    public struct ResponseMessage
    {
        public string role;
        public string content;
    }

    [System.Serializable]
    public struct ResponseChoice
    {
        public int index;
        public ResponseMessage message;
    }

    [System.Serializable]
    public struct Response
    {
        public string id;
        public ResponseChoice[] choices;
    }

    [System.Serializable]
    public struct RequestMessage
    {
        public string role;
        public string content;
    }

    [System.Serializable]
    public struct Request
    {
        public string model;
        public RequestMessage[] messages;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值