Skip to content

Commit

Permalink
Merge pull request #23 from FalkorDB/fix-chat-history
Browse files Browse the repository at this point in the history
History to Cypher generation prompt
  • Loading branch information
swilly22 authored Oct 22, 2024
2 parents 275dfc6 + 6e35f68 commit 7f42654
Show file tree
Hide file tree
Showing 23 changed files with 233 additions and 285 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,9 @@ You can update the KG at any time by processing more sources with the `process_s

### Graph RAG

At this point, you have a Knowledge Graph that can be queried using this SDK. You can use the `ask` method for single questions or `chat_session` for conversations.
At this point, you have a Knowledge Graph that can be queried using this SDK. Use the method `chat_session` for start a conversation.

```python
# Single question.
response = kg.ask("What were the last five fights? When were they? How many rounds did they have?")
print(response)

# Conversation.
chat = kg.chat_session()
Expand Down
32 changes: 21 additions & 11 deletions examples/movies/demo-movies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -29,7 +29,7 @@
"True"
]
},
"execution_count": 1,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -59,7 +59,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -84,14 +84,20 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"model = OpenAiGenerativeModel(model_name=\"gpt-4o\")\n",
"\n",
"boundaries = \"\"\"\n",
" Extract only the most relevant information about all the movies, actors, and directors over the text.\n",
" Avoid creating entities for details that can be expressed as attributes.\n",
"\"\"\"\n",
"\n",
"ontology = Ontology.from_sources(\n",
" sources=sources,\n",
" boundaries=boundaries,\n",
" model=model,\n",
")"
]
Expand All @@ -107,7 +113,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -130,29 +136,33 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Lilly Wachowski, Lana Wachowski\n",
"The director is connected to Keanu Reeves through the movies \"The Matrix Reloaded,\" \"The Matrix,\" and \"The Matrix Revolutions,\" in which Keanu Reeves starred.\n"
"The directors of the movie \"The Matrix\" are Lilly Wachowski and Lana Wachowski.\n",
"The directors, Lilly Wachowski and Lana Wachowski, are connected to Keanu Reeves through their collaboration on the movies \"The Matrix,\" \"The Matrix Reloaded,\" and \"The Matrix Revolutions,\" in which Keanu Reeves acted.\n",
"The director of the movie \"Side by Side\" is Chris Kenneally.\n",
"The directors, ordered lexically, are Chris Kenneally, Lana Wachowski, and Lilly Wachowski.\n"
]
}
],
"source": [
"chat = kg.chat_session()\n",
"\n",
"print(chat.send_message(\"Who is the director of the movie The Matrix?\"))\n",
"print(chat.send_message(\"How this director connected to Keanu Reeves?\"))"
"print(chat.send_message(\"How this director connected to Keanu Reeves?\"))\n",
"print(chat.send_message(\"Who is the director of the movie Side by Side?\"))\n",
"print(chat.send_message(\"Order the directors that you mentioned in all of our conversation by lexical order.\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"display_name": "graphrag-sdk-ceBr3E7y-py3.12",
"language": "python",
"name": "python3"
},
Expand All @@ -166,7 +176,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
8 changes: 8 additions & 0 deletions examples/trip/data/restaurants.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,13 @@
"city": "Sorrento",
"rating": 4.7,
"food_type": "Seafood"
},
{
"id": 11,
"name": "Tonnarello",
"description": "Casual stop for pasta, meatballs & other simple Roman dishes, plus patio seating & acoustic guitar.",
"city": "Rome",
"rating": 4.7,
"food_type": "Italian"
}
]
136 changes: 99 additions & 37 deletions examples/trip/demo_orchestrator_trip.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
}
],
"source": [
"\n",
"from json import loads\n",
"from dotenv import load_dotenv\n",
"from graphrag_sdk.orchestrator import Orchestrator\n",
Expand Down Expand Up @@ -271,11 +270,11 @@
")\n",
"\n",
"# Add nodes and edges to the KGs from the data\n",
"with open(\"data/cities.json\") as f:\n",
"with open(\"examples/trip/data/cities.json\") as f:\n",
" cities = loads(f.read())\n",
"with open(\"data/restaurants.json\") as f:\n",
"with open(\"examples/trip/data/restaurants.json\") as f:\n",
" restaurants = loads(f.read())\n",
"with open(\"data/attractions.json\") as f:\n",
"with open(\"examples/trip/data/attractions.json\") as f:\n",
" attractions = loads(f.read())\n",
"\n",
"for city in cities:\n",
Expand Down Expand Up @@ -394,7 +393,6 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"# Initialize the orchestrator and register agents\n",
"orchestrator = Orchestrator(\n",
" model,\n",
Expand All @@ -404,7 +402,7 @@
"orchestrator.register_agent(attractions_agent)\n",
"\n",
"# Query the orchestrator\n",
"runner = orchestrator.ask(\"Create a two-day itinerary for a trip to Rome. Please don't ask me any questions; just provide the best itinerary you can.\")"
"runner = orchestrator.ask(\"Create a two-day itinerary for a trip to Rome. Please don't ask me any questions. Just provide the best itinerary you can.\")"
]
},
{
Expand All @@ -416,64 +414,128 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Thank you for your question about creating a two-day itinerary for a trip to Rome. Here is a detailed itinerary that covers key attractions and recommended dining options, thoughtfully curated to give you the best experience during your stay.\n",
"\n",
"### Day 1 Itinerary\n",
"\n",
"**Morning:**\n",
"Certainly! Here's a detailed two-day itinerary for your trip to Rome, ensuring you make the most out of your visit to this historically rich and vibrant city. We’ve arranged a balanced mix of iconic attractions and delightful dining experiences for your enjoyment.\n",
"\n",
"1. **Colosseum**: Begin your adventure with a visit to the Colosseum, an ancient amphitheater renowned for gladiatorial contests and public spectacles. It is one of Rome's most iconic landmarks, offering an incredible glimpse into the city’s historical grandeur.\n",
"**Day 1: Discovering Ancient Rome and Modern Flavors**\n",
"- **Morning: Colosseum**\n",
" Start your day by visiting the Colosseum, the iconic ancient amphitheater known for its gladiatorial contests and grand spectacles. Allow yourself a few hours to explore the structure and absorb the rich history of ancient Rome that it represents.\n",
"\n",
"**Mid-Morning:**\n",
"- **Lunch: Tonnarello**\n",
" After exploring, head to Tonnarello, a restaurant praised for its casual ambiance and delicious Roman cuisine. Enjoy dishes like homemade pasta and meatballs while soaking in the atmosphere of authentic Roman hospitality. \n",
"\n",
"2. **Vatican Museums**: Next, head to the Vatican Museums, a complex of fascinating museums and galleries showcasing remarkable works of art collected by Popes over centuries. This is a must-see for art and history enthusiasts.\n",
"- **Afternoon: Trevi Fountain**\n",
" Rejuvenated from lunch, stroll towards the Trevi Fountain. This Baroque masterpiece not only offers stunning sculptures but also the tradition of coin tossing, promising a future return trip to Rome.\n",
"\n",
"**Afternoon:**\n",
"- **Dinner: Il Pagliaccio**\n",
" Conclude your first day with dinner at Il Pagliaccio. This Michelin-starred restaurant offers a contemporary Italian dining experience, perfect for indulging in exquisite flavors and creative dishes.\n",
"\n",
"3. **Trevi Fountain**: In the afternoon, visit the Trevi Fountain, a magnificent Baroque fountain famous for its impressive sculptures and the tradition of tossing coins. Relax and enjoy the beautiful surroundings.\n",
"**Day 2: Religious Artistry and Cozy Feasts**\n",
"- **Morning: Vatican Museums**\n",
" Begin your second day by exploring the Vatican Museums, home to centuries of art collected by the Popes. Allocate sufficient time here to truly appreciate the vast collections, including the Sistine Chapel's renowned ceiling.\n",
"\n",
"**Evening:**\n",
"- **Lunch: Trattoria da Enzo**\n",
" For a cozy and traditional Roman dining experience, visit Trattoria da Enzo. Known for its welcoming atmosphere and scrumptious local dishes, it provides the perfect lunchtime escape.\n",
"\n",
"4. **Basilica di Santa Maria Maggiore**: End your day with an evening tour of the Basilica di Santa Maria Maggiore. This major basilica is rich in history and features stunning architecture that will leave you in awe.\n",
"- **Afternoon: Basilica di Santa Maria Maggiore**\n",
" Spend your afternoon visiting the Basilica di Santa Maria Maggiore. This major basilica is renowned for its impressive architecture and deep historical roots, representing a significant piece of Rome's religious heritage.\n",
"\n",
"**Dining Recommendations:**\n",
"- **Evening: Stroll through Rome**\n",
" Conclude your Roman adventure with a leisurely evening walk through the city's streets, taking in the lively Italian spirit as you reminisce over the sights and experiences from the past two days.\n",
"\n",
"- **Breakfast:** Start your day with breakfast at **Trattoria da Enzo**, known for its traditional Roman dishes and warm, welcoming atmosphere.\n",
"- **Lunch:** For lunch, indulge in an elegant dining experience at **Il Pagliaccio**, a Michelin-starred restaurant offering contemporary Italian cuisine.\n",
"- **Dinner:** Continue your culinary journey at **Il Pagliaccio** for an evening meal to savor the exquisite flavors once more.\n",
"\n",
"### Day 2 Itinerary\n",
"This itinerary ensures a wonderful balance between exploration of Rome's historical grandeur and indulgence in its exquisite culinary offerings. Enjoy your trip!\n"
]
}
],
"source": [
"print(runner.output)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Certainly! Based on the two-day itinerary for Rome that was previously provided to you, the restaurant mentioned for dinner on the first day is \"Il Pagliaccio.\" This restaurant is renowned for its elegant ambiance and Michelin-starred contemporary Italian cuisine, offering a perfect dining experience to conclude your day of exploring ancient Rome. Enjoy your dinner and the remarkable culinary delights it has to offer!\n"
]
}
],
"source": [
"runner = orchestrator.ask(\"Please tell me only the name of the restaurant for dinner at the first day that you mention in the itinerary of the trip\")\n",
"print(runner.output)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Certainly! I've updated your two-day itinerary for Rome with a new dinner option for the first day. Here is the revised itinerary:\n",
"\n",
"**Morning:**\n",
"**Day 1: Discovering Ancient Rome and Modern Flavors**\n",
"- **Morning: Colosseum**\n",
" Begin your day with a visit to the iconic Colosseum, where you'll delve into the historical essence of ancient Rome. Take a few hours to explore this magnificent structure and the stories it holds about the past.\n",
"\n",
"1. **Vatican Museums**: Begin your second day by exploring more of the Vatican Museums. Take your time to appreciate the depth and breadth of the collections housed here.\n",
"- **Lunch: Tonnarello**\n",
" Enjoy your lunch at Tonnarello, a restaurant highly praised for its casual ambiance and delicious Roman cuisines such as pasta and meatballs. This spot also features patio seating and acoustic guitar, enhancing your dining experience.\n",
"\n",
"**Mid-Morning:**\n",
"- **Afternoon: Trevi Fountain**\n",
" After lunch, take a leisurely walk to the Trevi Fountain. Revel in the beauty of this Baroque masterpiece, famous for its stunning sculptures and the tradition of tossing a coin for a promise to return.\n",
"\n",
"2. **Trevi Fountain**: Enjoy another visit to the Trevi Fountain to capture the morning light and experience the vibrant atmosphere of this iconic location once more.\n",
"- **Dinner: Tonnarello (Alternative choice)**\n",
" For dinner, continue enjoying the delightful offerings at Tonnarello. With its refreshing and laid-back atmosphere, you can relish more of their simple yet satisfying Roman dishes, ensuring a memorable end to your day.\n",
"\n",
"**Afternoon:**\n",
"**Day 2: Religious Artistry and Cozy Feasts**\n",
"- **Morning: Vatican Museums**\n",
" Begin your second day by exploring the Vatican Museums, where vast collections of art amassed by the Popes await you. Take your time to appreciate these historical artifacts, including the Sistine Chapel's awe-inspiring ceiling.\n",
"\n",
"3. **Basilica di Santa Maria Maggiore**: Revisit the Basilica di Santa Maria Maggiore to explore more of its treasures and immerse yourself deeper into its historical and cultural significance.\n",
"- **Lunch: Trattoria da Enzo**\n",
" Experience a cozy and traditional Roman ambiance at Trattoria da Enzo for lunch. Known for its welcoming atmosphere and scrumptious local dishes, it serves as a perfect midday retreat.\n",
"\n",
"**Dining Recommendations:**\n",
"- **Afternoon: Basilica di Santa Maria Maggiore**\n",
" Spend the afternoon at the Basilica di Santa Maria Maggiore. This basilica is celebrated for its impressive architecture and historical significance, providing a deep dive into Rome's religious heritage.\n",
"\n",
"- **Breakfast:** Start your second day with breakfast again at **Trattoria da Enzo**, for another delightful experience of traditional Roman cuisine.\n",
"- **Lunch:** For lunch, enjoy another delightful meal at **Il Pagliaccio**, where you can continue to relish the modern twists on Italian dishes.\n",
"- **Dinner:** For dinner, you may choose to return to either **Trattoria da Enzo** or **Il Pagliaccio**, depending on whether you prefer a traditional or contemporary Italian dining experience.\n",
"- **Evening: Stroll through Rome**\n",
" End your Roman adventure with a relaxed evening stroll through the city's lively streets, taking in the sights and sounds while reminiscing about your enriching experiences over the past two days.\n",
"\n",
"This itinerary combines Rome's rich history, cultural landmarks, and exceptional cuisine to provide a comprehensive and enjoyable experience over your two days in the city. I hope you find this itinerary both exciting and satisfying as you explore the delights of Rome! Have a fantastic trip!\n"
"This updated itinerary ensures you have a fulfilled and delightful experience while exploring the cultural and culinary richness of Rome. Enjoy your trip!\n"
]
}
],
"source": [
"runner = orchestrator.ask(\"Can you change this restaurant to another one and give me the updated itinerary?\")\n",
"print(runner.output)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Certainly! For the two-day itinerary in Rome that was previously provided, the attraction mentioned for the morning on the first day is the **Colosseum**. This iconic ancient amphitheater is known for its gladiatorial contests and grand spectacles, making it a must-see when exploring the history and culture of ancient Rome. Visiting the Colosseum allows you to delve into Rome's fascinating past and appreciate the architectural marvels and historical significance of this world-renowned site. Enjoy your exploration of this magnificent landmark!\n"
]
}
],
"source": [
"runner = orchestrator.ask(\"Please tell me attraction on the morning at the first day that you mention in the itinerary of the trip\")\n",
"print(runner.output)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "graphrag-sdk-ceBr3E7y-py3.12",
"language": "python",
"name": "python3"
},
Expand All @@ -487,7 +549,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 7f42654

Please sign in to comment.