1+ {
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "metadata" : {
6+ "id" : " ZH_nR-SvvkDG"
7+ },
8+ "source" : [
9+ " # Chile Government Services Assistant - AI Chatbot"
10+ ]
11+ },
12+ {
13+ "cell_type" : " markdown" ,
14+ "metadata" : {
15+ "id" : " w8B741JgvpFj"
16+ },
17+ "source" : [
18+ " 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."
19+ ]
20+ },
21+ {
22+ "cell_type" : " markdown" ,
23+ "metadata" : {
24+ "id" : " y8jiJYf4FA0m"
25+ },
26+ "source" : [
27+ " [](https://colab.research.google.com/github/DhivyaBharathy-web/PraisonAI/blob/main/examples/cookbooks/Chile_Government_Services_Assistant.ipynb)\n "
28+ ]
29+ },
30+ {
31+ "cell_type" : " markdown" ,
32+ "metadata" : {
33+ "id" : " RRw8sPG89KNb"
34+ },
35+ "source" : [
36+ " # Install dependencies"
37+ ]
38+ },
39+ {
40+ "cell_type" : " code" ,
41+ "execution_count" : null ,
42+ "metadata" : {
43+ "id" : " rW8ltqCICV8o"
44+ },
45+ "outputs" : [],
46+ "source" : [
47+ " !pip install flask firecrawl praisonaiagents google-genai python-dotenv deep-translator"
48+ ]
49+ },
50+ {
51+ "cell_type" : " markdown" ,
52+ "metadata" : {
53+ "id" : " XGjyt-B_EfbM"
54+ },
55+ "source" : [
56+ " # Set API Keys"
57+ ]
58+ },
59+ {
60+ "cell_type" : " code" ,
61+ "execution_count" : null ,
62+ "metadata" : {
63+ "id" : " qf8B_YltDiIe"
64+ },
65+ "outputs" : [],
66+ "source" : [
67+ " import os\n " ,
68+ " \n " ,
69+ " os.environ['FIRECRAWL_API_KEY'] = \" your api key here\"\n " ,
70+ " os.environ['OPENAI_API_KEY'] = \" your api key here\" "
71+ ]
72+ },
73+ {
74+ "cell_type" : " markdown" ,
75+ "metadata" : {
76+ "id" : " ccO0vwvCEqUJ"
77+ },
78+ "source" : [
79+ " # Import Libraries & Translator"
80+ ]
81+ },
82+ {
83+ "cell_type" : " code" ,
84+ "execution_count" : null ,
85+ "metadata" : {
86+ "id" : " 0prDQ5TpDnFu"
87+ },
88+ "outputs" : [],
89+ "source" : [
90+ " from firecrawl import FirecrawlApp, ScrapeOptions\n " ,
91+ " from deep_translator import GoogleTranslator\n " ,
92+ " import re\n " ,
93+ " \n " ,
94+ " def translate_to_spanish(text):\n " ,
95+ " try:\n " ,
96+ " return GoogleTranslator(source='auto', target='es').translate(text)\n " ,
97+ " except Exception as e:\n " ,
98+ " print(\" Translation to Spanish failed:\" , e)\n " ,
99+ " return text\n " ,
100+ " \n " ,
101+ " def translate_to_english(text):\n " ,
102+ " try:\n " ,
103+ " # Remove Markdown images and None values before translation\n " ,
104+ " text = str(text).replace(\" None\" , \"\" )\n " ,
105+ " text = re.sub(r'!\\ [.*?\\ ]\\ (.*?\\ )', '', text)\n " ,
106+ " return GoogleTranslator(source='auto', target='en').translate(text)\n " ,
107+ " except Exception as e:\n " ,
108+ " print(\" Translation to English failed:\" , e)\n " ,
109+ " return text"
110+ ]
111+ },
112+ {
113+ "cell_type" : " markdown" ,
114+ "metadata" : {
115+ "id" : " WxOlCHMmEuK2"
116+ },
117+ "source" : [
118+ " # Firecrawl Tool Class"
119+ ]
120+ },
121+ {
122+ "cell_type" : " code" ,
123+ "execution_count" : null ,
124+ "metadata" : {
125+ "id" : " G4RyzJ5mDp0t"
126+ },
127+ "outputs" : [],
128+ "source" : [
129+ " class FirecrawlTool:\n " ,
130+ " def __init__(self, api_key, instruction: str, template: str):\n " ,
131+ " if not api_key:\n " ,
132+ " raise ValueError(\" Firecrawl API key not provided.\" )\n " ,
133+ " self.app = FirecrawlApp(api_key=api_key)\n " ,
134+ " self.instruction = instruction\n " ,
135+ " self.template = template\n " ,
136+ " \n " ,
137+ " def search(self, search: str) -> str:\n " ,
138+ " if not search or len(search) < 5:\n " ,
139+ " return \" Error: Please provide a valid search query (at least 5 characters).\"\n " ,
140+ " response_md = \"\"\n " ,
141+ " try:\n " ,
142+ " search_result = self.app.search(\n " ,
143+ " query=self.instruction + search,\n " ,
144+ " limit=2,\n " ,
145+ " country=\" cl\" ,\n " ,
146+ " lang=\" es\" , # Always search in Spanish for best results\n " ,
147+ " scrape_options=ScrapeOptions(formats=[\" markdown\" , \" links\" ])\n " ,
148+ " )\n " ,
149+ " if search_result and hasattr(search_result, 'data') and search_result.data:\n " ,
150+ " filtered_results = [\n " ,
151+ " result for result in search_result.data\n " ,
152+ " if str(result.get(\" url\" , \"\" )).startswith(\" https://www.chileatiende.gob.cl/fichas\" ) and not str(result.get(\" url\" , \"\" )).endswith(\" pdf\" )\n " ,
153+ " ]\n " ,
154+ " if filtered_results:\n " ,
155+ " for num, result in enumerate(filtered_results, start=1):\n " ,
156+ " response_md += self.template.format(\n " ,
157+ " result_number=num,\n " ,
158+ " page_title=str(result.get(\" title\" , \"\" )),\n " ,
159+ " page_url=str(result.get(\" url\" , \"\" )),\n " ,
160+ " page_content=str(result.get(\" markdown\" , \"\" ))\n " ,
161+ " )\n " ,
162+ " return response_md\n " ,
163+ " else:\n " ,
164+ " return None\n " ,
165+ " else:\n " ,
166+ " return None\n " ,
167+ " except Exception as e:\n " ,
168+ " return f\" Error during search: {e}\" "
169+ ]
170+ },
171+ {
172+ "cell_type" : " markdown" ,
173+ "metadata" : {
174+ "id" : " MjkjTWn_ExS0"
175+ },
176+ "source" : [
177+ " # Firecrawl Prompt Template"
178+ ]
179+ },
180+ {
181+ "cell_type" : " code" ,
182+ "execution_count" : null ,
183+ "metadata" : {
184+ "id" : " AfivymU8Dufz"
185+ },
186+ "outputs" : [],
187+ "source" : [
188+ " FIRECRAWL_INSTRUCTION = \" ChileAtiende: \"\n " ,
189+ " FIRECRAWL_TEMPLATE = \"\"\"\n " ,
190+ " # Result {result_number}\n " ,
191+ " \n " ,
192+ " ## Page Name:\n " ,
193+ " \" {page_title}\"\n " ,
194+ " \n " ,
195+ " ## URL:\n " ,
196+ " {page_url}\n " ,
197+ " \n " ,
198+ " ## Content:\n " ,
199+ " {page_content}\n " ,
200+ " \n " ,
201+ " \"\"\" "
202+ ]
203+ },
204+ {
205+ "cell_type" : " markdown" ,
206+ "metadata" : {
207+ "id" : " zK8AA_DlEz9K"
208+ },
209+ "source" : [
210+ " # Initialize Firecrawl Tool"
211+ ]
212+ },
213+ {
214+ "cell_type" : " code" ,
215+ "execution_count" : null ,
216+ "metadata" : {
217+ "id" : " c3NKK0ZjDwKT"
218+ },
219+ "outputs" : [],
220+ "source" : [
221+ " firecrawl_tool = FirecrawlTool(\n " ,
222+ " api_key=os.environ['FIRECRAWL_API_KEY'],\n " ,
223+ " instruction=FIRECRAWL_INSTRUCTION,\n " ,
224+ " template=FIRECRAWL_TEMPLATE\n " ,
225+ " )"
226+ ]
227+ },
228+ {
229+ "cell_type" : " markdown" ,
230+ "metadata" : {
231+ "id" : " uzXYIF_gE3XV"
232+ },
233+ "source" : [
234+ " # Main Chat Loop"
235+ ]
236+ },
237+ {
238+ "cell_type" : " code" ,
239+ "execution_count" : null ,
240+ "metadata" : {
241+ "colab" : {
242+ "base_uri" : " https://localhost:8080/"
243+ },
244+ "id" : " TXMgZQNkDx7n" ,
245+ "outputId" : " 76303cd1-a576-483f-a22d-9857e5e6d797"
246+ },
247+ "outputs" : [
248+ {
249+ "name" : " stdout" ,
250+ "output_type" : " stream" ,
251+ "text" : [
252+ " Hello! I am your ChileAtiende assistant, Tomás. How can I help you today?\n " ,
253+ " You can ask me, for example: How to renew your ID card, How to apply for the Winter Bonus, etc.\n " ,
254+ " \n " ,
255+ " You: exit\n " ,
256+ " Tomás: It was a pleasure to help you. Goodbye!\n "
257+ ]
258+ }
259+ ],
260+ "source" : [
261+ " print(\" Hello! I am your ChileAtiende assistant, Tomás. How can I help you today?\" )\n " ,
262+ " print(\" You can ask me, for example: How to renew your ID card, How to apply for the Winter Bonus, etc.\" )\n " ,
263+ " \n " ,
264+ " while True:\n " ,
265+ " user_input = input(\"\\ nYou: \" )\n " ,
266+ " if user_input.lower() in [\" exit\" , \" quit\" ]:\n " ,
267+ " print(\" Tomás: It was a pleasure to help you. Goodbye!\" )\n " ,
268+ " break\n " ,
269+ " \n " ,
270+ " # Translate English input to Spanish for Firecrawl\n " ,
271+ " spanish_query = translate_to_spanish(user_input)\n " ,
272+ " spanish_answer = firecrawl_tool.search(spanish_query)\n " ,
273+ " \n " ,
274+ " # Only translate if we got a real answer\n " ,
275+ " if spanish_answer and isinstance(spanish_answer, str) and spanish_answer.strip() and \" Error\" not in spanish_answer:\n " ,
276+ " try:\n " ,
277+ " english_answer = translate_to_english(spanish_answer)\n " ,
278+ " print(\"\\ nTomás (in English):\\ n\" , english_answer)\n " ,
279+ " except Exception as e:\n " ,
280+ " print(f\"\\ nTomás: I found information, but couldn't translate it. Here it is in Spanish:\\ n{spanish_answer}\\ n(Translation error: {e})\" )\n " ,
281+ " else:\n " ,
282+ " print(\"\\ nTomás: Sorry, I couldn't find relevant information. Try rephrasing your question or ask about another service.\" )"
283+ ]
284+ }
285+ ],
286+ "metadata" : {
287+ "colab" : {
288+ "provenance" : []
289+ },
290+ "kernelspec" : {
291+ "display_name" : " Python 3" ,
292+ "name" : " python3"
293+ },
294+ "language_info" : {
295+ "name" : " python"
296+ }
297+ },
298+ "nbformat" : 4 ,
299+ "nbformat_minor" : 0
300+ }
0 commit comments