/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2024 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ fun cbCloseEdit(p)= let p -> [ctrlurl ctrlkey ctrlmodel ctrlrole ctrltemp ctrlmaxtoken] in let getEdCtrlTextLineValue ctrlurl -> url in let getEdCtrlTextLineValue ctrlkey -> key in let getSelectedEdCtrlSelect ctrlmodel -> model in let getEdCtrlEditTextValue ctrlrole -> role in let getEdCtrlFloatValue ctrltemp -> temp in let ftoi getEdCtrlFloatValue ctrlmaxtoken -> maxtoken in ["url" url]:: ["key" key]:: ["model" model]:: ["role" role]:: ["temp" ftoa temp]:: ["maxtoken" itoa maxtoken]:: nil;; //TODO list models from https://platform.openai.com/docs/api-reference/models/list with https://api.openai.com/v1/models fun cbGetModels(url, data, p) = let p -> [ctrls prompt] in let ctrls -> [ctrlurl ctrlkey ctrlmodel model] in if (data != nil) then ( let parseJson data -> jsonvalue in let getJsonObjectValue jsonvalue "data" -> jsdata in let getJsonArray jsdata -> jsmodels in let nil -> lmodels in if (jsmodels != nil) then ( while (jsmodels != nil) do ( let hd jsmodels -> jsmodel in let getJsonObjectValue jsmodel "id" -> jsid in let getJsonString jsid -> sid in set lmodels = sid::lmodels; set jsmodels = tl jsmodels; ); set lmodels = revertlist lmodels; fillEdCtrlSelect ctrlmodel lmodels; selectEdCtrlSelect ctrlmodel model; 0; ) else ( let getJsonObjectValue jsonvalue "error" -> jserror in let getJsonObjectValue jserror "message" -> jsmessage in let getJsonString jsmessage -> errormesg in if (errormesg != nil) then ( addLogMessage strcatn "ChatGPT PlugIT : "::url::" failed! > "::errormesg::nil; 0; ) else ( addLogMessage strcatn "ChatGPT PlugIT : "::url::" failed! > "::data::nil; 0; ); 0; ); ) else ( addLogMessage strcatn "ChatGPT PlugIT : "::url::" failed or empty response!"::nil; 0; ); 0;; fun getModels(p)= let p -> [ctrlurl ctrlkey ctrlmodel model] in let getEdCtrlTextLineValue ctrlurl -> url in let getEdCtrlTextLineValue ctrlkey -> key in let "Content-Type: application/json"::(strcat "Authorization: Bearer " key)::nil -> header in let strcat url "/v1/models" -> url in downloadFilePost url nil header mkfun3 @cbGetModels [p 0]; 0;; fun cbBtnUpdate(ctrlbtn, p)= getModels p;; fun dynamicedit(winstr, inst, viewstr, applybtn)= let [400 300] -> [iw ih] in ( setEdWindowSize winstr iw ih; let getPluginInstanceParam inst "url" -> burl in let if (burl == nil) then "https://api.openai.com" else burl -> url in let getPluginInstanceParam inst "key" -> key in let if key == nil then "" else key -> key in let (getPluginInstanceParam inst "model") -> model in let if model == nil then "gpt-3.5-turbo" else model -> model in let (getPluginInstanceParam inst "role") -> role in let if role == nil then "You are an consistent generic assistant." else role -> role in let atof (getPluginInstanceParam inst "temp") -> temp in let if temp == nil then 0.7 else temp -> temp in let atoi (getPluginInstanceParam inst "maxtoken") -> maxtoken in let if maxtoken == nil then 500 else maxtoken -> maxtoken in let 10 -> ypos in let crEdCtrlLabel winstr 10 ypos + 2 160 20 (loc "OS3DCHATGPT_0000") nil -> lurl in let crEdCtrlTextLine winstr 180 ypos 200 20 url nil EDWIN_RESIZE_MW -> ctrlurl in let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 (loc "OS3DCHATGPT_0001") nil -> lkey in let crEdCtrlTextLine winstr 180 ypos 200 20 key nil EDWIN_RESIZE_MW -> ctrlkey in let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 (loc "OS3DCHATGPT_0002") nil -> lmodel in let crEdCtrlSelect winstr 180 ypos 200 120 EDWIN_RESIZE_MW -> ctrlmodel in let crEdCtrlButton winstr 380 ypos 20 20 "R" nil -> updatebtn in let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 (loc "OS3DCHATGPT_0003") nil -> ltemp in let crEdCtrlFloat winstr 180 ypos 100 20 temp 0.1 2.0 0.01 2 nil EDWIN_RESIZE_MW -> ctrltemp in let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 (loc "OS3DCHATGPT_0004") nil -> ltoken in let crEdCtrlFloat winstr 180 ypos 100 20 itof maxtoken 1.0 4096.0 1.0 0 nil EDWIN_RESIZE_MW -> ctrlmaxtoken in let crEdCtrlLabel winstr 10 ((set ypos = ypos + 25) + 2) 160 20 strcat (loc "OS3DCHATGPT_0005") ":" nil -> lrole in let crEdCtrlEditText winstr 10 (set ypos = ypos + 25) (iw - 20) 140 role nil EDWIN_RESIZE_MW -> ctrlrole in ( if ((burl == nil) || ((!strcmp url "https://api.openai.com") && (!strcmp key ""))) then ( fillEdCtrlSelect ctrlmodel "gpt-4o"::"gpt-4"::"gpt-4-32k"::"gpt-4-turbo"::"gpt-3.5-turbo"/*::"davinci-002"*/::nil; 0; ) else ( getModels [ctrlurl ctrlkey ctrlmodel model]; 0; ); setEdCtrlButtonCb updatebtn mkfun2 @cbBtnUpdate [ctrlurl ctrlkey ctrlmodel model]; selectEdCtrlSelect ctrlmodel model; [mkfun1 @cbCloseEdit [ctrlurl ctrlkey ctrlmodel ctrlrole ctrltemp ctrlmaxtoken] nil]; ); );;