Skip to content

Commit

Permalink
Refactor RirikoAI-NLP.js, remove duplicated response, implement retri…
Browse files Browse the repository at this point in the history
…es, better prompt trimming (#170)

* Refactor RirikoAI-NLP.js

* Fix NLPCloudProvider.test.js test
  • Loading branch information
earnestangel authored Jul 9, 2023
1 parent bfca61c commit 0a73f35
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 206 deletions.
9 changes: 8 additions & 1 deletion src/app/Providers/AI/NLPCloudProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ class NLPCloudProvider extends AIProviderBase {
return this.nlpCloudClient;
}

/**
* Send chat to NLP Cloud
* @param {String} messageText
* @param {String} context
* @param {Array} history
* @returns {Promise<*>}
*/
async sendChat(messageText, context, history) {
try {
// Send request to NLP Cloud.
const response = await this.nlpCloudClient.chatbot(
messageText,
context + history
context + history.join("\n")
);

if (typeof response.data["response"] !== "undefined") {
Expand Down
2 changes: 1 addition & 1 deletion src/app/Providers/AI/NLPCloudProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("NLPCloudProvider", () => {
const response = await nlpCloudProvider.sendChat(
"hello",
"This is a chat",
{}
[]
);
expect(response).toBe("example response");
});
Expand Down
11 changes: 9 additions & 2 deletions src/app/Providers/AI/OpenAIProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ class OpenAIProvider extends AIProviderBase {
return this.openAiClient;
}

/**
* Send chat to OpenAI
* @param {String} messageText
* @param {String} context
* @param {Array} history
* @returns {Promise<*>}
*/
async sendChat(messageText, context, history) {
try {
const model = config.AI.GPTModel; // davinci or gpt35
const prompt = context + history.join("\n") + "\n";
const prompt = `${context}${history.join("\n")}\nHuman: ${messageText}\n`;

if (model === "gpt35") {
const convertedChatHistory = history.map((message) => {
Expand Down Expand Up @@ -61,7 +68,7 @@ class OpenAIProvider extends AIProviderBase {
model: "text-davinci-003",
prompt,
temperature: 1,
max_tokens: 1900,
max_tokens: 2000,
top_p: 0.2,
frequency_penalty: 0.9,
presence_penalty: 1.9,
Expand Down
Loading

0 comments on commit 0a73f35

Please sign in to comment.