79181021

Date: 2024-11-12 12:09:26
Score: 0.5
Natty:
Report link

Your best bet is to use langchain, and initialize two different conversation chains, using two different instances of ConversationBufferMemory(), in order to retain different memories. You can then decide how to handle the back and forth between the two bot, changing the behavior inside the loop

from langchain_community.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory

number_of_turns = 5
llm_model = ChatOpenAI(model_name="gpt-4o-mini", openai_api_key=OPENAI_API_KEY)

# initialize two different memories for the two bot
bot1_memory = ConversationBufferMemory()
bot2_memory = ConversationBufferMemory()

# Create two conversation chains with separate memories
bot1_chain = ConversationChain(llm=llm_model, memory=bot1_memory)
bot2_chain = ConversationChain(llm=llm_model, memory=bot2_memory)

# Start messages for each bot
bot1_start = (
    "I want to play a game with you: ... my word start with a, can you guess it?"
)
bot2_start = (
    "I want to play a game with you too: ...my word start with b, can you guess it?"
)

# first turn
bot1_reply = bot1_chain.run(input=bot1_start)
print(f"Bot 1: {bot1_reply}")
bot2_reply = bot2_chain.run(input=bot2_start)
print(f"Bot 2: {bot2_reply}")

# loop for as many turns as you want
for turn in range(number_of_turns):
    print(f"Turn number {turn}")
    bot1_reply = bot1_chain.run(input=bot2_reply)
    print(f"Bot 1: {bot1_reply}")
    bot2_reply = bot2_chain.run(input=bot1_reply)
    print(f"Bot 2: {bot2_reply}")
Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Andrea Figini