-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve gpt command and use redis cache to prevent generate sa…
…me thing again
- Loading branch information
Showing
4 changed files
with
142 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.