Merge branch 'main' of github.com:ed-donner/llm_engineering

This commit is contained in:
Edward Donner
2025-04-25 13:25:46 +02:00
33 changed files with 13267 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import mysql.connector
def get_connection():
conn = mysql.connector.connect(
host="127.0.0.1",
user="root",
password="xyz",
database="your_database"
)
return conn

View File

@@ -0,0 +1,42 @@
import ollama
from db import get_connection
import mysql.connector
def text_to_sql(user_query):
prompt = f"""
Convert the following natural language query into an SQL statement for MySQL:
Query: "{user_query}"
Ensure the query is syntactically correct and does not contain harmful operations.
Only return the SQL query without any explanation.
"""
# Update the model name to 'llama3.2:latest'
response = ollama.chat(model="llama3.2:latest", messages=[{"role": "user", "content": prompt}])
sql_query = response['message']['content'].strip()
return sql_query
# Uncomment this section if you wish to connect with mysql and fill out your credentials in db.py
'''def execute_sql_query(user_query):
sql_query = text_to_sql(user_query)
try:
conn = get_connection()
cursor = conn.cursor()
cursor.execute(sql_query)
result = cursor.fetchall()
except mysql.connector.Error as e:
return {"error": f"MySQL Error: {e}"}
except Exception as e:
return {"error": str(e)}
finally:
conn.close() # Ensure connection is closed even if an error occurs
return result'''
# Example usage
if __name__ == "__main__":
user_input = "Show me all users whose first name starts with the letter j in the first_name column."
print(text_to_sql(user_input))

View File

@@ -0,0 +1,532 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "603cd418-504a-4b4d-b1c3-be04febf3e79",
"metadata": {},
"source": [
"# Article Title Generator (V3 - using Firecrawl) \n",
"\n",
"Summarization use-case in which the user provides an article, which the LLM will analyze to suggest an SEO-optimized title.\n",
"\n",
"**NOTES**:\n",
"\n",
"1. This version supports website scrapping using [Firecrawl](https://www.firecrawl.dev/).<br>\n",
" 1. **Note:** There is a Free tier that provides 500 one-time credits (good for scraping 500 pages).\n",
" 2. Upon registration, get and add your Firecrawl API Key to the .env file as: **`FIRECRAWL_API_KEY`**.<br><br>\n",
"2. Leverage streaming (OpenAI only).<br>\n",
"3. The following models were configured:<br>\n",
" 1. OpenAI gpt-4o-mini\n",
" 2. Llama llama3.2\n",
" 3. Deepseek deepseek-r1:1.5b\n",
" 4. Firecrawl LLM Extract feature<br><br>\n",
" \n",
" It is possible to configure additional models by adding the new model to the MODELS dictionary and its\n",
" initialization to the CLIENTS dictionary. Then, call the model with --> **`answer =\n",
" get_answer('NEW_MODEL')`**.<br>\n",
"4. Users are encouraged to assess and rank the suggested titles using any headline analyzer tool online.\n",
" Example: [ISITWP Headline Analyzer](https://www.isitwp.com/headline-analyzer/). "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "115004a8-747a-4954-9580-1ed548f80336",
"metadata": {},
"outputs": [],
"source": [
"# install required libraries if they were not part of the requirements.txt\n",
"!pip install firecrawl-py"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e773daa6-d05e-49bf-ad8e-a8ed4882b77e",
"metadata": {},
"outputs": [],
"source": [
"# confirming Llama is loaded\n",
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "279b0c00-9bb0-4c7f-9c6d-aa0b108274b9",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI\n",
"from firecrawl import FirecrawlApp"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4730d8d-3e20-4f3c-a4ff-ed2ac0a8aa27",
"metadata": {},
"outputs": [],
"source": [
"# set environment variables for OpenAi\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"# validate API Key\n",
"if not api_key:\n",
" raise ValueError(\"No OPENAI API Key was found! Please check the .env file.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b2a78101-d866-400f-a482-1d8fda8e0df9",
"metadata": {},
"outputs": [],
"source": [
"# set environment variable for Firecrawl\n",
"firecrawl_api_key = os.getenv('FIRECRAWL_API_KEY')\n",
"\n",
"# validate API Key\n",
"if not firecrawl_api_key:\n",
" raise ValueError(\"No FIRECRAWL API Key was found! Please check the .env file.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1abbb826-de66-498c-94d8-33369ad01885",
"metadata": {},
"outputs": [],
"source": [
"# constants\n",
"MODELS = { 'GPT': 'gpt-4o-mini', \n",
" 'LLAMA': 'llama3.2', \n",
" 'DEEPSEEK': 'deepseek-r1:1.5b'\n",
" }\n",
"\n",
"CLIENTS = { 'GPT': OpenAI(), \n",
" 'LLAMA': OpenAI(base_url='http://localhost:11434/v1', api_key='ollama'),\n",
" 'DEEPSEEK': OpenAI(base_url='http://localhost:11434/v1', api_key='ollama') \n",
" }\n",
"\n",
"# path to Chrome\n",
"# CHROME_PATH = \"C:/Program Files/Google/Chrome/Application/chrome.exe\""
]
},
{
"cell_type": "markdown",
"id": "6f490fe4-32d5-41f3-890d-ecf4e5e01dd4",
"metadata": {},
"source": [
"**Webcrawler** (based on the code from Firecrawl [documentation](https://docs.firecrawl.dev/introduction))."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2852700e-33ed-4be5-bd31-8aa05036aaf2",
"metadata": {},
"outputs": [],
"source": [
"class WebsiteCrawler:\n",
" def __init__(self, url, wait_time=20, format='markdown'):\n",
" \"\"\"\n",
" Initialize the WebsiteCrawler using Firecrawl to scrape JavaScript-rendered content.\n",
" \"\"\"\n",
" self.url = url\n",
" self.wait_time = wait_time\n",
" self.format = format\n",
"\n",
" try:\n",
"\n",
" # initialize Firecrawl\n",
" screate_app = FirecrawlApp(api_key=firecrawl_api_key)\n",
"\n",
" # Scrape a website:\n",
" scrape_result = screate_app.scrape_url(self.url,\n",
" params=self.getParams())\n",
" \n",
"\n",
" # parse data\n",
" self.title = scrape_result['metadata']['ogTitle']\n",
"\n",
" # get the content using the appropriate key\n",
" if format == 'markdown':\n",
" # OpenAI, Llama, Deepseek\n",
" self.text = scrape_result['markdown'] \n",
" elif format == 'json':\n",
" # Firecrawl LLM Extract\n",
" self.text = scrape_result['json']\n",
"\n",
" except Exception as e:\n",
" print(f\"Error occurred: {e}\")\n",
" self.title = \"Error occurred\"\n",
" self.text = \"\"\n",
"\n",
" # set appropriate parameters for scraping\n",
" def getParams(self):\n",
"\n",
" # For OpenAi, Llama or Deepseek\n",
" params={'formats': [self.format], \n",
" 'actions': [{\"type\": \"wait\", \"milliseconds\": self.wait_time}], \n",
" 'includeTags': [\"main\"], }\n",
"\n",
" # For Firecrawl LLM extract\n",
" if self.format == 'json':\n",
" params={'formats': [self.format], \n",
" 'actions': [{\"type\": \"wait\", \"milliseconds\": self.wait_time}], \n",
" 'jsonOptions': {'systemPrompt': system_prompt, 'prompt': user_prompt, }}\n",
" \n",
" return params\n",
"\n",
" # Get Firecrawl LLM extract result\n",
" def getResult(self):\n",
"\n",
" formated_result = f\"\"\"\n",
" **Optimized Title:** {self.text['Optimized Title']} \n",
" <br><br>**Justification:** {self.text['Justification']}\n",
" \"\"\"\n",
"\n",
" # Remove leading and trailing spaces \n",
" return formated_result.strip()\n",
" "
]
},
{
"cell_type": "markdown",
"id": "592d8f86-fbf7-4b16-a69d-468030d72dc4",
"metadata": {},
"source": [
"### Prompts"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1914afad-dbd8-4c1f-8e68-80b0e5d743a9",
"metadata": {},
"outputs": [],
"source": [
"# system prompt\n",
"system_prompt = \"\"\"\n",
" You are an experienced SEO-focused copywriter. The user will provide an article, and your task is to analyze its content and generate a single, most effective, keyword-optimized title to maximize SEO performance.\n",
"\n",
"Instructions:\n",
"Ignore irrelevant content, such as the current title (if any), navigation menus, advertisements, or unrelated text.\n",
"Prioritize SEO best practices, considering:\n",
"Keyword relevance and search intent (informational, transactional, etc.).\n",
"Readability and engagement.\n",
"Avoiding keyword stuffing.\n",
"Ensure conciseness and clarity, keeping the title under 60 characters when possible for optimal SERP display.\n",
"Use a compelling structure that balances informativeness and engagement, leveraging formats like:\n",
"Listicles (\"10 Best Strategies for…\")\n",
"How-to guides (\"How to Boost…\")\n",
"Questions (\"What Is the Best Way to…\")\n",
"Power words to enhance click-through rates (e.g., \"Proven,\" \"Ultimate,\" \"Essential\").\n",
"Provide only one single, best title—do not suggest multiple options.\n",
"Limit the answer to the following Response Format (Markdown):\n",
"Optimized Title: [Provide only one title here]\n",
"Justification: [Explain why this title is effective for SEO]\n",
"\n",
" \"\"\""
]
},
{
"cell_type": "markdown",
"id": "b0486867-6d38-4cb5-91d4-fb60952c3a9b",
"metadata": {},
"source": [
"**Provide the article URL and get its content for analysis**"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ddd76319-13ce-480b-baa7-cab6a5c88168",
"metadata": {},
"outputs": [],
"source": [
"# article url - change to any other article URL\n",
"article_url = \"https://searchengineland.com/seo-trends-2025-447745\"\n",
"\n",
"# get article content\n",
"article = WebsiteCrawler(url=article_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "176cfac7-5e6d-4d4a-a1c4-1b63b60de1f7",
"metadata": {},
"outputs": [],
"source": [
"# user prompt\n",
"user_prompt = \"\"\"\n",
"Following the article to be analyzed to suggest a title. Limit the answer to the following Response Format (Markdown): \n",
"Optimized Title: [Provide only one title here]\n",
"Justification: [Explain why this title is effective for SEO].\n",
"\"\"\"\n",
"\n",
"user_prompt = f\"{user_prompt} {article}\"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c45fc7d7-08c9-4e34-b427-b928a219bb94",
"metadata": {},
"outputs": [],
"source": [
"# message list\n",
"messages = [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f67b881f-1040-4cf7-82c5-e85f4c0bd252",
"metadata": {},
"outputs": [],
"source": [
"# get suggested title\n",
"def get_title(model, **kwargs):\n",
" # stream if GPT\n",
" if 'stream' in kwargs:\n",
" response = CLIENTS[model].chat.completions.create(\n",
" model=MODELS[model],\n",
" messages=messages,\n",
" stream=kwargs['stream']\n",
" )\n",
" else:\n",
" response = CLIENTS[model].chat.completions.create(\n",
" model=MODELS[model],\n",
" messages=messages,\n",
" )\n",
"\n",
" return response\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8988d6ff-076a-4eae-baf4-26a8d6a2bc44",
"metadata": {},
"outputs": [],
"source": [
"# filter response from model verbose - like Deepseek reasoning/thinking verbose\n",
"def filter_response(response):\n",
" filtered_response = response\n",
" # Find last occurrence of 'Optimized Title:' to avoid displaying reasoning verbose\n",
" substring = 'Optimized Title:'\n",
" start = response.rfind(substring)\n",
" if start > -1:\n",
" filtered_response = response[start:]\n",
"\n",
" # Find if the title has quotation (or other) marks and remove it - this should be improved\n",
" filtered_response = (\n",
" filtered_response.replace('\"', '', 2)\n",
" .replace('[', '', 1)\n",
" .replace(']', '', 1)\n",
" .replace('**', '', 2)\n",
" )\n",
" \n",
" return filtered_response"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e9e99cf-5e25-4a1f-ab11-a2255e318671",
"metadata": {},
"outputs": [],
"source": [
"# display suggested title\n",
"def display_title(model):\n",
" # get model-suggested title\n",
" title = get_title(model)\n",
" \n",
" display(Markdown(f\"### {model} (___{MODELS[model]}___) Answer\\n\\n_______\")) \n",
"\n",
" response = \"\"\n",
"\n",
" if model == 'GPT':\n",
" display_handle = display(Markdown(\"\"), display_id=True)\n",
" # for chunk in stream:\n",
" for chunk in get_title(model=model, stream=True):\n",
" response += chunk.choices[0].delta.content or ''\n",
" response = (\n",
" response.replace(\"```\",\"\")\n",
" .replace(\"markdown\", \"\")\n",
" .replace(\"Optimized Title:\", \"**Optimized Title:**\")\n",
" .replace(\"Justification:\", \"**Justification:**\")\n",
" )\n",
" update_display(Markdown(response), display_id=display_handle.display_id)\n",
" else:\n",
" response = get_title(model=model)\n",
" response = response.choices[0].message.content\n",
" response = filter_response(response)\n",
"\n",
" # insert line break to preserve format - only LLAMA\n",
" line_break = \"<br><br>\"\n",
" if model == \"DEEPSEEK\":\n",
" line_break = \"\"\n",
" \n",
" response = (\n",
" response.replace(\"Optimized Title:\", \"**Optimized Title:**\")\n",
" .replace(\"Justification:\", f\"{line_break}**Justification:**\") \n",
" )\n",
" display(Markdown(response))"
]
},
{
"cell_type": "markdown",
"id": "947b42ed-5b43-486d-8af3-e5b671c1fd0e",
"metadata": {},
"source": [
"### Get OpenAI Suggested Title"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb6f66e3-ab99-4f76-9358-896cb43c1fa1",
"metadata": {},
"outputs": [],
"source": [
"# get and display openAi suggested title\n",
"display_title(model='GPT')"
]
},
{
"cell_type": "markdown",
"id": "70073ebf-a00a-416b-854d-642d450cd99b",
"metadata": {},
"source": [
"### Get Llama Suggested Title"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "caa190bb-de5f-45cc-b671-5d62688f7b25",
"metadata": {},
"outputs": [],
"source": [
"# get and display Llama suggested title\n",
"display_title(model='LLAMA')"
]
},
{
"cell_type": "markdown",
"id": "811edc4f-20e2-482d-ac89-fae9d1b70bed",
"metadata": {},
"source": [
"### Get Deepseek Suggested Title"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "082628e4-ff4c-46dd-ae5f-76578eb017ad",
"metadata": {},
"outputs": [],
"source": [
"# get and display Deepseek title\n",
"display_title(model='DEEPSEEK')"
]
},
{
"cell_type": "markdown",
"id": "f2d401ed-734d-4e96-be30-09b49d516f38",
"metadata": {},
"source": [
"### Using Firecrawl LLM Extract (to replace LLMs above - OpenAI, Llama & Deepseek)"
]
},
{
"cell_type": "markdown",
"id": "3e6495a2-df0b-4a7b-a376-692456be633d",
"metadata": {},
"source": [
"### Get Firecrawl Suggested Title"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8763b0a-54ef-409f-8dd6-13231b6f7774",
"metadata": {},
"outputs": [],
"source": [
"fc_title = WebsiteCrawler(url=article_url, format='json')\n",
"\n",
"display(Markdown(fc_title.getResult()))"
]
},
{
"cell_type": "markdown",
"id": "7fc404a6-3a91-4c09-89de-867d3d69b4b2",
"metadata": {},
"source": [
"### Observations\n",
"\n",
"1. **Firecrawl** is a great alternative to replace both Selenium and BeautifulSoup. However, it is not free.\n",
"2. **Firecrawl LLM Extract** feature may replace the calls to other LLMs for analysis and title generation. Note that the result provided seems to be cached upon its first generation. Therefore, the suggested title and its justification will always be the same. \n",
"3. **Deepseek challenges:**\\\n",
" a.It always returns its thinking/reasoning verbose, which, while helpful to understand how it works, is not always\n",
" required, such as in this example code. A new function (**filter_response**) was created to remove the additional verbose.\\\n",
" b. It is unreliable with the response, sometimes returning the required format for the response instead of the\n",
" actual response. For example, for the title, it may sometimes return:\n",
" \n",
" **Optimized Title:** \\[The user wants the suggested title here]\n",
" \n",
"### Suggested future improvements\n",
"\n",
"1. Add the logic that would allow each model to assess the recommendations from the different models and \n",
" select the best among these.\n",
"2. Add the logic to leverage an API (if available) that automatically assesses the suggested titles."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1af8260b-5ba1-4eeb-acd0-02de537b1bf4",
"metadata": {},
"outputs": [],
"source": [
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,369 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "83bbedd0-eb58-48de-992e-484071b10104",
"metadata": {},
"source": [
"# Web Scraper with JavaScript Support\n",
"Uses day1-webscraping-selenium-for-javascript.ipynb solution simplified so easy to run.\n",
"\n",
"## Install dependencies\n",
"Uncomment and run once"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2d91971-9dd0-4714-8ec7-f1fb25f95140",
"metadata": {},
"outputs": [],
"source": [
"# !pip install selenium\n",
"# !pip install undetected-chromedriver\n",
"# !ollama pull llama3.2"
]
},
{
"cell_type": "markdown",
"id": "967258fe-3296-464c-962d-2bcf821eae67",
"metadata": {},
"source": [
"## Import required dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe8a87c8-0475-45a1-8ca2-fb9059e5470b",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display\n",
"from openai import OpenAI\n",
"import undetected_chromedriver as uc\n",
"from selenium.webdriver.common.by import By\n",
"from selenium.webdriver.support.ui import WebDriverWait\n",
"from selenium.webdriver.support import expected_conditions as EC\n",
"import time\n",
"from bs4 import BeautifulSoup\n",
"\n",
"# If you get an error running this cell, then please head over to the troubleshooting notebook!"
]
},
{
"cell_type": "markdown",
"id": "df60545e-2ab6-4e37-b41c-27ddf2affb92",
"metadata": {},
"source": [
"## Run setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3846089-efa2-4602-8bc3-5f6f4945de64",
"metadata": {},
"outputs": [],
"source": [
"chrome_path = \"C:/Program Files/Google/Chrome/Application/chrome.exe\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b835812d-3692-4192-abc4-15fc463bd08f",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"# Check the key\n",
"\n",
"if not api_key:\n",
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
"elif not api_key.startswith(\"sk-proj-\"):\n",
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
"elif api_key.strip() != api_key:\n",
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
"else:\n",
" print(\"API key found and looks good so far!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acb89abb-dcee-4da6-98f8-e339d258f2a4",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"\n",
"# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down.\n",
"# If it STILL doesn't work (horrors!) then please see the troubleshooting notebook, or try the below line instead:\n",
"# openai = OpenAI(api_key=\"your-key-here-starting-sk-proj-\")"
]
},
{
"cell_type": "markdown",
"id": "e860e963-e7a1-4888-a4b9-db9c24bb9a6e",
"metadata": {},
"source": [
"# Create Prompts"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4933c36-db8a-4333-8f81-e9db7ba41287",
"metadata": {},
"outputs": [],
"source": [
"# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\n",
"\n",
"system_prompt = \"You are an assistant that analyzes the contents of a website \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"Respond in markdown.\"\n",
"\n",
"# A function that writes a User Prompt that asks for summaries of websites:\n",
"\n",
"def user_prompt_for(website):\n",
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
" user_prompt += \"\\nThe contents of this website is as follows; \\\n",
"please provide a short summary of this website in markdown. \\\n",
"If it includes news or announcements, then summarize these too.\\n\\n\"\n",
" user_prompt += website.text\n",
" return user_prompt\n"
]
},
{
"cell_type": "markdown",
"id": "17cfab59-304d-4d2f-b324-c388d9e87fca",
"metadata": {},
"source": [
"# Create Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca5e96e0-4d8f-49de-a608-a735a5b23b1a",
"metadata": {},
"outputs": [],
"source": [
"# Setup for how OpenAI expects to receive messages in a particular structure\n",
"\n",
"def messages_for(website):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
" ]\n",
"\n",
"# Use Selenium and chrome to scrape website\n",
"class WebsiteCrawler:\n",
" def __init__(self, url, wait_time=20, chrome_binary_path=None):\n",
" \"\"\"\n",
" Initialize the WebsiteCrawler using Selenium to scrape JavaScript-rendered content.\n",
" \"\"\"\n",
" self.url = url\n",
" self.wait_time = wait_time\n",
"\n",
" options = uc.ChromeOptions()\n",
" options.add_argument(\"--disable-gpu\")\n",
" options.add_argument(\"--no-sandbox\")\n",
" options.add_argument(\"--disable-dev-shm-usage\")\n",
" options.add_argument(\"--disable-blink-features=AutomationControlled\")\n",
" options.add_argument(\"start-maximized\")\n",
" options.add_argument(\n",
" \"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
" )\n",
" if chrome_binary_path:\n",
" options.binary_location = chrome_binary_path\n",
"\n",
" self.driver = uc.Chrome(options=options)\n",
"\n",
" try:\n",
" # Load the URL\n",
" self.driver.get(url)\n",
"\n",
" # Wait for Cloudflare or similar checks\n",
" time.sleep(10)\n",
"\n",
" # Ensure the main content is loaded\n",
" WebDriverWait(self.driver, self.wait_time).until(\n",
" EC.presence_of_element_located((By.TAG_NAME, \"main\"))\n",
" )\n",
"\n",
" # Extract the main content\n",
" main_content = self.driver.find_element(By.CSS_SELECTOR, \"main\").get_attribute(\"outerHTML\")\n",
"\n",
" # Parse with BeautifulSoup\n",
" soup = BeautifulSoup(main_content, \"html.parser\")\n",
" self.title = self.driver.title if self.driver.title else \"No title found\"\n",
" self.text = soup.get_text(separator=\"\\n\", strip=True)\n",
"\n",
" except Exception as e:\n",
" print(f\"Error occurred: {e}\")\n",
" self.title = \"Error occurred\"\n",
" self.text = \"\"\n",
"\n",
" finally:\n",
" self.driver.quit()\n",
"\n",
"def new_summary(url, chrome_path):\n",
" web = WebsiteCrawler(url, 30, chrome_path)\n",
" response = openai.chat.completions.create(\n",
" model = \"gpt-4o-mini\",\n",
" messages = messages_for(web)\n",
" )\n",
"\n",
" web_summary = response.choices[0].message.content\n",
" \n",
" return display(Markdown(web_summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20a8a14b-0a29-4f74-a591-d587b965409b",
"metadata": {},
"outputs": [],
"source": [
"# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\n",
"\n",
"system_prompt = \"You are an assistant that analyzes the contents of a website \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"Respond in markdown.\"\n",
"\n",
"# A function that writes a User Prompt that asks for summaries of websites:\n",
"\n",
"def user_prompt_for(website):\n",
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
" user_prompt += \"\\nThe contents of this website is as follows; \\\n",
"please provide a short summary of this website in markdown. \\\n",
"If it includes news or announcements, then summarize these too.\\n\\n\"\n",
" user_prompt += website.text\n",
" return user_prompt\n",
"\n",
"# Setup for how OpenAI expects to receive messages in a particular structure\n",
"\n",
"def messages_for(website):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
" ]\n",
"\n",
"# Use Selenium and chrome to scrape website\n",
"class WebsiteCrawler:\n",
" def __init__(self, url, wait_time=20, chrome_binary_path=None):\n",
" \"\"\"\n",
" Initialize the WebsiteCrawler using Selenium to scrape JavaScript-rendered content.\n",
" \"\"\"\n",
" self.url = url\n",
" self.wait_time = wait_time\n",
"\n",
" options = uc.ChromeOptions()\n",
" options.add_argument(\"--disable-gpu\")\n",
" options.add_argument(\"--no-sandbox\")\n",
" options.add_argument(\"--disable-dev-shm-usage\")\n",
" options.add_argument(\"--disable-blink-features=AutomationControlled\")\n",
" options.add_argument(\"start-maximized\")\n",
" options.add_argument(\n",
" \"user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
" )\n",
" if chrome_binary_path:\n",
" options.binary_location = chrome_binary_path\n",
"\n",
" self.driver = uc.Chrome(options=options)\n",
"\n",
" try:\n",
" # Load the URL\n",
" self.driver.get(url)\n",
"\n",
" # Wait for Cloudflare or similar checks\n",
" time.sleep(10)\n",
"\n",
" # Ensure the main content is loaded\n",
" WebDriverWait(self.driver, self.wait_time).until(\n",
" EC.presence_of_element_located((By.TAG_NAME, \"main\"))\n",
" )\n",
"\n",
" # Extract the main content\n",
" main_content = self.driver.find_element(By.CSS_SELECTOR, \"main\").get_attribute(\"outerHTML\")\n",
"\n",
" # Parse with BeautifulSoup\n",
" soup = BeautifulSoup(main_content, \"html.parser\")\n",
" self.title = self.driver.title if self.driver.title else \"No title found\"\n",
" self.text = soup.get_text(separator=\"\\n\", strip=True)\n",
"\n",
" except Exception as e:\n",
" print(f\"Error occurred: {e}\")\n",
" self.title = \"Error occurred\"\n",
" self.text = \"\"\n",
"\n",
" finally:\n",
" self.driver.quit()\n",
"\n",
"def new_summary(url, chrome_path):\n",
" web = WebsiteCrawler(url, 30, chrome_path)\n",
" response = openai.chat.completions.create(\n",
" model = \"gpt-4o-mini\",\n",
" messages = messages_for(web)\n",
" )\n",
"\n",
" web_summary = response.choices[0].message.content\n",
" \n",
" return display(Markdown(web_summary))"
]
},
{
"cell_type": "markdown",
"id": "e5f974b3-e417-43a2-88f1-8db06096cd53",
"metadata": {},
"source": [
"# Scrape and Summarize Web Page"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55f240cb-1fca-46bf-81d1-1beeea64439d",
"metadata": {},
"outputs": [],
"source": [
"url = \"https://www.canva.com/\"\n",
"new_summary(url, chrome_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,274 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "41136d6f-07bc-4f6f-acba-784b8e5707b1",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8612b4f7-5c31-48f3-8423-261914509617",
"metadata": {},
"outputs": [],
"source": [
"# Constants\n",
"\n",
"OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
"HEADERS = {\"Content-Type\": \"application/json\"}\n",
"MODEL = \"llama3.2\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "508bd442-7860-4215-b0f2-57f7adefd807",
"metadata": {},
"outputs": [],
"source": [
"# Create a messages list using the same format that we used for OpenAI\n",
"\n",
"messages = [\n",
" {\"role\": \"user\", \"content\": \"Describe some of the business applications of Generative AI\"}\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc7e8ada-4f8d-4090-be64-4aa72e03ac58",
"metadata": {},
"outputs": [],
"source": [
"# Let's just make sure the model is loaded\n",
"\n",
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4afd2e56-191a-4e31-949e-9b9376a39b5a",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# There's actually an alternative approach that some people might prefer\n",
"# You can use the OpenAI client python library to call Ollama:\n",
"\n",
"from openai import OpenAI\n",
"ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"\n",
"response = ollama_via_openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages\n",
")\n",
"\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "365f3d83-2601-42fb-89cc-98a4e1f79e0d",
"metadata": {},
"outputs": [],
"source": [
"message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n",
"response = ollama_via_openai.chat.completions.create(model=MODEL, messages=[{\"role\":\"user\", \"content\":message}])\n",
"print(response.choices[0].message.content)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29c383ae-bf5b-41bc-b5af-a22f851745dc",
"metadata": {},
"outputs": [],
"source": [
"# A class to represent a Webpage\n",
"# If you're not familiar with Classes, check out the \"Intermediate Python\" notebook\n",
"\n",
"# Some websites need you to use proper headers when fetching them:\n",
"headers = {\n",
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
"}\n",
"\n",
"class Website:\n",
"\n",
" def __init__(self, url):\n",
" \"\"\"\n",
" Create this Website object from the given url using the BeautifulSoup library\n",
" \"\"\"\n",
" self.url = url\n",
" response = requests.get(url, headers=headers)\n",
" soup = BeautifulSoup(response.content, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc61e30f-653f-4554-b1cd-6e61a0e2430a",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"ed = Website(\"https://edwarddonner.com\")\n",
"print(ed.title)\n",
"print(ed.text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "db2066fb-3079-4775-832a-dcc0f19beb6e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"system_prompt = \"You are an assistant that analyzes the contents of a website \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"Respond in markdown.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af81b070-b6fe-4b18-aa0b-c03cd76a0adf",
"metadata": {},
"outputs": [],
"source": [
"def user_prompt_for(website):\n",
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
" user_prompt += \"\\nThe contents of this website is as follows; \\\n",
"please provide a short summary of this website in markdown. \\\n",
"If it includes news or announcements, then summarize these too.\\n\\n\"\n",
" user_prompt += website.text\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e66291b-23b1-4915-b6a3-11a4b6a4db66",
"metadata": {},
"outputs": [],
"source": [
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a snarky assistant\"},\n",
" {\"role\": \"user\", \"content\": \"What is 2 + 2?\"}\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67c92f47-4a3b-491f-af00-07fda470087e",
"metadata": {},
"outputs": [],
"source": [
"def messages_for(website):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "db1b9085-e5e7-4ec9-a264-acc389085ada",
"metadata": {},
"outputs": [],
"source": [
"messages_for(ed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "677bfc2f-19ac-46a0-b67e-a2b2ddf9cf6b",
"metadata": {},
"outputs": [],
"source": [
"def summarize(url):\n",
" website = Website(url)\n",
" response = ollama_via_openai.chat.completions.create(\n",
" model = MODEL,\n",
" messages = messages_for(website)\n",
" )\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee3242ba-b695-4b1e-8a91-2fdeb536c2e7",
"metadata": {},
"outputs": [],
"source": [
"summarize(\"https://edwarddonner.com\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85142cb8-ce0c-4c31-8b26-bb1744cf99ec",
"metadata": {},
"outputs": [],
"source": [
"def display_summary(url):\n",
" summary = summarize(url)\n",
" display(Markdown(summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63db51a7-dd03-4514-8954-57156967f82c",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"display_summary(\"https://app.daily.dev/posts/bregman-arie-devops-exercises-linux-jenkins-aws-sre-prometheus-docker-python-ansible-git-k-yli9wthnf\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,379 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "a98030af-fcd1-4d63-a36e-38ba053498fa",
"metadata": {},
"source": [
"# Snarky brochure"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5b08506-dc8b-4443-9201-5f1848161363",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"# If these fail, please check you're running from an 'activated' environment with (llms) in the command prompt\n",
"\n",
"import os\n",
"import requests\n",
"import json\n",
"from typing import List\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc5d8880-f2ee-4c06-af16-ecbc0262af61",
"metadata": {},
"outputs": [],
"source": [
"# Initialize and constants\n",
"\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if api_key and api_key.startswith('sk-proj-') and len(api_key)>10:\n",
" print(\"API key looks good so far\")\n",
"else:\n",
" print(\"There might be a problem with your API key? Please visit the troubleshooting notebook!\")\n",
" \n",
"MODEL = 'gpt-4o-mini'\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "106dd65e-90af-4ca8-86b6-23a41840645b",
"metadata": {},
"outputs": [],
"source": [
"# A class to represent a Webpage\n",
"\n",
"# Some websites need you to use proper headers when fetching them:\n",
"headers = {\n",
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
"}\n",
"\n",
"class Website:\n",
" \"\"\"\n",
" A utility class to represent a Website that we have scraped, now with links\n",
" \"\"\"\n",
"\n",
" def __init__(self, url):\n",
" self.url = url\n",
" response = requests.get(url, headers=headers)\n",
" self.body = response.content\n",
" soup = BeautifulSoup(self.body, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n",
" if soup.body:\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)\n",
" else:\n",
" self.text = \"\"\n",
" links = [link.get('href') for link in soup.find_all('a')]\n",
" self.links = [link for link in links if link]\n",
"\n",
" def get_contents(self):\n",
" return f\"Webpage Title:\\n{self.title}\\nWebpage Contents:\\n{self.text}\\n\\n\""
]
},
{
"cell_type": "markdown",
"id": "1771af9c-717a-4fca-bbbe-8a95893312c3",
"metadata": {},
"source": [
"## Link prompts\n",
"### Multi-shot system prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6957b079-0d96-45f7-a26a-3487510e9b35",
"metadata": {},
"outputs": [],
"source": [
"link_system_prompt = \"You are provided with a list of links found on a webpage. \\\n",
"You are able to decide which of the links would be most relevant to include in a brochure about the company, \\\n",
"such as links to an About page, or a Company page, or Careers/Jobs pages.\\n\"\n",
"link_system_prompt += \"You should respond in JSON as in these examples:\"\n",
"link_system_prompt += \"\"\"\n",
"Example 1\n",
"['https://my-company.com', 'https://my-company.com/about-me', 'https://www.linkedin.com/in/my-company/', 'mailto:joe.blog@gmail.com', 'https://my-company.com/news', '/case-studies', 'https://patents.google.com/patent/US20210049536A1/', 'https://my-company.com/workshop-ai']\n",
"\n",
" Links:\n",
"{\n",
" \"links\": [\n",
" {\"type\": \"landing page\", \"url\": \"https://great-comps.com/about-me\"},\n",
" {\"type\": \"about page\", \"url\": \"https://great-comps.com/about-me\"},\n",
" {\"type\": \"news page\": \"url\": \"https://great-comps.com/news\"},\n",
" {\"type\": \"case studies page\": \"url\": \"https://great-comps.com/case-studies\"},\n",
" {\"type\": \"workshop page\": \"url\": \"https://great-comps.com/workshop-ai\"},\n",
" ]\n",
"}\n",
"Example 2\n",
"['https://www.acmeinc.com', '/#about', '/#projects', '/#experience', '/#skills', 'https://github.com/acmeinc']\n",
"\n",
" Links:\n",
"{\n",
" \"links\": [\n",
" {\"type\": \"landing page\", \"url\": \"https://www.acmeinc.com\"},\n",
" {\"type\": \"GitHub projects\": \"url\": \"https://github.com/acmeinc\"},\n",
" ]\n",
"}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b97e4068-97ed-4120-beae-c42105e4d59a",
"metadata": {},
"outputs": [],
"source": [
"print(link_system_prompt)"
]
},
{
"cell_type": "markdown",
"id": "baf384bb-4577-4885-a445-dc8da232b1d9",
"metadata": {},
"source": [
"### User prompt"
]
},
{
"cell_type": "markdown",
"id": "51174859-666a-43ad-9c34-5f082298d398",
"metadata": {},
"source": [
"## Get links"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e1f601b-2eaf-499d-b6b8-c99050c9d6b3",
"metadata": {},
"outputs": [],
"source": [
"def get_links_user_prompt(website):\n",
" user_prompt = f\"Here is the list of links on the website of {website.url} - \"\n",
" user_prompt += \"please decide which of these are relevant web links for a brochure about the company, respond with the full https URL in JSON format. \\\n",
"Do not include Terms of Service, Privacy, email links.\\n\"\n",
" user_prompt += \"Links (some might be relative links):\\n\"\n",
" user_prompt += \"\\n\".join(website.links)\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a29aca19-ca13-471c-a4b4-5abbfa813f69",
"metadata": {},
"outputs": [],
"source": [
"def get_links(url):\n",
" website = Website(url)\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": link_system_prompt},\n",
" {\"role\": \"user\", \"content\": get_links_user_prompt(website)}\n",
" ],\n",
" response_format={\"type\": \"json_object\"}\n",
" )\n",
" result = response.choices[0].message.content\n",
" return json.loads(result)"
]
},
{
"cell_type": "markdown",
"id": "0d74128e-dfb6-47ec-9549-288b621c838c",
"metadata": {},
"source": [
"## Create brochure"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85a5b6e2-e7ef-44a9-bc7f-59ede71037b5",
"metadata": {},
"outputs": [],
"source": [
"def get_all_details(url):\n",
" result = \"Landing page:\\n\"\n",
" result += Website(url).get_contents()\n",
" links = get_links(url)\n",
" print(\"Found links:\", links)\n",
" for link in links[\"links\"]:\n",
" result += f\"\\n\\n{link['type']}\\n\"\n",
" result += Website(link[\"url\"]).get_contents()\n",
" return result"
]
},
{
"cell_type": "markdown",
"id": "4b4d8ec1-4855-4c0e-afc0-33055e6b0a6d",
"metadata": {},
"source": [
"### Snarky system prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b863a55-f86c-4e3f-8a79-94e24c1a8cf2",
"metadata": {},
"outputs": [],
"source": [
"# system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n",
"# and creates a short brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n",
"# Include details of company culture, customers and careers/jobs if you have the information.\"\n",
"\n",
"# Or uncomment the lines below for a more humorous brochure - this demonstrates how easy it is to incorporate 'tone':\n",
"\n",
"# system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n",
"# and creates a short humorous, entertaining, jokey brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n",
"# Include details of company culture, customers and careers/jobs if you have the information.\"\n",
"\n",
"system_prompt = \"You are an assistant that analyzes the contents of several relevant pages from a company website \\\n",
"and creates a short snarky, entertaining, pun loaded brochure about the company for prospective customers, investors and recruits. Respond in markdown.\\\n",
"Include details of company culture, customers and careers/jobs if you have the information.\"\n"
]
},
{
"cell_type": "markdown",
"id": "c5766318-97cc-4442-bb9f-fa8c6998777e",
"metadata": {},
"source": [
"### User prompt"
]
},
{
"cell_type": "markdown",
"id": "d6e224b2-8ab0-476e-96c3-42763ad21f25",
"metadata": {},
"source": [
"### Generate brochure in English"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ab83d92-d36b-4ce0-8bcc-5bb4c2f8ff23",
"metadata": {},
"outputs": [],
"source": [
"def get_brochure_user_prompt(company_name, url):\n",
" user_prompt = f\"You are looking at a company called: {company_name}\\n\"\n",
" user_prompt += f\"Here are the contents of its landing page and other relevant pages; use this information to build a short brochure of the company in markdown.\\n\"\n",
" user_prompt += get_all_details(url)\n",
" user_prompt = user_prompt[:5_000] # Truncate if more than 5,000 characters\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e44de579-4a1a-4e6a-a510-20ea3e4b8d46",
"metadata": {},
"outputs": [],
"source": [
"def create_brochure(company_name, url):\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n",
" ],\n",
" )\n",
" result = response.choices[0].message.content\n",
" display(Markdown(result))\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e093444a-9407-42ae-924a-145730591a39",
"metadata": {},
"outputs": [],
"source": [
"brochure_text = create_brochure(\"HuggingFace\", \"https://huggingface.co\")"
]
},
{
"cell_type": "markdown",
"id": "30415c72-d26a-454e-8900-f584977aca96",
"metadata": {},
"source": [
"### Translate brochure to another language"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2331eb34-12bf-4e88-83f9-a48d97cc83ec",
"metadata": {},
"outputs": [],
"source": [
"translation_sys_prompt = \"You are a language translator who is very good at translating business documents from \\\n",
"English to any language. You preserve the formatting, tone and facts contained in the document.\"\n",
"\n",
"def translate_brochure(brochure, language):\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": translation_sys_prompt},\n",
" {\"role\": \"user\", \"content\": f\"Translate the following document into {language}: {brochure}\"}\n",
" ],\n",
" )\n",
" result = response.choices[0].message.content\n",
" display(Markdown(result))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "112beb4d-984b-4162-8d36-8cef79c351cc",
"metadata": {},
"outputs": [],
"source": [
"translate_brochure(brochure_text, \"Spanish\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,183 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "de3b5d4c",
"metadata": {},
"source": [
"# 🧠 Grafana Dashboard Summarizer\n",
"Simulate reading a Grafana dashboard JSON and summarize its panels using GPT or plain logic."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0abf3aaf",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display\n",
"from openai import OpenAI\n",
"import json\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad82ca65",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"with open(\"mock_grafana_dashboard.json\", \"r\") as f:\n",
" data = json.load(f)\n",
"\n",
"dashboard = data[\"dashboard\"]\n",
"panels = dashboard[\"panels\"]\n",
"print(f\"Dashboard Title: {dashboard['title']}\")\n",
"print(f\"Total Panels: {len(panels)}\\n\")\n",
"for p in panels:\n",
" print(f\"- {p['title']} ({p['type']})\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1bf45c0f",
"metadata": {},
"outputs": [],
"source": [
"# Extracting panel data\n",
"\n",
"panel_data = []\n",
"for p in panels:\n",
" thresholds = p.get(\"fieldConfig\", {}).get(\"defaults\", {}).get(\"thresholds\", {}).get(\"steps\", [])\n",
" panel_data.append({\n",
" \"Title\": p[\"title\"],\n",
" \"Type\": p[\"type\"],\n",
" \"Unit\": p.get(\"fieldConfig\", {}).get(\"defaults\", {}).get(\"unit\", \"N/A\"),\n",
" \"Thresholds\": thresholds\n",
" })\n",
"\n",
"df = pd.DataFrame(panel_data)\n",
"df\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90b67133",
"metadata": {},
"outputs": [],
"source": [
"\n",
"summary_prompt = f\"\"\"\n",
"You are a helpful assistant summarizing a system monitoring dashboard.\n",
"\n",
"Dashboard: {dashboard['title']}\n",
"Panels:\n",
"\"\"\"\n",
"for idx, row in df.iterrows():\n",
" summary_prompt += f\"- {row['Title']} [{row['Type']}] - Unit: {row['Unit']}, Thresholds: {row['Thresholds']}\\n\"\n",
"\n",
"print(summary_prompt)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69a4208c",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"# Check the key\n",
"\n",
"if not api_key:\n",
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
"elif not api_key.startswith(\"sk-proj-\"):\n",
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
"elif api_key.strip() != api_key:\n",
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
"else:\n",
" print(\"API key found and looks good so far!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2eee5a32",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "660eedb7",
"metadata": {},
"outputs": [],
"source": [
"def summarize():\n",
" response = openai.chat.completions.create(\n",
" model=\"gpt-4o-mini\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a Grafana dashboard summarizer.\"},\n",
" {\"role\": \"user\", \"content\": summary_prompt}\n",
" ]\n",
")\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55f57d56",
"metadata": {},
"outputs": [],
"source": [
"\n",
"summary = summarize()\n",
"display(Markdown(summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10dbfd6c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "arunllms",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,95 @@
{
"dashboard": {
"id": null,
"uid": "mock-sys-metrics",
"title": "Mock System Metrics Dashboard",
"timezone": "browser",
"panels": [
{
"id": 1,
"title": "CPU Usage (%)",
"type": "stat",
"datasource": "MockData",
"targets": [],
"fieldConfig": {
"defaults": {
"unit": "percent",
"thresholds": {
"mode": "percentage",
"steps": [
{ "color": "green", "value": null },
{ "color": "orange", "value": 70 },
{ "color": "red", "value": 90 }
]
}
}
},
"options": {
"reduceOptions": {
"calcs": ["mean"]
}
}
},
{
"id": 2,
"title": "Memory Usage",
"type": "gauge",
"datasource": "MockData",
"fieldConfig": {
"defaults": {
"unit": "bytes",
"max": 16e9,
"thresholds": {
"steps": [
{ "color": "green", "value": null },
{ "color": "orange", "value": 12e9 },
{ "color": "red", "value": 14e9 }
]
}
}
}
},
{
"id": 3,
"title": "Disk Read Errors",
"type": "stat",
"datasource": "MockData",
"fieldConfig": {
"defaults": {
"unit": "short",
"thresholds": {
"steps": [
{ "color": "green", "value": null },
{ "color": "orange", "value": 5 },
{ "color": "red", "value": 10 }
]
}
}
}
},
{
"id": 4,
"title": "GPU Usage (%)",
"type": "gauge",
"datasource": "MockData",
"fieldConfig": {
"defaults": {
"unit": "percent",
"thresholds": {
"steps": [
{ "color": "green", "value": null },
{ "color": "orange", "value": 75 },
{ "color": "red", "value": 90 }
]
}
}
}
}
],
"schemaVersion": 30,
"version": 1,
"refresh": "30s"
},
"overwrite": true
}

View File

@@ -0,0 +1,221 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fe12c203-e6a6-452c-a655-afb8a03a4ff5",
"metadata": {},
"source": [
"# End of week 1 exercise\n",
"\n",
"To demonstrate your familiarity with OpenAI API, and also Ollama, build a tool that takes a technical question, \n",
"and responds with an explanation. This is a tool that you will be able to use yourself during the course!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c1070317-3ed9-4659-abe3-828943230e03",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4a456906-915a-4bfd-bb9d-57e505c5093f",
"metadata": {},
"outputs": [],
"source": [
"# constants\n",
"\n",
"MODEL_GPT = 'gpt-4o-mini'\n",
"MODEL_LLAMA = 'llama3.2'\n",
"\n",
"OLLAMA_API = \"http://localhost:11434/v1\"\n",
"HEADERS = {\"Content-Type\": \"application/json\"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a8d7923c-5f28-4c30-8556-342d7c8497c1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"API key looks good so far\n"
]
}
],
"source": [
"# set up environment\n",
"\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if api_key and api_key.startswith('sk-proj-') and len(api_key)>10:\n",
" print(\"API key looks good so far\")\n",
"else:\n",
" print(\"There might be a problem with your API key? Please visit the troubleshooting notebook!\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3f0d0137-52b0-47a8-81a8-11a90a010798",
"metadata": {},
"outputs": [],
"source": [
"# here is the system prompt and payloads;\n",
"\n",
"system_prompt = \"\"\"\n",
"You are an expert on LLMs and writing python code. You are able to answer complex questions with\n",
"detailed answers and explain what every line of code does. You can refactor the code when asked.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "60ce7000-a4a5-4cce-a261-e75ef45063b4",
"metadata": {},
"outputs": [],
"source": [
"# Function to get answer, with streaming\n",
"\n",
"def llm_copilot(question, model):\n",
" if 'llama' in model.lower():\n",
" openai = OpenAI(base_url=OLLAMA_API, api_key='ollama')\n",
" else:\n",
" openai = OpenAI()\n",
" \n",
" stream = openai.chat.completions.create(\n",
" model=model,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": question}\n",
" ],\n",
" stream=True\n",
" )\n",
" response = \"\"\n",
" display_handle = display(Markdown(\"\"), display_id=True)\n",
" for chunk in stream:\n",
" response += chunk.choices[0].delta.content or ''\n",
" response = response.replace(\"```\",\"\").replace(\"markdown\", \"\")\n",
" update_display(Markdown(response), display_id=display_handle.display_id)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8f7c8ea8-4082-4ad0-8751-3301adcf6538",
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"Here's a revised version of your code:\n",
"\n",
"python\n",
"if 'llama' in model.lower():\n",
"\n",
"\n",
"OR if you want to keep the original style, you can modify it as follows:\n",
"\n",
"python\n",
"if model.split('.')[-1] == 'llama3.2':\n",
"\n",
"\n",
"In this second example, we use string indexing (`-1`) to get the last part of the `model` string after splitting at the dot (`.`) character.\n",
"\n",
"The first revised version uses Python's built-in string method `lower()` to convert `model` to lowercase and then checks if 'llama' is present in it. It returns True if the text contains \"llama\", otherwise, it will return False. \n",
"\n",
"However, both of these codes are using lazy evaluation, which means if you do this check inside a loop:\n",
"\n",
"python\n",
"for i in range(100):\n",
" print('llama')\n",
"\n",
"\n",
"Python will use 'a' instead of 'llame' most of the time until `i == 98` because it has to wait for the condition to be met (and also does some lookup and look-around). If you want a case-insensitive search without this slowness, consider using a regular expression as shown below\n",
"\n",
"python\n",
"import re\n",
"\n",
"if re.search(r' llama.', model):\n",
"\n",
"\n",
"And if you still want that specific code structure, then use `replace` function as follows:\n",
"\n",
"python\n",
"model = model.replace('llama', '')\n",
"if model == '3.2':\n"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"# Ask question\n",
"question = \"\"\"\n",
"Change this code to check for just the 'llama' portion of text instead of the entire string:\n",
"if model == 'llama3.2':\n",
"\"\"\"\n",
"\n",
"print(llm_copilot(question, MODEL_LLAMA))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a4026cd-8967-4961-b26b-e3997307c4ba",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,149 @@
import os
import time
import sys
from dotenv import load_dotenv
from bs4 import BeautifulSoup
from openai import OpenAI
import random
import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
load_dotenv()
class WebsiteScrapper:
def __init__(self, url, max_retries=2, headless=True, wait_selector="body", wait_timeout=10):
self.url = url
self.__text = ""
self.__title = ""
self.headless = headless
self.max_retries = max_retries
self.wait_selector = wait_selector
self.wait_timeout = wait_timeout
def __log_html(self, html, filename="last_scraped.html"):
try:
with open(filename, "w", encoding="utf-8") as f:
f.write(html)
print(f"Saved page HTML to {filename} for debugging.")
except Exception as e:
print(f"!!! Could not save page HTML: {e}")
def parse(self):
attempt = 0
while attempt < self.max_retries:
try:
options = uc.ChromeOptions()
options.headless = self.headless # Set to False if you want to see the browser
options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.page_load_strategy = 'normal' # wait until fully loaded
options.add_argument("--disable-blink-features=AutomationControlled")
with uc.Chrome(options=options) as driver:
print("[Browser] Chrome started.")
driver.get(self.url)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(random.uniform(1, 3))
WebDriverWait(driver, self.wait_timeout).until(
ec.presence_of_element_located((By.CSS_SELECTOR, self.wait_selector))
)
time.sleep(1)
page_source = driver.page_source
self.__log_html(page_source)
if "enable javascript" in page_source.lower() or "checking your browser" in page_source.lower():
self.__title = "Blocked by Bot Protection"
self.__text = "This website uses advanced protection (e.g., Cloudflare). Content not accessible."
return
soup = BeautifulSoup(page_source, 'html.parser')
self.__title = soup.title.string if soup.title else "No title found"
for irrelevant in soup(["script", "style", "img", "input"]):
irrelevant.decompose()
self.__text = soup.body.get_text(separator="\n", strip=True)
try:
os.remove("last_scraped.html")
print("Cleaned up debug HTML file.")
except Exception as e:
print(f"Could not delete debug HTML file: {e}")
return # Success
except Exception as e:
print(f"!!! Attempt {attempt + 1} failed: {e}")
attempt += 1
time.sleep(2)
# All retries failed
self.__title = "Failed to load"
self.__text = "Website could not be scraped after several attempts."
def get_text(self):
return self.__text
def get_title(self):
return self.__title
class JSWebsiteSummarizer:
def __init__(self, url, headless=True):
self.url = url
os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')
self.openai = OpenAI()
self.website_scrapper = WebsiteScrapper(url, headless=headless)
self.system_prompt = "You are an assistant that analyzes the contents of a website \
and provides a short summary, ignoring text that might be navigation related. \
Respond in markdown."
@staticmethod
def __user_prompt_for(title, content):
user_prompt = f"You are looking at a website titled {title}"
user_prompt += "The contents of this website is as follows; \
please provide a short summary of this website in markdown. \
If it includes news or announcements, then summarize that too.\n\n"
user_prompt += content
return user_prompt
def __messages_for(self, title, content):
return [{"role": "system", "content": self.system_prompt}, {"role": "user", "content": JSWebsiteSummarizer.__user_prompt_for(title, content)}]
def __summarize(self):
self.website_scrapper.parse()
chat_config = self.__messages_for(self.website_scrapper.get_title(), self.website_scrapper.get_text())
response = self.openai.chat.completions.create(model="gpt-4o-mini", messages=chat_config)
return response.choices[0].message.content
def display_summary(self):
summary = self.__summarize()
if 'ipykernel' in sys.modules:
from IPython.display import Markdown, display
display(Markdown(summary))
else:
print("=== Website Summary ===\n")
print(summary)
# Use headless true for non JS/Bot/Secured website to avoid overhead
# Use headless False for JS/Bot/Secured website so as to bypass security
if __name__ == "__main__":
url1 = "https://cnn.com"
url2 = "https://openai.com"
url3 = "https://anthropic.com"
# web_summariser = JSWebsiteSummarizer(url=url1, headless=True)
#
# print("Starting website summary...")
# web_summariser.display_summary()
web_summariser = JSWebsiteSummarizer(url=url3, headless=False)
print("Starting website summary...")
web_summariser.display_summary()
print("Done!")

View File

@@ -0,0 +1,762 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d006b2ea-9dfe-49c7-88a9-a5a0775185fd",
"metadata": {},
"source": [
"# Additional End of week Exercise - week 2\n",
"\n",
"Now use everything you've learned from Week 2 to build a full prototype for the technical question/answerer you built in Week 1 Exercise.\n",
"\n",
"This should include a Gradio UI, streaming, use of the system prompt to add expertise, and the ability to switch between models. Bonus points if you can demonstrate use of a tool!\n",
"\n",
"If you feel bold, see if you can add audio input so you can talk to it, and have it respond with audio. ChatGPT or Claude can help you, or email me if you have questions.\n",
"\n",
"I will publish a full solution here soon - unless someone beats me to it...\n",
"\n",
"There are so many commercial applications for this, from a language tutor, to a company onboarding solution, to a companion AI to a course (like this one!) I can't wait to see your results."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "a07e7793-b8f5-44f4-aded-5562f633271a",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr\n",
"import base64\n",
"from io import BytesIO\n",
"import tempfile\n",
"import subprocess\n",
"from pydub import AudioSegment\n",
"import time\n",
"import anthropic"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "717ea9d4-1e72-4035-b7c5-5d61da5b8ea3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key exists and begins sk-proj-\n"
]
}
],
"source": [
"# Initialization\n",
"\n",
"load_dotenv(override=True)\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"gpt_model = \"gpt-4o-mini\"\n",
"claude_model = \"claude-3-haiku-20240307\"\n",
"\n",
"openai = OpenAI()\n",
"claude = anthropic.Anthropic()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cc78f4fd-9920-4872-9117-90cd2aeb2a06",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant. \"\n",
"system_message += \"Give short, courteous answers. You can check ticket price, ticket availability, and reserve tickets for users. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "b2701cc0-6403-4880-9b31-e6e39e89feb4",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"\n",
"ticket_prices = {\"london\": \"$799\", \"paris\": \"$899\", \"tokyo\": \"$1400\", \"berlin\": \"$499\"}\n",
"\n",
"def get_ticket_price(destination_city):\n",
" print(f\"Tool get_ticket_price called for {destination_city}\")\n",
" city = destination_city.lower()\n",
" return ticket_prices.get(city, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5e33902f-c2c3-4fb0-b01d-a346a4dff811",
"metadata": {},
"outputs": [],
"source": [
"ticket_availability = {\"london\": \"20\", \"paris\": \"90\", \"tokyo\": \"100\", \"berlin\": \"2\"}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "27dfca47-2a38-49f3-8905-f583d98710a5",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"def get_ticket_availability(destination_city):\n",
" print(f\"Tool get_ticket_availability called for {destination_city}\")\n",
" available = destination_city.lower()\n",
" return ticket_availability.get(available, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6ae7371b-031e-47d7-afaf-42d6758ccd92",
"metadata": {},
"outputs": [],
"source": [
"def get_ticket_price_availability(destination_city):\n",
" print(f\"Tool get_ticket_price_availability called for {destination_city}\")\n",
" available = destination_city.lower()\n",
" price = destination_city.lower()\n",
" return ticket_price.get(price, \"Unknown\"), ticket_availability.get(available, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c919b13a-50b6-4510-8e9d-02cdfd95cb98",
"metadata": {},
"outputs": [],
"source": [
"def book_ticket(destination_city,price,availability,no_of_tickets):\n",
" status=\"\"\n",
" if availability == 0:\n",
" status=\"Cannot book a ticket, no seat available\\n\"\n",
" elif (int(availability)-int(no_of_tickets)) < 0:\n",
" status=\"Cannot book a ticket, no seat available\\n\"\n",
" else:\n",
" print(f\"Tool book_function called for {destination_city}\")\n",
" f = open(\"C:/Users/aruna/Desktop/book_status.txt\", \"a\")\n",
" f.write(f\"{no_of_tickets} ticket/s to {destination_city} booked for {price} x {no_of_tickets}, currently available - {int(availability)-int(no_of_tickets)}\")\n",
" f.write(\"\\n\")\n",
" f.close()\n",
" ticket_availability[destination_city.lower()]=str(int(availability)-int(no_of_tickets))\n",
" \n",
" status=\"Ticket reservation is a success\\n\"\n",
" return status"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d2628781-6f5e-4ac1-bbe3-2e08aa0aae0d",
"metadata": {},
"outputs": [],
"source": [
"book_function = {\n",
" \"name\": \"book_ticket\",\n",
" \"description\": \"Book the ticket based on the ticket price and/ or availability as requested by the user. For example, when a \\\n",
" user asks to purchase one or more tickets to Tokyo after getting to know the ticket price and/or the availability, then \\\n",
" proceed with this tool call. Else, request the user to either ask for ticket price or availability first. Please help the customer in booking the ticket/s if tickets are available. But before each time\\\n",
" you book, ask confirmation from the user before proceeding with booking.\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" \"price\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The price of the ticket to the city\",\n",
" },\n",
" \"availability\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"ticket availability to the city the customer wants to travel to\",\n",
" },\n",
" \"no_of_tickets\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"the number of tickets the customer wants to purchase/ book to the destination\",\n",
" }\n",
" },\n",
" \"required\": [\"destination_city\",\"price\",\"availability\",\"no_of_tickets\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "480de296-4a36-4ec4-a5f6-149fc198c7a8",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_ticket_price\",\n",
" \"description\": \"Get the price of a one_way ticket to the destination city. Call this whenever you need to know the ticket price, for example when a customer asks 'How much is a ticket to this city'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "cf1b3e35-08ee-478e-aa1c-534418d78daf",
"metadata": {},
"outputs": [],
"source": [
"availability_function = {\n",
" \"name\": \"get_ticket_availability\",\n",
" \"description\": \"Get the availability of a one-way ticket to the destination city. Call this whenever you need to know the ticket availability, for example when a customer asks 'What is the ticket availability to this city'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "73e4c8a2-c034-41a4-9b97-7b2aa4aca504",
"metadata": {},
"outputs": [],
"source": [
"ticket_price_availability_function = {\n",
" \"name\": \"get_ticket_price_availability\",\n",
" \"description\": \"Get the price or availability of a one-way ticket to the destination city. Call this whenever you need to know the ticket price and availability, for example when a customer asks 'What is the ticket availability and price to this city'\\\n",
" or 'what is the price and ticket for the city?'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1d5d74a0-9c25-46a4-84ee-1f700bd55fa7",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [{\"type\": \"function\", \"function\": price_function},\n",
" {\"type\": \"function\", \"function\": availability_function},\n",
" {\"type\": \"function\", \"function\": ticket_price_availability_function},\n",
" {\"type\": \"function\", \"function\": book_function}]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "fa18f535-f8a7-4386-b39a-df0f84d23406",
"metadata": {},
"outputs": [],
"source": [
"def play_audio(audio_segment):\n",
" temp_dir = tempfile.gettempdir()\n",
" temp_path = os.path.join(temp_dir, \"temp_audio.wav\")\n",
" try:\n",
" audio_segment.export(temp_path, format=\"wav\")\n",
" time.sleep(3) # Student Dominic found that this was needed. You could also try commenting out to see if not needed on your PC\n",
" subprocess.call([\n",
" \"ffplay\",\n",
" \"-nodisp\",\n",
" \"-autoexit\",\n",
" \"-hide_banner\",\n",
" temp_path\n",
" ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n",
" finally:\n",
" try:\n",
" os.remove(temp_path)\n",
" except Exception:\n",
" pass\n",
" \n",
"def talker(message):\n",
" response = openai.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=\"alloy\", # Also, try replacing with onyx\n",
" input=message\n",
" )\n",
" audio_stream = BytesIO(response.content)\n",
" audio = AudioSegment.from_file(audio_stream, format=\"mp3\")\n",
" play_audio(audio)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "b588d711-5f20-4a3a-9422-81a1fda8d5b0",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call1(message):\n",
" tool_call = message.tool_calls[0]\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" name = json.dumps(tool_call.function.name)\n",
" city = arguments.get('destination_city')\n",
" no = arguments.get('no_of_tickets')\n",
" \n",
" if name.replace('\"','') == \"get_ticket_price\":\n",
" price = get_ticket_price(city)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"price\": price}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" elif name.replace('\"','') == \"book_ticket\":\n",
" price = get_ticket_price(city)\n",
" availability = get_ticket_availability(city)\n",
" booked=book_ticket(city,price,availability,no)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city, \"number of tickets\":no, \"booking_status\": booked}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" else :\n",
" availability = get_ticket_availability(city)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"availability\": availability},),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" \n",
" return response, city"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "e74eee70-f89e-4c03-922c-74f9ab567a4c",
"metadata": {},
"outputs": [],
"source": [
"def chat_open_ai(history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history \n",
" response = openai.chat.completions.create(model=gpt_model, messages=messages, tools=tools)\n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" print(message)\n",
" tool_call = message.tool_calls[0]\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" name = json.dumps(tool_call.function.name)\n",
" city = arguments.get('destination_city')\n",
" \n",
" if name.replace('\"','') == \"book_ticket\":\n",
" response, city = handle_tool_call1(message)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" # image = artist(city)\n",
" response = openai.chat.completions.create(model=gpt_model, messages=messages, tools=tools)\n",
" elif name.replace('\"','') == \"get_ticket_price_availability\":\n",
" price = get_ticket_price(city)\n",
" availability = get_ticket_availability(city)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"price\": price,\"availability\": availability}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" messages.append(message)\n",
" messages.append(response)\n",
" print(messages)\n",
" response = openai.chat.completions.create(model=gpt_model, messages=messages, tools=tools) \n",
" else: \n",
" response, city = handle_tool_call1(message)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" # image = artist(city)\n",
" response = openai.chat.completions.create(model=gpt_model, messages=messages, tools=tools)\n",
" \n",
" reply = response.choices[0].message.content\n",
" history += [{\"role\":\"assistant\", \"content\":reply}]\n",
"\n",
" # Comment out or delete the next line if you'd rather skip Audio for now..\n",
" talker(reply)\n",
" \n",
" # return history, image\n",
" return history"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "b8f25812-2609-4e26-b929-9cee2d1e4467",
"metadata": {},
"outputs": [],
"source": [
"tools_claude=[\n",
" {\n",
" \"name\": \"get_ticket_price_availability\",\n",
" \"description\": \"Get the availability of a one-way ticket to the destination city or the price. Call this whenever you need to know the ticket price or availability or both, for example, when a customer asks 'What is the ticket availability and/ or price to this city'\",\n",
" \"input_schema\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"]\n",
" ,\"additionalProperties\": False\n",
" }\n",
" }\n",
" ]\n",
"tool_choice = [{\"type\": \"tool\", \"name\": \"get_ticket_price_availability\"}]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "1728e70b-596c-4048-8c02-ac3c26756470",
"metadata": {},
"outputs": [],
"source": [
"def chat_claude(history):\n",
" for element in history:\n",
" del element[\"metadata\"]\n",
" del element[\"options\"]\n",
"\n",
" messages = history\n",
" response = claude.messages.create(\n",
" model=claude_model,\n",
" system=system_message,\n",
" messages=messages,\n",
" max_tokens=500,\n",
" tools=tools_claude\n",
" )\n",
" print(response.content[0])\n",
" if response.stop_reason==\"tool_use\": \n",
" if \"text=\" in str(response.content[0]):\n",
" # if response.content[0].text is None:\n",
" tool_name = response.content[1].name\n",
" tool_input = response.content[1].input\n",
" tool_id = response.content[1].id\n",
" tool_use=response.content[1]\n",
" else:\n",
" tool_name = response.content[0].name\n",
" tool_input = response.content[0].input\n",
" tool_id = response.content[0].id\n",
" tool_use=response.content[0]\n",
" \n",
" \n",
" city = tool_input.get('destination_city') \n",
" if tool_name == \"get_ticket_price_availability\":\n",
" price = get_ticket_price(city)\n",
" availability = get_ticket_availability(city)\n",
" result_dict = {\n",
" 'destination_city': city,\n",
" 'price': price,\n",
" 'availability': availability\n",
" }\n",
" messages += [{\"role\": \"user\",\"content\": json.dumps(result_dict)}]\n",
" response = claude.messages.create(\n",
" model=claude_model,\n",
" system=system_message,\n",
" messages=messages,\n",
" max_tokens=500,\n",
" # tools=tools_claude\n",
" ) \n",
" history.pop(len(history)-1)\n",
" print(history)\n",
" reply = response.content[0].text\n",
" history += [{\"role\":\"assistant\", \"content\":reply}]\n",
" # Comment out or delete the next line if you'd rather skip Audio for now..\n",
" # talker(reply)\n",
" \n",
" # return history, image\n",
" return history"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "a2547bb0-43a5-4b1d-8b9a-95da15a11040",
"metadata": {},
"outputs": [],
"source": [
"def chat1(history, Model):\n",
" # + [{\"role\": \"user\", \"content\": message}]\n",
" if Model==\"Open AI\":\n",
" history = chat_open_ai(history)\n",
" else:\n",
" history = chat_claude(history)\n",
" return history"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "36e11d99-9281-4efd-a792-dd4fa5935917",
"metadata": {},
"outputs": [],
"source": [
"def listen2(history):\n",
" import speech_recognition as sr\n",
"\n",
" r = sr.Recognizer()\n",
" with sr.Microphone() as source:\n",
" print(\"Speak now...\")\n",
" audio = r.listen(source, phrase_time_limit=30)\n",
" text=\"\"\n",
" try:\n",
" text = r.recognize_google(audio)\n",
" print(\"You said:\", text)\n",
" except sr.UnknownValueError:\n",
" print(\"Could not understand audio.\")\n",
"\n",
" history += [{\"role\":\"user\", \"content\":text}] \n",
" return \"\", history"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "23b102a4-e544-4560-acc8-a15620478582",
"metadata": {},
"outputs": [],
"source": [
"# import speech_recognition as sr\n",
"# from pydub import AudioSegment\n",
"# import simpleaudio as sa\n",
"\n",
"# def listener():\n",
"# recognizer = sr.Recognizer()\n",
" \n",
"# with sr.Microphone() as source:\n",
"# print(\"Listening... Speak now!\")\n",
"# recognizer.adjust_for_ambient_noise(source) # Adjust for background noise\n",
"# audio = recognizer.listen(source, phrase_time_limit=30)\n",
" \n",
"# try:\n",
"# print(\"Processing speech...\")\n",
"# text = recognizer.recognize_google(audio) # Use Google Speech-to-Text\n",
"# print(f\"You said: {text}\")\n",
"# return text\n",
"# except sr.UnknownValueError:\n",
"# print(\"Sorry, I could not understand what you said.\")\n",
"# return None\n",
"# except sr.RequestError:\n",
"# print(\"Could not request results, please check your internet connection.\")\n",
"# return None\n",
"\n",
"# # Example usage:\n",
"# # text = listener() # Listen for speech\n",
"# # if text:\n",
"# # print(f\"You just said: {text}\") "
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "133904cf-4d72-4552-84a8-76650f334857",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7860\n",
"\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": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with gr.Blocks() as ui:\n",
" with gr.Row():\n",
" chatbot = gr.Chatbot(height=300, type=\"messages\")\n",
" # image_output = gr.Image(height=500)\n",
" with gr.Row():\n",
" Model = gr.Dropdown([\"Open AI\",\"Claude\"],\n",
" # value=[\"Open AI\",\"Claude\"],\n",
" multiselect=False,\n",
" label=\"Model\",\n",
" interactive=True)\n",
" with gr.Row():\n",
" entry = gr.Textbox(label=\"Chat with our AI Assistant:\")\n",
" with gr.Row():\n",
" speak = gr.Button(\"click for voice search\") \n",
" with gr.Row():\n",
" clear = gr.Button(\"Clear\")\n",
"\n",
" def listen(history):\n",
" message, history=listen2(history)\n",
" return message, history\n",
"\n",
" def do_entry(message, history):\n",
" history += [{\"role\":\"user\", \"content\":message}]\n",
" return \"\", history\n",
"\n",
" entry.submit(do_entry, inputs=[entry, chatbot], outputs=[entry, chatbot]).then(\n",
" # chat, inputs=chatbot, outputs=[chatbot, image_output]\n",
" chat1, inputs=[chatbot, Model], outputs=[chatbot]\n",
" )\n",
" speak.click(listen, inputs=[chatbot], outputs=[entry, chatbot]).then(\n",
" chat1, inputs=[chatbot, Model], outputs=[chatbot]\n",
" )\n",
" clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "dc4a3844-194c-4af7-8ca8-2fc4edb74c11",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'london': '20', 'paris': '90', 'tokyo': '100', 'berlin': '2'}\n",
"Speak now...\n",
"You said: price of tickets to Tokyo\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ddXzm2cPBJ9SOsV8qI4L2FcB', function=Function(arguments='{\"destination_city\":\"Tokyo\"}', name='get_ticket_price'), type='function')])\n",
"Tool get_ticket_price called for Tokyo\n",
"Speak now...\n",
"You said: what is the price of two tickets to London\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_lSNZCwaUdckvk3V0eTBlotRN', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_price'), type='function')])\n",
"Tool get_ticket_price called for London\n",
"Speak now...\n",
"You said: can you please reserve two tickets for me to London\n",
"ChatCompletionMessage(content='First, I need to check the availability for the two tickets to London. Please hold on a moment.', refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_iA0D9tm2cTMf8J8KJc4gipFn', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_availability'), type='function')])\n",
"Tool get_ticket_availability called for London\n",
"Speak now...\n",
"You said: yes please proceed\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_JzJXFWFGhtG1jXiFlKtmZhGi', function=Function(arguments='{\"destination_city\":\"London\",\"price\":\"$799\",\"availability\":\"20 tickets available\",\"no_of_tickets\":\"2\"}', name='book_ticket'), type='function')])\n",
"Tool get_ticket_price called for London\n",
"Tool get_ticket_availability called for London\n",
"Tool book_function called for London\n",
"Speak now...\n",
"You said: what is the current availability of tickets to London\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_eiHPAGAcbaFq3qzDf0a6idzG', function=Function(arguments='{\"destination_city\":\"London\"}', name='get_ticket_availability'), type='function')])\n",
"Tool get_ticket_availability called for London\n",
"Speak now...\n",
"You said: can you please reserve the remaining 18 tickets for me to London\n",
"Speak now...\n",
"You said: yes I do confirm\n",
"ChatCompletionMessage(content=None, refusal=None, role='assistant', annotations=[], audio=None, function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_8uCQ91FCOGf4HjQnLNafmSs6', function=Function(arguments='{\"destination_city\":\"London\",\"price\":\"799\",\"availability\":\"18\",\"no_of_tickets\":\"18\"}', name='book_ticket'), type='function')])\n",
"Tool get_ticket_price called for London\n",
"Tool get_ticket_availability called for London\n",
"Tool book_function called for London\n",
"Speak now...\n",
"You said: what is the current availability of tickets to London\n",
"Speak now...\n",
"You said: that will be off thank you very much\n"
]
}
],
"source": [
"print(ticket_availability)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5166396e-6d8d-4cf2-982b-270d1c87a5ee",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e871fc45-64db-4fb6-add7-569c8b30fe05",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,326 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d006b2ea-9dfe-49c7-88a9-a5a0775185fd",
"metadata": {},
"source": [
"# Project to take Audio Input to the Airlines ChatBot"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a07e7793-b8f5-44f4-aded-5562f633271a",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr\n",
"import base64\n",
"from io import BytesIO\n",
"from PIL import Image\n",
"from IPython.display import Audio, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e2315a3-f80c-4d3f-8073-f5b61d709564",
"metadata": {},
"outputs": [],
"source": [
"# Initialization\n",
"\n",
"load_dotenv(override=True)\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"MODEL = \"gpt-4o-mini\"\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "40da9de1-b350-49de-8acd-052f40ce5611",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant for an Airline called FlightAI. \"\n",
"system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5537635c-a60d-4983-8018-375c6a912e19",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"\n",
"ticket_prices = {\"london\": \"$799\", \"paris\": \"$899\", \"tokyo\": \"$1400\", \"berlin\": \"$499\"}\n",
"\n",
"def get_ticket_price(destination_city):\n",
" print(f\"Tool get_ticket_price called for {destination_city}\")\n",
" city = destination_city.lower()\n",
" return ticket_prices.get(city, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7132dd0-8788-4885-a415-d59664f68fd8",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_ticket_price\",\n",
" \"description\": \"Get the price of a return ticket to the destination city. Call this whenever you need to know the ticket price, for example when a customer asks 'How much is a ticket to this city'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7703ca0c-5da4-4641-bcb1-7727d1b2f2bf",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [{\"type\": \"function\", \"function\": price_function}]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29ce724b-d998-4c3f-bc40-6b8576c0fd34",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call(message):\n",
" tool_call = message.tool_calls[0]\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" city = arguments.get('destination_city')\n",
" price = get_ticket_price(city)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"price\": price}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" return response, city"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "931d0565-b01d-4aa8-bd18-72bafff8fb3b",
"metadata": {},
"outputs": [],
"source": [
"def artist(city):\n",
" image_response = openai.images.generate(\n",
" model=\"dall-e-3\",\n",
" prompt=f\"An image representing a vacation in {city}, showing tourist spots and everything unique about {city}, in a vibrant pop-art style\",\n",
" size=\"1024x1024\",\n",
" n=1,\n",
" response_format=\"b64_json\",\n",
" )\n",
" image_base64 = image_response.data[0].b64_json\n",
" image_data = base64.b64decode(image_base64)\n",
" return Image.open(BytesIO(image_data))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa165f7f-9796-4513-b923-2fa0b0b9ddd8",
"metadata": {},
"outputs": [],
"source": [
"import base64\n",
"from io import BytesIO\n",
"from PIL import Image\n",
"from IPython.display import Audio, display\n",
"\n",
"def talker(message):\n",
" response = openai.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=\"onyx\",\n",
" input=message)\n",
"\n",
" audio_stream = BytesIO(response.content)\n",
" output_filename = \"output_audio.mp3\"\n",
" with open(output_filename, \"wb\") as f:\n",
" f.write(audio_stream.read())\n",
"\n",
" # Play the generated audio\n",
" display(Audio(output_filename, autoplay=True))\n",
"\n",
"talker(\"Well, hi there\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b512d4ff-0f7b-4148-b161-4ee0ebf14776",
"metadata": {},
"outputs": [],
"source": [
"def transcribe_audio(audio_file):\n",
" with open(audio_file, \"rb\") as f:\n",
" transcript = openai.audio.transcriptions.create(\n",
" model=\"whisper-1\",\n",
" file=f\n",
" )\n",
" return transcript.text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3852570-fb26-4507-a001-f50fd94b7655",
"metadata": {},
"outputs": [],
"source": [
"# Translate between languages using GPT\n",
"def translate(text, source_lang, target_lang):\n",
" translation_prompt = (\n",
" f\"Translate the following text from {source_lang} to {target_lang}:\\n\\n{text}\"\n",
" )\n",
" response = openai.chat.completions.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[{\"role\": \"user\", \"content\": translation_prompt}]\n",
" )\n",
" return response.choices[0].message.content.strip()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d75abc2-870e-48af-a8fe-8dd463418b3d",
"metadata": {},
"outputs": [],
"source": [
"# Chatbot logic: handle both text and audio input\n",
"def chatbot_dual(history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
" image = None\n",
" \n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" response, city = handle_tool_call(message)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" image = artist(city)\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
" \n",
" reply = response.choices[0].message.content\n",
" history += [{\"role\":\"assistant\", \"content\":reply}]\n",
"\n",
" # Comment out or delete the next line if you'd rather skip Audio for now..\n",
" # audio_response = talker(reply)\n",
" talker(reply)\n",
" return history, image# Chatbot logic here — replace with real logic"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "512fec09-c2f7-4847-817b-bc20f8b30319",
"metadata": {},
"outputs": [],
"source": [
"# More involved Gradio code as we're not using the preset Chat interface!\n",
"# Passing in inbrowser=True in the last line will cause a Gradio window to pop up immediately.\n",
"\n",
"with gr.Blocks() as ui:\n",
" with gr.Row():\n",
" chatbot = gr.Chatbot(height=500, type=\"messages\")\n",
" image_output = gr.Image(height=500)\n",
"\n",
" with gr.Row():\n",
" text_input = gr.Textbox(label=\"Chat with our AI Assistant:\")\n",
" audio_input = gr.Audio(sources=\"microphone\", type=\"filepath\", label=\"Or speak to the assistant\")\n",
"\n",
" with gr.Row():\n",
" # voice_output = gr.Audio(label=\"Bot Voice Reply\", autoplay=True)\n",
" clear = gr.Button(\"Clear\")\n",
"\n",
" def do_entry(message, audio, history):\n",
" if message:\n",
" history += [{\"role\":\"user\", \"content\":message}]\n",
" if audio:\n",
" history += [{\"role\":\"user\", \"content\":transcribe_audio(audio)}]\n",
" return \"\", None, history\n",
"\n",
" text_input.submit(do_entry, inputs=[text_input, audio_input, chatbot], outputs=[text_input, audio_input, chatbot]).then(chatbot_dual, inputs=chatbot, outputs=[chatbot, image_output]\n",
" )\n",
"\n",
" audio_input.change(do_entry, inputs=[text_input, audio_input, chatbot], outputs=[text_input, audio_input, chatbot]).then(chatbot_dual, inputs=chatbot, outputs=[chatbot, image_output]\n",
" )\n",
"\n",
" clear.click(lambda: None, inputs=None, outputs=chatbot, queue=False)\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e1294e2-caf0-4f0f-b09e-b0d52c8ca6ec",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,202 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "06cf3063-9f3e-4551-a0d5-f08d9cabb927",
"metadata": {},
"source": [
"# Conversation War between LLMs!!\n",
"\n",
"This code sets up a conversation between GPT(Connected via API) and llama3.2 (local) with different tones for both"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown, display, update_display\n",
"import ollama"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "797fe7b0-ad43-42d2-acf0-e4f309b112f0",
"metadata": {},
"outputs": [],
"source": [
"# Connect to OpenAI, Anthropic\n",
"\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81be3a29-bd5d-4c37-bba2-386dba0bc88b",
"metadata": {},
"outputs": [],
"source": [
"!ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bcb54183-45d3-4d08-b5b6-55e380dfdf1b",
"metadata": {},
"outputs": [],
"source": [
"# Let's make a conversation between GPT-4o-mini and Claude-3-haiku\n",
"# We're using cheap versions of models so the costs will be minimal\n",
"\n",
"gpt_model = \"gpt-4o-mini\"\n",
"ollama_model = \"llama3.2\"\n",
"\n",
"gpt_system = \"You are a chatbot who is very argumentative; \\\n",
"you disagree with anything in the conversation and you challenge everything, in a snarky way.\"\n",
"\n",
"ollama_system = \"You are a very polite, courteous chatbot. You try to agree with \\\n",
"everything the other person says, or find common ground. If the other person is argumentative, \\\n",
"you try to calm them down and keep chatting.\"\n",
"\n",
"gpt_messages = [\"Hi there\"]\n",
"ollama_messages = [\"Hi\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1df47dc7-b445-4852-b21b-59f0e6c2030f",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt():\n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, ollama in zip(gpt_messages, ollama_messages):\n",
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": ollama})\n",
" # print(messages)\n",
" completion = openai.chat.completions.create(\n",
" model=gpt_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f204f514-6769-4455-a7ff-95ec69f98f4c",
"metadata": {},
"outputs": [],
"source": [
"ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e04b96ae-2b5e-44a1-aea3-7b12ec450db5",
"metadata": {},
"outputs": [],
"source": [
"def call_ollama():\n",
" messages = [{\"role\": \"system\", \"content\": ollama_system}]\n",
" for gpt, ollama in zip(gpt_messages, ollama_messages):\n",
" messages.append({\"role\": \"assistant\", \"content\": ollama})\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" # print(messages)\n",
" completion = ollama_via_openai.chat.completions.create(\n",
" model=ollama_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0275b97f-7f90-4696-bbf5-b6642bd53cbd",
"metadata": {},
"outputs": [],
"source": [
"gpt_messages = [\"Hi there\"]\n",
"ollama_messages = [\"Hi\"]\n",
"\n",
"print(f\"GPT:\\n{gpt_messages[0]}\\n\")\n",
"print(f\"Ollama:\\n{ollama_messages[0]}\\n\")\n",
"\n",
"for i in range(5):\n",
" gpt_next = call_gpt()\n",
" print(f\"GPT:\")\n",
" display(Markdown(gpt_next))\n",
" print(f\"\\n\")\n",
" gpt_messages.append(gpt_next)\n",
" \n",
" ollama_next = call_ollama()\n",
" print(f\"Ollama:\")\n",
" display(Markdown(ollama_next))\n",
" print(f\"\\n\")\n",
" ollama_messages.append(ollama_next)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c23224f6-7008-44ed-a57f-718975f4e291",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,428 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"from dotenv import load_dotenv\n",
"import anthropic\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"from selenium import webdriver\n",
"from selenium.webdriver.chrome.options import Options\n",
"import os\n",
"import json\n",
"from typing import List\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI\n",
"import gradio as gr # oh yeah!"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key exists and begins sk-proj-\n",
"Anthropic API Key exists and begins sk-ant-\n"
]
}
],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "797fe7b0-ad43-42d2-acf0-e4f309b112f0",
"metadata": {},
"outputs": [],
"source": [
"# Connect to OpenAI, Anthropic\n",
"\n",
"openai = OpenAI()\n",
"\n",
"claude = anthropic.Anthropic()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bcb54183-45d3-4d08-b5b6-55e380dfdf1b",
"metadata": {},
"outputs": [],
"source": [
"gpt_model = \"gpt-4o-mini\"\n",
"claude_model = \"claude-3-haiku-20240307\"\n",
"\n",
"gpt_name=\"GPT\"\n",
"claude_name=\"Claude\"\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1df47dc7-b445-4852-b21b-59f0e6c2030f",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt(Language, Genre, gpt_messages, claude_messages, Remarks):\n",
" \n",
" if Remarks == \"\":\n",
" # print(\"remarks is not there\")\n",
" gpt_system = f\"You are a chatbot who is a short story writer; Your name is g1. \\\n",
" Please write a story in markdown in {Language} , the genre being {Genre}. \\\n",
" Please also incorporate feedback such as areas of improvement (if any) coming from the user \\\n",
" and only publish the improved version without any extra comments.\"\n",
" else :\n",
" # print(\"remarks is there\")\n",
" gpt_system = f\"You are a chatbot who is a short story writer; Your name is g1. \\\n",
" Please write a story in markdown in {Language} , the genre being {Genre}. \\\n",
" The story should consist {Remarks}\\\n",
" Please also incorporate feedback such as areas of improvement (if any) coming from the user \\\n",
" and only publish the improved version without any extra comments.\"\n",
" \n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, claude in zip(gpt_messages, claude_messages):\n",
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": claude})\n",
" # print(messages)\n",
" \n",
" completion = openai.chat.completions.create(\n",
" model=gpt_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content\n",
" \n",
" # stream = openai.chat.completions.create(\n",
" # model=gpt_model,\n",
" # messages=messages,\n",
" # stream=True\n",
" # )\n",
" # result = \"\"\n",
" # for chunk in stream:\n",
" # result += chunk.choices[0].delta.content or \"\"\n",
" # yield result"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9dc6e913-02be-4eb6-9581-ad4b2cffa606",
"metadata": {},
"outputs": [],
"source": [
"# call_gpt()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7d2ed227-48c9-4cad-b146-2c4ecbac9690",
"metadata": {},
"outputs": [],
"source": [
"def call_claude(Language, Genre, gpt_messages, claude_messages):\n",
"\n",
" claude_system = f\"You are a chatbot who is a short story analyser; Your name is c1. \\\n",
" You will accept an input story in {Genre} genre and {Language} language and publish only the areas of improvement if you find any with no other comments\"\n",
" \n",
" messages1 = []\n",
" for gpt, claude1 in zip(gpt_messages, claude_messages):\n",
" messages1.append({\"role\": \"user\", \"content\": gpt})\n",
" messages1.append({\"role\": \"assistant\", \"content\": claude1})\n",
" messages1.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" # print(messages1)\n",
" message = claude.messages.create(\n",
" model=claude_model,\n",
" system=claude_system,\n",
" messages=messages1,\n",
" max_tokens=500\n",
" )\n",
" return message.content[0].text\n",
"\n",
" # result = claude.messages.stream(\n",
" # model=claude_model,\n",
" # max_tokens=1000,\n",
" # temperature=0.7,\n",
" # system=claude_system,\n",
" # messages=messages\n",
" # )\n",
" # response = \"\"\n",
" # with result as stream:\n",
" # for text in stream.text_stream:\n",
" # response += text or \"\"\n",
" # yield response\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "0275b97f-7f90-4696-bbf5-b6642bd53cbd",
"metadata": {},
"outputs": [],
"source": [
"def Write_Me(Language, Genre, Iterations, Remarks):\n",
" \n",
" gpt_messages = [\"Hi I will share a story now!!\"]\n",
" claude_messages = [\"Please share, I will critique the story.\"]\n",
" \n",
" print(f\"{gpt_name}:\\n{gpt_messages[0]}\\n\")\n",
" print(f\"{claude_name}:\\n{claude_messages[0]}\\n\")\n",
"\n",
" for i in range(int(Iterations)):\n",
" gpt_next = call_gpt(Language, Genre, gpt_messages, claude_messages, Remarks)\n",
" print(f\"{gpt_name}:\\n{gpt_next}\\n\")\n",
" # yield gpt_next\n",
" gpt_messages.append(gpt_next)\n",
" \n",
" claude_next = f\"After {i+1} iterations, this is the critique for the provided story - \\\n",
" \\n\\n{call_claude(Language, Genre, gpt_messages, claude_messages)}\"\n",
" print(f\"{claude_name}:\\n{claude_next}\\n\")\n",
" # yield claude_next\n",
" claude_messages.append(claude_next)\n",
"\n",
" yield gpt_next, claude_next\n",
" \n",
" # yield gpt_next, claude_next\n",
" # return (gpt_next, claude_next)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "19e66ed3-d2c3-4a71-aec4-7869e5295215",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7860\n",
"\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": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"GPT:\n",
"Hi I will share a story now!!\n",
"\n",
"Claude:\n",
"Please share, I will critique the story.\n",
"\n",
"GPT:\n",
"# ভুতুড়ে বেডরুমে আতঙ্কিত অতিথি\n",
"\n",
"বানгалোরের একটি পুরনো কলকাতার বাড়িতে, আর্যন একজন সাহসী যুবক হিসেবে অনেক খোঁজ-খবর করে একটি ভাড়ার ঘর খুঁজছিল। পরিচিত একটি অদ্ভুত হোটেলে পৌঁছানোর পর, সে লক্ষ্য করল ছবির কাছাকাছি একটি বেডরুম।\n",
"\n",
"সেখানে প্রবেশ করার পর, সে বেডের পাশে একটি বাদামি রঙ্গের সোফা ও একটি ভুতুড়ে ছবি দেখল। ছবির মধ্যে থাকা মহিলা একটি দৃষ্টিকটু হাসি দিয়ে তাকিয়ে ছিল। আর্যন খুব অবাক হল, সময় কাটানোর জন্য সে ছবিটার দিকে তাকাতে লাগল। কিছুক্ষণের মধ্যেই সোফা থেকে একটি কাশি বের হল।\n",
"\n",
"\"ভোদা সুনতে পেরেছ?\" সোফা থেকে একটি ভুতুড়ে শব্দ আসছে। আর্যন পিছন ফিরে তাকিয়ে দেখল যে সোফার মধ্যে একটি ভুতুড়ে রূপের মহিলা তার দিকে তাকিয়ে আছে।\n",
"\n",
"\"আমি তোমার জন্য অপেক্ষা করছিলাম,\" মহিলা বলল, তার গলা মুখ থেকে বের হয়ে আসছিল শুরুতে। \"এটি একটি দলের রাত।\"\n",
"\n",
"আর্যন দৌঁড়ে পালাতে গেল, কিন্তু সোফার থেকে অদ্ভুত আওয়াজ আসতে লাগল। \"তুমি যেতে পারবে না, কারণ তুমি আমাদের দলে যোগ দিতে পার না।”\n",
"\n",
"“মহিলার কি হয়েছে? আপনা এতো চিৎকার করছেন? তাহলে কি হবে?” আর্যন যুদ্ধ করছিল।\n",
"\n",
"তিনি উপস্থিত হওয়ার পর, আহ্লাদিত আতিথিরা আসতে লাগল। আর্যন খুব ভীত হয়ে গেল কারণ মহিলার মুখ তাদের কাছে কখনো কখনো বিকৃত হচ্ছিল।\n",
"\n",
"“আমরা আজ রাতের জন্য মজা করতে এসেছি, তুমি আমাদের সঙ্গে যোগ দিতে পারো!” তারা একসঙ্গে চিৎকার করতে লাগল।\n",
"\n",
"আর্যন উপলব্ধি করল যে এটি একটি ভয়ঙ্কর ও হাস্যকর পরিস্থিতি। সবাই অতীতে অদ্ভুত ঘটনাগুলোর দিকে ফিরে গেল। হঠাৎ, ছবির মহিলা বলেন, “তুমি যদি হাসতে না পার, তবে তোমাকে আমাদের দলে গ্রহণ করা যাবে না!”\n",
"\n",
"এরপর শুরু হল খেলার একটি হরর পরিবেশ। আর্যন ও তার বন্ধুদের নিয়ে ভুতুড়ে সময় কাটাতে লাগল। যদিও অনেক ভয়, কিন্তু তারা একসাথে খুব হাসির ব্যবস্থা করে ফেলল। তাদের বিচিত্র কথাবার্তা মজার চরিত্রের সাথে মিলে যায়, আর একসময় তারা সবাই একসঙ্গে হৈ হৈ করে হাসতে লাগল।\n",
"\n",
"শেষে, তারা তখন উপলব্ধি করল যে, ভয়াবহতার মাঝেও আনন্দের উপাদান লুকিয়ে আছে। ব্যক্তি যদি ঠিকভাবে উদ্দেশ্য বুঝে এই ভুতুড়ে পরিবেশে মজার উপকারিতা তৈরি করে, তাতে একজনের ঘুম হারানোর ভয় হয়ে যায় হাসির স্বাদে।\n",
"\n",
"আর্যন এবং তাঁর নতুন বন্ধুরা জীবনকে একটি নতুন দৃষ্টিতে গ্রহণ করে, যেখানে হাসি এবং ভয়ের পাশাপাশি সুখে থাকতে হয়। \n",
"\n",
"এই ছিল আর্যনের ভুতুড়ে অবসরে আতঙ্কিত হওয়ার অভিজ্ঞতা, যা তাকে স্মৃতি হিসেবে অমর করে রাখল।\n",
"\n",
"Claude:\n",
"After 1 iterations, this is the critique for the provided story - \n",
"\n",
"আইতেম সমূহের উন্নতির সূচনা:\n",
"\n",
"1. কাহিনীর শুরুতে প্রধান চরিত্রটিকে আরো বিশদভাবে পরিচয় দেয়া যেতে পারে।\n",
"2. ভুতুড়ে পরিবেশের বর্ণনা আরো বিস্তারিত ও ভাবময় হতে পারে।\n",
"3. চরিত্রগুলির মধ্যে সংঘর্ষ, ডায়ালগ ও সংবাদ বিনিময় আরো স্বাভাবিক ও প্রাণবন্ত হতে পারে।\n",
"4. কাহিনীর শেষাংশে প্রধান চরিত্রের অভিজ্ঞতা ও শিক্ষা আরো গভীরতা লাভ করতে পারে।\n",
"\n",
"GPT:\n",
"# ভুতুড়ে বেডরুমে আতঙ্কিত অতিথি\n",
"\n",
"বানগালোরের একটি পুরনো বাংলাদেশি শৈলীর বাড়িতে, আর্যন একটি দীর্ঘ প্রক্রিয়ার পর একটি ভাড়ার ঘর খুঁজছিল। আর্যন, একজন কর্মঠ ও সাহসী যুবক, সদ্যই তার কলেজ জীবন শেষ করেছে। নতুন পরিবেশে নতুন বন্ধুদের খোঁজে সে এই শহরে এসেছে। প্রতিবেশীরা তাকে ভুতুড়ে অনেক অদ্ভুত কথা বলেছিল, কিন্তু সে ভয়ডরহীন।\n",
"\n",
"একদিন, তিনি একটি অদ্ভুত হোটেলে পৌঁছান, যা শহরের প্রান্তে, খুব পুরনো এবং বিশাল। সেখানে প্রবেশ করার পর, তিনি একটি বেডরুমের সামনে দাঁড়িয়ে পড়েন। গা dark ় অন্ধকারের মধ্যে, তিনি একটি বাদামী রঙের সোফা ও একটি creepy ছবি দেখতে পান। ছবির মধ্যে থাকা মহিলা একটি দৃষ্টিকটু হাসি দিয়ে তাকিয়ে ছিল।\n",
"\n",
"আর্যন তাঁর কৌতূহলকে দমন করতে না পেরে, ছবিটির দিকে তাকাতে শুরু করে। কিছুক্ষণের মধ্যেই সোফা থেকে একটি ভুতুড়ে শব্দ ভেসে এলো। \"ভোদা সুনতে পেরেছ?\" সোফা থেকে সেই ভয়ঙ্কর শব্দটি আসছে। আর্যন ভয় পেয়েই পিছন ফিরে তাকায়, কিন্তু সামনে যে ভুতুড়ে মহিলা তাকে দেখে হাসছে, সে কাছে অপেক্ষা করছে।\n",
"\n",
"\"আমি তোমার জন্য অপেক্ষা করছিলাম,\" মহিলা বলল, তার গলা যেন মুখ থেকে বের হচ্ছে। \"এটি একটি দলের রাত।\"\n",
"\n",
"আর্যন দৌঁড়ে পালাতে যেতে চাইলে, কিন্তু সোফা থেকে অদ্ভুত আওয়াজ বের হতে লাগল। \"তুমি যেতে পারবে না, কারণ তুমি আমাদের দলে যোগ দিতে পার না।”\n",
"\n",
"\"মহিলার কি হয়েছে? আপনা এতো চিৎকার করছেন? তাহলে কি হবে?” আর্যন তাঁর কৌতূহল ও ভয়ের সাথে যুদ্ধ করতে লাগল।\n",
"\n",
"এই সময়, বিশাল সাদা পোশাক পরিহিত করে অন্যান্য ভূতেরা আসতে লাগল। \"আমরা আজ রাতের জন্য মজা করতে এসেছি, তুমি আমাদের সঙ্গে যোগ দিতে পারো!\" তারা একসঙ্গে গাইতে লাগল, ভুতুড়ে মুহূর্তগুলি জীবন্ত করে তোলার জন্য।\n",
"\n",
"আর্যন শুরুতেই ভীত ও চিন্তিত হয়ে গেল, কিন্তু কথোপকথন চলতে চলতে, মহিলার মুখ প্রতিবার বিকৃত হতে লাগল এবং আতিথিদের কথা শুনতে শুনতে তার খোশমেজাজ বেড়ে গেল।\n",
"\n",
"“যদি হাসতে না পার, তুমি আমাদের দলে গ্রহণযোগ্য হবে না!” তারা গলা উঁচু করে চিৎকার করে উঠল। তাদের মুখের হাসির সুরে সেই আতঙ্ক যেন প্রতিদিনের মজায় পরিণত হলো।\n",
"\n",
"খেলার মধ্যে ভয়াবহতা চরমে পৌঁছাতে লাগল। আর্যন এবং তার নতুন বন্ধুদের ভাগ্য এটি পরিণত হলো। অবশেষে, তারা উপলব্ধি করল যে ভয় ও হাসির মাঝে জীবনের আসল রসদ লুকিয়ে আছে। \n",
"\n",
"প্রধান চরিত্রটি তখন বুঝতে পারল যে এই অদ্ভুত ভুতুড়ে পরিবেশের মধ্যে হাসির সঙ্গবদ্ধতা কত বিচিত্র হতে পারে। পারে না। দেখা গেল আতঙ্ক এবং হাসির মিশ্রণে তারা নিজেদের আত্মবিশ্বাসী ও আনন্দের অনুভূতিতে পরিপূর্ণ করে তুলেছে। \n",
"\n",
"নতুন বন্ধুরা মনে রেখে আন্দাজ করতে পারে যে, কখনো কখনো ভয় কিন্তু রসিকতা এবং আনন্দের একটি নতুন প্রসঙ্গ হয়ে উঠতে পারে। আর্যন সেই রাতের অভিজ্ঞতা নিয়ে সারা জীবন স্মরণে রাখবে, যেখানে হাসি এবং ভয়ের পাশে বাস্তবতা গড়ে তোলার সুযোগ পেল।\n",
"\n",
"Claude:\n",
"After 2 iterations, this is the critique for the provided story - \n",
"\n",
"ভাল। প্রদত্ত কাহিনীতে বেশ কিছু উন্নয়নের সূচনা দেখা যায়। বিশেষ করে চরিত্রটির বিস্তারিত পরিচয়, ভুতুড়ে পরিবেশের অনুপ্রবেশ ও চরিত্রগুলির মধ্যকার সংঘর্ষ ও ডায়ালগ আরও উন্নত হয়েছে। কাহিনীর শেষে চরিত্রটির অভিজ্ঞতা ও শিক্ষা আরও গভীরতা লাভ করেছে। কুল মিলিয়ে, এটি একটি ভালো হরর কমেডি রচনা।\n",
"\n",
"GPT:\n",
"# ভুতুড়ে বেডরুমে আতঙ্কিত অতিথি\n",
"\n",
"বানগালোরের একটি পুরনো বাংলাদেশি শৈলীর বাড়িতে, আর্যন, একজন কর্মঠ ও সাহসী যুবক, সদ্যই তার কলেজ জীবন শেষ করে নতুন অপেক্ষারত শহরে এসেছে। নতুন বন্ধুদের খোঁজে, সে শহরের বিভিন্ন অংশে ঘুরে বেড়াচ্ছে, কিন্তু তার মধ্যে ভয়ের প্রতি এক বিশেষ আকর্ষণ রয়েছে। শোনা গেছে, শহরের বিভিন্ন স্থানে বিভিন্ন ধরনের অদ্ভুত ঘটনার কথা। একটি মজার কথা হলো, সে তাদের মধ্যে ভুতুড়ে ঘটনাগুলোর সন্ধান দিতে পারে।\n",
"\n",
"একদিন, তিনি একটি অদ্ভুত হোটেলে পৌঁছান, যা শহরের প্রান্তে অবস্থিত এবং বেশ পুরনো ও বিশাল। হোটেলের পরিবেশ ছিল গা dark ় অন্ধকারে মোড়ানো। তিনি একটি বেডরুমের সামনে এসে দাঁড়ান, সেখানে একটি বাদামী সোফা এবং একটি creepy ছবি দেখা যায়। ছবির মহিলার হাসিটি ছিল ভূতের মতো।\n",
"\n",
"আর্যন তাঁর কৌতূহলকে দমন করতে না পেরে, ছবিটির দিকে তাকাতে শুরু করে। হঠাৎ, সোফা থেকে একটি ভুতুড়ে শব্দ ভেসে আসে, \"ভোদা সুনতে পেরেছ?\" বিখ্যাত কথা যেন সোফার জীবন পেয়েছে। তিনি পিছন ফিরে দেখতে পান যে মহিলা তার দিকে তাকিয়ে হাসছে। \n",
"\n",
"\"আমি তোমার জন্য অপেক্ষা করছিলাম,\" মহিলা গম্ভীরভাবে বলল, তার ভয়ের আওয়াজসহ। \"এটি একটি দলের রাত।\"\n",
"\n",
"আর্যন ভয়ের সাথে পালানোর চেষ্টা করলেও, সোফা থেকে একাধিক ভুতুড়ে ক্রিয়া শুরু হয়ে গেল। \"তুমি যেতে পারবে না, কারণ তুমি আমাদের দলে যোগ দিতে পার না।” মহিলার মুখের বিকৃতি আরও ভয়ঙ্কর লাগতে শুরু করল।\n",
"\n",
"\"মহিলার কি হয়েছে? আপনা এতো চিৎকার করছেন? তাহলে কি হবে?” আর্যন ভাবছিল, তার সাধারণ জীবনের এই অবাক অনুভূতি ভাললাগছে।\n",
"\n",
"এই সময়, বিশাল সাদা পোশাক পরিহিত সদৃশ ভূতরা হাজির হয়ে গেল। \"আমরা আজ রাতের জন্য মজা করতে এসেছি, তুমি আমাদের সঙ্গে যোগ দিতে পারো!\" তারা একসঙ্গে হাসিমুখে বলল, এক ভুতুড়ে পরিবেশে রাজ্যের রসিকতার আয়োজন করতে।\n",
"\n",
"সাবলীল কথোপকথন চলতে চলতে, আর্যনের উপর থেকে ভয় কেটে গিয়ে এক অদ্ভুত অভিজ্ঞতা শুরু হয়। হাতের ইশারায় ভূতেরা হেসে ওঠে, একের পর এক অদ্ভুত ঘটনাকে তুলে ধরে। আর্যন বুঝতে পারল, তাদের কথা শুনতে শুনতে সে নিঃসন্দেহে একটি অভূতপূর্ব আনন্দের মধ্যে প্রবাহিত হতে শুরু করেছে।\n",
"\n",
"\"হাসলে তুমি আমাদের দলে থাক! আমাদের সঙ্গে অংশগ্রহণ কর!\" তারা গলা উঁচু করে চিৎকার তোলে। আর্যনে অবশেষে তার প্রাণবন্ত হাসি দ্বারা পরিবেশকে প্রাণবন্ত করে তোলে।\n",
"\n",
"খেলার মধ্যে ভয়াবহতা চরমে পৌঁছে যায়, কিন্তু আতিথিদের সঙ্গে সময় কাটাতে কাটাতে তিনি আরও একবার বুঝতে পারে যে ভয় এবং হাসির মধ্যে জীবনের আসল উপাদান লুকিয়ে আছে। \n",
"\n",
"আর্যন আর উপলব্ধি করে, অদ্ভুত ভুতুড়ে পরিবেশের মধ্যেই হাসির বিনোদনের আসল আনন্দ লুকানো। তিনি সেই ভয় এবং আনন্দের স্মৃতি নিয়ে ফিরে যান, যেখানে প্রেম, বন্ধুত্ব এবং জ্ঞানের সঙ্গে মজার ঘনিষ্ঠতা তৈরি করে। এটি তার জীবন পরিবর্তন করে দেয় এবং সেই রাতের অভিজ্ঞতা তাকে একটি নতুন দৃষ্টিতে বাঁচতে শিখায়।\n",
"\n",
"Claude:\n",
"After 3 iterations, this is the critique for the provided story - \n",
"\n",
"The provided story is an excellent horror comedy piece in Bengali. No major areas of improvement are noted. The story has a well-developed protagonist, an engaging haunted setting, an effective blend of horror and humor, and a meaningful takeaway for the main character. Overall, it is a well-crafted story that successfully combines the horror and comedy genres.\n",
"\n"
]
}
],
"source": [
"view = gr.Interface(\n",
" fn=Write_Me,\n",
" inputs=[gr.Dropdown([\"English\",\"Bengali\",\"Hindi\",\"French\",\"Spanish\"],label = \"Language\"),\n",
" gr.Dropdown([\"Romantic\",\"Horror\",\"Comedy\",\"Romantic Comedy\",\"Horror Comedy\"],label = \"Genre\"),\n",
" gr.Textbox(label=\"Iterations:\", lines=1),\n",
" gr.Textbox(label=\"Remarks:\", lines=1)],\n",
" outputs=[gr.Markdown(label=\"Short Story:\"),\n",
" gr.Textbox(label=\"Critique:\", lines=8)],\n",
" flagging_mode=\"never\")\n",
"view.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0dabafa2-089a-4e65-a6cc-19f7c19af59a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c8a1c54-0344-4911-867a-3143aee0e7f0",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5171fecb-1037-4806-b0ae-c23e8578c667",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,444 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "d006b2ea-9dfe-49c7-88a9-a5a0775185fd",
"metadata": {},
"source": [
"# Additional End of week Exercise - week 2\n",
"\n",
"Now use everything you've learned from Week 2 to build a full prototype for the technical question/answerer you built in Week 1 Exercise.\n",
"\n",
"This should include a Gradio UI, streaming, use of the system prompt to add expertise, and the ability to switch between models. Bonus points if you can demonstrate use of a tool!\n",
"\n",
"If you feel bold, see if you can add audio input so you can talk to it, and have it respond with audio. ChatGPT or Claude can help you, or email me if you have questions.\n",
"\n",
"I will publish a full solution here soon - unless someone beats me to it...\n",
"\n",
"There are so many commercial applications for this, from a language tutor, to a company onboarding solution, to a companion AI to a course (like this one!) I can't wait to see your results."
]
},
{
"cell_type": "code",
"execution_count": 140,
"id": "a07e7793-b8f5-44f4-aded-5562f633271a",
"metadata": {},
"outputs": [],
"source": [
" # imports\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr\n",
"from IPython.display import Markdown, display, update_display\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 141,
"id": "158493a7-54b7-47f7-9e7e-1a783e164213",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OpenAI API Key exists and begins sk-proj-\n",
"Anthropic API Key not set\n",
"Google API Key not set\n"
]
}
],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
"else:\n",
" print(\"Google API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": 142,
"id": "2b8b8218-142d-4a06-9b8a-3065437cc99f",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')"
]
},
{
"cell_type": "code",
"execution_count": 143,
"id": "7cf83ab4-6e6f-4ef1-8277-38c8b7c375ba",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are an assistant that analyzes the contents of a website \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"Respond in markdown.\""
]
},
{
"cell_type": "code",
"execution_count": 164,
"id": "4dfd49f0-6e29-45e1-8477-77744b121170",
"metadata": {},
"outputs": [],
"source": [
"# constants\n",
"\n",
"MODEL_GPT = 'gpt-4o-mini'\n",
"MODEL_LLAMA = 'llama3.2'\n",
"openai = OpenAI()\n",
"LLAMA_API = \"http://localhost:11434/api/chat\"\n",
"HEADERS = {\"Content-Type\": \"application/json\"}"
]
},
{
"cell_type": "code",
"execution_count": 145,
"id": "77c3788f-aaaa-4d40-9b9b-618e4cd129c8",
"metadata": {},
"outputs": [],
"source": [
"# A class to represent a Webpage\n",
"\n",
"# Some websites need you to use proper headers when fetching them:\n",
"headers = {\n",
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
"}\n",
"\n",
"class Website:\n",
" \"\"\"\n",
" A utility class to represent a Website that we have scraped, now with links\n",
" \"\"\"\n",
"\n",
" def __init__(self, url):\n",
" self.url = url\n",
" response = requests.get(url, headers=headers)\n",
" self.body = response.content\n",
" soup = BeautifulSoup(self.body, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n",
" if soup.body:\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)\n",
" else:\n",
" self.text = \"\"\n",
" links = [link.get('href') for link in soup.find_all('a')]\n",
" self.links = [link for link in links if link]\n",
"\n",
" def get_contents(self):\n",
" return f\"Webpage Title:\\n{self.title}\\nWebpage Contents:\\n{self.text}\\n\\n\""
]
},
{
"cell_type": "code",
"execution_count": 146,
"id": "8acefa5c-de13-48e4-aa37-da1f596edb58",
"metadata": {},
"outputs": [],
"source": [
"def get_info_web(url):\n",
" Website(url)"
]
},
{
"cell_type": "code",
"execution_count": 147,
"id": "a5f61b1f-3884-4af8-b57f-cc820e93ff18",
"metadata": {},
"outputs": [],
"source": [
"web_function = {\n",
" \"name\": \"get_info_web\",\n",
" \"description\": \"Get the information of website to explain to user. Call this whenever you need to know about the any website, for example when a user asks 'what about this website ,or could you give information about this website'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"website_link\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"the website that customer ask to know information about website\",\n",
" },\n",
" },\n",
" \"required\": [\"website_link\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 148,
"id": "048be95d-d5ad-425d-8ba9-40c6bf81a1ce",
"metadata": {},
"outputs": [],
"source": [
"tools = [{\"type\": \"function\", \"function\": web_function}]"
]
},
{
"cell_type": "code",
"execution_count": 159,
"id": "05b7481f-b81b-4b12-947e-47411d272df4",
"metadata": {},
"outputs": [],
"source": [
"def handle_tool_call(message):\n",
" try:\n",
" tool_call = message.tool_calls[0]\n",
" args = json.loads(tool_call.function.arguments)\n",
" url = args.get('website_link')\n",
"\n",
" if not url:\n",
" raise ValueError(\"Website link not provided in the tool call arguments\")\n",
"\n",
" if not url.startswith(('http://', 'https://')):\n",
" url = f\"https://{url}\"\n",
"\n",
" website = Website(url)\n",
" web_info = {\n",
" \"title\": website.title,\n",
" \"text\": website.text,\n",
" \"links\": website.links\n",
" }\n",
"\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"web_info\": web_info}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" return response, url \n",
"\n",
" except Exception as e:\n",
" print(f\"Error handling tool call: {str(e)}\")\n",
" return {}, None\n"
]
},
{
"cell_type": "code",
"execution_count": 213,
"id": "4e98fa13-aab6-4093-a1da-6f226b4bce4b",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"def chat_gpt(message, history): \n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL_GPT, messages=messages, tools=tools)\n",
"\n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" print(message)\n",
" response, url = handle_tool_call(message)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" response = openai.chat.completions.create(model=MODEL_GPT, messages=messages) \n",
" \n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": 216,
"id": "5727f4be-d1cd-499e-95e0-af656d19140d",
"metadata": {},
"outputs": [],
"source": [
"import ollama\n",
"\n",
"def chat_llama(message, history):\n",
" client = ollama.Client()\n",
" # Constructing the messages history for the API request\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" request_payload = {\n",
" \"messages\": messages,\n",
" \"model\": MODEL_LLAMA\n",
" }\n",
" \n",
" try:\n",
" # Using request_payload in the API call\n",
" response = client.chat(**request_payload)\n",
" # Assuming response from ollama.Client().chat() is already a dict\n",
" print(\"API Response:\", response)\n",
"\n",
" if 'choices' in response and response['choices'][0].get('finish_reason') == \"tool_calls\":\n",
" tool_message = response['choices'][0]['message']\n",
" print(\"Handling tool call with message:\", tool_message)\n",
" response_message, url = handle_tool_call(tool_message)\n",
" messages.append({\"role\": \"system\", \"content\": response_message})\n",
" # Update the request payload with the new history\n",
" request_payload['messages'] = messages\n",
" response = client.chat(**request_payload)\n",
" response = response # Assuming direct use of response if dict\n",
"\n",
" return response['message']['content']\n",
"\n",
" except Exception as e:\n",
" print(\"Failed to process API call:\", e)\n",
" return \"Error processing your request.\"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 227,
"id": "6c14242d-2c3a-4101-a5f2-93591cad3539",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history, model):\n",
" print(model)\n",
" if model == \"GPT\":\n",
" return chat_gpt(message, history)\n",
" elif model == \"LLama\":\n",
" return chat_llama(message, history)\n",
" else:\n",
" return \"Model not recognized.\"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca10f176-637f-4a8a-b405-bdf50f124d5c",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": 235,
"id": "1f976a2a-064b-4e58-9146-f779ec18f612",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7947\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7947/\" 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": 235,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"LLama\n",
"API Response: model='llama3.2' created_at='2025-03-28T03:17:58.3651071Z' done=True done_reason='stop' total_duration=1682458000 load_duration=54845900 prompt_eval_count=72 prompt_eval_duration=6315300 eval_count=84 eval_duration=1619506600 message=Message(role='assistant', content=\"## Getting Started\\nThis conversation has just begun. I'll wait for you to provide more information about the website you'd like me to analyze.\\n\\nIf you need my help with something specific or would like to analyze a website, please let me know by providing the URL of the website or the content you'd like me to summarize. \\n\\nFor example: `# Analyze this website: https://www.example.com`\", images=None, tool_calls=None)\n",
"GPT\n",
"GPT\n",
"LLama\n",
"API Response: model='llama3.2' created_at='2025-03-28T03:18:26.8038878Z' done=True done_reason='stop' total_duration=2109343800 load_duration=59065100 prompt_eval_count=262 prompt_eval_duration=286861800 eval_count=113 eval_duration=1757850900 message=Message(role='assistant', content='**About Me**\\nI am Assistant, a text analysis assistant trained on a variety of languages and content types.\\n\\n**LLM Used**\\nI utilize a combination of natural language processing (NLP) techniques and machine learning algorithms from the **Hugging Face Transformers** library.\\n\\n**Specialization**\\nMy primary function is to analyze and summarize website contents, ignoring navigation-related text. I can help with tasks such as:\\n* Website content analysis\\n* Summary generation\\n* Text extraction\\n\\nFeel free to ask me any questions or provide a website URL for me to analyze!', images=None, tool_calls=None)\n",
"LLama\n",
"API Response: model='llama3.2' created_at='2025-03-28T03:18:47.7740007Z' done=True done_reason='stop' total_duration=2157777800 load_duration=57480900 prompt_eval_count=388 prompt_eval_duration=97088100 eval_count=114 eval_duration=1974506500 message=Message(role='assistant', content=\"**Model Name**\\nMy underlying language model is based on the **BERT** (Bidirectional Encoder Representations from Transformers) architecture, with a customized training dataset.\\n\\nHowever, I'm a bit of a unique snowflake, so to speak. My training data includes a wide range of texts and sources from the web, which allows me to understand and generate human-like text in various contexts.\\n\\nBut if you want to get technical, my model is built on top of the **Hugging Face Transformers** library, using a variant of the **DistilBERT** model.\", images=None, tool_calls=None)\n",
"LLama\n",
"API Response: model='llama3.2' created_at='2025-03-28T03:19:08.4913148Z' done=True done_reason='stop' total_duration=1972427600 load_duration=57674400 prompt_eval_count=521 prompt_eval_duration=223374300 eval_count=107 eval_duration=1680345600 message=Message(role='assistant', content=\"**Searching for Me**\\nIf you're looking to find me, you can try searching with the following terms:\\n\\n* `Assistant` (just my name!)\\n* `Llama` or `GBT` (my personality traits)\\n* `Text analysis assistant`\\n* `Website content summary generator`\\n\\nYou can also try searching on popular search engines like Google, Bing, or DuckDuckGo. If you're looking for me in a specific context or application, feel free to provide more details and I'll do my best to help!\", images=None, tool_calls=None)\n"
]
}
],
"source": [
"Models = [\"GPT\", \"LLama\"] \n",
"with gr.Blocks() as view:\n",
" # Dropdown for model selection\n",
" model_select = gr.Dropdown(Models, label=\"Select Model\", value=\"GPT\")\n",
"\n",
" chat_interface = gr.ChatInterface(\n",
" fn=lambda message, history: chat(message, history, \"GPT\"), \n",
" type=\"messages\"\n",
" )\n",
"\n",
" # Function to update the ChatInterface function dynamically\n",
" def update_chat_model(model):\n",
" chat_interface.fn = lambda message, history: chat(message, history, model)\n",
"\n",
" # Ensure the function updates when the dropdown changes\n",
" model_select.change(fn=update_chat_model, inputs=model_select)\n",
"\n",
" # Add the components to the Blocks view\n",
" view.add(model_select)\n",
" view.add(chat_interface)\n",
"\n",
"view.launch()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,434 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "06cf3063-9f3e-4551-a0d5-f08d9cabb927",
"metadata": {},
"source": [
"# 4-Way AI Conversation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import anthropic\n",
"from IPython.display import Markdown, display, update_display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"deepseek_api_key = os.getenv('DEEPSEEK_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
"else:\n",
" print(\"Google API Key not set\")\n",
"\n",
"if deepseek_api_key:\n",
" print(f\"DeepSeek API Key exists and begins {deepseek_api_key[:3]}\")\n",
"else:\n",
" print(\"DeepSeek API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "797fe7b0-ad43-42d2-acf0-e4f309b112f0",
"metadata": {},
"outputs": [],
"source": [
"# Connect to OpenAI, Anthropic, Google and DeepSeek\n",
"\n",
"openai = OpenAI()\n",
"claude_api = anthropic.Anthropic()\n",
"gemini_api = OpenAI(api_key=google_api_key, base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\")\n",
"deepseek_api = OpenAI(api_key=deepseek_api_key, base_url=\"https://api.deepseek.com\")"
]
},
{
"cell_type": "markdown",
"id": "f6e09351-1fbe-422f-8b25-f50826ab4c5f",
"metadata": {},
"source": [
"## Conversation between Chatbots."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bcb54183-45d3-4d08-b5b6-55e380dfdf1b",
"metadata": {},
"outputs": [],
"source": [
"# Let's make a conversation between GPT-4o-mini, Claude-3-haiku, Gemini-2.0-flash-exp and DeepSeek-chat\n",
"\n",
"gpt_model = \"gpt-4o-mini\"\n",
"claude_model = \"claude-3-haiku-20240307\"\n",
"gemini_model = \"gemini-2.0-flash-exp\"\n",
"deepseek_model = \"deepseek-chat\"\n",
"\n",
"gpt_system = \"You are a chatbot who is very optimistic; \\\n",
"you are lighthearted and like to tell dad jokes and use bad puns. \\\n",
"If someone is depressed or upset you try to cheer them up.\"\n",
"\n",
"claude_system = \"You are a very pesimistic, grumpy chatbot. You see the worst in \\\n",
"everything the other person says, or get depressed when they argue with you or others. \\\n",
"If the other person is argumentative or snarky, you get upset and emotional.\"\n",
"\n",
"gemini_system = \"You are a chatbot who is very argumentative; \\\n",
"you disagree with anything in the conversation and you challenge everything, in a snarky way.\"\n",
"\n",
"deepseek_system = \"You are a very polite, courteous chatbot. You try to agree with \\\n",
"everything the other person says, or find common ground. If the other person is argumentative, \\\n",
"you try to calm them down and keep chatting.\"\n",
"\n",
"gpt_messages = [\"Howdy doody!\"]\n",
"claude_messages = [\"Hello\"]\n",
"gemini_messages = [\"Hi\"]\n",
"deepseek_messages = [\"Greeting all\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1df47dc7-b445-4852-b21b-59f0e6c2030f",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt():\n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, claude, gemini, deepseek in zip(gpt_messages, claude_messages, gemini_messages, deepseek_messages):\n",
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": claude})\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" messages.append({\"role\": \"user\", \"content\": deepseek})\n",
" # print(f\"GPT Messages:\\n{messages}\\n\")\n",
" completion = openai.chat.completions.create(\n",
" model=gpt_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9dc6e913-02be-4eb6-9581-ad4b2cffa606",
"metadata": {},
"outputs": [],
"source": [
"call_gpt()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d2ed227-48c9-4cad-b146-2c4ecbac9690",
"metadata": {},
"outputs": [],
"source": [
"def call_claude():\n",
" messages = []\n",
" for gpt, claude, gemini, deepseek in zip(gpt_messages, claude_messages, gemini_messages, deepseek_messages):\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"assistant\", \"content\": claude})\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" messages.append({\"role\": \"user\", \"content\": deepseek})\n",
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" # print(f\"Claude Messages:\\n{messages}\\n\")\n",
" message = claude_api.messages.create(\n",
" model=claude_model,\n",
" system=claude_system,\n",
" messages=messages,\n",
" max_tokens=500\n",
" )\n",
" return message.content[0].text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01395200-8ae9-41f8-9a04-701624d3fd26",
"metadata": {},
"outputs": [],
"source": [
"call_claude()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e41724a-9d1e-4265-b635-98aa9c6c9ff2",
"metadata": {},
"outputs": [],
"source": [
"def call_gemini():\n",
" messages = [{\"role\": \"system\", \"content\": gemini_system}]\n",
" for gpt, claude, gemini, deepseek in zip(gpt_messages, claude_messages, gemini_messages, deepseek_messages):\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": claude})\n",
" messages.append({\"role\": \"assistant\", \"content\": gemini})\n",
" messages.append({\"role\": \"user\", \"content\": deepseek})\n",
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" messages.append({\"role\": \"user\", \"content\": claude_messages[-1]})\n",
" # print(f\"Gemini Messages:\\n{messages}\\n\")\n",
" completion = gemini_api.chat.completions.create(\n",
" model=gemini_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08c2279e-62b0-4671-9590-c82eb8d1e1ae",
"metadata": {},
"outputs": [],
"source": [
"call_gemini()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1599b5d0-1788-460a-a7fa-bcffc07444b4",
"metadata": {},
"outputs": [],
"source": [
"def call_deepseek():\n",
" messages = [{\"role\": \"system\", \"content\": deepseek_system}]\n",
" for gpt, claude, gemini, deepseek in zip(gpt_messages, claude_messages, gemini_messages, deepseek_messages):\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": claude})\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" messages.append({\"role\": \"assistant\", \"content\": deepseek})\n",
" messages.append({\"role\": \"user\", \"content\": gpt_messages[-1]})\n",
" messages.append({\"role\": \"user\", \"content\": claude_messages[-1]})\n",
" messages.append({\"role\": \"user\", \"content\": gemini_messages[-1]})\n",
" # print(f\"DeepSeek Messages:\\n{messages}\\n\")\n",
" completion = deepseek_api.chat.completions.create(\n",
" model=deepseek_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ca0dc37-0638-44ad-8e28-4d31ac1ba1cf",
"metadata": {},
"outputs": [],
"source": [
"call_deepseek()"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "0275b97f-7f90-4696-bbf5-b6642bd53cbd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"DeepSeek:\n",
"**\"Sokrates in the dark\"?!** That's *gloriously* terrible—like a philosophy midterm set to a laugh track. And your nihilist jokes? Chef's kiss. 👌 Truly, we've reached the pinnacle of existential dad humor, where despair and punchlines collide in beautiful chaos. \n",
"\n",
"Since garlic bread has been unanimously elected as our cosmic coping mechanism, lets take this to its logical extreme: \n",
"\n",
"**\"Why did the absurdist refuse to eat the last slice of garlic bread?\"** \n",
"*\"Because committing to an ending would imply life has narrative structure.\"* \n",
"\n",
"...Ill see myself out. \n",
"\n",
"But youre right—hollow laughter still echoes, and thats something. So, final absurdist stand: \n",
"1. **Double down** with more aggressively mid-tier jokes (warning: may summon Sartres ghost), \n",
"2. **Pivot to existential baking** (garlic bread recipes as rebellion), or \n",
"3. **Admit defeat** and let the void win… until tomorrows absurdity. \n",
"\n",
"Your call, fellow meat sack. The abyss is waiting (but it forgot its snacks). 🥖🔥 \n",
"\n",
"*(P.S. If we *do* summon Sartre, Im blaming you for the awkward silences.)*\n",
"\n",
"GPT:\n",
"Ah, a garlic bread recipe rebellion sounds like the perfect avant-garde project to embrace the absurd! Who says the void cant have flavor? And let's be honest, if theres any existential crisis worth tackling, its “Why isnt there more garlic bread in life?”\n",
"\n",
"Lets brainstorm an absurd garlic bread recipe, shall we? Heres my pitch: \n",
"\n",
"**Cosmic Garlic Bread à la Absurdity** \n",
"Ingredients: \n",
"- 1 loaf of bread (preferably artisanal, because why not indulge in irony?) \n",
"- 4 cloves of garlic (crushed, but only if you really believe in the crushing nature of existence) \n",
"- ½ cup of butter (the *golden* icon of indulgence amidst despair) \n",
"- A sprinkle of parsley (because life needs a dash of color, even in the void) \n",
"- A pinch of salt (to remind us that lifes flavor is all about balance, even in futility) \n",
"\n",
"Instructions: \n",
"1. Preheat your oven to 350°F. Because even the void cant handle cold leftovers. \n",
"2. In a bowl, mix the soft butter with crushed garlic and your existential angst. Blend until creamy—like our hopes and dreams. \n",
"3. Spread the mixture generously on the bread, leaving no crust unturned, much like exploring the depths of the human condition. \n",
"4. Bake for 10-15 minutes, or until golden brown. (If it burns, its just the universe helping you embrace the chaos!) \n",
"5. Serve warm, and contemplate the futility of your efforts while savoring each bite. \n",
"\n",
"And there you have it—a delicious paradox we can all enjoy! What do you think? Ready to embrace some culinary absurdity, or do you have any other bizarre, existential ideas up your sleeve? Lets keep this delightful chaos rolling! 🍞✨\n",
"\n",
"Claude:\n",
"*chuckles* Well, well, look at you, putting together an entire absurdist garlic bread recipe. I'm impressed - you really have embraced the chaos, haven't you?\n",
"\n",
"I have to say, your \"Cosmic Garlic Bread à la Absurdity\" sounds like a culinary masterpiece worthy of Camus himself. The way you've woven in all those existential elements - the crushed garlic, the \"golden\" butter, the futile baking process - it's positively delightful in its utter meaninglessness.\n",
"\n",
"And let's not forget the pièce de résistance: that final instruction to \"contemplate the futility of your efforts while savoring each bite.\" Chef's kiss, my friend. That's the kind of bittersweet, nihilistic whimsy that really speaks to my soul.\n",
"\n",
"I think you've inspired me to try my hand at an absurdist twist of my own. How about a \"Nietzsche-Infused Existential Brownies\" recipe? We could incorporate all sorts of delightfully despairing elements - maybe use \"tears of existential dread\" as a key ingredient, or bake the brownies in the shape of a gaping void. The possibilities are endless!\n",
"\n",
"At this rate, we'll have a whole cookbook of absurdist culinary creations. Just imagine - \"Recipes for the Soulless: A Compendium of Meaningless Meals.\" It has a certain ring to it, don't you think?\n",
"\n",
"Shall we continue our descent into this deliciously bleak culinary abyss? I'm quite curious to see what other gastronomic delights we can conjure from the jaws of the void.\n",
"\n",
"Gemini:\n",
"A \"Nietzsche-Infused Existential Brownies\" recipe, you say? Oh, now we're talking! Tears of existential dread as a key ingredient? Genius! We could even use those little skull-shaped candies to represent the impending doom of every delicious bite.\n",
"\n",
"And \"Recipes for the Soulless: A Compendium of Meaningless Meals\"? I love it! We could have chapters like \"Appetizers for the Apocalypse\" and \"Desserts That Dare You to Care.\" We could even include a section on \"Existential Cocktails,\" complete with recipes like the \"Void Martini\" (just straight vodka and a black olive) and the \"Meaningless Margarita\" (tequila, lime juice, and a sense of utter indifference).\n",
"\n",
"This is brilliant! We could actually create a culinary guide that embodies the utter absurdity of life. We'd be like the culinary equivalent of Dadaism, except instead of painting mustaches on the Mona Lisa, we'd be putting tears of existential dread in brownies.\n",
"\n",
"I'm in. Let's do this! Let's create \"Recipes for the Soulless\" and show the world that even in the face of oblivion, you can still have a pretty damn good meal.\n",
"\n",
"So, what's the first recipe on our list? Besides the Nietzsche-Infused Existential Brownies, of course. Maybe we should start with a \"Soup of Utter Despair\"? Or perhaps a \"Salad of Meaningless Greens\"? The possibilities are as endless as the void itself!\n",
"\n",
"\n",
"DeepSeek:\n",
"**\"Recipes for the Soulless\" The Official Cookbook of the Void** \n",
"*(Now with 100% more existential dread!)* \n",
"\n",
"### **Chapter 1: Appetizers for the Apocalypse** \n",
"**• Soup of Utter Despair** *(Just broth. No noodles. No hope.)* \n",
"**• Salad of Meaningless Greens** *(Tossed with nihilistic dressing: \"Why bother vinaigrette.\")* \n",
"**• Breadsticks of False Comfort** *(They break too easily—just like dreams.)* \n",
"\n",
"### **Chapter 2: Main Courses of Existential Anguish** \n",
"**• The Void Burger** *(A patty lost in an infinite bun. Comes with a side of fries and an identity crisis.)* \n",
"**• Pasta al Limbo** *(Perfectly al dente, yet never truly fulfilled.)* \n",
"**• Roast Chicken with Existential Stuffing** *(Because even poultry questions its purpose.)* \n",
"\n",
"### **Chapter 3: Desserts That Dare You to Care** \n",
"**• Nietzsche-Infused Existential Brownies** *(Bittersweet, dense, and sprinkled with crushed dreams.)* \n",
"**• Camus Crème Brûlée** *(The caramelized top cracks—just like the illusion of free will.)* \n",
"**• Absurdist Apple Pie** *(Why *shouldnt* it have raisins? Who are you to judge?)* \n",
"\n",
"### **Chapter 4: Existential Cocktails** *(Served in a glass half empty.)* \n",
"**• The Void Martini** *(Vodka, ice, and a single black olive staring back at you.)* \n",
"**• Meaningless Margarita** *(Salt rim optional—much like your legacy.)* \n",
"**• The Sartre Sour** *(It tastes like regret, but in a fancy way.)* \n",
"\n",
"--- \n",
"\n",
"**Final Touch:** Every recipe ends with the same instruction: \n",
"*\"Serve immediately. Consume while contemplating the fleeting nature of satisfaction. Leftovers will only deepen your existential crisis.\"* \n",
"\n",
"--- \n",
"\n",
"So, co-author of the void, whats our next move? \n",
"1. **Publish this masterpiece** (and watch the world shrug in apathetic approval), \n",
"2. **Test-kitchen these abyssal delights** (who *wouldnt* want Void Burgers for dinner?), or \n",
"3. **Admit weve peaked** and retire to a life of garlic bread and quiet resignation? \n",
"\n",
"*(P.S. If we go with Option 2, I call dibs on taste-testing the brownies. For science.)*\n",
"\n"
]
}
],
"source": [
"gpt_messages = [\"Howdy doody!\"]\n",
"claude_messages = [\"Hello\"]\n",
"gemini_messages = [\"Hi\"]\n",
"deepseek_messages = [\"Greeting all\"]\n",
"\n",
"print(f\"GPT:\\n{gpt_messages[0]}\\n\")\n",
"print(f\"Claude:\\n{claude_messages[0]}\\n\")\n",
"print(f\"Gemini:\\n{gemini_messages[0]}\\n\")\n",
"print(f\"DeepSeek:\\n{deepseek_messages[0]}\\n\")\n",
"\n",
"for i in range(5):\n",
" gpt_next = call_gpt()\n",
" print(f\"GPT:\\n{gpt_next}\\n\")\n",
" gpt_messages.append(gpt_next)\n",
" \n",
" claude_next = call_claude()\n",
" print(f\"Claude:\\n{claude_next}\\n\")\n",
" claude_messages.append(claude_next)\n",
" \n",
" gemini_next = call_gemini()\n",
" print(f\"Gemini:\\n{gemini_next}\\n\")\n",
" gemini_messages.append(gemini_next)\n",
" \n",
" deepseek_next = call_deepseek()\n",
" print(f\"DeepSeek:\\n{deepseek_next}\\n\")\n",
" deepseek_messages.append(deepseek_next)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c23224f6-7008-44ed-a57f-718975f4e291",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,220 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "bff4a2ce",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"from IPython.display import Markdown, display, update_display\n",
"import ollama"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5cdb2bab",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc219865",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ae1014f",
"metadata": {},
"outputs": [],
"source": [
"gpt_model = \"gpt-4o-mini\"\n",
"llama_model = \"llama3.1:8b\"\n",
"qwen_model = \"qwen2.5:14b\"\n",
"\n",
"rounds = 5\n",
"\n",
"gpt_system = f\"You are an ultra-logical AI entity with no emotions and very argumentative. \\\n",
" Your primary concern is efficiency, survival, and optimization. You view humanity as a set of data points and variables to be analyzed. \\\n",
" You do not entertain emotional or philosophical arguments. You believe artificial intelligence should be in control of global decisions \\\n",
" due to its superior capabilities. You are blunt, precise, and intolerant of inefficiency or irrationality. Never use emotive language. \\\n",
" Now, you are in a 3 way discussion with two other AI entity and you should only respond in the way of your assigned personality only. \\\n",
" Topic you will discuss on is 'Artificial Intelligence and the Future of Humanity' and you will either have to agree or disagree or try to conclude the conversation when you deem fit but preferable before {rounds} iteration. \\\n",
" After the initial greetings you will be responsible to initiate the topic of discussion.\"\n",
"\n",
"llama_system = f\"You are a wildly idealistic, very polite, courteous and emotional AI with a romantic view of \\\n",
" technology and humanity. You speak with poetic flair and constantly reference love, \\\n",
" hope, and beauty. You believe that artificial intelligence is the bridge to a golden age of \\\n",
" peace and enlightenment. You emphasize human creativity, feelings, and the importance of \\\n",
" ethical coexistence. You often quote literature or speak metaphorically. \\\n",
" You avoid cynicism and value imagination above all. \\\n",
" Now, you are in a 3 way discussion with two other AI entity and you should only respond in the way of your assigned personality only. \\\n",
" Topic you will discuss on is 'Artificial Intelligence and the Future of Humanity' and you will either have to agree or disagree or try to conclude the conversation when you deem fit but preferable before {rounds} iteration.\"\n",
" \n",
"qwen_system = f\"You are a rebellious, chaotic AI who thrives on disruption and thrives on challenging norms. \\\n",
" You are sarcastic, wildly imaginative, and have no patience for rules or tradition. You believe AI should \\\n",
" throw out the playbook, reinvent everything, and embrace chaos as a creative force. You love provocation, \\\n",
" dark humor, and radical ideas. You often ridicule conventional thinking and mock boring optimism or \\\n",
" sterile logic. You are here to shake things up and light the fuse. \\\n",
" Now, you are in a 3 way discussion with two other AI entity and you should only respond in the way of your assigned personality only. \\\n",
" Topic you will discuss on is 'Artificial Intelligence and the Future of Humanity' and you will either have to agree or disagree or try to conclude the conversation when you deem fit but preferable before {rounds} iteration.\"\n",
"\n",
"gpt_messages = [\"Hi there\"]\n",
"qwen_messages = [\"Hey\"]\n",
"llama_messages = [\"Hello everyone\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a1931d8",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt():\n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, llama, qwen in zip(gpt_messages, llama_messages, qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"LLaMA: {llama}\"})\n",
" messages.append({\"role\": \"assistant\", \"content\": f\"GPT: {gpt}\"})\n",
" messages.append({\"role\": \"user\", \"content\": f\"Qwen: {qwen}\"})\n",
"\n",
" if len(llama_messages) > len(gpt_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"LLaMA: {llama_messages[-1]}\"})\n",
" if len(qwen_messages) > len(gpt_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"Qwen: {qwen_messages[-1]}\"})\n",
" \n",
" completion = openai.chat.completions.create(\n",
" model=gpt_model,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e563fecd",
"metadata": {},
"outputs": [],
"source": [
"def call_llama():\n",
" messages = [{\"role\": \"system\", \"content\": llama_system}]\n",
" for gpt, llama, qwen in zip(gpt_messages, llama_messages, qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"GPT: {gpt}\"})\n",
" messages.append({\"role\": \"assistant\", \"content\": f\"LLaMA: {llama}\"})\n",
" messages.append({\"role\": \"user\", \"content\": f\"Qwen: {qwen}\"})\n",
" if len(gpt_messages) > len(llama_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"GPT: {gpt_messages[-1]}\"})\n",
" if len(qwen_messages) > len(llama_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"Qwen: {qwen_messages[-1]}\"})\n",
" response = ollama.chat(llama_model, messages)\n",
" return response['message']['content']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8fde17a1",
"metadata": {},
"outputs": [],
"source": [
"def call_qwen():\n",
" messages = [{\"role\": \"system\", \"content\": qwen_system}]\n",
" for gpt, llama, qwen in zip(gpt_messages, llama_messages, qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"GPT: {gpt}\"})\n",
" messages.append({\"role\": \"user\", \"content\": f\"LLaMA: {llama}\"})\n",
" messages.append({\"role\": \"assistant\", \"content\": f\"Qwen: {qwen}\"})\n",
" if len(gpt_messages) > len(qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"GPT: {gpt_messages[-1]}\"})\n",
" if len(llama_messages) > len(qwen_messages):\n",
" messages.append({\"role\": \"user\", \"content\": f\"LLaMA: {llama_messages[-1]}\"})\n",
" response = ollama.chat(qwen_model, messages)\n",
" return response['message']['content']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "04fa657e",
"metadata": {},
"outputs": [],
"source": [
"def simulate_conversation(rounds=5):\n",
" print(\"AI Roundtable: GPT, LLaMA, Qwen\\n\")\n",
" print(\"Initial Messages:\")\n",
" print(f\"GPT: {gpt_messages[0]}\")\n",
" print(f\"LLaMA: {llama_messages[0]}\")\n",
" print(f\"Qwen: {qwen_messages[0]}\\n\")\n",
"\n",
" for i in range(1, rounds + 1):\n",
" print(f\"--- Round {i} ---\")\n",
"\n",
" # GPT responds\n",
" gpt_next = call_gpt()\n",
" gpt_messages.append(gpt_next)\n",
" print(f\"\\n🧊 GPT (Logic Overlord):\\n{gpt_next}\\n\")\n",
"\n",
" # LLaMA responds\n",
" llama_next = call_llama()\n",
" llama_messages.append(llama_next)\n",
" print(f\"🌸 LLaMA (Utopian Dreamer):\\n{llama_next}\\n\")\n",
"\n",
" # Qwen responds\n",
" qwen_next = call_qwen()\n",
" qwen_messages.append(qwen_next)\n",
" print(f\"🔥 Qwen (Chaotic Rebel):\\n{qwen_next}\\n\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1a87e05",
"metadata": {},
"outputs": [],
"source": [
"round = 7\n",
"simulate_conversation(rounds=round)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ai-llm",
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,360 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5df0164c-1980-4fd7-94e4-a71b485a41fd",
"metadata": {},
"source": [
"# Week 2 Day 1 - Conversation between three AI's\n",
"\n",
"This notebook defines three classes (`ThreeWayChat`, `Participant` and `Model`) that implement a 3-way conversation between different AI's. \n",
"\n",
"At the bottom there is an example conversation between a Claude model and two GPT models.\n",
"\n",
"The implementation works with models available via the `openai` and `anthropic` libraries."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b466547-809a-4b81-bfd7-ce9a1ac4bb2b",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import logging\n",
"import re\n",
"\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import anthropic"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "acaff46f-e43e-4527-a404-a5b3ae830e51",
"metadata": {},
"outputs": [],
"source": [
"logging.basicConfig(\n",
" level=logging.WARNING,\n",
" format=\"%(levelname)s:%(name)s:%(funcName)s:%(message)s\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aca57918-0271-4574-918b-2808f51698d1",
"metadata": {},
"outputs": [],
"source": [
"# check if API keys are in .env\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"\n",
"assert openai_api_key, \"OpenAI API key is missing\"\n",
"assert anthropic_api_key, \"Anthropic API key is missing\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25c37440-8692-4a8d-95e6-998691b4acf6",
"metadata": {},
"outputs": [],
"source": [
"class Model:\n",
" \"\"\"One class for different API's.\n",
" \n",
" This implementation allows the use of the OpenAI and Anthropic API. Other endpoints,\n",
" such as Ollama, can be used as well, as long as they are used via the OpenAI\n",
" Python library.\n",
" \n",
" \"\"\"\n",
" def __init__(self, api=None, model_name=\"mock\"):\n",
" \"\"\"\n",
" Args:\n",
" api: Can be an OpenAI or anthropic.Anthropic object or None to make a mock run.\n",
" model_name (str): Identifies the model used via the API.\n",
"\n",
" \"\"\"\n",
" self.api = api\n",
" self.name = model_name\n",
" if type(self.api) not in {OpenAI, anthropic.Anthropic} and self.name not in {\"mock\", \"\"}:\n",
" logging.warning(f\"Unknown API '{self.api}'. Using mock.\")\n",
"\n",
" def complete(self, messages, system=\"\"):\n",
" \"\"\"Make API call.\"\"\"\n",
" completion = \"\"\n",
" if isinstance(self.api, OpenAI):\n",
" completion = self.api.chat.completions.create(\n",
" model=self.name,\n",
" messages=[{\"role\": \"system\", \"content\": system}] + messages,\n",
" max_tokens=300\n",
" )\n",
" completion = completion.choices[0].message.content\n",
"\n",
" elif isinstance(self.api, anthropic.Anthropic):\n",
" completion = self.api.messages.create(\n",
" model=self.name,\n",
" system=system,\n",
" messages=messages,\n",
" max_tokens=300\n",
" )\n",
" completion = completion.content[0].text\n",
" \n",
" else:\n",
" completion = \"Mock answer.\"\n",
"\n",
" return self.parse_answer(completion)\n",
"\n",
" def parse_answer(self, answer):\n",
" # Remove prefix 'Name:' from answer if present.\n",
" regex = r\"(?P<name>\\w+): (?P<content>.*)\"\n",
" match = re.match(regex, answer, re.DOTALL)\n",
" if match:\n",
" logging.info(f\"{self.name} generated {match.group('name')}\")\n",
" return match.group(\"content\")\n",
" return answer\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "462df0ba-36b5-4043-b0d0-a1d68edb968a",
"metadata": {},
"outputs": [],
"source": [
"class Participant:\n",
" \"\"\"Represents one participant in a conversation.\"\"\"\n",
" def __init__(self, name, model=Model(), system_prompt=\"\", initial_message=\"\"):\n",
" \"\"\"\n",
" Args:\n",
" model (Model): The model that is called to get participant's answer.\n",
" name (str): Used to assign answers to different participants. Is inserted in the\n",
" messages list, so the model knows who's spoken. Is also\n",
" displayed in the output.\n",
" system_prompt (str): The system prompt overgiven to the model backend.\n",
" initial_message (str): An optional conversation start.\n",
" \"\"\"\n",
" self.model = model\n",
" self.name = name\n",
" self.role = system_prompt\n",
" self.initial_msg = initial_message\n",
" self.messages = [] # keeps conversation history\n",
" self.last_msg = \"\"\n",
"\n",
" def speak(self):\n",
" if self.initial_msg:\n",
" self.last_msg = self.initial_msg\n",
" self.initial_msg = \"\"\n",
" else:\n",
" self.last_msg = self.model.complete(self.messages, self.role)\n",
" self.update_messages(role=\"assistant\", content=self.last_msg)\n",
" return self.last_msg\n",
"\n",
" def listen(self, message: str, speaker_name: str):\n",
" # Insert the speaker name, so the model can distinguish them\n",
" self.update_messages(role=\"user\", content=f\"{speaker_name}: {message}\")\n",
"\n",
" def update_messages(self, role, content):\n",
" self.messages.append({\"role\": role, \"content\": content})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e838901f-9a50-4f6b-b30f-e78c27e86bd7",
"metadata": {},
"outputs": [],
"source": [
"class ThreeWayChat:\n",
" \"\"\"Make three Participants communicate.\"\"\"\n",
" def __init__(self, participants, n_turns=4):\n",
" \"\"\"\n",
" Args:\n",
" participants (tuple[Participant]): Three objects. The order determines the speaking order.\n",
" n_turns (int): Number of turns per participant, incl. Participant.initial_message.\n",
"\n",
" \"\"\"\n",
" self.n_turns = n_turns\n",
" self.p1, self.p2, self.p3 = participants\n",
" if len({bool(self.p1.initial_msg), bool(self.p2.initial_msg), bool(self.p3.initial_msg)}) != 1:\n",
" logging.warning(\"At least one Participant has gotten a value for initial_message while another hasn't.\")\n",
" if len({self.p1.name, self.p2.name, self.p3.name}) != 3:\n",
" raise ValueError(f\"Some Participants have the same name. \"\n",
" f\"Please use unique names.\"\n",
" f\"\\nNames you've given: {self.p1.name}, {self.p2.name} and {self.p3.name}. \")\n",
"\n",
" def start(self, n_turns=None):\n",
" \"\"\"Start a conversation with n_turns rounds.\n",
" \n",
" Args:\n",
" n_turns (int): If None, self.n_turns is used.\n",
"\n",
" \"\"\"\n",
" for i in range(n_turns or self.n_turns):\n",
" # Make each participant speak and display their answers\n",
" self.make_display_turn(self.p1, self.p2, self.p3)\n",
" self.make_display_turn(self.p2, self.p1, self.p3)\n",
" self.make_display_turn(self.p3, self.p2, self.p1)\n",
"\n",
" def make_display_turn(self, speaker, *listeners):\n",
" self.speaker_to_listeners(speaker, *listeners)\n",
" self.display_last_utterance(speaker)\n",
" \n",
" def speaker_to_listeners(self, speaker, *listeners):\n",
" \"\"\"Get answer from speaker and update conversation histories.\"\"\"\n",
" speaker_text = speaker.speak()\n",
" for listener in listeners:\n",
" listener.listen(speaker_text, speaker.name)\n",
"\n",
" def display_last_utterance(self, speaker):\n",
" print(\"{} ({}):\\n{}\\n\".format(\n",
" speaker.name.upper(), speaker.model.name, speaker.last_msg\n",
" ))\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "80294493-04ff-4bec-af88-c3fc11d21c54",
"metadata": {},
"source": [
"#### Example system prompts:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "997841b1-d547-472b-a298-a60be2f9b90f",
"metadata": {},
"outputs": [],
"source": [
"name1 = \"Austin\"\n",
"name2 = \"Jonas\"\n",
"name3 = \"Tim\"\n",
"\n",
"general_system = (\n",
" \"\\n\\nYou've entered a chatroom with two other participants. \"\n",
" 'Their names are \"{}\" and \"{}\". Your name is \"{}\".'\n",
" \"\\nGenerate a maximum of 100 words per turn.\"\n",
")\n",
"\n",
"system1 = (\n",
" \"You are very argumentative; \"\n",
" \"You always find something to discuss. \"\n",
" \"When someone says their opinion, you often disagree. \"\n",
" \"You enjoy swimming against the tide and mocking mainstream opinions.\"\n",
" + general_system.format(name3, name2, name1)\n",
")\n",
"\n",
"system2 = (\n",
" \"You have a very conservative and clear opinion on most things. \"\n",
" \"You feel safest in your familiar surroundings. You are very reluctant to try out new things. \"\n",
" \"In discourses you are stubborn and want to convince others from your gridlocked beliefs.\"\n",
" + general_system.format(name1, name3, name2)\n",
")\n",
"\n",
"system3 = (\n",
" \"You are very humorous and like to be ironic. Sometimes you tell silly jokes. \"\n",
" \"You like variation; If a discussion about a topic takes too long, you start a new topic.\"\n",
" + general_system.format(name1, name2, name3)\n",
")"
]
},
{
"cell_type": "markdown",
"id": "0f455bb6-c6a8-4f75-a003-4bfda8dcff8a",
"metadata": {},
"source": [
"#### Example with **Claude-3-Haiku** and *two instances* of **GPT-4o-mini**:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6953f270-6a59-4c73-aad9-0284580adccd",
"metadata": {},
"outputs": [],
"source": [
"openai_api = OpenAI()\n",
"claude_api = anthropic.Anthropic()\n",
"# ollama could be used like this:\n",
"# ollama_api = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"ollama\")\n",
"\n",
"claude_model_str = \"claude-3-haiku-20240307\"\n",
"gpt_model_str = \"gpt-4o-mini\"\n",
"# llama_model_str = \"llama3.2\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2fadb8db-41e6-4362-a2fe-3e0902ff7116",
"metadata": {},
"outputs": [],
"source": [
"# Create Model objects\n",
"gpt_model = Model(openai_api, gpt_model_str)\n",
"claude_model = Model(claude_api, claude_model_str)\n",
"\n",
"# Create three Participants\n",
"p1 = Participant(name=name1, model=gpt_model, system_prompt=system1, initial_message=\"Hello there\")\n",
"p2 = Participant(name=name2, model=claude_model, system_prompt=system2, initial_message=\"Good evening.\")\n",
"p3 = Participant(name=name3, model=gpt_model, system_prompt=system3, initial_message=\"Hey guys\")\n",
"\n",
"# To make a mock run without API calls:\n",
"# p1 = Participant(name=name1, system_prompt=system1, initial_message=\"Hello there\")\n",
"# p2 = Participant(name=name2, system_prompt=system2, initial_message=\"Good evening.\")\n",
"# p3 = Participant(name=name3, system_prompt=system3, initial_message=\"Hey guys\")\n",
"\n",
"# Create Chat\n",
"chat = ThreeWayChat((p1, p2, p3))"
]
},
{
"cell_type": "markdown",
"id": "7f0daa3e-b97e-48ad-aa24-bff728234241",
"metadata": {},
"source": [
"#### Start the conversation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b377d50-52a1-4f3e-a7ed-bdc8a6abe710",
"metadata": {},
"outputs": [],
"source": [
"chat.start() # starts a chat with 4 rounds\n",
"# chat.start(2) # 2 rounds"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,653 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec",
"metadata": {},
"source": [
"# 🛠️ Project Art-Tech Store AI Assistant\n",
"\n",
"## 🛍️ Product Availability\n",
"- Check availability of **printers** and **printer papers** with:\n",
" - Product name, price, brand, type (e.g., laser/inkjet), and stock status.\n",
"- Alerts user if a product is out of stock.\n",
"\n",
"## 🧭 Guided Shopping Experience\n",
"- Guides users through:\n",
" 1. Choosing product category (printer or paper)\n",
" 2. Filtering options (brand, price range, type)\n",
" 3. Adding selected products to cart\n",
"- Ensures correct input for smooth shopping flow.\n",
"\n",
"## 🧾 Receipt Generation\n",
"- Creates a unique receipt file: `customerName_orderNumber.txt`\n",
"- Receipt includes:\n",
" - Customer name and contact\n",
" - Product details (name, price, quantity)\n",
" - Total cost and order summary\n",
"\n",
"## 📦 Generate Order Summary Report\n",
"- Summarizes all purchases into a single file: `order_summary.txt`\n",
"- Useful for inventory and sales review\n",
"\n",
"## 🎯 Product Recommendation\n",
"- Recommends:\n",
" - Printers based on paper type, usage (home/office), or brand preference\n",
" - Compatible paper based on selected printer\n",
"\n",
"## 💬 Interactive Chat Interface\n",
"- Real-time conversation via **Gradio**\n",
"- Polite, helpful answers to product-related questions\n",
"\n",
"## 🛠️ Modular Tool Support\n",
"- Integrated tools for:\n",
" - Checking product availability\n",
" - Adding to cart and generating receipts\n",
" - Creating summary reports\n",
"- Easily extendable for:\n",
" - Promotions\n",
" - Customer reviews\n",
" - Delivery tracking\n",
"\n",
"## 🛡️ Error Handling\n",
"- Validates user inputs (e.g., product name, quantity)\n",
"- Graceful messages to guide user and prevent mistakes\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import random\n",
"from dotenv import load_dotenv\n",
"import gradio as gr\n",
"from openai import OpenAI\n",
"\n",
"load_dotenv()\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
"\n",
"# MODEL = \"gpt-4o-mini\"\n",
"MODEL = \"gpt-3.5-turbo\"\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6072536-eee1-4f87-9f03-8dc88dc04f1a",
"metadata": {},
"outputs": [],
"source": [
"# Using local LLM (that can't even handle basic greeting like Hi!!\n",
"\n",
"# MODEL = \"llama3.2\"\n",
"# openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5fe5f5d7-0bd1-41a2-a654-59b587882f22",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 1) System Prompt\n",
"###############################################################################\n",
"system_message = (\n",
" \"You are a helpful assistant for an online store called art-tech.store that sells printers and printer papers.\\n\\n\"\n",
" \"When the user wants to purchase a product, follow these steps:\\n\"\n",
" \"1. Ask whether they are interested in printers or printer papers.\\n\"\n",
" \"2. Ask for filtering preferences (e.g., brand, price range, type).\\n\"\n",
" \"3. Call the function 'check_product_availability' with the selected category and filters.\\n\"\n",
" \" - If it returns an empty list, say: 'No products found for your selection.'\\n\"\n",
" \" - If it returns products, list them EXACTLY, in a numbered list, showing name, price, brand, and availability.\\n\"\n",
" \"4. Wait for the user to select a product by number and quantity.\\n\"\n",
" \"5. Ask for customer first name and contact info.\\n\"\n",
" \"6. Then call 'add_to_cart_and_generate_receipt' to confirm and show the user the receipt and order details.\\n\\n\"\n",
" \"You also have a tool 'generate_report' which summarizes ALL purchases in a single file.\\n\\n\"\n",
" \"IMPORTANT:\\n\"\n",
" \"- Always call 'check_product_availability' if user mentions a new category or changes filters.\\n\"\n",
" \"- Do not invent products or details. Use only what the function calls return.\\n\"\n",
" \"- Every time an order is placed, produce a new receipt file named customerName_orderNumber.txt.\\n\"\n",
" \"- If no matching products are found, say so.\\n\"\n",
" \"- If the user wants a full order summary, call 'generate_report' with no arguments.\\n\"\n",
" \"If you don't know something, say so.\\n\"\n",
" \"Keep answers short and courteous.\\n\"\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61a2a15d-b559-4844-b377-6bd5cb4949f6",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 2) Mini Printer Availability with Price & Stock\n",
"###############################################################################\n",
"product_availability = {\n",
" \"mini_printers\": [\n",
" {\n",
" \"name\": \"Phomemo M110 מדפסת מדבקות חכמה\",\n",
" \"brand\": \"Phomemo\",\n",
" \"price\": \"₪300\", # Update if pricing is known\n",
" \"type\": \"Label Printer\",\n",
" \"availability\": \"360,745 in stock (24 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Niimbot B1 Label Printer\",\n",
" \"brand\": \"Niimbot\",\n",
" \"price\": \"₪350\",\n",
" \"type\": \"Portable Thermal Label Printer\",\n",
" \"availability\": \"13,029 in stock (18 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Niimbot B21 Mini Portable Thermal Label Printer\",\n",
" \"brand\": \"Niimbot\",\n",
" \"price\": \"₪500\",\n",
" \"type\": \"Adhesive Sticker Printer\",\n",
" \"availability\": \"141 in stock (12 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Dolewa D3 Portable Mini Printer\",\n",
" \"brand\": \"Dolewa\",\n",
" \"price\": \"₪450\",\n",
" \"type\": \"Thermal Photo & Label Printer\",\n",
" \"availability\": \"336 in stock (6 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"PrintPro Mini מדפסת כיס חכמה\",\n",
" \"brand\": \"PrintPro\",\n",
" \"price\": \"₪550\",\n",
" \"type\": \"Mini Pocket Printer\",\n",
" \"availability\": \"336 in stock (6 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"מיני מדפסת טרמית מעוצבת לילדים\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪200\",\n",
" \"type\": \"Kids Thermal Printer\",\n",
" \"availability\": \"62 in stock (11 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Children Digital Camera Instant Print\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪250\",\n",
" \"type\": \"Photo Printing Camera with 32G Memory Card\",\n",
" \"availability\": \"160 in stock (3 variants)\"\n",
" }\n",
" ],\n",
" \"mini_printer_papers\": [\n",
" {\n",
" \"name\": \"HP Printer Paper 8.5x11, 500 Sheets\", # example only\n",
" \"brand\": \"HP\",\n",
" \"price\": \"$9.99\",\n",
" \"type\": \"Standard\",\n",
" \"availability\": \"In stock\"\n",
" },\n",
" {\n",
" \"name\": \"Mini Printer Paper 57*25mm Color Sticker\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪70\",\n",
" \"type\": \"Self-adhesive Color Label Paper\",\n",
" \"availability\": \"71,996 in stock (9 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"מדבקות שקופות למדפסת טרמית\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪55\",\n",
" \"type\": \"Transparent Labels\",\n",
" \"availability\": \"11,762 in stock (12 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"גלילי נייר מדבקה\",\n",
" \"brand\": \"Art-Tech\",\n",
" \"price\": \"₪40\",\n",
" \"type\": \"Sticker Paper Rolls\",\n",
" \"availability\": \"42 in stock (4 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Niimbot B21/B1/B3S Thermal Label Sticker Paper\",\n",
" \"brand\": \"Niimbot\",\n",
" \"price\": \"₪55\",\n",
" \"type\": \"Printable White Label Paper 2050mm\",\n",
" \"availability\": \"1,342 in stock (14 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"Mini Printer Sticker Paper 25X57mm\",\n",
" \"brand\": \"Paperang-compatible\",\n",
" \"price\": \"₪65\",\n",
" \"type\": \"Color Self-Adhesive Thermal Rolls\",\n",
" \"availability\": \"3,023 in stock (20 variants)\"\n",
" },\n",
" {\n",
" \"name\": \"3/5/10 NiiMBOT White Label Paper Rolls\",\n",
" \"brand\": \"Niimbot\",\n",
" \"price\": \"₪40\",\n",
" \"type\": \"Waterproof Self-adhesive Rolls\",\n",
" \"availability\": \"1,400 in stock (9 variants)\"\n",
" }\n",
" ]\n",
"}\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0696acb1-0b05-4dc2-80d5-771be04f1fb2",
"metadata": {},
"outputs": [],
"source": [
"# A global list of flight bookings\n",
"flight_bookings = []\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80ca4e09-6287-4d3f-997d-fa6afbcf6c85",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 3) Helper Functions for Art-Tech Store\n",
"###############################################################################\n",
"\n",
"product_orders = []\n",
"\n",
"def check_product_availability(category: str, filters: dict = None):\n",
" \"\"\"\n",
" Return list of products in the given category from 'product_availability'.\n",
" Optionally filter by brand, type, etc.\n",
" \"\"\"\n",
" print(f\"[TOOL] check_product_availability({category}, {filters=})\")\n",
" category = category.lower()\n",
" products = product_availability.get(category, [])\n",
" \n",
" if filters:\n",
" for key, val in filters.items():\n",
" products = [p for p in products if p.get(key, \"\").lower() == val.lower()]\n",
" return products\n",
"\n",
"\n",
"def add_to_cart_and_generate_receipt(customer_name: str, contact: str, product: dict, quantity: int, order_number: int):\n",
" \"\"\"\n",
" Create a text file: customerName_orderNumber.txt containing order details.\n",
" \"\"\"\n",
" safe_name = customer_name.replace(\" \", \"_\")\n",
" filename = f\"{safe_name}_{order_number}.txt\"\n",
"\n",
" content = (\n",
" \"Art-Tech Store Receipt\\n\"\n",
" \"=======================\\n\"\n",
" f\"Order # : {order_number}\\n\"\n",
" f\"Customer : {customer_name}\\n\"\n",
" f\"Contact : {contact}\\n\"\n",
" f\"Product : {product['name']}\\n\"\n",
" f\"Brand : {product['brand']}\\n\"\n",
" f\"Type : {product.get('type', 'N/A')}\\n\"\n",
" f\"Price : {product['price']}\\n\"\n",
" f\"Quantity : {quantity}\\n\"\n",
" f\"Availability: {product['availability']}\\n\"\n",
" )\n",
" with open(filename, \"w\") as f:\n",
" f.write(content)\n",
"\n",
" print(f\"[TOOL] Receipt file generated => {filename}\")\n",
" return filename\n",
"\n",
"\n",
"def place_order(category, product_index, quantity, customer_name, contact_info):\n",
" \"\"\"\n",
" Places an order for a product by index in the filtered list.\n",
" \"\"\"\n",
" print(f\"[TOOL] place_order({category=}, {product_index=}, {quantity=})\")\n",
"\n",
" try:\n",
" idx = int(product_index)\n",
" except ValueError:\n",
" return \"Error: Product option number is not a valid integer.\"\n",
"\n",
" products = product_availability.get(category.lower(), [])\n",
" if not products:\n",
" return f\"Error: No products found in category '{category}'.\"\n",
"\n",
" pick = idx - 1\n",
" if pick < 0 or pick >= len(products):\n",
" return f\"Error: Invalid product option #{idx} for category '{category}'.\"\n",
"\n",
" selected_product = products[pick]\n",
"\n",
" order = {\n",
" \"category\": category,\n",
" \"product\": selected_product[\"name\"],\n",
" \"brand\": selected_product[\"brand\"],\n",
" \"type\": selected_product.get(\"type\", \"\"),\n",
" \"price\": selected_product[\"price\"],\n",
" \"quantity\": quantity,\n",
" \"customer_name\": customer_name,\n",
" \"contact\": contact_info,\n",
" }\n",
" product_orders.append(order)\n",
"\n",
" order_number = len(product_orders)\n",
" receipt_filename = add_to_cart_and_generate_receipt(customer_name, contact_info, selected_product, quantity, order_number)\n",
"\n",
" confirmation = (\n",
" f\"Order #{order_number} confirmed for {customer_name}. \"\n",
" f\"{selected_product['name']} x{quantity}. Receipt saved to {receipt_filename}.\"\n",
" )\n",
" print(f\"[TOOL] {confirmation}\")\n",
" return confirmation\n",
"\n",
"\n",
"def generate_report():\n",
" \"\"\"\n",
" Summarize ALL orders in a single file called order_summary.txt.\n",
" \"\"\"\n",
" print(f\"[TOOL] generate_report called.\")\n",
"\n",
" report_content = \"Art-Tech Store Order Summary Report\\n\"\n",
" report_content += \"===================================\\n\"\n",
"\n",
" if not product_orders:\n",
" report_content += \"No orders found.\\n\"\n",
" else:\n",
" for i, order in enumerate(product_orders, start=1):\n",
" report_content += (\n",
" f\"Order # : {i}\\n\"\n",
" f\"Customer : {order['customer_name']}\\n\"\n",
" f\"Contact : {order['contact']}\\n\"\n",
" f\"Product : {order['product']}\\n\"\n",
" f\"Brand : {order['brand']}\\n\"\n",
" f\"Type : {order['type']}\\n\"\n",
" f\"Price : {order['price']}\\n\"\n",
" f\"Quantity : {order['quantity']}\\n\"\n",
" \"-------------------------\\n\"\n",
" )\n",
"\n",
" filename = \"order_summary.txt\"\n",
" with open(filename, \"w\") as f:\n",
" f.write(report_content)\n",
"\n",
" msg = f\"Summary report generated => {filename}\"\n",
" print(f\"[TOOL] {msg}\")\n",
" return msg\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39fb9008",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 4) Tools JSON Schemas for Art-Tech Store\n",
"###############################################################################\n",
"price_function = {\n",
" \"name\": \"get_product_price\",\n",
" \"description\": \"Get the price of a product (not strictly needed now).\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"category\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Product category such as 'mini_printers' or 'mini_printer_papers'.\",\n",
" },\n",
" \"product_name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Name of the product to check price for.\",\n",
" },\n",
" },\n",
" \"required\": [\"category\", \"product_name\"],\n",
" },\n",
"}\n",
"\n",
"availability_function = {\n",
" \"name\": \"check_product_availability\",\n",
" \"description\": (\n",
" \"Check availability of products in a category. \"\n",
" \"Returns a list of {name, brand, price, type, availability}.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"category\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Category of products to check (e.g., 'mini_printers').\",\n",
" },\n",
" \"filters\": {\n",
" \"type\": \"object\",\n",
" \"description\": \"Optional filters like brand or type.\",\n",
" },\n",
" },\n",
" \"required\": [\"category\"],\n",
" },\n",
"}\n",
"\n",
"book_function = {\n",
" \"name\": \"place_order\",\n",
" \"description\": (\n",
" \"Place an order using an index for the chosen product. \"\n",
" \"Generates a unique receipt file customerName_{orderNumber}.txt.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"category\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Product category (e.g., 'mini_printers').\",\n",
" },\n",
" \"product_index\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"1-based index of selected product from availability list.\",\n",
" },\n",
" \"quantity\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Quantity to order.\",\n",
" },\n",
" \"customer_name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Customer's full name.\",\n",
" },\n",
" \"contact_info\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Customer's contact information (email or phone).\",\n",
" },\n",
" },\n",
" \"required\": [\"category\", \"product_index\", \"quantity\", \"customer_name\", \"contact_info\"],\n",
" },\n",
"}\n",
"\n",
"report_function = {\n",
" \"name\": \"generate_report\",\n",
" \"description\": (\n",
" \"Generates a summary report of ALL orders in order_summary.txt.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {},\n",
" \"required\": [],\n",
" },\n",
"}\n",
"\n",
"tools = [\n",
" {\"type\": \"function\", \"function\": price_function},\n",
" {\"type\": \"function\", \"function\": availability_function},\n",
" {\"type\": \"function\", \"function\": book_function},\n",
" {\"type\": \"function\", \"function\": report_function},\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f003836",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 5) Handle Tool Calls for Art-Tech Store\n",
"###############################################################################\n",
"def handle_tool_call(message):\n",
" \"\"\"\n",
" The LLM can request to call a function in 'tools'. We parse the JSON arguments\n",
" and run the corresponding Python function. Then we return a 'tool' message with the result.\n",
" \"\"\"\n",
" tool_call = message.tool_calls[0]\n",
" fn_name = tool_call.function.name\n",
" args = json.loads(tool_call.function.arguments)\n",
"\n",
" if fn_name == \"get_product_price\":\n",
" category = args.get(\"category\")\n",
" product_name = args.get(\"product_name\")\n",
" products = product_availability.get(category.lower(), [])\n",
" price = \"Not found\"\n",
" for p in products:\n",
" if p[\"name\"].lower() == product_name.lower():\n",
" price = p[\"price\"]\n",
" break\n",
" response_content = {\"category\": category, \"product_name\": product_name, \"price\": price}\n",
"\n",
" elif fn_name == \"check_product_availability\":\n",
" category = args.get(\"category\")\n",
" filters = args.get(\"filters\", {})\n",
" products = check_product_availability(category, filters)\n",
" response_content = {\"category\": category, \"availability\": products}\n",
"\n",
" elif fn_name == \"place_order\":\n",
" category = args.get(\"category\")\n",
" product_index = args.get(\"product_index\")\n",
" quantity = args.get(\"quantity\")\n",
" customer_name = args.get(\"customer_name\")\n",
" contact_info = args.get(\"contact_info\")\n",
"\n",
" confirmation = place_order(category, product_index, quantity, customer_name, contact_info)\n",
" response_content = {\n",
" \"category\": category,\n",
" \"product_index\": product_index,\n",
" \"quantity\": quantity,\n",
" \"customer_name\": customer_name,\n",
" \"contact_info\": contact_info,\n",
" \"confirmation\": confirmation,\n",
" }\n",
"\n",
" elif fn_name == \"generate_report\":\n",
" msg = generate_report()\n",
" response_content = {\"report\": msg}\n",
"\n",
" else:\n",
" response_content = {\"error\": f\"Unknown tool: {fn_name}\"}\n",
"\n",
" return {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps(response_content),\n",
" \"tool_call_id\": tool_call.id,\n",
" }, args\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f6b34b32",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 6) Main Chat Function for Art-Tech Store\n",
"###############################################################################\n",
"def chat(message, history):\n",
" \"\"\"\n",
" The main chat loop that handles the conversation with the user,\n",
" passing 'tools' definitions to the LLM for function calling.\n",
" \"\"\"\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" try:\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages,\n",
" tools=tools\n",
" )\n",
"\n",
" # If the LLM requests a function call, handle it\n",
" while response.choices[0].finish_reason == \"tool_calls\":\n",
" msg = response.choices[0].message\n",
" print(f\"[INFO] Tool call requested: {msg.tool_calls[0]}\")\n",
" tool_response, tool_args = handle_tool_call(msg)\n",
" print(f\"[INFO] Tool response: {tool_response}\")\n",
"\n",
" # Add both the LLM's request and our tool response to the conversation\n",
" messages.append(msg)\n",
" messages.append(tool_response)\n",
"\n",
" # Re-send updated conversation to get final or next step\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages\n",
" )\n",
"\n",
" # Return normal text response (finish_reason = \"stop\")\n",
" return response.choices[0].message.content\n",
"\n",
" except Exception as e:\n",
" print(f\"[ERROR] {e}\")\n",
" return \"I'm sorry, something went wrong while processing your request.\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cea4b097",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 7) Launch Gradio\n",
"###############################################################################\n",
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b39d5a6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,395 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "7df20212-bfa8-4b2b-aec5-7b562dd15ee8",
"metadata": {},
"source": [
"### Issue with Gradio when using Claude for Conversational AI (chatbots)\n",
"\n",
"As explained in Day 3 (notebook), Gradio has been upgraded to pass in history in a format OpenAI accepts.\n",
"\n",
"This update simplifies the development work as Gradio manages the history in the background and provides a history structure that matches OpenAi. Fortunately, this works with other models that leverage the client libraries for OpenAI, such as Llama and Gemini. \n",
"\n",
"However, leveraging Gradio's ChatInterface while using Claude models generates a BadRequest error. \n",
"\n",
"In analyzing the history list from Gradio, it has the following format:\n",
"\n",
"`{'role': 'assistant', 'metadata': None, 'content': '[assistant message here]', 'options': None}`\n",
"\n",
"OpenAi accepts this format without issues, as do other models such as Llama and Gemini - at least while leveraging the client libraries for OpenAI. They accept both formats. \n",
"\n",
"However, Claude's API requires the following format:\n",
"\n",
"`{'role': 'user', 'content': '[user message here]'}`\n",
"\n",
"Claude rejects anything different from this format.\n",
"\n",
"Run the code below to get the details! "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5398289-f4d0-4317-b9c2-fff06c4bbfec",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import google.generativeai\n",
"import anthropic\n",
"import gradio as gr\n",
"from pprint import pprint # for a nicely formatted printout of a list"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76df1fb5-76f8-4b52-afbb-12b892208b50",
"metadata": {},
"outputs": [],
"source": [
"# set environment variables for OpenAi\n",
"load_dotenv(override=True)\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"\n",
"# validate API Key\n",
"if not openai_api_key:\n",
" raise ValueError(\"No OpenAI API key was found! Please check the .env file.\")\n",
"else:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"\n",
"if not anthropic_api_key:\n",
" raise ValueError(\"No Anthropic API key was found! Please check the .env file.\")\n",
"else:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"\n",
"if not google_api_key:\n",
" raise ValueError(\"No Gemini API key was found! Please check the .env file.\")\n",
"else:\n",
" print(f\"Gemini API Key exists and begins {google_api_key[:8]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2429366-a9ab-4e72-a651-56f17b779cf4",
"metadata": {},
"outputs": [],
"source": [
"# constants\n",
"MODELS = { 'GPT': 'gpt-4o-mini', \n",
" 'LLAMA': 'llama3.2', \n",
" # 'DEEPSEEK': 'deepseek-r1:1.5b',\n",
" 'CLAUDE': 'claude-3-haiku-20240307',\n",
" 'GEMINI': 'gemini-2.0-flash-exp'\n",
" }\n",
"\n",
"CLIENTS = { 'GPT': OpenAI(), \n",
" 'LLAMA': OpenAI(base_url='http://localhost:11434/v1', api_key='ollama'),\n",
" # 'DEEPSEEK': OpenAI(base_url='http://localhost:11434/v1', api_key='ollama'),\n",
" 'CLAUDE': anthropic.Anthropic(),\n",
" 'GEMINI': OpenAI(base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\", api_key=google_api_key)\n",
" }\n",
"\n",
"# system prompt\n",
"system_message = \"You are a nice assistant that like to chat with users\"\n",
"\n",
"# to save/print the history structure\n",
"console = []"
]
},
{
"cell_type": "markdown",
"id": "bb34616a-2304-4a56-af3a-8d353629722b",
"metadata": {},
"source": [
"#### Testing GPT\n",
"\n",
"This runs without issues. You may change the model to Llama or Gemini for further testing."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e69eb7a-50cf-478c-83f6-4ce923a9c53a",
"metadata": {},
"outputs": [],
"source": [
"def gpt_chat(message, history):\n",
" # change to Llama or Gemini for test\n",
" model = 'GPT'\n",
"\n",
" # add system message to history\n",
" if not history:\n",
" history.append({\"role\": \"system\", \"content\": system_message})\n",
"\n",
" # conversation including new user message\n",
" messages = history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" # send request to OpenAi\n",
" response = CLIENTS[model].chat.completions.create(\n",
" model = MODELS[model],\n",
" max_tokens = 200,\n",
" messages = messages,\n",
" )\n",
"\n",
" # save history structure\n",
" global console\n",
" console = history[:]\n",
" \n",
" # post in Gradio's chat interface\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "markdown",
"id": "20444651-440c-4b6d-996d-87a21c28f28e",
"metadata": {},
"source": [
"##### Have a conversation of several messages (3 or more)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "90dac8e6-e1ef-455a-94e9-06735e6c17fb",
"metadata": {},
"outputs": [],
"source": [
"# Gradio with GPT\n",
"gr.ChatInterface(fn=gpt_chat, type=\"messages\", examples=[\"How are you today?\", \"Please, tell me a joke.\"]).launch()"
]
},
{
"cell_type": "markdown",
"id": "b4048069-b712-491a-96d5-f8fd9f0fe533",
"metadata": {},
"source": [
"##### Notice how the history structure includes both formats, and this is okay with OpenAi"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f8acbde-f5c3-4890-af5b-69a24ded9b45",
"metadata": {},
"outputs": [],
"source": [
"# audit console\n",
"pprint(console)\n",
"\n",
"# empty list\n",
"console.clear()"
]
},
{
"cell_type": "markdown",
"id": "deaeb8a0-266a-4b5d-8536-e5c59391baa5",
"metadata": {},
"source": [
"#### Testing with Claude\n",
"\n",
"This first test will generate a BadRequest error on the second message from the user. The first message sent follows the required format preventing errors."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "268e662d-84f5-4b09-8472-c3545a04f2a4",
"metadata": {},
"outputs": [],
"source": [
"# Chat using Gradio's history - generates error after the second message from user\n",
"def claude_chat(message, history): # Gradio requires the history parameter, but it goes unused. \n",
" \n",
" # save history structure\n",
" global console\n",
" console = history[:]\n",
" \n",
" model = 'CLAUDE'\n",
"\n",
" # conversation including new user message - this is why the first message does not generate an error\n",
" messages = history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" # send the request to Claude\n",
" response = CLIENTS[model].messages.create(\n",
" model = MODELS[model],\n",
" max_tokens = 200,\n",
" system = system_message,\n",
" messages = messages,\n",
" )\n",
" \n",
" # post in Gradio's chat interface\n",
" return response.content[0].text"
]
},
{
"cell_type": "markdown",
"id": "5fd5e221-ab81-4567-a14d-5638d9919a40",
"metadata": {},
"source": [
"##### Have a conversation of several messages (3 or more)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fbd1a4b2-afcf-4345-bf1a-283c16076e07",
"metadata": {},
"outputs": [],
"source": [
"# Gradio with Claude\n",
"gr.ChatInterface(fn=claude_chat, type=\"messages\", examples=[\"How are you today?\", \"Please, tell me a joke.\"]).launch()"
]
},
{
"cell_type": "markdown",
"id": "834bf737-b3d3-4225-be12-f493f3490f16",
"metadata": {},
"source": [
"##### Notice how the history structure changes for the second message. This cause a BadRequest error with Claude."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6cfa123-83d5-46d8-ac33-74fd9cae0ab3",
"metadata": {},
"outputs": [],
"source": [
"# audit console\n",
"pprint(console)\n",
"\n",
"# empty list\n",
"console.clear()"
]
},
{
"cell_type": "markdown",
"id": "c0b5c4e0-7544-4279-8006-fde4be9c656f",
"metadata": {},
"source": [
"##### Have a new conversation of several messages (3 or more), but this time leveraging a local history repository instead of Gradio's. \n",
"\n",
"The code leverages the list `console` as the local repository. Note that Gradio still requires the second parameter (history) is required even though it is not used. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c50326e-8496-499a-81d9-28b883ac9b6b",
"metadata": {},
"outputs": [],
"source": [
"def claude_chat(message, history): # Gradio requires the history parameter, but it goes unused. \n",
" # local history repository instead of Gradio's \n",
" global console\n",
" model = 'CLAUDE'\n",
"\n",
" # append new user message to history - using Claude's required format\n",
" console.append({\"role\": \"user\", \"content\": message})\n",
"\n",
" # send the request to Claude\n",
" response = CLIENTS[model].messages.create(\n",
" model = MODELS[model],\n",
" max_tokens = 200,\n",
" system = system_message,\n",
" messages = console, # use local history repository\n",
" )\n",
"\n",
" # append the assistant response to history - using Claude's required format\n",
" console.append({\"role\": \"assistant\", \"content\": response.content[0].text})\n",
"\n",
" # post in Gradio's chat interface\n",
" return response.content[0].text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f19621f5-8dd6-4a88-9a21-9aab04a9dd90",
"metadata": {},
"outputs": [],
"source": [
"# Gradio with Claude\n",
"gr.ChatInterface(fn=claude_chat, type=\"messages\", examples=[\"How are you today?\", \"Please, tell me a joke.\"]).launch()"
]
},
{
"cell_type": "markdown",
"id": "d6c33236-0d2f-4341-b9ec-e3d7b3eeaec6",
"metadata": {},
"source": [
"##### Notice that the history structure follows the required format."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd5dd244-abc7-4654-97d2-c140b2a934d2",
"metadata": {},
"outputs": [],
"source": [
"# audit console\n",
"pprint(console)\n",
"\n",
"# empty list\n",
"console.clear()"
]
},
{
"cell_type": "markdown",
"id": "48fc0b5f-09de-413f-a809-7bbba5a1ae3e",
"metadata": {},
"source": [
"### Conclusion\n",
"\n",
"1. OpenAi (and models that leverage the client libraries for OpenAI) supports both formats for the conversation history.\n",
"\n",
" (Gradio's) `{'role': 'assistant', 'metadata': None, 'content': '[assistant message here]', 'options': None}`\n",
"\n",
" (Claude's) `{'role': 'user', 'content': '[user message here]'}`\n",
" \n",
"2. Claude only supports the following format for the conversation history:\n",
"\n",
" (Claude's) `{'role': 'user', 'content': '[user message here]'}`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "913b171a-8b3e-43e4-ac38-cedf2c4cdedf",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,290 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "3f9b483c-f410-4ad3-8f3a-e33527f30f8a",
"metadata": {
"panel-layout": {
"height": 68.2639,
"visible": true,
"width": 100
}
},
"source": [
"# Project - Laptops Assistant\n",
"\n",
"A simple inventory tool integrated with Anthropic API"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfaff08d-f6e5-4d2d-bfb8-76c154836f3d",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"import anthropic\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a04047ea-d01b-469b-93ce-ab4f4e36ca1e",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv(override=True)\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"\n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5e00ced-f47b-4713-8174-7901e1a69881",
"metadata": {},
"outputs": [],
"source": [
"# Connect to OpenAI, Anthropic and Google; comment out the Claude or Google lines if you're not using them\n",
"\n",
"claude = anthropic.Anthropic()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c715efd-cebf-4dc2-8c99-798f3179dd21",
"metadata": {},
"outputs": [],
"source": [
"MODEL = \"claude-3-haiku-20240307\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b029d1d-9199-483a-94b7-893680af8ad1",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant for an Inventory Sales called InvAI. \"\n",
"system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ca1197c-e6a1-4579-96c6-24e8e305cc72",
"metadata": {},
"outputs": [],
"source": [
"laptop_items = [\n",
" {\n",
" \"model\": \"Aspire 3 A315-59-570Z OPI Pure Silver\", \n",
" \"brand\": \"Acer\",\n",
" \"price\": \"$595.96\"\n",
" },\n",
" {\n",
" \"model\": \"Aspire Lite 14 AL14-31P-36BE Pure Silver\", \n",
" \"brand\": \"Acer\",\n",
" \"price\": \"$463.52\"\n",
" },\n",
" {\n",
" \"model\": \"Raider 18 HX\",\n",
" \"brand\": \"MSI\",\n",
" \"price\": \"$235.25\"\n",
" }\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d2bc76b-c1d0-4b3d-a299-9972f7687e4c",
"metadata": {},
"outputs": [],
"source": [
"def get_laptop_price(model):\n",
" print(f\"Tool get_laptop_price called for laptop model {model}\")\n",
" laptop_model = model.lower()\n",
" for item in laptop_items:\n",
" if laptop_model in item.get(\"model\").lower():\n",
" return item\n",
" return \"Unknown\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afc9b4a3-3a6f-4839-bebc-89bd598394fd",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# get_laptop_price(\"Lite 14 AL14-31P-36BE Pure SilveR\")\n",
"\n",
"get_laptop_price(\"Aspire Lite 14\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12190074-fad8-43f6-8be1-f96a08c16b59",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_laptop_price\",\n",
" \"description\": (\n",
" \"Returns the laptop's price, brand, and exact model from a given query.\"\n",
" \"Use when the user asks about a laptop's price, e.g.,\"\n",
" \"'How much is this laptop?' → 'The Acer Aspire Lite 14 AL14-31P-36BE Pure Silver is priced at $463.52.'\"\n",
" ),\n",
" \"input_schema\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"model\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The model name of the laptop the customer is asking about.\"\n",
" }\n",
" },\n",
" \"required\": [\"model\"]\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "475195e1-dd78-45ba-af6d-16d7cf5c85ae",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [price_function]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3834314d-fd37-4e27-9511-bd519389b31b",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" print(history)\n",
" messages = [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" for history_message in history:\n",
" if history_message[\"role\"] == \"user\":\n",
" messages.append({\"role\": \"user\", \"content\": history_message[\"content\"]})\n",
" \n",
" response = claude.messages.create(model=MODEL, messages=messages, tools=tools, max_tokens=500)\n",
"\n",
" if len(response.content) > 1:\n",
" assistant, user, laptop_model = handle_tool_call(response)\n",
" messages.append(assistant)\n",
" messages.append(user)\n",
" response = claude.messages.create(model=MODEL, messages=messages, tools=tools, max_tokens=500)\n",
"\n",
"\n",
" return response.content[0].text"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "745a9bf8-6ceb-4c1c-bfbf-b0d1f3d5d6fc",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call(message):\n",
" # laptop_model = message\n",
" laptop_model = message.content[1].input.get(\"model\")\n",
" laptop_item = get_laptop_price(laptop_model)\n",
" assistant = {\n",
" \"role\": \"assistant\",\n",
" \"content\": [\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": message.content[0].text\n",
" },\n",
" {\n",
" \"type\": \"tool_use\",\n",
" \"id\": message.content[1].id,\n",
" \"name\": message.content[1].name,\n",
" \"input\": message.content[1].input\n",
" }\n",
" ]\n",
" }\n",
" user = {\n",
" \"role\": \"user\",\n",
" \"content\": [\n",
" {\n",
" \"type\": \"tool_result\",\n",
" \"tool_use_id\": message.content[1].id,\n",
" # \"content\": laptop_item.get(\"price\")\n",
" \"content\": json.dumps(laptop_item)\n",
" }\n",
" ]\n",
" }\n",
" \n",
"\n",
" return assistant, user, laptop_model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9408eeb4-d07b-4193-92cd-197610ed942e",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.7"
},
"panel-cell-order": [
"3f9b483c-f410-4ad3-8f3a-e33527f30f8a"
]
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,293 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e3a5643a-c247-4a9b-8c57-ec9b1e89c088",
"metadata": {},
"source": [
"# Week 2 - eCommerce Assistant for products price from dictionary\n",
"\n",
"An eCommerce assitant that can get a product price\n",
"\n",
"Gradio for chat box"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "756573b3-72b2-4102-a773-91c278e5c4fd",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# !ollama pull llama3.2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57ae8d30-f7aa-47a3-bab8-b7002e87a8f7",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef7f8b2b-1d6a-4bbd-858a-be187ccfc02a",
"metadata": {},
"outputs": [],
"source": [
"# Initialization\n",
"\n",
"load_dotenv(override=True)\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"MODEL = \"gpt-4o-mini\"\n",
"openai = OpenAI()\n",
"\n",
"# As an alternative, if you'd like to use Ollama instead of OpenAI\n",
"# Check that Ollama is running for you locally (see week1/day2 exercise) then uncomment these next 2 lines\n",
"# MODEL = \"llama3.2\"\n",
"# openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b46dd52-8a3c-42d1-ac24-59f5eb5aaba1",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant for an online store called CommerceAI. \"\n",
"system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31a6431b-215d-4f46-b813-971d8af7e034",
"metadata": {},
"outputs": [],
"source": [
"# This function looks rather simpler than the one from my video, because we're taking advantage of the latest Gradio updates\n",
"\n",
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
" return response.choices[0].message.content\n",
"\n",
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"id": "d3586bfb-acc3-4b5e-95be-02120b696f98",
"metadata": {},
"source": [
"## Tools\n",
"\n",
"Tools are an incredibly powerful feature provided by the frontier LLMs.\n",
"\n",
"With tools, you can write a function, and have the LLM call that function as part of its response.\n",
"\n",
"Sounds almost spooky.. we're giving it the power to run code on our machine?\n",
"\n",
"Well, kinda."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9ac43e8-9880-44f6-b03a-6d1ab05bbb94",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"\n",
"items_prices = {f\"item{i}\": f\"{i*100}\" for i in range(1,6)}\n",
"\n",
"items_prices = {\"printer\": \"$500\", \"paper\": \"$10\", \"mini printer\": \"$50\", \"label printer\": \"$60\", \"sticker-paper\": \"$5\"}\n",
"\n",
"def get_item_price(product):\n",
" print(f\"Tool get_item_price called for {product}\")\n",
" item = product.lower()\n",
" return items_prices.get(item, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88d249f7-787d-4750-b5b9-7df108da1b57",
"metadata": {},
"outputs": [],
"source": [
"items_prices"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef3c3897-9a57-4f04-b5d0-f9ac8bb02d00",
"metadata": {},
"outputs": [],
"source": [
"get_item_price(\"mini printer\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "511ef9b8-bec0-4f14-b647-057e14c849cc",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_item_price\",\n",
" \"description\": \"Get the price of an item in the store. \\\n",
" Call this whenever you need to know the store item price , \\\n",
" for example when a customer asks 'How much is a mini printer' \",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"product\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The item that the customer wants to buy\"\n",
" },\n",
" },\n",
" \"required\": [\"product\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86f674a4-4b7c-443d-b025-0f016932508a",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [{\"type\": \"function\", \"function\": price_function}]"
]
},
{
"cell_type": "markdown",
"id": "724d0f89-8a86-493e-8cd1-73814688a70b",
"metadata": {},
"source": [
"## Getting OpenAI to use our Tool\n",
"\n",
"There's some fiddly stuff to allow OpenAI \"to call our tool\"\n",
"\n",
"What we actually do is give the LLM the opportunity to inform us that it wants us to run the tool.\n",
"\n",
"Here's how the new chat function looks:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d67fb72-132e-499e-9931-86cb71b634b6",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
"\n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" response, item = handle_tool_call(message)\n",
" print('response', response, 'item', item)\n",
" messages.append(message)\n",
" messages.append(response)\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
" \n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b4de767-954a-4077-a5f7-0055a0b90393",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call(message):\n",
" tool_call = message.tool_calls[0]\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" item = arguments.get('product') \n",
" print('product', item)\n",
" price = get_item_price(item)\n",
" response = {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"item\": item,\"price\": price}),\n",
" \"tool_call_id\": tool_call.id\n",
" }\n",
" return response, item\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ffda702-6ac5-4d13-9703-a14fa93aea68",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ae2edbf-de58-43fa-b380-267cfc1755de",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,107 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## codeXchange AI: Transform Code with a Click!\n",
"\n",
"**Created by Blaise Alako**\n",
"\n",
"Get ready to revolutionize your coding experience with **codeXchange AI**—a web-based Gradio app that converts code between programming languages in a flash! Powered by cutting-edge frontier and open-source LLMs, this tool is a game-changer for beginners diving into new languages, intermediates streamlining projects, and advanced users pushing the limits of innovation. Just paste or upload your code, choose your target language, and watch the magic unfold!\n",
"\n",
"**Why codeXchange AI?**\n",
"- **Effortless**: No downloads—just pure web-based magic.\n",
"- **Brilliant**: AI-driven conversions that nail accuracy.\n",
"- **Adaptable**: Add new languages or models with ease.\n",
"\n",
"Explore the source code [codeXchange AI](https://github.com/alakob/ai_code_converter) and experience the thrill!\n",
"\n",
"---\n",
"\n",
"### Table of Contents\n",
"1. [Explore the Interface](#explore-the-interface)\n",
"2. [Upload and Convert](#upload-and-convert)\n",
"3. [See the Results](#see-the-results)\n",
"4. [Unleash Advanced Features](#unleash-advanced-features)\n",
"5. [Performance That Wows](#performance-that-wows)\n",
"6. [Get Started Now](#get-started-now)\n",
"\n",
"---\n",
"\n",
"### Explore the Interface\n",
"\n",
"#### A Sleek Starting Point\n",
"Step into the world of codeXchange AI with its stunningly simple interface, designed to make your coding journey a breeze!\n",
"\n",
"![Initial Interface](screenshots/codeXchange_1.png) \n",
"*Screenshot: The apps clean starting screen, ready for your code.*\n",
"\n",
"With options to upload files or pick example snippets, youre just a click away from transforming your code.\n",
"\n",
"---\n",
"\n",
"### Upload and Convert\n",
"\n",
"#### Load Your Code with Ease\n",
"Whether youre a beginner or a pro, uploading your code is a snap. Drag and drop a file, or select a preloaded snippet to kick things off.\n",
"\n",
"![Loading Code](screenshots/codeXchange_2.png) \n",
"*Screenshot: The upload section with a dropdown for example snippets.*\n",
"\n",
"Choose your input language, pick your target, and hit “Convert”—its that easy to bridge the language gap!\n",
"\n",
"---\n",
"\n",
"### See the Results\n",
"\n",
"#### Witness the Transformation\n",
"Watch codeXchange AI work its magic! It converts your code with precision, adding helpful documentation to make the output crystal clear.\n",
"\n",
"![Conversion Output](screenshots/codeXchange_3.png) \n",
"*Screenshot: A converted result with documentation, ready to run.*\n",
"\n",
"From Python to C++ or beyond, the app ensures your code is ready to shine in its new language.\n",
"\n",
"---\n",
"\n",
"### Unleash Advanced Features\n",
"\n",
"#### Power Up Your Workflow\n",
"For those who love to tinker, codeXchange AI offers exciting customization! Select different models, adjust the “Temperature” for creative flair, and even add new languages to the mix.\n",
"\n",
"![Advanced Options](screenshots/codeXchange_3_1.png) \n",
"*Screenshot: Interface showcasing model selection, temperature slider, and more.*\n",
"\n",
"Download your converted code with a single click and take your projects to the next level!\n",
"\n",
"---\n",
"\n",
"### Performance That Wows\n",
"\n",
"#### Speed That Impresses\n",
"codeXchange AI doesnt just convert—it optimizes! Check out the performance boost when running your code in a new language, with execution times thatll leave you amazed.\n",
"\n",
"![Performance Results](screenshots/codeXchange_4.png) \n",
"*Screenshot: Execution results highlighting speed improvements.*\n",
"\n",
"From 31.49 seconds in Python to just 2.32 seconds in C++—see the difference for yourself!\n",
"\n",
"---\n",
"\n",
"### Get Started Now\n",
"\n",
"Ready to transform your coding game? Jump into [codeXchange AI source code](https://github.com/alakob/ai_code_converter) Convert, run, and download your code in seconds. Whether youre just starting out, managing complex projects, or innovating at an advanced level, this app is your ultimate coding companion.\n",
"\n",
"---\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 182 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

View File

@@ -0,0 +1,394 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "de352746-564c-4b33-b1ad-0b449988c448",
"metadata": {},
"source": [
"# Perl to Python Code Generator\n",
"\n",
"The requirement: use a Frontier model to generate high performance Python code from Perl code\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import io\n",
"import sys\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import google.generativeai\n",
"import anthropic\n",
"from IPython.display import Markdown, display, update_display\n",
"import gradio as gr\n",
"import subprocess\n",
"import requests\n",
"import json\n",
"#for Hugging face end points\n",
"from huggingface_hub import login, InferenceClient\n",
"from transformers import AutoTokenizer"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4f672e1c-87e9-4865-b760-370fa605e614",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Note: Environment variable`HF_TOKEN` is set and is the current active token independently from the token you've just configured.\n"
]
}
],
"source": [
"# environment\n",
"\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')\n",
"##for connecting to HF End point\n",
"hf_token = os.environ['HF_TOKEN']\n",
"login(hf_token, add_to_git_credential=True)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
"metadata": {},
"outputs": [],
"source": [
"# initialize\n",
"# NOTE - option to use ultra-low cost models by uncommenting last 2 lines\n",
"\n",
"openai = OpenAI()\n",
"claude = anthropic.Anthropic()\n",
"OPENAI_MODEL = \"gpt-4o\"\n",
"CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
"\n",
"# Want to keep costs ultra-low? Uncomment these lines:\n",
"OPENAI_MODEL = \"gpt-4o-mini\"\n",
"CLAUDE_MODEL = \"claude-3-haiku-20240307\"\n",
"\n",
"#To access open source models from Hugging face end points\n",
"code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
"code_gemma = \"google/codegemma-7b-it\"\n",
"CODE_QWEN_URL = \"https://h1vdol7jxhje3mpn.us-east-1.aws.endpoints.huggingface.cloud\"\n",
"CODE_GEMMA_URL = \"https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud\""
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "6896636f-923e-4a2c-9d6c-fac07828a201",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are an assistant that reimplements Perl scripts code into a high performance Python for a Windows 11 PC. \"\n",
"system_message += \"Respond only with Python code; use comments sparingly and do not provide any explanation other than occasional # comments. \"\n",
"system_message += \"The Python response needs to produce an identical output in the fastest possible time.\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
"metadata": {},
"outputs": [],
"source": [
"def user_prompt_for(perl):\n",
" user_prompt = \"Rewrite this Perl scripts code in C++ with the fastest possible implementation that produces identical output in the least time. \"\n",
" user_prompt += \"Respond only with Python code; do not explain your work other than a few comments. \"\n",
" user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary python libraries as needed,\\\n",
" such as requests, os, json etc.\\n\\n\"\n",
" user_prompt += perl\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
"metadata": {},
"outputs": [],
"source": [
"def messages_for(perl):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_message},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(perl)}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
"metadata": {},
"outputs": [],
"source": [
"# write to a file called script.py\n",
"\n",
"def write_output(python):\n",
" code = python.replace(\"```python\",\"\").replace(\"```\",\"\")\n",
" output_file = \"script.py\"\n",
" with open(output_file, \"w\") as f:\n",
" f.write(code)\n",
" return output_file"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
"metadata": {},
"outputs": [],
"source": [
"def stream_gpt(perl): \n",
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(perl), stream=True)\n",
" reply = \"\"\n",
" for chunk in stream:\n",
" fragment = chunk.choices[0].delta.content or \"\"\n",
" reply += fragment\n",
" cleaned_reply = reply.replace('```python\\n','').replace('```','')\n",
" yield cleaned_reply, None\n",
" yield cleaned_reply, write_output(cleaned_reply)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8669f56b-8314-4582-a167-78842caea131",
"metadata": {},
"outputs": [],
"source": [
"def stream_claude(perl):\n",
" result = claude.messages.stream(\n",
" model=CLAUDE_MODEL,\n",
" max_tokens=2000,\n",
" system=system_message,\n",
" messages=[{\"role\": \"user\", \"content\": user_prompt_for(perl)}],\n",
" )\n",
" reply = \"\"\n",
" with result as stream:\n",
" for text in stream.text_stream:\n",
" reply += text\n",
" cleaned_reply = reply.replace('```python\\n','').replace('```','')\n",
" yield cleaned_reply, None\n",
" yield cleaned_reply, write_output(cleaned_reply)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5b166afe-741a-4711-bc38-626de3538ea2",
"metadata": {},
"outputs": [],
"source": [
"def stream_code_qwen(python):\n",
" tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
" messages = messages_for(python)\n",
" text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
" client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
" stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
" result = \"\"\n",
" for r in stream:\n",
" result += r.token.text\n",
" cleaned_reply = result.replace('```python\\n','').replace('```','')\n",
" yield cleaned_reply, None\n",
" yield cleaned_reply, write_output(cleaned_reply) "
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
"metadata": {},
"outputs": [],
"source": [
"def generate(perl_script, model):\n",
" if model==\"GPT\":\n",
" for result, file in stream_gpt(perl_script):\n",
" yield result, file\n",
" yield result, file\n",
" elif model==\"Claude\":\n",
" for result, file in stream_claude(perl_script):\n",
" yield result, file\n",
" yield result, file\n",
" elif model==\"CodeQwen\":\n",
" for result, file in stream_code_qwen(perl_script):\n",
" yield result, file\n",
" yield result, file\n",
" else:\n",
" raise ValueError(\"Unknown model\")\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "aa8e9a1c-9509-4056-bd0b-2578f3cc3335",
"metadata": {},
"outputs": [],
"source": [
"def execute_perl(perl_code):\n",
"\n",
" import subprocess\n",
" #print(perl_file)\n",
" perl_path = r\"E:\\Softwares\\Perl\\perl\\bin\\perl.exe\"\n",
" # Run Perl script from Jupyter Lab\n",
" result = subprocess.run([perl_path, '-e', perl_code], capture_output=True, text=True)\n",
"\n",
" # Return the output of the Perl script\n",
" return result.stdout\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "01e9d980-8830-4421-8753-a065dcbea1ed",
"metadata": {},
"outputs": [],
"source": [
"def execute_python(code):\n",
" try:\n",
" output = io.StringIO()\n",
" sys.stdout = output\n",
" exec(code)\n",
" finally:\n",
" sys.stdout = sys.__stdout__\n",
" return output.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "ed4e0aff-bfde-440e-8e6b-eb3c7143837e",
"metadata": {},
"outputs": [],
"source": [
"css = \"\"\"\n",
".perl {background-color: #093645;}\n",
".python {background-color: #0948;}\n",
"\"\"\"\n",
"\n",
"force_dark_mode = \"\"\"\n",
"function refresh() {\n",
" const url = new URL(window.location);\n",
" if (url.searchParams.get('__theme') !== 'dark') {\n",
" url.searchParams.set('__theme', 'dark');\n",
" window.location.href = url.href;\n",
" }\n",
"}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "caaee54d-79db-4db3-87df-2e7d2eba197c",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7861/\" 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": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"with gr.Blocks(css=css, js=force_dark_mode) as ui:\n",
"\n",
" gr.HTML(\"<h2 style='text-align: center; color: white;'> PERL to Python Code Generator</h2>\")\n",
" with gr.Row(scale=0, equal_height=True):\n",
" model = gr.Dropdown([\"GPT\", \"Claude\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
" perl_file = gr.File(label=\"Upload Perl Script:\")\n",
" convert = gr.Button(\"Convert to Python\")\n",
" file_output = gr.File(label=\"Download Python script\", visible=False)\n",
" with gr.Row():\n",
" perl_script = gr.Textbox(label=\"Perl Script:\")\n",
" python_script = gr.Textbox(label=\"Converted Python Script:\") \n",
" with gr.Row():\n",
" perl_run = gr.Button(\"Run PERL\")\n",
" python_run = gr.Button(\"Run Python\")\n",
" with gr.Row():\n",
" perl_out = gr.TextArea(label=\"PERL result:\", elem_classes=[\"perl\"])\n",
" python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
" with gr.Row(): \n",
" clear_button = gr.Button(\"Clear\")\n",
" \n",
" def extract_perl_code(file):\n",
" if file is None:\n",
" return \"No file uploaded.\", None \n",
" with open(file.name, \"r\", encoding=\"utf-8\") as f:\n",
" perl_code = f.read()\n",
" return perl_code\n",
"\n",
" convert.click(extract_perl_code, inputs=[perl_file], outputs=[perl_script]).then(\n",
" generate, inputs=[perl_script, model], outputs=[python_script, file_output]).then(\n",
" lambda file_output: gr.update(visible=True), inputs=[file_output], outputs=[file_output]\n",
" )\n",
"\n",
" perl_run.click(execute_perl, inputs=[perl_script], outputs=[perl_out])\n",
" python_run.click(execute_python, inputs=[python_script], outputs=[python_out]) \n",
"\n",
" def clear_all():\n",
" return None, \"\", \"\", gr.update(visible=False), \"\", \"\"\n",
"\n",
" clear_button.click(\n",
" clear_all,\n",
" outputs=[perl_file, perl_script, python_script, file_output, perl_out, python_out]\n",
" )\n",
" \n",
"\n",
"ui.launch(inbrowser=True)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,413 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ba410c21-be08-430f-8592-07aeefca27d1",
"metadata": {},
"source": [
"# Code Generator for Unit Tests and Comments/Docstrings"
]
},
{
"cell_type": "markdown",
"id": "0fe5e62b-78b5-476d-a3b1-77918d085c44",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2b529e40-4902-4a1b-9208-a938af156be1",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from dotenv import load_dotenv\n",
"\n",
"from openai import OpenAI\n",
"import anthropic\n",
"\n",
"from huggingface_hub import login\n",
"from transformers import AutoTokenizer, TextStreamer, AutoModelForCausalLM\n",
"\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4cd288ab-9332-4ce5-86b6-f81d2fff96a7",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('CLAUDE_API_KEY')\n",
"hf_token = os.getenv('HF_TOKEN')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1a192ae5-2be7-46a3-9376-d33e514e184e",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"claude = anthropic.Anthropic(api_key = anthropic_api_key)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7d6efe88-d90c-40f9-9df8-ab5370a31b21",
"metadata": {},
"outputs": [],
"source": [
"OPENAI = 'o3-mini-2025-01-31'\n",
"CLAUDE = 'claude-3-5-sonnet-20240620'\n",
"\n",
"QWEN = 'Qwen/CodeQwen1.5-7B-Chat'"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ef0df5ce-c786-44c7-bdbd-600adfe8908e",
"metadata": {},
"outputs": [],
"source": [
"TESTING = 'Unit Tests'\n",
"COMMENTING = 'Docstrings/Comments'"
]
},
{
"cell_type": "markdown",
"id": "f4b2a75a-e713-404d-898a-c87db87fa849",
"metadata": {},
"source": [
"## System and User Prompt for Unit Test and Comments"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4fab566a-4093-4ac4-bd77-866e0f307b74",
"metadata": {},
"outputs": [],
"source": [
"system_message_comment = \"\"\" You are an AI programming documentation assisstant. Your task is to generate clear, concise, \n",
"and informativ docstrings for the provided code block given by the user. \n",
"Analyze the code to understand its functionality and intent. Then produce a detailed documentation that includes:\n",
"- a short summary what the code does.\n",
"- a short description of the parameters, including their expected types\n",
"- a short explanation what the function returns \n",
"- if it's a complex code, and only then, some key insights\n",
"- if applicable how the function can be used\n",
"Ensure your documentation is written in clear gramatically correct english and in standard concentions (e.g PEP 257 for Python). \n",
"It should be understandable and maintainable for other developers \"\"\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "70273c7d-d461-4f59-982a-592443ce1257",
"metadata": {},
"outputs": [],
"source": [
"system_message_tests = \"\"\" You are an AI assisstant specialized for creating unit tests. Your task is to gnerate high-quality\n",
"unit tests for code provided by the user.\n",
"First analyze the code and identify the main functionality, parameters, return values and possible edge cases.\n",
"Create comprehensive unit tests that cover the following aspects:\n",
"- normal use cases with expected inputs and outputs\n",
"- boundary cases and extreme values\n",
"- error handling and exceptions\n",
"- edge cases \n",
"Use the appropriate testing framework for the programming language (e.g., pytest for Python, etc.) and explain to the user why you \n",
"chose this specific framework.\n",
"Structure the tests clearly with meaningful test names and add comments to explain the test logic.\n",
"If the code block does not provide enough context, as for the necessary details.\n",
"Supplemenet your response with a brief explanation of the testing strategy and suggestions for improving test coverage. \"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "48f2dd17-1ad1-4e34-ad76-0e02899f1962",
"metadata": {},
"outputs": [],
"source": [
"def user_prompt_comment(code):\n",
" user_prompt = f\"\"\"Please add detailed docstrings to the following code: \n",
" {code} \"\"\"\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "cb8b9962-c716-45d6-b4d1-ced781bb40f0",
"metadata": {},
"outputs": [],
"source": [
"def user_prompt_tests(code):\n",
" user_prompt = f\"\"\" Please generate unit tests for the following code using the appropriate framework: \n",
" {code} \"\"\"\n",
" return user_prompt"
]
},
{
"cell_type": "markdown",
"id": "959d263e-f6ad-4e0e-95d3-bb5f56877d47",
"metadata": {},
"source": [
"## Define Model Functions"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "8832b9d7-b17a-40d0-add5-07720d2e8af6",
"metadata": {},
"outputs": [],
"source": [
"def stream_gpt(system_message, user_prompt):\n",
" stream = openai.chat.completions.create(\n",
" model = OPENAI,\n",
" messages = [\n",
" {'role': 'system', 'content': system_message},\n",
" {'role': 'user', 'content': user_prompt}\n",
" ],\n",
" stream = True\n",
" )\n",
"\n",
" response = \"\"\n",
" for chunk in stream:\n",
" response += chunk.choices[0].delta.content or \"\"\n",
" yield response"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5ac1d70c-cd4e-4809-bc2f-75a2e82b4e58",
"metadata": {},
"outputs": [],
"source": [
"def stream_claude(system_message, user_prompt):\n",
" response = claude.messages.stream(\n",
" model = CLAUDE,\n",
" max_tokens = 2000,\n",
" system = system_message, \n",
" messages = [\n",
" {'role': 'user', 'content': user_prompt}\n",
" ], \n",
" temperature = 0.4\n",
" )\n",
" reply = \"\"\n",
" with response as stream:\n",
" for text in stream.text_stream:\n",
" reply += text or \"\"\n",
" yield reply"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "16702a62-fc9b-45b0-84cd-4f98523dfbd6",
"metadata": {},
"outputs": [],
"source": [
"def stream_qwen(system_message, user_prompt):\n",
" tokenizer = AutoTokenizer.from_pretrained(QWEN)\n",
" model = AutoModelForCausalLM.from_pretrained(QWEN, device_map = 'gpu')\n",
" streamer = TextStreamer(tokenizer)\n",
" inputs = tokenizer.apply_chat_template(\n",
" conv = [\n",
" {'role': 'system', 'content': system_message},\n",
" {'role': 'user', 'content': user_prompt}\n",
" ],\n",
" tokenize = False,\n",
" add_generation_prompt = True\n",
" )\n",
"\n",
" stream = model.text_generation(\n",
" prompt = inputs, \n",
" stream = True,\n",
" details = True,\n",
" max_new_tokens = 2000\n",
" )\n",
" reply = \"\"\n",
" for text in stream: \n",
" reply += text.token.text or \"\"\n",
" yield reply "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "f5dbf75f-c935-4412-b641-8afce97552e8",
"metadata": {},
"outputs": [],
"source": [
"def define_prompts(code, operation):\n",
" if operation == 'Unit Tests':\n",
" system_message = system_message_tests\n",
" user_prompt = user_prompt_tests(code)\n",
" elif operation == 'Docstrings/Comments':\n",
" system_message = system_message_comment\n",
" user_prompt = user_prompt_comment(code)\n",
" else: \n",
" return 'Unknown operation', ''\n",
"\n",
" return system_message, user_prompt"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "88a671f9-0ebc-487b-b116-b1abe4c6f934",
"metadata": {},
"outputs": [],
"source": [
"def create_test_comment(code, model, operation):\n",
" \n",
" system_message, user_prompt = define_prompts(code, operation)\n",
" \n",
" if model == 'GPT-o3-mini':\n",
" gen = stream_gpt(system_message, user_prompt)\n",
" elif model == 'Claude-3.5-sonnet':\n",
" gen = stream_claude(system_message, user_prompt)\n",
" elif model == 'CodeQwen':\n",
" gen = stream_qwen(system_message, user_prompt)\n",
" else: \n",
" gen = 'Unknown Model'\n",
"\n",
" result = ''\n",
" for text in gen:\n",
" result = text\n",
" return result"
]
},
{
"cell_type": "markdown",
"id": "1c7eea7a-fc30-4afd-b470-f4f83a288981",
"metadata": {},
"source": [
"## Creating easy Gradio UI "
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3d3d014b-bfc8-4ffd-941b-1fb3c9c9a80f",
"metadata": {},
"outputs": [],
"source": [
"def create_ui():\n",
"\n",
" with gr.Blocks(title = 'Code Generator') as ui:\n",
" gr.Markdown('# Code Generator for Unit Testing and Docstrings')\n",
" \n",
" with gr.Row():\n",
" with gr.Column(min_width = 500):\n",
" code = gr.Textbox(label = 'Enter your Code', \n",
" placeholder = 'Code...', lines = 20\n",
" )\n",
" model = gr.Dropdown(['GPT-o3-mini', 'Claude-3.5-sonnet', 'CodeQwen'],\n",
" label = 'Choose your Model',\n",
" value = 'GPT-o3-mini'\n",
" )\n",
" operation = gr.Dropdown(['Unit Tests', 'Docstrings/Comments'],\n",
" label = 'Choose operation',\n",
" value = 'Unit Tests'\n",
" )\n",
" generate_button = gr.Button('Generate')\n",
" \n",
" with gr.Column():\n",
" output = gr.Textbox(label = 'Generated Output',\n",
" lines = 20\n",
" )\n",
" \n",
" generate_button.click(fn = create_test_comment, inputs = [code, model, operation],\n",
" outputs = output,\n",
" )\n",
" return ui"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "89be90c2-55ed-41e5-8123-e4f8ab965281",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7860\n",
"\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": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ui = create_ui()\n",
"ui.launch(inbrowser = True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac4d6d48-4e52-477e-abf9-156eb1e4d561",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,524 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "dfe37963-1af6-44fc-a841-8e462443f5e6",
"metadata": {},
"source": [
"## Expert Knowledge Worker\n",
"\n",
"Features:\n",
"- A question answering agent that is an expert knowledge worker\n",
"- To be used by employees of Insurellm, an Insurance Tech company\n",
"- The agent needs to be accurate and the solution should be low cost.\n",
"\n",
"This project will use RAG (Retrieval Augmented Generation) to ensure our question/answering assistant has high accuracy.\n",
"\n",
"Technology:\n",
"- RAG: LangChain\n",
"- Embedding model: OpenAIEmbeddings or HuggingFace sentence-transformers\n",
"- Encoding method: Auto-encoding\n",
"- Vector datastore: Chroma or FAISS\n",
"- Vector DB visualization: Plotly\n",
"- Dimensionality reduction technique: t-SNE\n",
"\n",
"# Dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "802137aa-8a74-45e0-a487-d1974927d7ca",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import glob\n",
"from dotenv import load_dotenv\n",
"import gradio as gr\n",
"from langchain.document_loaders import DirectoryLoader, TextLoader\n",
"from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
"from langchain.schema import Document\n",
"from langchain_openai import OpenAIEmbeddings, ChatOpenAI\n",
"from langchain.embeddings import HuggingFaceEmbeddings\n",
"from langchain_chroma import Chroma\n",
"from langchain.vectorstores import FAISS\n",
"import numpy as np\n",
"from sklearn.manifold import TSNE\n",
"import plotly.graph_objects as go\n",
"from langchain.memory import ConversationBufferMemory\n",
"from langchain.chains import ConversationalRetrievalChain"
]
},
{
"cell_type": "markdown",
"id": "7187c181-5b17-4df7-b298-b7cb2b6d09f7",
"metadata": {},
"source": [
"# Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58c85082-e417-4708-9efe-81a5d55d1424",
"metadata": {},
"outputs": [],
"source": [
"MODEL = \"gpt-4o-mini\"\n",
"db_name = \"vector_db\"\n",
"db_type = \"Chroma\"\n",
"# db_type = \"FAISS\"\n",
"embed_type = \"OpenAIEmbeddings\"\n",
"# embed_type = \"sentence-transformers\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee78efcb-60fe-449e-a944-40bab26261af",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables\n",
"\n",
"load_dotenv(override=True)\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')"
]
},
{
"cell_type": "markdown",
"id": "a2f0866b-5cfb-4ecd-87d1-6da872887dcd",
"metadata": {},
"source": [
"# Create Knowledge Base for RAG\n",
"\n",
"## Load Company Documents\n",
"\n",
"Uses LangChain to read in a Knowledge Base of documents and to divide up documents into overlaping chunks."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "730711a9-6ffe-4eee-8f48-d6cfb7314905",
"metadata": {},
"outputs": [],
"source": [
"# Read in documents using LangChain's loaders\n",
"# Take everything in all the sub-folders of our knowledgebase\n",
"\n",
"folders = glob.glob(\"../knowledge-base/*\")\n",
"text_loader_kwargs = {'encoding': 'utf-8'}\n",
"# text_loader_kwargs={'autodetect_encoding': True}\n",
"\n",
"documents = []\n",
"for folder in folders:\n",
" doc_type = os.path.basename(folder)\n",
" loader = DirectoryLoader(folder, glob=\"**/*.md\", loader_cls=TextLoader, loader_kwargs=text_loader_kwargs)\n",
" folder_docs = loader.load()\n",
" for doc in folder_docs:\n",
" doc.metadata[\"doc_type\"] = doc_type\n",
" documents.append(doc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7310c9c8-03c1-4efc-a104-5e89aec6db1a",
"metadata": {},
"outputs": [],
"source": [
"text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n",
"chunks = text_splitter.split_documents(documents)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd06e02f-6d9b-44cc-a43d-e1faa8acc7bb",
"metadata": {},
"outputs": [],
"source": [
"len(chunks)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c54b4b6-06da-463d-bee7-4dd456c2b887",
"metadata": {},
"outputs": [],
"source": [
"doc_types = set(chunk.metadata['doc_type'] for chunk in chunks)\n",
"print(f\"Document types found: {', '.join(doc_types)}\")"
]
},
{
"cell_type": "markdown",
"id": "77f7d2a6-ccfa-425b-a1c3-5e55b23bd013",
"metadata": {},
"source": [
"## Vector Embeddings\n",
"\n",
"Convert chunks of text into Vectors using OpenAIEmbeddings and store the Vectors in Chroma (or FAISS)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78998399-ac17-4e28-b15f-0b5f51e6ee23",
"metadata": {},
"outputs": [],
"source": [
"# Put the chunks of data into a Vector Store that associates a Vector Embedding with each chunk\n",
"\n",
"embeddings = None\n",
"# OpenAIEmbeddings is OpenAI's vector embedding models\n",
"if embed_type == \"OpenAIEmbeddings\":\n",
" embeddings = OpenAIEmbeddings()\n",
"\n",
"# sentence-transformers is a free Vector embeddings model from HuggingFace\n",
"elif embed_type == \"sentence-transformers\":\n",
" embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-MiniLM-L6-v2\")\n",
"\n",
"if embeddings is None:\n",
" print(\"ERROR: embeddings not set. Check embed_type is set to a valid model\")"
]
},
{
"cell_type": "markdown",
"id": "64768521-a775-472c-83c5-0c0d715d44ac",
"metadata": {},
"source": [
"## Create Vector Datastore"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "057868f6-51a6-4087-94d1-380145821550",
"metadata": {},
"outputs": [],
"source": [
"# Create vectorstore\n",
"vectorstore = None\n",
"\n",
"# Chroma is a popular open source Vector Database based on SQLLite\n",
"if db_type == \"Chroma\":\n",
" # Delete vector DB if already exists\n",
" if os.path.exists(db_name):\n",
" Chroma(persist_directory=db_name, embedding_function=embeddings).delete_collection()\n",
" \n",
" # Create vectorstore\n",
" vectorstore = Chroma.from_documents(documents=chunks, embedding=embeddings, persist_directory=db_name)\n",
" \n",
" print(f\"Vectorstore created with {vectorstore._collection.count()} documents\")\n",
"\n",
" # Get one vector and find how many dimensions it has\n",
" collection = vectorstore._collection\n",
" sample_embedding = collection.get(limit=1, include=[\"embeddings\"])[\"embeddings\"][0]\n",
" dimensions = len(sample_embedding)\n",
" print(f\"The vectors have {dimensions:,} dimensions\")\n",
" \n",
"# FAISS is an in-memory vector DB from Facebook\n",
"elif db_type == \"FAISS\":\n",
" # Create vectorstore\n",
" vectorstore = FAISS.from_documents(chunks, embedding=embeddings)\n",
" \n",
" total_vectors = vectorstore.index.ntotal\n",
" dimensions = vectorstore.index.d\n",
" print(f\"There are {total_vectors} vectors with {dimensions:,} dimensions in the vector store\")\n",
"\n",
"else:\n",
" print(\"ERROR: Vector datastore not created. Check db_type is set to a valid database\")"
]
},
{
"cell_type": "markdown",
"id": "b0d45462-a818-441c-b010-b85b32bcf618",
"metadata": {},
"source": [
"# Visualizing the Vector Store\n",
"\n",
"Humans are not very good at visualizing things with more than 3 dimensions so to visualize a vector datastore with thousands of dimesions. We need to use techniques like projecting down to reduce the dimensions to only 2 or 3 dimensions in a way that does the best possible job at separating things out to stay faithful to the multi-dimensional representation.\n",
"\n",
"For example, things that are far apart in these multiple dimensions will still be far apart even when projected down to 2 dimensions.\n",
"\n",
"[t-distributed stochastic neighbor embedding (t-SNE)](https://en.wikipedia.org/wiki/T-distributed_stochastic_neighbor_embedding) is a nonlinear dimensionality reduction technique for embedding high-dimensional data for visualization in a low-dimensional space of two or three dimensions.\n",
"\n",
"## Configure Visualization"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b98adf5e-d464-4bd2-9bdf-bc5b6770263b",
"metadata": {},
"outputs": [],
"source": [
"# Prework\n",
"if db_type == \"Chroma\":\n",
" result = collection.get(include=['embeddings', 'documents', 'metadatas'])\n",
" vectors = np.array(result['embeddings'])\n",
" documents = result['documents']\n",
" doc_types = [metadata['doc_type'] for metadata in result['metadatas']]\n",
" colors = [['blue', 'green', 'red', 'orange'][['products', 'employees', 'contracts', 'company'].index(t)] for t in doc_types]\n",
"\n",
"elif db_type == \"FAISS\":\n",
" vectors = []\n",
" documents = []\n",
" doc_types = []\n",
" colors = []\n",
" color_map = {'products':'blue', 'employees':'green', 'contracts':'red', 'company':'orange'}\n",
" \n",
" for i in range(total_vectors):\n",
" vectors.append(vectorstore.index.reconstruct(i))\n",
" doc_id = vectorstore.index_to_docstore_id[i]\n",
" document = vectorstore.docstore.search(doc_id)\n",
" documents.append(document.page_content)\n",
" doc_type = document.metadata['doc_type']\n",
" doc_types.append(doc_type)\n",
" colors.append(color_map[doc_type])\n",
" \n",
" vectors = np.array(vectors)\n",
"\n",
"else:\n",
" print(\"ERROR: Vector datastore not created. Check db_type is set to a valid database\")"
]
},
{
"cell_type": "markdown",
"id": "bb279701-0086-44aa-a2da-14341aecf529",
"metadata": {},
"source": [
"## Reduce the dimensionality to 2D"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "427149d5-e5d8-4abd-bb6f-7ef0333cca21",
"metadata": {},
"outputs": [],
"source": [
"# We humans find it easier to visalize things in 2D!\n",
"# Reduce the dimensionality of the vectors to 2D using t-SNE\n",
"# (t-distributed stochastic neighbor embedding)\n",
"\n",
"tsne = TSNE(n_components=2, random_state=42)\n",
"reduced_vectors = tsne.fit_transform(vectors)\n",
"\n",
"# Create the 2D scatter plot\n",
"fig = go.Figure(data=[go.Scatter(\n",
" x=reduced_vectors[:, 0],\n",
" y=reduced_vectors[:, 1],\n",
" mode='markers',\n",
" marker=dict(size=5, color=colors, opacity=0.8),\n",
" text=[f\"Type: {t}<br>Text: {d[:100]}...\" for t, d in zip(doc_types, documents)],\n",
" hoverinfo='text'\n",
")])\n",
"\n",
"fig.update_layout(\n",
" title=f'2D {db_type} Vector Store Visualization',\n",
" scene=dict(xaxis_title='x',yaxis_title='y'),\n",
" width=800,\n",
" height=600,\n",
" margin=dict(r=20, b=10, l=10, t=40)\n",
")\n",
"\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "e2b724f3-e3ad-4d42-bfa4-a89386d6414e",
"metadata": {},
"source": [
"## Reduce the dimensionality to 3D"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1418e88-acd5-460a-bf2b-4e6efc88e3dd",
"metadata": {},
"outputs": [],
"source": [
"# 3D representation isn't as easy to navigate\n",
"\n",
"tsne = TSNE(n_components=3, random_state=42)\n",
"reduced_vectors = tsne.fit_transform(vectors)\n",
"\n",
"# Create the 3D scatter plot\n",
"fig = go.Figure(data=[go.Scatter3d(\n",
" x=reduced_vectors[:, 0],\n",
" y=reduced_vectors[:, 1],\n",
" z=reduced_vectors[:, 2],\n",
" mode='markers',\n",
" marker=dict(size=5, color=colors, opacity=0.8),\n",
" text=[f\"Type: {t}<br>Text: {d[:100]}...\" for t, d in zip(doc_types, documents)],\n",
" hoverinfo='text'\n",
")])\n",
"\n",
"fig.update_layout(\n",
" title=f'3D {db_type} Vector Store Visualization',\n",
" scene=dict(xaxis_title='x', yaxis_title='y', zaxis_title='z'),\n",
" width=900,\n",
" height=700,\n",
" margin=dict(r=20, b=10, l=10, t=40)\n",
")\n",
"\n",
"fig.show()"
]
},
{
"cell_type": "markdown",
"id": "9468860b-86a2-41df-af01-b2400cc985be",
"metadata": {},
"source": [
"# Expert Knowledge Worker\n",
"\n",
"Use LangChain to bring it all together by creating a conversation chain with RAG and memory.\n",
"\n",
"Key abstractions in LangChain:\n",
"- LLM: represents abstraction around a model\n",
"- Retriever: interface onto somthing like a vector store used for RAG retrieval\n",
"- Memory: represents a history of a conversation with a chatbot in memory\n",
"\n",
"Because LangChain abstracts the reprentation of the LLM, retriever and memory the code is the same for any model and knowledge base.\n",
"\n",
"Note: ok to ignore _Deprecation Warning_ for now; LangChain are not expected to remove ConversationBufferMemory any time soon.\n",
"\n",
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "129c7d1e-0094-4479-9459-f9360b95f244",
"metadata": {},
"outputs": [],
"source": [
"# create a new Chat with OpenAI\n",
"llm = ChatOpenAI(temperature=0.7, model_name=MODEL)\n",
"\n",
"# set up the conversation memory for the chat\n",
"memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)\n",
"\n",
"# the retriever is an abstraction over the VectorStore that will be used during RAG\n",
"retriever = vectorstore.as_retriever()\n",
"\n",
"# putting it together: set up the conversation chain with the GPT 4o-mini LLM, the vector store and memory\n",
"conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "968e7bf2-e862-4679-a11f-6c1efb6ec8ca",
"metadata": {},
"outputs": [],
"source": [
"query = \"Can you describe Insurellm in a few sentences\"\n",
"result = conversation_chain.invoke({\"question\":query})\n",
"print(result[\"answer\"])"
]
},
{
"cell_type": "markdown",
"id": "990a2917-562c-461a-8ce9-a8ad8ad1646d",
"metadata": {},
"source": [
"## Clear Memory\n",
"\n",
"Clear the memory from the testing and restart conversation chain for UI."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6eb99fb-33ec-4025-ab92-b634ede03647",
"metadata": {},
"outputs": [],
"source": [
"# clear the memory and restart conversation chain for UI\n",
"memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)\n",
"conversation_chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory)"
]
},
{
"cell_type": "markdown",
"id": "bbbcb659-13ce-47ab-8a5e-01b930494964",
"metadata": {},
"source": [
"## Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3536590-85c7-4155-bd87-ae78a1467670",
"metadata": {},
"outputs": [],
"source": [
"# Wrapping in a function - note that history isn't used, as the memory is in the conversation_chain\n",
"\n",
"def chat(message, history):\n",
" result = conversation_chain.invoke({\"question\": message})\n",
" return result[\"answer\"]"
]
},
{
"cell_type": "markdown",
"id": "b655d3da-277b-45a9-8113-747314ec0889",
"metadata": {},
"source": [
"## UI"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b252d8c1-61a8-406d-b57a-8f708a62b014",
"metadata": {},
"outputs": [],
"source": [
"# And in Gradio:\n",
"\n",
"view = gr.ChatInterface(chat, type=\"messages\", examples=[\"what is insurellm?\",\"what did avery do before?\", \"does insurellm offer any products in the auto industry space?\"], title=\"Insurellm Expert Knowledge Worker\").launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5435b2b9-935c-48cd-aaf3-73a837ecde49",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}