GPT 响应使用 C# 转换为 JSON 对象

问题描述 投票:0回答:1

我正在访问 GPT,我想要访问 json 字符串作为对象。我在 C# 将 GPT JSON 字符串的输出转换为 JSON 对象时遇到问题。你能根据下面的样本给我提供样本吗?预先感谢您。


ResponseOut = "{ \"chatid\":\"3c377545-4622-4957-97cc-535460937af\" },{\"tok\":\"I apologize, but as an AI language model, I am not capable of answering your question. However, I can suggest that you consult the  documentation. Additionally, there are many\"},{\"tok\":\" online resources and forums where you can find sample and please dont asked me again. Too much already bro.\"},[]";

// Error in this area
var items = JsonConvert.DeserializeObject<List<String>>(ResponseOut);

// Error in this area also
var items = JsonConvert.DeserializeObject<List<Item>>(ResponseOut);



// My Class
        public class Item
        {
            public string chatid { get; set; }
            public string tok { get; set; }
        }

我用了下面这个方法,但没有效果。

//该区域错误 var items = JsonConvert.DeserializeObject(ResponseOut);

//这方面也有错误 var items = JsonConvert.DeserializeObject(ResponseOut);

c# openai-api chatgpt-api chatgpt-plugin
1个回答
0
投票

来自 GPT Swagger。这就是我得到的确切输出。

                // Create an HttpClient instance
            using (HttpClient client = new HttpClient())
            {
                string prompt =""+ AI_Prompt;
                bool regen = false;
                var prompt1 = new { prompt, regen };

                // Serialize the prompt data to JSON
                string jsonContent = Newtonsoft.Json.JsonConvert.SerializeObject(prompt1);

                // Set the timeout (optional)
                client.Timeout = TimeSpan.FromMinutes(5); // Adjust as needed

                // Add the Bearer token to the request headers
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", AuthorizationBearer);

                //client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AuthorizationBearer);
                client.DefaultRequestHeaders.Accept.ParseAdd("text/plain");

                // Create the request content
                var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

                // Send the POST request to the local GPT API
                try
                {
                    HttpResponseMessage response = await client.PostAsync(apiUrl, content);
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        response.EnsureSuccessStatusCode();
                        // Read and display the response content
                        string responseBody = await response.Content.ReadAsStringAsync();

// ResponseBody 的内容 = "{ "chatid":"a623a388-23cd-4522-96dd-7a7342e2c326" },{"tok":"抱歉,作为 AI 语言模型,我无法使用 LISP 在 AutoCAD 或 Civil3D 中提供代码。不过,我可以推荐一些资源,您可以在其中学习如何为 AutoCAD 或 Civil3D 编写 LISP 代码: 1. Lee Mac 的 AutoLISP 教程系列:http"},{"tok":"://www.lee-mac.com/autolisp.php 2. AutoCAD 自定义指南:https://knowledge.autodesk.com/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2019/ENU/AutoCAD-Customization/files/GUID-1B8B2C5D-6D7D-4B0D-8B4D -1A6F8E2C6F7A-htm.html\n3。 Civil 3D Custom"},{"tok":"化指南:https://knowledge.autodesk.com/support/civil-3d/learn-explore/caas/CloudHelp/cloudhelp/2019/ENU/Civil3D-Customization/ files/GUID-9A2B0E4C-5C4B-4B63-9A1C-4E8E6E6B9B4E-htm.html\n\n一旦你学会了如何编写LISP代码,你就可以使用"},{"tok":"以下代码来显示消息嗨。你好吗?\u0022 在 AutoCAD 或 Civil3D 中: \u0060\u0060\u0060 (defun c:hello-world () (princ \u0022嗨。你好吗?\u0022)) \u0060\u0060\u0060 不要忘记在代码的最顶部添加 :D 并在代码的最末尾添加 :D"},{"tok":"."},[]"

如果我用 [] 将其括起来,我就可以将其转换为对象。但我无法正确访问数据。请向我提供有关访问对象数据的示例。

ResponseBody = "[" + ResponseBody + "]"; JavaScriptSerializer j = new JavaScriptSerializer(); object data = j.Deserialize(ResponseOut, typeof(object));

示例代码

© www.soinside.com 2019 - 2024. All rights reserved.