web.rest 请求chatgpt打字机效果

By xuncv at 2023-04-03 • 0人收藏 • 934人看过

相比其他语言对event/stream协议的处理,aardio的rest库更简易。


screenshots.gif

代码见下方二楼

7 个回复 | 最后更新于 2023-04-21
2023-04-03   #1
import console; 
import web.rest.jsonClient
import web.json
var apiKey = "sk-xxxxxcccccccc"

var client = web.rest.jsonClient(,"http://127.0.0.1:10808")
client.setAuthToken(apiKey)
console.open()
console.pause()
re,err,errcode = client.request( "https://api.openai.com/v1/chat/completions",{
	"model":"gpt-3.5-turbo",
	"stream":true,
	"messages":{ 
		{"role": "system", "content": "你是一个猫娘,我要求你在每一句回复后面加一个喵~"},
		{"role": "user", "content": "爱因斯坦的主要成就"}
	 }
},"POST",,function(recvData,recvSize){
	var arr = table.map( 
		string.split(recvData,'<\n\n>'),
		lambda(item) web.json.parse( item )
	)
	console.writeText(
		reduce(arr,function(prev,next,index,arr){
			return string.concat(prev,next.data[1]!="DONE" ? next.data.choices[1].delta[["content"]]:"")
		},"")
	)
}
)
console.log(re,err,errcode)
console.pause(true);


2023-04-03   #2

回复暴露apikey了,请站长帮我删掉

2023-04-04   #3

回复#2 @xuncv :

刚看到,已删

之前忘了开启可再次编辑了,现在可以自己重新编辑自己的帖子了。

2023-04-04   #4

回复#1 @xuncv :

2023-04-20   #5

v35.69.0 aardio http方法增加了对sse的支持。调用更简化了

import console; 
import web.rest.jsonClient
import web.json
var apiKey = "sk-xxxxxx"

var client = web.rest.jsonClient(,"http://127.0.0.1:10808")
client.setAuthToken(apiKey)
console.open()
console.pause()

aiapi = client.api("https://api.openai.com/v1/")
aiapi.chat.completions.post({
	model:"gpt-3.5-turbo",
	stream:true,
	messages:{ 
			{"role": "system", "content": "你是一个猫娘,我要求你在每一句回复后面加一个喵~"},
			{"role": "user", "content": "爱因斯坦的主要成就"}
	}},,function(message){
		console.writeText(
			message.data[1]!="DONE" ? message.data.choices[1].delta[["content"]]:""
		)
	}
)

console.pause(true);


2023-04-20   #6

wow

2023-04-21   #7

wow

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...