Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,301 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"id": "c50ab117",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import sqlite3\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr\n",
"\n",
"load_dotenv(override=True)\n",
"\n",
"client = OpenAI(\n",
" base_url=\"https://ollama.com/v1\",\n",
" api_key=os.getenv(\"OLLAMA_API_KEY\")\n",
")\n",
"\n",
"MODEL = \"gpt-oss:120b\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "edb5e14e",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"\"\"\n",
"You are a helpful assistant for a bookstore called BookStoreAI.\n",
"Give short, polite answers (keep it short).\n",
"Be accurate.\n",
"If you don’t know something, say so.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f6da8904",
"metadata": {},
"outputs": [],
"source": [
"DB = \"books.db\"\n",
"\n",
"with sqlite3.connect(DB) as conn:\n",
" cursor = conn.cursor()\n",
" cursor.execute(\"\"\"\n",
" CREATE TABLE IF NOT EXISTS books (\n",
" title TEXT PRIMARY KEY,\n",
" price REAL\n",
" )\n",
" \"\"\")\n",
" conn.commit()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "14e86c66",
"metadata": {},
"outputs": [],
"source": [
"def set_book_price(title, price):\n",
" with sqlite3.connect(DB) as conn:\n",
" cursor = conn.cursor()\n",
" cursor.execute(\"\"\"\n",
" INSERT INTO books (title, price)\n",
" VALUES (?, ?)\n",
" ON CONFLICT(title)\n",
" DO UPDATE SET price = ?\n",
" \"\"\", (title.lower(), price, price))\n",
" conn.commit()\n",
"\n",
"# Add sample books\n",
"books = {\n",
" \"harry potter\": 499,\n",
" \"atomic habits\": 399,\n",
" \"the alchemist\": 299,\n",
" \"deep work\": 450\n",
"}\n",
"\n",
"for title, price in books.items():\n",
" set_book_price(title, price)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "163f0af7",
"metadata": {},
"outputs": [],
"source": [
"def get_book_price(title):\n",
" print(f\"DATABASE TOOL CALLED: Getting price for {title}\", flush=True)\n",
" with sqlite3.connect(DB) as conn:\n",
" cursor = conn.cursor()\n",
" cursor.execute(\"SELECT price FROM books WHERE title = ?\", (title.lower(),))\n",
" result = cursor.fetchone()\n",
" \n",
" if result:\n",
" return f\"The price of '{title}' is ₹{result[0]}\"\n",
" else:\n",
" return \"No price data available for this book\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "cfb31fb4",
"metadata": {},
"outputs": [],
"source": [
"book_price_function = {\n",
" \"name\": \"get_book_price\",\n",
" \"description\": \"Get the price of a book from the bookstore database.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"title\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The title of the book the customer wants\"\n",
" }\n",
" },\n",
" \"required\": [\"title\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ea3bd016",
"metadata": {},
"outputs": [],
"source": [
"tools = [\n",
" {\n",
" \"type\": \"function\",\n",
" \"function\": book_price_function\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d9c8119f",
"metadata": {},
"outputs": [],
"source": [
"def handle_tool_calls(message):\n",
" responses = []\n",
"\n",
" for tool_call in message.tool_calls:\n",
" if tool_call.function.name == \"get_book_price\":\n",
" args = json.loads(tool_call.function.arguments)\n",
"\n",
" title = args.get(\"title\")\n",
" price_details = get_book_price(title)\n",
"\n",
" responses.append({\n",
" \"role\" : \"tool\",\n",
" \"content\" : price_details,\n",
" \"tool_call_id\" : tool_call.id\n",
" })\n",
" \n",
" return responses"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "a543adf3",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
"\n",
" history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n",
"\n",
" messages = (\n",
" [{\"role\": \"system\", \"content\": system_message}]\n",
" + history\n",
" + [{\"role\": \"user\", \"content\": message}]\n",
" )\n",
"\n",
" response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages,\n",
" tools=tools\n",
" )\n",
"\n",
" while response.choices[0].finish_reason == \"tool_calls\":\n",
"\n",
" message = response.choices[0].message\n",
"\n",
" responses = handle_tool_calls(message)\n",
"\n",
" messages.append(message)\n",
" messages.extend(responses)\n",
"\n",
" response = client.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages,\n",
" tools=tools\n",
" )\n",
"\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b6fe98e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7860\n",
"* To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"DATABASE TOOL CALLED: Getting price for The Alchemist\n",
"DATABASE TOOL CALLED: Getting price for Atomic habits\n",
"DATABASE TOOL CALLED: Getting price for Deep work\n",
"DATABASE TOOL CALLED: Getting price for Where the Crawdads Sing\n"
]
}
],
"source": [
"gr.ChatInterface(\n",
" fn=chat,\n",
" type=\"messages\",\n",
" title=\"📚 BookStoreAI Assistant\"\n",
").launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b04fcdf2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "llm-engineering (3.12.12)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file not shown.