From 1405c3bbb0b6ecb64dbb43c97d3d6c8cbbaf13d7 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Wed, 30 Oct 2024 10:21:02 -0400 Subject: [PATCH] two llms talking --- examples/two_llms_talking.py | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 examples/two_llms_talking.py diff --git a/examples/two_llms_talking.py b/examples/two_llms_talking.py new file mode 100644 index 0000000..4971867 --- /dev/null +++ b/examples/two_llms_talking.py @@ -0,0 +1,58 @@ +import simplemind as sm +import time + + +class ConversationPlugin(sm.BasePlugin): + def post_send_hook(self, conversation, response): + # Print the LLM model and the response text. + print(f"{conversation.llm_model}:\n{response.text.strip()}\n\n------------\n") + + +def have_conversation(rounds=3): + # Create two conversations - one for each AI + with ( + sm.create_conversation( + llm_model="claude-3-5-sonnet-20241022", llm_provider="anthropic" + ) as claude_conv, + sm.create_conversation( + llm_model="llama3.2", llm_provider="ollama" + ) as llama_conv, + ): + + # Add our plugin to both + plugin = ConversationPlugin() + claude_conv.add_plugin(plugin) + llama_conv.add_plugin(plugin) + + # Start the conversation + prompt = "What do you think about the future of artificial intelligence? Please keep your response brief." + claude_conv.add_message("user", prompt, meta={}) + claude_response = claude_conv.send() + + # Have them discuss back and forth + for _ in range(rounds): + # Llama responds to Claude + llama_conv.add_message( + "user", + f"Respond to this statement from another AI: {claude_response.text}", + meta={}, + ) + llama_response = llama_conv.send() + + time.sleep(1) # Add a small delay between responses + + # Claude responds to Llama + claude_conv.add_message( + "user", + f"Respond to this statement from another AI: {llama_response.text}", + meta={}, + ) + claude_response = claude_conv.send() + + time.sleep(1) + + +if __name__ == "__main__": + print("Starting AI conversation...\n") + have_conversation() + print("\nConversation ended.")