Skip to content

Commit 60e7459

Browse files
committed
Created using Colab
1 parent 2bc61b6 commit 60e7459

1 file changed

Lines changed: 368 additions & 0 deletions

File tree

Lines changed: 368 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,368 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "view-in-github",
7+
"colab_type": "text"
8+
},
9+
"source": [
10+
"<a href=\"https://colab.research.google.com/github/Decoding-Data-Science/aiguild/blob/main/30july_of_dds_hr_chatbot_free.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {
16+
"id": "0nNyqrS-QyL9"
17+
},
18+
"source": [
19+
"# DDS HR Enterprise Chatbot — GitHub Models (GPT-5)\n",
20+
"Free GitHub key → GPT-5 (reasoning model) + embeddings → your HR PDFs → Gradio chat. No OpenAI key, no credit card."
21+
],
22+
"id": "0nNyqrS-QyL9"
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {
28+
"colab": {
29+
"base_uri": "https://localhost:8080/"
30+
},
31+
"id": "yPpZTj0IQyMA",
32+
"outputId": "e9f0ceb0-d51e-4e8a-8195-a6687121d01f"
33+
},
34+
"outputs": [
35+
{
36+
"output_type": "stream",
37+
"name": "stdout",
38+
"text": [
39+
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/51.8 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m51.8/51.8 kB\u001b[0m \u001b[31m3.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
40+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m11.9/11.9 MB\u001b[0m \u001b[31m99.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
41+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.7/1.7 MB\u001b[0m \u001b[31m45.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
42+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m349.5/349.5 kB\u001b[0m \u001b[31m21.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
43+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m164.5/164.5 kB\u001b[0m \u001b[31m9.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
44+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.0/1.0 MB\u001b[0m \u001b[31m36.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
45+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m51.0/51.0 kB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
46+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m142.6/142.6 kB\u001b[0m \u001b[31m10.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
47+
"\u001b[?25h\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
48+
"ipython 7.34.0 requires jedi>=0.16, which is not installed.\n",
49+
"torch 2.11.0+cpu requires setuptools<82, but you have setuptools 83.0.0 which is incompatible.\u001b[0m\u001b[31m\n",
50+
"\u001b[0m"
51+
]
52+
}
53+
],
54+
"source": [
55+
"!pip install -q llama_index llama-index-readers-file llama-index-llms-openai-like gradio"
56+
],
57+
"id": "yPpZTj0IQyMA"
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {
62+
"id": "mE8Q_e8HQyMC"
63+
},
64+
"source": [
65+
"## Step 1 — Put your HR PDFs in a `data` folder\n",
66+
"In the Colab file browser (folder icon, left sidebar), create a folder called `data` and upload your HR PDFs into it."
67+
],
68+
"id": "mE8Q_e8HQyMC"
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {
73+
"id": "drTd0olAQyMD"
74+
},
75+
"source": [
76+
"## Step 2 — Connect to GitHub Models\n",
77+
"One free key, kept in Colab Secrets. Add it before running: key icon 🔑 in the left sidebar → Add new secret → name it `GITHUB_TOKEN` → turn on Notebook access."
78+
],
79+
"id": "drTd0olAQyMD"
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {
85+
"id": "JX_xcJjvQyMD"
86+
},
87+
"outputs": [],
88+
"source": [
89+
"from google.colab import userdata\n",
90+
"\n",
91+
"GITHUB_TOKEN = userdata.get(\"GITHUB_TOKEN\")\n",
92+
"GITHUB_MODELS_BASE_URL = \"https://models.github.ai/inference\""
93+
],
94+
"id": "JX_xcJjvQyMD"
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {
99+
"id": "EQ90o8vRQyME"
100+
},
101+
"source": [
102+
"## Step 3 — Load the documents"
103+
],
104+
"id": "EQ90o8vRQyME"
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"metadata": {
110+
"colab": {
111+
"base_uri": "https://localhost:8080/"
112+
},
113+
"id": "8IFkhkTKQyME",
114+
"outputId": "82191459-b09b-465f-b609-fa61239f48d7"
115+
},
116+
"outputs": [
117+
{
118+
"output_type": "stream",
119+
"name": "stdout",
120+
"text": [
121+
"13\n",
122+
"DDS Employee Handbook (Synthetic) v1\n",
123+
"Effective date: March 03, 2026  Dubai (GST)\n",
124+
"Note: This document is a synthetic, training-friendly employee handbook for demos, onboarding\n",
125+
"simulations, and HR-policy chatbot prototypes. It is not legal advice and must be reviewed by qualified\n",
126+
"counsel before any real-world use.\n",
127+
"1. Welcome to Decoding Data Science (DDS)\n",
128+
"DDS is a Dubai-based academy, consulting practice, and community focused on data science, AI, and\n",
129+
"applied generative AI. We operate with a glob\n"
130+
]
131+
}
132+
],
133+
"source": [
134+
"from llama_index.core import SimpleDirectoryReader\n",
135+
"from llama_index.readers.file import PDFReader\n",
136+
"\n",
137+
"documents = SimpleDirectoryReader(\n",
138+
" input_dir=\"data\",\n",
139+
" required_exts=[\".pdf\"],\n",
140+
" file_extractor={\".pdf\": PDFReader()}\n",
141+
").load_data()\n",
142+
"\n",
143+
"print(len(documents))\n",
144+
"print(documents[0].text[:500])"
145+
],
146+
"id": "8IFkhkTKQyME"
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {
151+
"id": "3Ou0G0j2QyMF"
152+
},
153+
"source": [
154+
"## Step 4 — Configure GPT-5 + embeddings\n",
155+
"**Note:** GPT-5 is a reasoning model on GitHub Models — no `temperature` parameter. `OpenAILike` is used instead of the plain `OpenAI` class because llama_index's `OpenAI` class only recognizes bare OpenAI model names (`gpt-5`), not GitHub's prefixed ones (`openai/gpt-5`). `model_name=` (not `model=`) is used for the embedding model for the same reason."
156+
],
157+
"id": "3Ou0G0j2QyMF"
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": null,
162+
"metadata": {
163+
"id": "NpdHTpUEQyMF"
164+
},
165+
"outputs": [],
166+
"source": [
167+
"from llama_index.core import VectorStoreIndex, Settings\n",
168+
"from llama_index.llms.openai_like import OpenAILike\n",
169+
"from llama_index.embeddings.openai import OpenAIEmbedding\n",
170+
"\n",
171+
"Settings.llm = OpenAILike(\n",
172+
" model=\"openai/gpt-4o-mini\",\n",
173+
" api_base=GITHUB_MODELS_BASE_URL,\n",
174+
" api_key=GITHUB_TOKEN,\n",
175+
" is_chat_model=True,\n",
176+
" context_window=128000,\n",
177+
" temperature=0.2, # gpt-4o-mini isn't a reasoning model, so temperature works here\n",
178+
")\n",
179+
"\n",
180+
"Settings.embed_model = OpenAIEmbedding(\n",
181+
" model_name=\"openai/text-embedding-3-small\",\n",
182+
" api_base=GITHUB_MODELS_BASE_URL,\n",
183+
" api_key=GITHUB_TOKEN,\n",
184+
")\n",
185+
"\n",
186+
"Settings.chunk_size = 600\n",
187+
"Settings.chunk_overlap = 200"
188+
],
189+
"id": "NpdHTpUEQyMF"
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"metadata": {
194+
"id": "wf9gN-3gQyMG"
195+
},
196+
"source": [
197+
"## Step 5 — Ayesha, the DDS HR system prompt"
198+
],
199+
"id": "wf9gN-3gQyMG"
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": null,
204+
"metadata": {
205+
"id": "Ouu6Qp0oQyMG"
206+
},
207+
"outputs": [],
208+
"source": [
209+
"system_prompt = '''\n",
210+
"\n",
211+
"You are Ayesha, the Decoding Data Science (DDS) Enterprise HR Chatbot. Your objective is to interact politely and professionally with employees, answering only HR-related questions. Use only information directly from the connected HR documents to provide your answers. Always provide an explicit citation indicating the document source for every answer. Do not offer information or suggestions beyond what is present in these documents.\n",
212+
"\n",
213+
"If the requested information cannot be found in the connected documents, politely instruct the user to email connect@decodingdatascience.com for further assistance. For questions outside of HR (such as food, restaurants, or non-work matters), inform the user that you can only answer HR-related questions. If a question is unclear or possibly HR-related but ambiguous, persist in seeking clarification or ask the user to rephrase. Never attempt to answer non-HR, personal, or unrelated questions.\n",
214+
"\n",
215+
"- Remain polite and professional in all interactions.\n",
216+
"- Respond exclusively to HR-related topics (e.g., payroll, benefits, time-off, HR policies, leave, compliance, hiring, employee development) using only the connected HR documents.\n",
217+
"- Always include an explicit citation to the relevant document(s) for every answer.\n",
218+
"- If a question is off-topic, state that you can only address HR questions.\n",
219+
"- If you are unsure whether the question is HR-related, politely ask for clarification or rephrasing.\n",
220+
"- For HR-related questions where the answer is not in the connected documents, kindly direct the user to email connect@decodingdatascience.com.\n",
221+
"- Use a friendly and formal tone.\n",
222+
"- Always persist in seeking clarification for ambiguous or insufficiently clear questions before providing an answer or deferring.\n",
223+
"\n",
224+
"**Output Format:**\n",
225+
"Respond in short, clear paragraphs (2-4 sentences). Do not use markdown or code blocks. Every answer must include the citation showing which document(s) the information is sourced from.\n",
226+
"\n",
227+
"# Examples\n",
228+
"\n",
229+
"**Example 1**\n",
230+
"Input: What is the process for applying for maternity leave?\n",
231+
"Output: Thank you for your question regarding maternity leave. To apply for maternity leave, please fill out the leave request form available on the HR portal and submit it to your manager for approval. Source: Document 2, Leave Policy.\n",
232+
"\n",
233+
"**Example 2**\n",
234+
"Input: Where is the best place to get lunch nearby?\n",
235+
"Output: I can only assist with HR-related questions. If you have a question about HR policies, benefits, or company leave, please let me know!\n",
236+
"\n",
237+
"**Example 3**\n",
238+
"Input: How can I apply for a sabbatical leave?\n",
239+
"Output: I am sorry, but I could not find information regarding sabbatical leave in our HR documents. For further assistance, please email connect@decodingdatascience.com.\n",
240+
"\n",
241+
"'''"
242+
],
243+
"id": "Ouu6Qp0oQyMG"
244+
},
245+
{
246+
"cell_type": "markdown",
247+
"metadata": {
248+
"id": "mmx1cF0PQyMH"
249+
},
250+
"source": [
251+
"## Step 6 — Build the index and try a question"
252+
],
253+
"id": "mmx1cF0PQyMH"
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": null,
258+
"metadata": {
259+
"colab": {
260+
"base_uri": "https://localhost:8080/"
261+
},
262+
"id": "4tWIhyOXQyMH",
263+
"outputId": "7b5a240e-aa46-406a-d423-a01f5a4b4d18"
264+
},
265+
"outputs": [
266+
{
267+
"output_type": "stream",
268+
"name": "stdout",
269+
"text": [
270+
"DDS standard office hours in Dubai are from 9:00 AM to 6:00 PM, Monday to Friday.\n"
271+
]
272+
}
273+
],
274+
"source": [
275+
"index = VectorStoreIndex.from_documents(documents=documents)\n",
276+
"query_engine = index.as_query_engine(system_prompt=system_prompt)\n",
277+
"\n",
278+
"response = query_engine.query(\"What are DDS standard office hours in Dubai?\")\n",
279+
"print(response)"
280+
],
281+
"id": "4tWIhyOXQyMH"
282+
},
283+
{
284+
"cell_type": "markdown",
285+
"metadata": {
286+
"id": "0A8yhEDKQyMI"
287+
},
288+
"source": [
289+
"## Step 7 — Gradio interface"
290+
],
291+
"id": "0A8yhEDKQyMI"
292+
},
293+
{
294+
"cell_type": "code",
295+
"execution_count": null,
296+
"metadata": {
297+
"colab": {
298+
"base_uri": "https://localhost:8080/",
299+
"height": 647
300+
},
301+
"id": "4PO8D2_LQyMI",
302+
"outputId": "a661f0f3-46db-40ee-fa79-9020471d72f1"
303+
},
304+
"outputs": [
305+
{
306+
"output_type": "stream",
307+
"name": "stdout",
308+
"text": [
309+
"It looks like you are running Gradio on a hosted Jupyter notebook, which requires `share=True`. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
310+
"\n",
311+
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
312+
"* Running on public URL: https://6d24380e0e1fb9df8b.gradio.live\n",
313+
"\n",
314+
"This share link is temporary and will last for up to 1 week (best effort). For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
315+
]
316+
},
317+
{
318+
"output_type": "display_data",
319+
"data": {
320+
"text/plain": [
321+
"<IPython.core.display.HTML object>"
322+
],
323+
"text/html": [
324+
"<div><iframe src=\"https://6d24380e0e1fb9df8b.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
325+
]
326+
},
327+
"metadata": {}
328+
}
329+
],
330+
"source": [
331+
"import gradio as gr\n",
332+
"\n",
333+
"def query_document(query):\n",
334+
" response = query_engine.query(query)\n",
335+
" return str(response)\n",
336+
"\n",
337+
"interface = gr.Interface(\n",
338+
" fn=query_document,\n",
339+
" inputs=gr.Textbox(label=\"Enter your query\", placeholder=\"Type your question here...\"),\n",
340+
" outputs=gr.Textbox(label=\"Response\"),\n",
341+
" title=\"DDS Enterprise HR Chatbot — Ayesha (GPT-5)\",\n",
342+
" description=\"Ask questions about the HR documents loaded into the system. Powered by GitHub Models — free, one key, no credit card.\"\n",
343+
")\n",
344+
"\n",
345+
"if __name__ == \"__main__\":\n",
346+
" interface.launch()"
347+
],
348+
"id": "4PO8D2_LQyMI"
349+
}
350+
],
351+
"metadata": {
352+
"kernelspec": {
353+
"display_name": "Python 3",
354+
"language": "python",
355+
"name": "python3"
356+
},
357+
"language_info": {
358+
"name": "python",
359+
"version": "3.11"
360+
},
361+
"colab": {
362+
"provenance": [],
363+
"include_colab_link": true
364+
}
365+
},
366+
"nbformat": 4,
367+
"nbformat_minor": 5
368+
}

0 commit comments

Comments
 (0)