How to save messages completion in after execute process_commands_query many times
Assistant
To save messages completions after executing process_commands_query multiple times, you can use a data structure like an array or an object to store these completions. You can then save this data to a file (e.g., JSON file) at regular intervals or after each execution.
Here’s an approach to achieve this:
Initialize a Storage Structure: Create an array to store completions.
Modify process_commands_query: Add logic to store each completion in this array.
Save to File: Use a function to write the array to a file.
Below is the updated version of your function with these additions:
javascript
const fs = require('fs');
const path = require('path');
const completionsFilePath = path.join(__dirname, 'completions.json');
let completions = [];
// Load existing completions from the file if it exists
if (fs.existsSync(completionsFilePath)) {
const data = fs.readFileSync(completionsFilePath, 'utf8');
completions = JSON.parse(data);
}
async function process_commands_query(voice_Connection, client_openai, txt, mapKey, user) {
if (txt && txt.length) {
let val = guildMap.get(mapKey);
val.text_Channel.send(user.username + ': ' + txt);
const completion = await client_openai.chat.completions.create({
model: "llama3-70b-8192",
messages: [
{"role": "system", "content": "Вы - полезный помощник."},
{"role": "user", "content": txt}
],
temperature: 1,
max_tokens: 50,
top_p: 1,
stream: false,
});
const responseText = completion.choices[0].message.content;
console.log('Completion: ' + responseText);
// Save the completion text along with other relevant information
completions.push({
user: user.username,
query: txt,
response: responseText,
timestamp: new Date().toISOString()
});
// Write completions to the JSON file
fs.writeFileSync(completionsFilePath, JSON.stringify(completions, null, 2), 'utf8');
const speaker = JSON.parse(fs.readFileSync('./default_speaker.json', 'utf8'));
}
}
module.exports = { process_commands_query };
Explanation:
Initialization and Loading:
The completions array is initialized and populated with existing data from completions.json if the file exists.
Modify Function to Store Completions:
Each time process_commands_query is called, a new entry is added to the completions array containing the user, query, response, and a timestamp.
Save to File:
After each call to process_commands_query, the completions array is written to completions.json.
This setup ensures that all completions are persistently stored in a file, allowing you to review them later. If you have a high frequency of function calls, you might want to batch save operations or use a more robust database solution for scalability.