Skip to content

Commit

Permalink
chore: improve gpt command and use redis cache to prevent generate sa…
Browse files Browse the repository at this point in the history
…me thing again
  • Loading branch information
dyaskur committed Nov 3, 2024
1 parent a534c5a commit 16592c0
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 42 deletions.
33 changes: 28 additions & 5 deletions helpers/gpt.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
import OpenAI from 'openai';
import Redis from 'ioredis';
import crypto from 'crypto';

/**
* @param {string} something - thing that will get a random of it
* @param {string} lastAnswers - last answers to generate new thing
* @returns {string} the prompt for gpt
*/
function generatePrompt(something) {
return `Please give me a random ${something}`;
function getSomethingToAsk(something, lastAnswers) {
if (lastAnswers) {
return `${something} (last answer: ${lastAnswers})`;
}
return `${something}`;
}

// eslint-disable-next-line require-jsdoc
export async function getRandomFromGpt(something) {
const somethingMd5 = crypto.createHash('md5').update(something).digest('hex');
const client = new Redis(process.env.REDIS_URL);
const lastAnswers = await client.get(somethingMd5);
const openai = new OpenAI({
apiKey: process.env?.OPENAI_API_KEY,
});
console.log('lastAnswers', lastAnswers);

const completion = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{role: 'user', content: generatePrompt(something)}],
temperature: 0.6,
max_tokens: 65,
messages: [
{
role: 'system', content: 'You are a system that generate randomly thing from user input. ' +
'If user dont ask how many thing to generate, just give 1 result' +
'don\'t use common answer.' +
'if user says animals, you list 1000 animals from the database and pick at random,' +
'to the point, no say \'sure\'' +
'',
},
{role: 'user', content: getSomethingToAsk(something, lastAnswers)},
],
temperature: 1,
max_tokens: 75,
});

let answer = 'Sorry I don\'t know what you mean';
if (completion?.choices.length > 0) {
answer = completion.choices.map((a) => a.message.content.replace(/(\r\n|\n|\r)/gm, '')).join('\n');
}
await client.set(somethingMd5, lastAnswers ? lastAnswers + ', ' + answer : answer);
return answer;
}

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function app(req, res) {
console.log(JSON.stringify(event));
console.log(event.type,
event.common?.invokedFunction || event.message?.slashCommand?.commandId || event.message?.argumentText,
event.user.displayName, event.user.email, event.space.type, event.space.name, event.threadKey);
event.user?.displayName, event.user?.email, event.space.type, event.space.name, event.threadKey);
event.threadKey = event.threadKey ?? event.message?.thread?.name;
let reply = {};
// Dispatch slash and action events
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@google-cloud/tasks": "^3.1.2",
"googleapis": "^118.0.0",
"ioredis": "^5.4.1",
"openai": "^4.68.4"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 16592c0

Please sign in to comment.