在处理gpt返回数据时需要 处理的流式问题疑问?

aardio 11月前 692

现在 gpt 返回的数据都是流的形式,怎么样才能把它转换为类似正常的一串字符串呢?请教,下面内容 ,实际我要取的是 response 对应 的内容

{"model":"vicuna","created_at":"2023-12-13T09:44:44.330511Z","response":"\n","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.398591Z","response":"G","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.475653Z","response":"PT","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.566181Z","response":":","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.630396Z","response":" Hello","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.70039Z","response":"!","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.773188Z","response":" It","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.841229Z","response":"'","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.904017Z","response":"s","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:44.971747Z","response":" nice","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.035208Z","response":" to","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.099293Z","response":" meet","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.165876Z","response":" you","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.22995Z","response":" too","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.291708Z","response":".","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.35596Z","response":" What","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.418584Z","response":" can","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.479481Z","response":" I","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.546164Z","response":" help","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.614087Z","response":" you","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.668333Z","response":" with","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.729018Z","response":" today","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.798808Z","response":"?","done":false}
{"model":"vicuna","created_at":"2023-12-13T09:44:45.856087Z","response":"","done":true,"context":[319,13563,1546,263,12758,1404,322,385,23116,21082,20255,29889,450,20255,4076,8444,29892,13173,29892,322,1248,568,6089,304,278,1404,29915,29879,5155,29889,13,13,11889,29901,20103,304,5870,366,29973,13,22933,9047,13566,29901,13,13,29954,7982,29901,15043,29991,739,29915,29879,7575,304,5870,366,2086,29889,1724,508,306,1371,366,411,9826,29973],"total_duration":3704897916,"prompt_eval_count":46,"prompt_eval_duration":2217278000,"eval_count":23,"eval_duration":1462403000}


最新回复 (7)
  • aardio 11月前
    0 2
    生成 这样一句" Hello! It's nice to meet you too. What can I help you with today?
  • aardio 11月前
    0 3

    尝试了一下,大概的方法是先将内容按换行拆,再提取内容组合起来,利用正则提取关键的 2 组数据

    preg('"created_at":"(.*?)","response":"(.*?)"');
    regex.gmatch( test  )

    但是这种方法并没有对时间进行排序。。。

  • 小光芒 11月前
    0 4

    这个玩意不是json吗,转table直接用呗

  • aardio 11月前
    0 5
    嘿嘿嘿。。。合起来不是 Json
  • 瞌睡蟲子 11月前
    0 6

    如果是http流式请求,可以在onReceive里面处理,每一条是一个json。

    // http rest客户端
    var http = web.rest.jsonClient(); 
    var api = http.api("https://xxx.xxx.com/"); 
    
    http.beforeRequestHeaders = function(params){	
    	return {"Authorization":"Bearer xxx"}; 
    }
    
    http._http.onReceive = function(str,size,contentLength){
    	if(str["data"]["choices"]){
    		for(k,v in str["data"]["choices"]){
    			lastMsg += v["delta"]["content"];
    			mainForm.edit_log.appendText(v["delta"]["content"]);		
    			//mainForm.edit_log.print(v["delta"]["content"]);
    		}
    	}else {		
    		table.insert(history,{ "role": "assistant", "content": lastMsg });
    		if(#history > msgSize){
    			table.pop(history);
    		}
    		lastMsg = "";
    		mainForm.edit_log.print("");
    	}	
    }


  • aardio 11月前
    0 7
    瞌睡蟲子 如果是http流式请求,可以在onReceive里面处理,每一条是一个json。// http rest客户端 var http&nbs ...
    十分感谢。。。。我还在想如果返回的数据量大,后续的处理就成问题了 代码 让我想起了以前aardio 作者写的机器人。。。
  • aardio 11月前
    0 8
    "stream": false 可以避免返回数据的问题
返回