Created
February 13, 2025 02:56
-
-
Save LeonNicholls/b3fc710ca4e4e62e61118a08e4859c31 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const { GoogleGenerativeAI } = require('@google/generative-ai'); | |
| // Create a simple class to manage conversations between LLM instances | |
| class ConversationSimulator { | |
| constructor(apiKey) { | |
| this.genAI = new GoogleGenerativeAI(apiKey); | |
| this.model = this.genAI.getGenerativeModel({ model: 'gemini-pro' }); | |
| this.CONSENSUS_MARKER = '[CONSENSUS]'; | |
| } | |
| // Create a character with a specific role | |
| async createCharacter(role, personality) { | |
| const chat = this.model.startChat({ | |
| history: [], | |
| generationConfig: { | |
| temperature: 0.9, | |
| maxOutputTokens: 1000, | |
| }, | |
| }); | |
| // Initialize the character with their role and personality | |
| await chat.sendMessage( | |
| `You are ${role}. ${personality} Please stay in character during our conversation. | |
| Respond in a natural conversational way without mentioning that you are an AI.` | |
| ); | |
| return chat; | |
| } | |
| // Check if consensus was reached | |
| hasReachedConsensus(message) { | |
| return message.includes(this.CONSENSUS_MARKER); | |
| } | |
| // Simulate a conversation between two characters | |
| async simulateConversation(character1, character2, topic, maxTurns = 5) { | |
| const conversation = { | |
| turns: [], | |
| consensusReached: false, | |
| finalTurn: 0 | |
| }; | |
| let currentSpeaker = character1; | |
| let currentListener = character2; | |
| let currentMessage = `Let's discuss: ${topic}`; | |
| for (let turn = 0; turn < maxTurns; turn++) { | |
| console.log(`\nTurn ${turn + 1}:`); | |
| console.log(`Message: ${currentMessage}\n`); | |
| // Get response from current speaker | |
| const response = await currentListener.sendMessage(currentMessage); | |
| const responseText = response.response.text(); | |
| // Store the conversation turn | |
| conversation.turns.push({ | |
| speaker: turn % 2 === 0 ? 'Character 1' : 'Character 2', | |
| message: responseText | |
| }); | |
| console.log(`Response: ${responseText}\n`); | |
| // Check for consensus | |
| if (this.hasReachedConsensus(responseText)) { | |
| console.log('Consensus reached! Ending conversation.'); | |
| conversation.consensusReached = true; | |
| conversation.finalTurn = turn + 1; | |
| break; | |
| } | |
| // Switch speakers for next turn | |
| [currentSpeaker, currentListener] = [currentListener, currentSpeaker]; | |
| currentMessage = responseText; | |
| if (turn > maxTurns / 2) { | |
| currentMessage += '\n\n' + `Note: If we reach a consensus, either of us can end our response with "${this.CONSENSUS_MARKER}"`; | |
| } | |
| } | |
| // If we reached max turns without consensus | |
| if (!conversation.consensusReached) { | |
| conversation.finalTurn = maxTurns; | |
| } | |
| return conversation; | |
| } | |
| } | |
| // Example usage | |
| async function main() { | |
| // Replace with your actual API key | |
| const simulator = new ConversationSimulator('YOUR_API_KEY'); | |
| // Create two characters | |
| const writer = await simulator.createCharacter( | |
| 'a passionate screenwriter', | |
| 'You are creative, detail-oriented, and care deeply about storytelling. You want to create the best possible story while being open to collaboration.' | |
| ); | |
| const director = await simulator.createCharacter( | |
| 'an experienced film director', | |
| 'You are visually-minded, practical, and focused on bringing stories to life on screen. You are collaborative and solution-oriented.' | |
| ); | |
| // Start a conversation between them | |
| const conversation = await simulator.simulateConversation( | |
| writer, | |
| director, | |
| 'Ideas for a chase sene in our upcoming thriller movie.', | |
| 10 // Maximum number of conversation turns | |
| ); | |
| console.log('\nFull Conversation Summary:'); | |
| conversation.turns.forEach((turn, index) => { | |
| console.log(`\n${turn.speaker}:`); | |
| console.log(turn.message); | |
| }); | |
| console.log('\nConversation Stats:'); | |
| console.log(`Total Turns: ${conversation.finalTurn}`); | |
| console.log(`Consensus Reached: ${conversation.consensusReached}`); | |
| } | |
| // Run the simulation | |
| main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment