Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Asiri Hewage committed Jan 17, 2023
1 parent 29baae5 commit e26fe5b
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 355 deletions.
6 changes: 3 additions & 3 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const opts = {
temperature: 0.9,
max_tokens: 150,
top_p: 1,
frequency_penalty: 0.0,
frequency_penalty: 0.1,
presence_penalty: 0.6
};


ChatGPTIntl(text, openAiKey, opts, lang).then((res) => {
console.log(res);
console.log("Demo result", res);
}).catch((er)=> {
console.log(er);
console.log("Demo error", er);
});
60 changes: 37 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,30 @@ const ChatGPTIntl = async (text, openAiKey, opts, lang) => {

const ChatGPT_Intl = () => {
return new Promise(async (resolve, reject) => {
const configuration = new Configuration({
apiKey: openAiKey
return await Translator(text, { to: "en" }).then( async (translateIn) => {
const configuration = new Configuration({
apiKey: openAiKey
});
const openai = new OpenAIApi(configuration);
return await openai.createCompletion({
model: opts.model.toString(),
prompt: translateIn.text,
temperature: parseFloat(opts.temperature),
max_tokens: parseInt(opts.max_tokens),
top_p: parseInt(opts.top_p),
frequency_penalty: parseFloat(opts.frequency_penalty),
presence_penalty: parseFloat(opts.presence_penalty)
});
}).then( async (response) => {
return await Translator(response.data.choices[0].text, {to: lang})
}).then((translateOut) => {
return resolve({response: translateOut.text, query: text, lang: lang});
}).catch( er => {
return reject({error: er})
});
const translateIn = await Translator(text, { to: "en" });

const openai = new OpenAIApi(configuration);

const response = await openai.createCompletion({
model: opts.model || "text-davinci-003",
prompt: translateIn.text,
temperature: opts.temperature || 0.9,
max_tokens: opts.max_tokens || 150,
top_p: opts.top_p || 1,
frequency_penalty: opts.frequency_penalty || 0.0,
presence_penalty: opts.presence_penalty || 0.6,
});

const translateOut = await Translator(response.data.choices[0].text, { to: lang | "en" });

return translateOut.text
? resolve({response: translateOut.text, query: text, lang: lang})
: reject({error: errors[2]});
}
);
}
};

if (text && openAiKey && lang && opts && opts.model && opts.temperature && opts.max_tokens && opts.top_p && opts.frequency_penalty && opts.presence_penalty) {
if(!isSupported(lang)){
Expand All @@ -51,7 +50,22 @@ const ChatGPTIntl = async (text, openAiKey, opts, lang) => {
return ChatGPT_Intl();
}
}else {
return Promise.reject({error: errors[1], data: {opts, text , openAiKey , lang}});
return Promise.reject({
error: errors[1],
data: {
opts: !!opts ? {
model: !!opts.model,
temperature: !!opts.temperature,
max_tokens: !!opts.max_tokens,
top_p: !!opts.top_p,
frequency_penalty: !!opts.frequency_penalty,
presence_penalty: !!opts.presence_penalty
} : false ,
text: !!text ,
openAiKey : !!openAiKey ,
lang: !!lang
}
});
}

}
Expand Down
Loading

0 comments on commit e26fe5b

Please sign in to comment.