浅尝chatgpt新角色-function

By xuncv at 2023-06-15 • 1人收藏 • 403人看过

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);

QQ图片20230615135601.png


2 个回复 | 最后更新于 2023-06-15
2023-06-15   #1

hoho....

原来要通过插件来完成的功能 ,现在直接可以了

天气问题是经典..

2023-06-15   #2

回复#1 @hi_aardio :

简单套了下官方的文档。这个东西最适合的还是做智能客服或类似智能音箱的交互设备


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



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

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

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...