ChatGPT 桌面版 gpt-3.5-turbo 模型中的参数问题
By
hi_aardio
at 2023-03-02 • 0人收藏 • 1138人看过
Jacen He 有个关于“编写 ChatGPT 桌面版,改进支持上下文对话”的公众号代码分享,如下:
import win.ui; /*DSG{{*/ var winform = win.form(text="ChatGPT 桌面智能助理";right=661;bottom=721) /*}}*/ import web.view; var wb = web.view(winform); import web.rest.jsonClient; var http = web.rest.jsonClient(); //参数请改成您的 API key(或者获取 API key 的网址也可以)。 http.setAuthToken("API key"); var ai = http.api("https://api.openai.com/v1/"); var conversation = {}; var chatGptName = "桌面智能助理"; var chatGptInfo = "是一个快乐的程序员,精通多种编程语言"; wb.external = { ask = function(q){ table.push(conversation,"用户:"+q); var prompt; do{ prompt = "提示:你叫" + chatGptName + "," + chatGptInfo + "。<|endoftext|>" + "当前时间:" + tostring(time()) + "。<|endoftext|>" + string.join(conversation,"<|endoftext|>") + "<|endoftext|>" + chatGptName + ":"; if(#prompt < 2000) break; table.shift(conversation); }while(true); var ret,err = ai.completions({ "prompt": prompt,//发送提示 "model": "text-davinci-003",//GPT 3.5 模型 "temperature": 0.8,//热度,0~1 之间。 "max_tokens": 2048,//最大允许的字符数量 "stop": {"<|endoftext|>"} }) if(ret){ var a = ret.choices[1].text; table.push(conversation,chatGptName+":"+a); return a; } else { if(err[1]=='{'#){ var errObject = web.json.tryParse(err)[["error"]]; if(errObject[["code"]]="invalid_api_key"){ return "您的 API key 已经失效了!"; } if(errObject[["message"]]) return errObject[["message"]]; } return err; } }; } wb.html = /** <!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title>ChatGPT 桌面智能助理</title> <script src="https://lib.baomitu.com/react/18.2.0/umd/react.production.min.js"></script> <script src="https://lib.baomitu.com/react-dom/18.2.0/umd/react-dom.production.min.js"></script> <script src="https://lib.baomitu.com/chatui-core/2.4.2/index.min.js"></script> <link rel="stylesheet" href="https://lib.baomitu.com/chatui-core/2.4.2/index.min.css"> <script src="https://lib.baomitu.com/babel-standalone/7.20.14/babel.min.js"></script> <style type="text/css">html,body,#app{height:100%}</style> </head> <body> <script type="text/babel"> const { useState,useEffect,useCallback,useRef } = React; const { default: Chat, Bubble, useMessages } = ChatUI; const App = () => { const { messages, appendMsg, setTyping } = useMessages([{ type: 'text', content: { text: '主人好,我是桌面智能助理,您的贴心小助手~' }, user: { avatar: 'https://gw.alicdn.com/tfs/TB1DYHLwMHqK1RjSZFEXXcGMXXa-56-62.svg' }, }]); function handleSend(type, val) { if (type === 'text' && val.trim()) { appendMsg({ type: 'text', content: { text: val }, position: 'right', }); setTyping(true); aardio.ask(val).then( text=>{ appendMsg({ type: 'text', content: { text: text }, }); }) } } function renderMessageContent(msg) { const { content } = msg; return <Bubble content={content.text} />; } return ( <Chat navbar={{ title: '' }} messages={messages} renderMessageContent={renderMessageContent} onSend={handleSend} /> ); }; const root = ReactDOM.createRoot(document.getElementById('app')); root.render(<App />); </script> <div id="app"></div> **/ winform.show(); win.loopMessage();
这里用的模型是
text-davinci-003
而最新open ai出的更便宜好用的3.5模型是gpt-3.5-turbo and gpt-3.5-turbo-0301
新模型对应的介绍如下:
API Reference - OpenAI API
与原来模型相比,主要是post时参数上有所变化。(原来都是json方式,一对一,这次的新模型多了【】,需要提供多个参数值)
新模型参数:
{ "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello!"}] }
这里涉及到messages后面参数
[{"role": "user", "content": "Hello!"}]
想请教各位,在aardio中,如何来写这一部分内容?
谢谢
8 个回复 | 最后更新于 2023-07-20
主要改2个地方
prompt = {{"role": "user", "content":"提示:你叫" + chatGptName + "," + chatGptInfo + "。<|endoftext|>" + "当前时间:" + tostring(time()) + "。<|endoftext|>" + string.join(conversation,"<|endoftext|>") + "<|endoftext|>" + chatGptName + ":"}};
还有就是返回的内容
var a = ret.choices[1].message.content;
这样就可以正常使用了
2023-03-02
#6
回复#5 @hi_aardio :
import fonts.fontAwesome; import win.ui; /*DSG{{*/ mainForm = win.form(text="chatsonic";right=1152;bottom=594;bgcolor=16777215;border="none") mainForm.add( plsAsk={cls="plus";left=34;top=508;right=1046;bottom=578;align="left";bgcolor=16777215;border={color=-16777216;width=1};color=16711680;db=1;dl=1;dr=1;editable=1;font=LOGFONT(h=-16;name='微软雅黑');link=1;multiline=1;wrap=1;z=2}; plsLang={cls="plus";text="中文";left=861;top=9;right=1041;bottom=35;color=16711680;dr=1;dt=1;font=LOGFONT(h=-20;name='FontAwesome');iconStyle={align="left";font=LOGFONT(h=-20;name='FontAwesome');padding={left=40}};iconText='\uF096 ';z=5}; plsLog={cls="plus";left=34;top=41;right=1121;bottom=496;align="left";bgcolor=16777215;border={color=-16777216;width=1};color=8388608;db=1;dl=1;dr=1;dt=1;editable=1;font=LOGFONT(h=-16;name='微软雅黑');link=1;multiline=1;wrap=1;z=1}; plsSend={cls="plus";text="Send";left=1059;top=508;right=1121;bottom=578;bgcolor=15793151;color=8388608;db=1;dr=1;font=LOGFONT(h=-16;name='微软雅黑';weight=700);notify=1;z=3}; plus={cls="plus";text="ChatSonic智能助手";left=32;top=10;right=275;bottom=38;align="right";color=8388608;dl=1;dt=1;font=LOGFONT(h=-20;name='FontAwesome';weight=700);iconColor=32768;iconStyle={align="left";font=LOGFONT(h=-29;name='FontAwesome');padding={left=10}};iconText='\uF1D7';z=4} ) /*}}*/ import inet.http; import web.json; import time; import win.ui.simpleWindow; win.ui.simpleWindow(mainForm); var; var head = { "accept": "application/json", "content-type": "application/json", "X-API-KEY": "替换你自己的token key" } var url = "https://api.writesonic.com/v2/business/content/chatsonic?engine=premium&language=" var history = {{"is_sent": true, "message": ""}, } mainForm.plsLang.skin({ color = { hover = 0xF0FF0000; }; checked = { iconText = '\uF046'; } }) mainForm.plsSend.skin( background = { hover = 0xFFFF771C; } ) mainForm.plsSend.oncommand = function(id,event){ mainForm.plsSend.disabled = true; if mainForm.plsLang.checked { lang = "zh"; } else { lang = "en"; } var payload = { "enable_google_results": true, "enable_memory": true, "input_text": mainForm.plsAsk.text, "history_data": history, } var http = inet.http(); http.addHeaders = head; var respstr = http.post(url+lang, web.json.stringify(payload, true)); if respstr { var resp = web.json.parse(respstr, true); tm = time.now() tm.format="%Y年%m月%d日 %H时%M分%S秒"; string.save("/res/dialog.txt", tostring(tm)+'\r\n', true); string.save("/res/dialog.txt", "You >>> "+mainForm.plsAsk.text+'\r\n', true); string.save("/res/dialog.txt", "GPT >>> "+resp.message+'\r\n', true); table.push(history, { "is_sent": true, "message": mainForm.plsAsk.text }, { "is_sent": false, "message": resp.message }, ) mainForm.plsLog.editBox.print("You >>> "+mainForm.plsAsk.text); mainForm.plsLog.editBox.print("GPT >>> "+resp.message); mainForm.plsAsk.text = null; } mainForm.plsSend.disabled = false; } mainForm.plsLang.checked = true; mainForm.show(); return win.loopMessage();
虽然我不是调用的openai的api,不过原理是一样的。
Jacen He 有个关于“编写 ChatGPT 桌面版“针对现在新版的gpt4和比较流行的自定义http url,分发key的方式有更新
import win.ui; import console.int; import web.rest.jsonClient; /*DSG{{*/ mainForm = win.form(text="Demo";right=757;bottom=467) mainForm.add( answer={cls="edit";left=132;top=145;right=670;bottom=325;edge=1;hscroll=1;multiline=1;vscroll=1;z=4}; button={cls="button";text="发送";left=58;top=376;right=223;bottom=417;z=1}; question={cls="edit";left=140;top=30;right=683;bottom=73;edge=1;multiline=1;z=2}; static={cls="static";text="问题";left=36;top=33;right=105;bottom=63;font=LOGFONT(h=-20);transparent=1;z=3}; static2={cls="static";text="答案";left=39;top=152;right=98;bottom=190;font=LOGFONT(h=-20);transparent=1;z=5} ) /*}}*/ import console; console.open(); //适合用gpt 3.5,4,自定义http和分发的key mainForm.button.oncommand = function(id, event) { //创建 API 客户端 var http = web.rest.jsonClient(); //设置 Key 或者获取 Key 的网址/或自定义分发的key http.setAuthToken("国内套壳提供的key"); //引入 OpenAI 接口/或自定义接口 var ai = http.api("https://套壳地址/v1/chat/"); //调用 API var ret = ai.completions({ "messages": { { "role": "user", "content": mainForm.question.text } }, "stream": false, "model": "gpt-4", "temperature": 0.5, "presence_penalty": 0, "frequency_penalty": 0, "top_p": 1 }) //显示回复 console.dumpTable(ret) mainForm.answer.clear(); mainForm.answer.print(ret.choices[1].message.content); } mainForm.show(); return win.loopMessage();
登录后方可回帖
新模型 API介绍
老模型API介绍