浅尝chatgpt新角色-function
6月14日openai公布了升级后的模型“gpt-3.5-turbo-0613”,实现了通过语言模型调用程序函数,并将运行结果交给chatgpt总结处理输出。语言模型可以作为中间层,链接用户层和程序层。
import console;
import web.rest.jsonClient
import web.json
var apiKey = "sk-xxxx"
var client = web.rest.jsonClient(,"http://127.0.0.1:10809")
client.setAuthToken(apiKey)
console.open()
aiapi = client.api("https://api.openai.com/v1/")
messgaes = {
{"role": "system", "content": "你是由史塔克公司开发的AI程序,名字叫贾维斯。你不是chatgpt,也和openai没有任何关系"},
}
get_current_weather = function(location,unit){
return web.json.stringify({ temperature: 22, unit: "celsius", description: "Sunny" });
}
ask = function(text){
if(text){
table.push( messgaes,{"role": "user", "content": text} )
}
res = aiapi.chat.completions.post({
model:"gpt-3.5-turbo-0613",
//stream:true,
messages:messgaes,
functions: {
{
name: "get_current_weather",
description: "Get the current weather in a given location",
parameters: {
["type"]: "object",
properties: {
location: {
["type"]: "string",
description: "The city and state, e.g. San Francisco, CA"
},
unit: {
["type"]: "string",
enum: {"celsius", "fahrenheit"}
}
},
required: {"location"}
}
}
}
})
if(res.choices[1].finish_reason=="function_call"){
var fc = res.choices[1].message.function_call
var weather = self[[fc.name]] ? self[fc.name](fc.arguments)
table.push( messgaes, {role: "function", name: fc.name, content: web.json.stringify(weather)} )
return ask();
}
if(res.choices[1].finish_reason == "stop"){
table.push(messgaes,{role: "assistant", content: res.choices[1].message.content})
console.log("assistant:",res.choices[1].message.content)
}
}
while(1){
console.log('\n')
var text = console.getText( "输入问题:" )
ask(text)
}
console.pause(true);
登录后方可回帖
hoho....
原来要通过插件来完成的功能 ,现在直接可以了
天气问题是经典..