-
-
Notifications
You must be signed in to change notification settings - Fork 718
Add Real Estate Chatbot Python Script #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
0f2dc2b
cf4229a
5a57b4c
dea4a7f
b906f8c
9733af4
18cdcc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,141 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# # -*- coding: utf-8 -*- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """Chile_Government_Services_Assistant .ipynb | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Automatically generated by Colab. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Original file is located at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# https://colab.research.google.com/drive/13IOn2Vhg0EUwuIGpVilImW0rEpYYMoFQ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# # Chile Government Services Assistant - AI Chatbot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# This notebook demonstrates how to use an AI-powered assistant to answer questions about Chilean government services and procedures, using the Firecrawl API and a friendly, step-by-step conversational approach. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# [](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Chile_Government_Services_Assistant.ipynb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# # Install dependencies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# !pip install flask firecrawl praisonaiagents google-genai python-dotenv deep-translator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """# Set API Keys""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
os.environ['FIRECRAWL_API_KEY'] = "your api key here" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
os.environ['OPENAI_API_KEY'] = "your api key here" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Security risk: Replace hardcoded API key placeholders. Hardcoded API key placeholders should be replaced with proper environment variable handling to prevent security issues. Apply this diff to use proper environment variable handling: -os.environ['FIRECRAWL_API_KEY'] = "your api key here"
-os.environ['OPENAI_API_KEY'] = "your api key here"
+# Load API keys from environment or prompt user
+if not os.getenv('FIRECRAWL_API_KEY'):
+ firecrawl_key = input("Enter your Firecrawl API key: ").strip()
+ os.environ['FIRECRAWL_API_KEY'] = firecrawl_key
+
+if not os.getenv('OPENAI_API_KEY'):
+ openai_key = input("Enter your OpenAI API key: ").strip()
+ os.environ['OPENAI_API_KEY'] = openai_key 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """# Import Libraries & Translator""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from firecrawl import FirecrawlApp, ScrapeOptions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from deep_translator import GoogleTranslator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import re | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def translate_to_spanish(text): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return GoogleTranslator(source='auto', target='es').translate(text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print("Translation to Spanish failed:", e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def translate_to_english(text): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Remove Markdown images and None values before translation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text = str(text).replace("None", "") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text = re.sub(r'!\[.*?\]\(.*?\)', '', text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return GoogleTranslator(source='auto', target='en').translate(text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print("Translation to English failed:", e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """# Firecrawl Tool Class""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class FirecrawlTool: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __init__(self, api_key, instruction: str, template: str): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not api_key: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise ValueError("Firecrawl API key not provided.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.app = FirecrawlApp(api_key=api_key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.instruction = instruction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self.template = template | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def search(self, search: str) -> str: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if not search or len(search) < 5: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return "Error: Please provide a valid search query (at least 5 characters)." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
response_md = "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
search_result = self.app.search( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
query=self.instruction + search, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
limit=2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
country="cl", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
lang="es", # Always search in Spanish for best results | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
scrape_options=ScrapeOptions(formats=["markdown", "links"]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if search_result and hasattr(search_result, 'data') and search_result.data: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
filtered_results = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result for result in search_result.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if str(result.get("url", "")).startswith("https://www.chileatiende.gob.cl/fichas") and not str(result.get("url", "")).endswith("pdf") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if filtered_results: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for num, result in enumerate(filtered_results, start=1): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
response_md += self.template.format( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
result_number=num, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
page_title=str(result.get("title", "")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
page_url=str(result.get("url", "")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
page_content=str(result.get("markdown", "")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return response_md | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return f"Error during search: {e}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """# Firecrawl Prompt Template""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# FIRECRAWL_INSTRUCTION = "ChileAtiende: " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# FIRECRAWL_TEMPLATE = """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# # Result {result_number} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# ## Page Name: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# "{page_title}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# ## URL: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# {page_url} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# ## Content: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# {page_content} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variables
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """# Initialize Firecrawl Tool""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
firecrawl_tool = FirecrawlTool( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
api_key=os.environ['FIRECRAWL_API_KEY'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
instruction=FIRECRAWL_INSTRUCTION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
template=FIRECRAWL_TEMPLATE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical issue: Undefined variables causing runtime error.
Apply this diff to uncomment and define the required variables: -# FIRECRAWL_INSTRUCTION = "ChileAtiende: "
-# FIRECRAWL_TEMPLATE = """
-# # Result {result_number}
-
-# ## Page Name:
-# "{page_title}"
-
-# ## URL:
-# {page_url}
-
-# ## Content:
-# {page_content}
-
-# """
+FIRECRAWL_INSTRUCTION = "ChileAtiende: "
+FIRECRAWL_TEMPLATE = """
+# Result {result_number}
+
+## Page Name:
+"{page_title}"
+
+## URL:
+{page_url}
+
+## Content:
+{page_content}
+
+""" 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Flake8 (7.2.0)[error] 112-112: expected 2 blank lines after class or function definition, found 1 (E305) [error] 114-114: undefined name 'FIRECRAWL_INSTRUCTION' (F821) [error] 115-115: undefined name 'FIRECRAWL_TEMPLATE' (F821) 🪛 Ruff (0.11.9)114-114: Undefined name (F821) 115-115: Undefined name (F821) 🪛 Pylint (3.3.7)[error] 114-114: Undefined variable 'FIRECRAWL_INSTRUCTION' (E0602) [error] 115-115: Undefined variable 'FIRECRAWL_TEMPLATE' (E0602) 🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# """# Main Chat Loop""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print("Hello! I am your ChileAtiende assistant, Tomás. How can I help you today?") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print("You can ask me, for example: How to renew your ID card, How to apply for the Winter Bonus, etc.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while True: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
user_input = input("\nYou: ") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if user_input.lower() in ["exit", "quit"]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print("Tomás: It was a pleasure to help you. Goodbye!") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Translate English input to Spanish for Firecrawl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
spanish_query = translate_to_spanish(user_input) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
spanish_answer = firecrawl_tool.search(spanish_query) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Only translate if we got a real answer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if spanish_answer and isinstance(spanish_answer, str) and spanish_answer.strip() and "Error" not in spanish_answer: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
english_answer = translate_to_english(spanish_answer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print("\nTomás (in English):\n", english_answer) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print(f"\nTomás: I found information, but couldn't translate it. Here it is in Spanish:\n{spanish_answer}\n(Translation error: {e})") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
print("\nTomás: Sorry, I couldn't find relevant information. Try rephrasing your question or ask about another service.") |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,208 @@ | ||||||||||||||||||||||||||||
# # -*- coding: utf-8 -*- | ||||||||||||||||||||||||||||
# """LegaliaAI_MiniCourt.ipynb | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Automatically generated by Colab. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Original file is located at | ||||||||||||||||||||||||||||
# https://colab.research.google.com/drive/1Pc6GCFJN9Fyq_oP4MCtPmeTSt_AZmRMT | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# # ⚖️ Legalia AI - Mini Court Simulation | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# A simplified court case simulation with essential AI agents. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# [](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/LegaliaAI_MiniCourt.ipynb) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# ## Install Dependencies | ||||||||||||||||||||||||||||
# """ | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# !pip install praisonaiagents openai python-dotenv | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# """## Import Libraries & Setup""" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
import os | ||||||||||||||||||||||||||||
from dotenv import load_dotenv | ||||||||||||||||||||||||||||
from IPython.display import display, HTML | ||||||||||||||||||||||||||||
import time | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
load_dotenv() | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Setup API key | ||||||||||||||||||||||||||||
api_key = os.getenv('OPENAI_API_KEY') | ||||||||||||||||||||||||||||
if not api_key: | ||||||||||||||||||||||||||||
print("🔑 Enter your OpenAI API key:") | ||||||||||||||||||||||||||||
api_key = input("API Key: ").strip() | ||||||||||||||||||||||||||||
os.environ['OPENAI_API_KEY'] = "Enter your api key" | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The environment variable
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix API key handling inconsistency. The code gets the API key from environment variable but then overwrites it with a hardcoded placeholder string, which will cause authentication failures. Apply this diff to fix the API key handling: # Setup API key
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
print("🔑 Enter your OpenAI API key:")
api_key = input("API Key: ").strip()
- os.environ['OPENAI_API_KEY'] = "Enter your api key"
+ os.environ['OPENAI_API_KEY'] = api_key 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
print("✅ Setup complete!") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# """## Create Mini Agents""" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
from praisonaiagents import Agent | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Judge Agent | ||||||||||||||||||||||||||||
judge = Agent( | ||||||||||||||||||||||||||||
name="Judge", | ||||||||||||||||||||||||||||
role="Preside over court proceedings", | ||||||||||||||||||||||||||||
llm="gpt-4o-mini", | ||||||||||||||||||||||||||||
instructions=[ | ||||||||||||||||||||||||||||
"You are an impartial judge", | ||||||||||||||||||||||||||||
"Make fair decisions based on evidence", | ||||||||||||||||||||||||||||
"Keep responses under 100 words" | ||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||
markdown=True | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Prosecutor Agent | ||||||||||||||||||||||||||||
prosecutor = Agent( | ||||||||||||||||||||||||||||
name="Prosecutor", | ||||||||||||||||||||||||||||
role="Present case against defendant", | ||||||||||||||||||||||||||||
llm="gpt-4o-mini", | ||||||||||||||||||||||||||||
instructions=[ | ||||||||||||||||||||||||||||
"You are a prosecutor seeking conviction", | ||||||||||||||||||||||||||||
"Present evidence methodically", | ||||||||||||||||||||||||||||
"Keep responses under 80 words" | ||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||
markdown=True | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Defense Agent | ||||||||||||||||||||||||||||
defense = Agent( | ||||||||||||||||||||||||||||
name="Defense", | ||||||||||||||||||||||||||||
role="Defend the accused", | ||||||||||||||||||||||||||||
llm="gpt-4o-mini", | ||||||||||||||||||||||||||||
instructions=[ | ||||||||||||||||||||||||||||
"You are a defense attorney", | ||||||||||||||||||||||||||||
"Create reasonable doubt", | ||||||||||||||||||||||||||||
"Keep responses under 80 words" | ||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||
markdown=True | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Witness Agent | ||||||||||||||||||||||||||||
witness = Agent( | ||||||||||||||||||||||||||||
name="Witness", | ||||||||||||||||||||||||||||
role="Provide testimony", | ||||||||||||||||||||||||||||
llm="gpt-4o-mini", | ||||||||||||||||||||||||||||
instructions=[ | ||||||||||||||||||||||||||||
"You are a witness testifying", | ||||||||||||||||||||||||||||
"Provide factual testimony", | ||||||||||||||||||||||||||||
"Keep responses under 60 words" | ||||||||||||||||||||||||||||
], | ||||||||||||||||||||||||||||
markdown=True | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
print("✅ Mini agents created!") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
"""# Case Details Input""" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Simple case details | ||||||||||||||||||||||||||||
case_title = input("Case Title (e.g., 'State vs. Smith'): ") or "State vs. Smith" | ||||||||||||||||||||||||||||
case_description = input("Case Description: ") or "Theft case involving stolen laptop" | ||||||||||||||||||||||||||||
evidence = input("Key Evidence: ") or "Security camera footage and witness testimony" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
print(f"\n📋 Case: {case_title}") | ||||||||||||||||||||||||||||
print(f"📝 Description: {case_description}") | ||||||||||||||||||||||||||||
print(f"🔍 Evidence: {evidence}") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
"""# Helper Functions (Fixed with .start())## Cell 4: Case Details Input""" | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Display function | ||||||||||||||||||||||||||||
def show_message(name, role, message, color="#4CAF50"): | ||||||||||||||||||||||||||||
html = f""" | ||||||||||||||||||||||||||||
<div style=" | ||||||||||||||||||||||||||||
background: {color}; | ||||||||||||||||||||||||||||
color: white; | ||||||||||||||||||||||||||||
padding: 15px; | ||||||||||||||||||||||||||||
border-radius: 10px; | ||||||||||||||||||||||||||||
margin: 10px 0; | ||||||||||||||||||||||||||||
"> | ||||||||||||||||||||||||||||
<strong>⚖️ {name}</strong> • <em>{role}</em><br> | ||||||||||||||||||||||||||||
<div style="margin-top: 10px;">{message}</div> | ||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
display(HTML(html)) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Run agent using .start() | ||||||||||||||||||||||||||||
def run_agent(agent, prompt, name, role, color="#4CAF50"): | ||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||
response = agent.start(prompt) | ||||||||||||||||||||||||||||
show_message(name, role, response, color) | ||||||||||||||||||||||||||||
return response | ||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||
show_message("System", "Error", str(e), "#f44336") | ||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
"""## Mini Court Simulation""" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Mini court simulation | ||||||||||||||||||||||||||||
print("🚀 Starting Mini Court Simulation...") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Day 1: Opening | ||||||||||||||||||||||||||||
display(HTML(f"<h2>📅 Day 1: Opening Statements</h2>")) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Judge opens court | ||||||||||||||||||||||||||||
judge_prompt = f""" | ||||||||||||||||||||||||||||
You are Judge presiding over "{case_title}". | ||||||||||||||||||||||||||||
Open the court proceedings professionally. | ||||||||||||||||||||||||||||
Case: {case_description} | ||||||||||||||||||||||||||||
Keep it brief and formal. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
judge_response = run_agent(judge, judge_prompt, "Judge", "Presiding Judge", "#8B4513") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Prosecutor opening | ||||||||||||||||||||||||||||
prosecutor_prompt = f""" | ||||||||||||||||||||||||||||
You are the Prosecutor for "{case_title}". | ||||||||||||||||||||||||||||
Give your opening statement. | ||||||||||||||||||||||||||||
Evidence: {evidence} | ||||||||||||||||||||||||||||
Be confident and factual. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
prosecutor_response = run_agent(prosecutor, prosecutor_prompt, "Prosecutor", "State Attorney", "#d32f2f") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Defense opening | ||||||||||||||||||||||||||||
defense_prompt = f""" | ||||||||||||||||||||||||||||
You are the Defense Attorney for "{case_title}". | ||||||||||||||||||||||||||||
Give your opening statement. | ||||||||||||||||||||||||||||
Challenge the prosecution's case. | ||||||||||||||||||||||||||||
Emphasize presumption of innocence. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
defense_response = run_agent(defense, defense_prompt, "Defense", "Defense Attorney", "#1976d2") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
time.sleep(1) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Day 2: Testimony | ||||||||||||||||||||||||||||
display(HTML(f"<h2>📅 Day 2: Witness Testimony</h2>")) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Witness testimony | ||||||||||||||||||||||||||||
witness_prompt = f""" | ||||||||||||||||||||||||||||
You are a witness in "{case_title}". | ||||||||||||||||||||||||||||
Provide your testimony about what you saw. | ||||||||||||||||||||||||||||
Evidence: {evidence} | ||||||||||||||||||||||||||||
Be factual and clear. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
witness_response = run_agent(witness, witness_prompt, "Witness", "Court Witness", "#ff9800") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
time.sleep(1) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Day 3: Verdict | ||||||||||||||||||||||||||||
display(HTML(f"<h2>📅 Day 3: Final Verdict</h2>")) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Judge's verdict | ||||||||||||||||||||||||||||
verdict_prompt = f""" | ||||||||||||||||||||||||||||
You are Judge presiding over "{case_title}". | ||||||||||||||||||||||||||||
Deliver your final verdict. | ||||||||||||||||||||||||||||
Consider all evidence: {evidence} | ||||||||||||||||||||||||||||
Be fair and explain your reasoning. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
verdict_response = run_agent(judge, verdict_prompt, "Judge", "Final Verdict", "#8B4513") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
print("\n✅ Mini court simulation completed!") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# """# Summary""" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
print("🎯 Mini Legalia AI Complete!") | ||||||||||||||||||||||||||||
print("\nThis simplified version demonstrates:") | ||||||||||||||||||||||||||||
print("- 4 Essential Agents: Judge, Prosecutor, Defense, Witness") | ||||||||||||||||||||||||||||
print("- 3-Day Trial: Opening, Testimony, Verdict") | ||||||||||||||||||||||||||||
print("- Real-time Interaction: Each agent responds based on case context") | ||||||||||||||||||||||||||||
print("- Easy Setup: Minimal dependencies and configuration") | ||||||||||||||||||||||||||||
print("\nPerfect for quick demonstrations and learning! ⚖️") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding API keys, even as placeholders, is a significant security risk. Load keys from environment variables using
os.getenv()
and provide instructions for users to set them up.