-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt.min.js
1 lines (1 loc) · 2.93 KB
/
chatgpt.min.js
1
let OPENAI_CONFIGURATION={base_url:"https://api.openai.com"};async function fetchGET(t,e,o){t=await fetch(""+OPENAI_CONFIGURATION.base_url+t,{method:"GET",headers:e});return t.ok?[await t.json(),0,""]:(o?o(t):console.log(t),[null,t.status,t.statusText])}async function fetchPOST(t,e,o,s){t=await fetch(""+OPENAI_CONFIGURATION.base_url+t,{method:"POST",headers:e,body:JSON.stringify(o)});return t.ok?[await t.json(),0,""]:(s?s(t):console.log(t),[null,t.status,t.statusText])}function OAccount(t,e,o){this.org=t||"",this.api_key=e||"",this.default_model=o||""}function Models(t){this.account=t}function OHistory(){this.history=[],this.token_usages=[],this.finish_reasons=[]}function historyElement(t,e){return{role:t,content:e}}function OChat(t,e){this.account=t||new OAccount,this.history=new OHistory,this.model=e||t.default_model}OAccount.prototype.headers=function(){return{Authorization:"Bearer "+this.api_key,"Content-Type":"application/json","OpenAI-Organization":this.org}},OAccount.prototype.save=async function(){await chrome.storage.sync.set({org:this.org||"",api_key:this.api_key||"",default_model:this.default_model||""})},OAccount.prototype.load=async function(){var t=await chrome.storage.sync.get(["org","api_key","default_model"]);this.org=t.org||"",this.api_key=t.api_key||"",this.default_model=t.default_model||""},Models.prototype.getType=async function(t){var e={gpt:"CHAT",ada:"TEXT",babbage:"TEXT",code:"TEXT",curie:"TEXT",davinci:"TEXT","text-ada":"TEXT","text-babbage":"TEXT","text-curie":"TEXT","text-davinci":"TEXT","text-embedding":"TEXT","text-search":"TEXT","text-similarity":"TEXT","text-moderation":"MODERATION",DALL:"IMAGE",whisper:"AUDIO"};for(const o in e)if(t.startsWith(o))return e[o];return"UNKNOWN"},Models.prototype.list=async function(t){var[t,e,o]=await fetchGET("/v1/models",this.account.headers(),t);if(0!=e)return[null,e,o];t=t.data||[];t.sort((t,e)=>t.id.localeCompare(e.id));for(const s of t)s.type=await this.getType(s.id);return[t,e,o]},Models.prototype.retrieve=async function(t,e){var[t,e,o]=await fetchGET("/v1/models/"+t,this.account.headers(),e);return[t,e,o]},OAccount.prototype.models=function(){return new Models(this)},OHistory.prototype.append=function(t,e,o){this.history.push(t),o=o||null,this.token_usages.push(e=e||null),this.finish_reasons.push(o)},OAccount.prototype.saveToStorage=async function(){await chrome.storage.sync.set({org:this.org,api_key:this.api_key,default_model:this.default_model||""})},OChat.prototype.create=async function(t,e,o,s,a){e=e||"user",(o=o||{}).model=this.model,o.messages=this.history.history,this.history.append(historyElement(e,t)),s&&s(this);var[e,t,s]=await fetchPOST("/v1/chat/completions",this.account.headers(),o,a);return 0==t?(o=e.choices[0].message,a=e.usage,e=e.choices[0].finish_reason,this.history.append(historyElement(o.role,o.content),a,e),[o,t,s]):[null,t,s]},OChat.prototype.ask=async function(t,e,o,s,a){var[t,e,o]=await this.create(t,e,o,s,a);return[t,e,o]},console.log("ChatGPT.js loaded.");