Applying semantic-router to the most recent interaction in a conversation can work for many cases but it misses scenarios where information provided in the latest interaction.
from semantic_router.schema import Conversation, Message
from semantic_router.encoders import FastEmbedEncoder
messages = [
"User: Hello! Can you tell me the latest news headlines?",
"Bot: Hi! Sure, here are the top news headlines for today...",
"User: That's quite interesting. I'm also looking for some new music to listen to.",
"Bot: What genre do you prefer?",
"User: I like pop music.",
"Bot: You might enjoy the latest album by Dua Lipa.",
"User: I'll give it a listen. Also, I'm planning a trip and need some travel tips.",
"Bot: Sure, where are you planning to go?",
"User: I'm thinking of visiting Italy.",
"Bot: Italy is a beautiful country. Make sure to visit the Colosseum in Rome and the canals in Venice.",
"User: Those sound like great suggestions. I also need some help with my diet.",
"Bot: What kind of diet are you following?",
"User: I'm trying to eat more protein.",
"Bot: Include lean meats, eggs, and legumes in your diet for a protein boost.",
"User: Thanks for the tips! I'll talk to you later.",
"Bot: You're welcome! Don't hesitate to reach out if you need more help.",
"User: I appreciate it. Goodbye!",
"Bot: Goodbye! Take care!",
]
encoder = FastEmbedEncoder(model_name="sentence-transformers/all-MiniLM-L6-v2")
convo = Conversation(
messages=[
Message(role=m.split(": ")[0], content=m.split(": ")[1]) for m in messages
]
)
convo.split_by_topic(
encoder=encoder, threshold=0.72, split_method="cumulative_similarity_drop"
)
/Users/jamesbriggs/opt/anaconda3/envs/decision-layer/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm 100%|██████████| 83.2M/83.2M [00:09<00:00, 8.45MiB/s]
{'split 1': ['User: Hello! Can you tell me the latest news headlines?', 'Bot: Hi! Sure, here are the top news headlines for today...'], 'split 2': ["User: That's quite interesting. I'm also looking for some new music to listen to.", 'Bot: What genre do you prefer?', 'User: I like pop music.', 'Bot: You might enjoy the latest album by Dua Lipa.', "User: I'll give it a listen. Also, I'm planning a trip and need some travel tips.", 'Bot: Sure, where are you planning to go?'], 'split 3': ["User: I'm thinking of visiting Italy.", 'Bot: Italy is a beautiful country. Make sure to visit the Colosseum in Rome and the canals in Venice.'], 'split 4': ['User: Those sound like great suggestions. I also need some help with my diet.', 'Bot: What kind of diet are you following?', "User: I'm trying to eat more protein.", 'Bot: Include lean meats, eggs, and legumes in your diet for a protein boost.', "User: Thanks for the tips! I'll talk to you later.", "Bot: You're welcome! Don't hesitate to reach out if you need more help.", 'User: I appreciate it. Goodbye!', 'Bot: Goodbye! Take care!']}