59 changed files with 1814 additions and 164 deletions
@ -0,0 +1,625 @@
|
||||
{ |
||||
"cells": [ |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "a98030af-fcd1-4d63-a36e-38ba053498fa", |
||||
"metadata": { |
||||
"editable": true, |
||||
"slideshow": { |
||||
"slide_type": "" |
||||
}, |
||||
"tags": [] |
||||
}, |
||||
"source": [ |
||||
"# A full business solution\n", |
||||
"\n", |
||||
"## Now we will take our project from Day 1 to the next level\n", |
||||
"\n", |
||||
"### BUSINESS CHALLENGE:\n", |
||||
"\n", |
||||
"Create a product that builds a Brochure for a company to be used for prospective clients, investors and potential recruits.\n", |
||||
"\n", |
||||
"We will be provided a company name and their primary website.\n", |
||||
"\n", |
||||
"See the end of this notebook for examples of real-world business applications.\n", |
||||
"\n", |
||||
"And remember: I'm always available if you have problems or ideas! Please do reach out." |
||||
] |
||||
}, |
||||
{ |
||||
"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\n", |
||||
"\n", |
||||
"# from Kamran; to use Llama instead of chatgpt;\n", |
||||
"# imports\n", |
||||
"\n", |
||||
"import ollama" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "fc5d8880-f2ee-4c06-af16-ecbc0262af61", |
||||
"metadata": { |
||||
"editable": true, |
||||
"slideshow": { |
||||
"slide_type": "" |
||||
}, |
||||
"tags": [] |
||||
}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# Initialize and constants\n", |
||||
"\n", |
||||
"# Commented out belwo lines;\n", |
||||
"# load_dotenv()\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()\n", |
||||
"\n", |
||||
"# Added by Kamran.\n", |
||||
"MODEL_LLAMA = 'llama3.2'" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "106dd65e-90af-4ca8-86b6-23a41840645b", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# A class to represent a Webpage\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)\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": null, |
||||
"id": "e30d8128-933b-44cc-81c8-ab4c9d86589a", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"ed = Website(\"https://edwarddonner.com\")\n", |
||||
"ed.links" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "1771af9c-717a-4fca-bbbe-8a95893312c3", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"## First step: Have GPT-4o-mini figure out which links are relevant\n", |
||||
"\n", |
||||
"### Use a call to gpt-4o-mini to read the links on a webpage, and respond in structured JSON. \n", |
||||
"It should decide which links are relevant, and replace relative links such as \"/about\" with \"https://company.com/about\". \n", |
||||
"We will use \"one shot prompting\" in which we provide an example of how it should respond in the prompt.\n", |
||||
"\n", |
||||
"This is an excellent use case for an LLM, because it requires nuanced understanding. Imagine trying to code this without LLMs by parsing and analyzing the webpage - it would be very hard!\n", |
||||
"\n", |
||||
"Sidenote: there is a more advanced technique called \"Structured Outputs\" in which we require the model to respond according to a spec. We cover this technique in Week 8 during our autonomous Agentic AI project." |
||||
] |
||||
}, |
||||
{ |
||||
"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 this example:\"\n", |
||||
"link_system_prompt += \"\"\"\n", |
||||
"{\n", |
||||
" \"links\": [\n", |
||||
" {\"type\": \"about page\", \"url\": \"https://full.url/goes/here/about\"},\n", |
||||
" {\"type\": \"careers page\": \"url\": \"https://another.full.url/careers\"}\n", |
||||
" ]\n", |
||||
"}\n", |
||||
"\"\"\"" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "b97e4068-97ed-4120-beae-c42105e4d59a", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"print(link_system_prompt)" |
||||
] |
||||
}, |
||||
{ |
||||
"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": "6bcbfa78-6395-4685-b92c-22d592050fd7", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"print(get_links_user_prompt(ed))" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "a29aca19-ca13-471c-a4b4-5abbfa813f69", |
||||
"metadata": { |
||||
"editable": true, |
||||
"slideshow": { |
||||
"slide_type": "" |
||||
}, |
||||
"tags": [] |
||||
}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# Get Llama 3.2 to answer\n", |
||||
"\n", |
||||
"# 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)\n", |
||||
"\n", |
||||
"def get_links(url):\n", |
||||
" website = Website(url)\n", |
||||
" response = ollama.chat(\n", |
||||
" model=MODEL_LLAMA,\n", |
||||
" messages=[\n", |
||||
" {\"role\": \"system\", \"content\": link_system_prompt},\n", |
||||
" {\"role\": \"user\", \"content\": get_links_user_prompt(website)}\n", |
||||
" ]\n", |
||||
" )\n", |
||||
" result = response['message']['content']\n", |
||||
" print(f\"About to parse this into json: {result}\")\n", |
||||
" return json.loads(result)\n" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "74a827a0-2782-4ae5-b210-4a242a8b4cc2", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"anthropic = Website(\"https://anthropic.com\")\n", |
||||
"anthropic.links" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "d3d583e2-dcc4-40cc-9b28-1e8dbf402924", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"get_links(\"https://anthropic.com\")" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "0d74128e-dfb6-47ec-9549-288b621c838c", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"## Second step: make the brochure!\n", |
||||
"\n", |
||||
"Assemble all the details into another prompt to GPT4-o" |
||||
] |
||||
}, |
||||
{ |
||||
"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": "code", |
||||
"execution_count": null, |
||||
"id": "5099bd14-076d-4745-baf3-dac08d8e5ab2", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"print(get_all_details(\"https://anthropic.com\"))" |
||||
] |
||||
}, |
||||
{ |
||||
"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" |
||||
] |
||||
}, |
||||
{ |
||||
"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[:20_000] # Truncate if more than 20,000 characters\n", |
||||
" return user_prompt" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "cd909e0b-1312-4ce2-a553-821e795d7572", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"get_brochure_user_prompt(\"Anthropic\", \"https://anthropic.com\")" |
||||
] |
||||
}, |
||||
{ |
||||
"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", |
||||
"\n", |
||||
"def create_brochure(company_name, url):\n", |
||||
" response = ollama.chat(\n", |
||||
" model=MODEL_LLAMA,\n", |
||||
" messages=[\n", |
||||
" {\"role\": \"system\", \"content\": system_prompt},\n", |
||||
" {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n", |
||||
" ]\n", |
||||
" )\n", |
||||
" result = response['message']['content']\n", |
||||
" display(Markdown(result))\n" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "e093444a-9407-42ae-924a-145730591a39", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"create_brochure(\"Anthropic\", \"https://anthropic.com\")" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "61eaaab7-0b47-4b29-82d4-75d474ad8d18", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"## Finally - a minor improvement\n", |
||||
"\n", |
||||
"With a small adjustment, we can change this so that the results stream back from OpenAI,\n", |
||||
"with the familiar typewriter animation" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "51db0e49-f261-4137-aabe-92dd601f7725", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# def stream_brochure(company_name, url):\n", |
||||
"# stream = 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", |
||||
"# stream=True\n", |
||||
"# )\n", |
||||
"\n", |
||||
"# # For just a simple output you can do the following two lines;\n", |
||||
"# # for chunk in stream:\n", |
||||
"# # print(chunk.choices[0].delta.content or '',end='')\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)\n", |
||||
"\n", |
||||
"def stream_brochure(company_name, url):\n", |
||||
" stream = ollama.chat(\n", |
||||
" model=MODEL_LLAMA,\n", |
||||
" messages=[\n", |
||||
" {\"role\": \"system\", \"content\": system_prompt},\n", |
||||
" {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n", |
||||
" ],\n", |
||||
" stream=True\n", |
||||
" )\n", |
||||
"\n", |
||||
" # For just a simple output you can do the following two lines;\n", |
||||
" # for chunk in stream:\n", |
||||
" # print(chunk['message']['content'] or '', end='')\n", |
||||
"\n", |
||||
" response = \"\"\n", |
||||
" display_handle = display(Markdown(\"\"), display_id=True)\n", |
||||
" for chunk in stream:\n", |
||||
" response += chunk['message']['content'] or ''\n", |
||||
" response = response.replace(\"```\", \"\").replace(\"markdown\", \"\")\n", |
||||
" update_display(Markdown(response), display_id=display_handle.display_id)\n" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "56bf0ae3-ee9d-4a72-9cd6-edcac67ceb6d", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"stream_brochure(\"Anthropic\", \"https://anthropic.com\")" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "fdb3f8d8-a3eb-41c8-b1aa-9f60686a653b", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# Try changing the system prompt to the humorous version when you make the Brochure for Hugging Face:\n", |
||||
"\n", |
||||
"stream_brochure(\"HuggingFace\", \"https://huggingface.co\")\n" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "5567d103-74ee-4a7a-997c-eaf2c3baf7f4", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"def test_llama_response_basic(company_name, url):\n", |
||||
" try:\n", |
||||
" response = ollama.chat(\n", |
||||
" model=MODEL_LLAMA,\n", |
||||
" messages=[\n", |
||||
" {\"role\": \"system\", \"content\": system_prompt},\n", |
||||
" {\"role\": \"user\", \"content\": get_brochure_user_prompt(company_name, url)}\n", |
||||
" ]\n", |
||||
" )\n", |
||||
"\n", |
||||
" # Print the entire raw response for debugging purposes\n", |
||||
" print(\"Raw response received:\", response)\n", |
||||
"\n", |
||||
" # Check if the response contains 'message' and 'content'\n", |
||||
" if 'message' in response and 'content' in response['message']:\n", |
||||
" response_content = response['message']['content']\n", |
||||
" print(\"Content from response:\", response_content)\n", |
||||
" return response_content\n", |
||||
" else:\n", |
||||
" print(\"Response does not contain expected 'message' or 'content'\")\n", |
||||
" return response\n", |
||||
"\n", |
||||
" except Exception as e:\n", |
||||
" print(f\"An error occurred: {e}\")\n", |
||||
" return {}\n", |
||||
"\n", |
||||
"# Example usage\n", |
||||
"test_llama_response_basic(\"HuggingFace\", \"https://huggingface.co\")\n", |
||||
"\n", |
||||
"\n" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "a27bf9e0-665f-4645-b66b-9725e2a959b5", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"<table style=\"margin: 0; text-align: left;\">\n", |
||||
" <tr>\n", |
||||
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", |
||||
" <img src=\"../business.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", |
||||
" </td>\n", |
||||
" <td>\n", |
||||
" <h2 style=\"color:#181;\">Business applications</h2>\n", |
||||
" <span style=\"color:#181;\">In this exercise we extended the Day 1 code to make multiple LLM calls, and generate a document.\n", |
||||
"\n", |
||||
"This is perhaps the first example of Agentic AI design patterns, as we combined multiple calls to LLMs. This will feature more in Week 2, and then we will return to Agentic AI in a big way in Week 8 when we build a fully autonomous Agent solution.\n", |
||||
"\n", |
||||
"Generating content in this way is one of the very most common Use Cases. As with summarization, this can be applied to any business vertical. Write marketing content, generate a product tutorial from a spec, create personalized email content, and so much more. Explore how you can apply content generation to your business, and try making yourself a proof-of-concept prototype.</span>\n", |
||||
" </td>\n", |
||||
" </tr>\n", |
||||
"</table>" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "14b2454b-8ef8-4b5c-b928-053a15e0d553", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"<table style=\"margin: 0; text-align: left;\">\n", |
||||
" <tr>\n", |
||||
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", |
||||
" <img src=\"../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", |
||||
" </td>\n", |
||||
" <td>\n", |
||||
" <h2 style=\"color:#900;\">Before you move to Week 2 (which is tons of fun)</h2>\n", |
||||
" <span style=\"color:#900;\">Please see the week1 EXERCISE notebook for your challenge for the end of week 1. This will give you some essential practice working with Frontier APIs, and prepare you well for Week 2.</span>\n", |
||||
" </td>\n", |
||||
" </tr>\n", |
||||
"</table>" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "17b64f0f-7d33-4493-985a-033d06e8db08", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"<table style=\"margin: 0; text-align: left;\">\n", |
||||
" <tr>\n", |
||||
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n", |
||||
" <img src=\"../resources.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n", |
||||
" </td>\n", |
||||
" <td>\n", |
||||
" <h2 style=\"color:#f71;\">A reminder on 2 useful resources</h2>\n", |
||||
" <span style=\"color:#f71;\">1. The resources for the course are available <a href=\"https://edwarddonner.com/2024/11/13/llm-engineering-resources/\">here.</a><br/>\n", |
||||
" 2. I'm on LinkedIn <a href=\"https://www.linkedin.com/in/eddonner/\">here</a> and I love connecting with people taking the course!\n", |
||||
" </span>\n", |
||||
" </td>\n", |
||||
" </tr>\n", |
||||
"</table>" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "b8fbce9d-51e5-4e8c-a7a9-c88ad02fffdf", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"import requests\n", |
||||
"from dotenv import load_dotenv\n", |
||||
"\n", |
||||
"load_dotenv()\n", |
||||
"hf_token=os.getenv(\"HF_TOKEN\")\n", |
||||
"print(f\"Using this HF Token: {hf_token}\")\n", |
||||
"\n", |
||||
"API_URL = \"https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B\"\n", |
||||
"headers = {\"Authorization\": f\"Bearer {hf_token}\"}\n", |
||||
"\n", |
||||
"def query(payload):\n", |
||||
"\tresponse = requests.post(API_URL, headers=headers, json=payload)\n", |
||||
"\treturn response.json()\n", |
||||
"\t\n", |
||||
"output = query({\n", |
||||
"\t\"inputs\": \"2 + 2 is \",\n", |
||||
"})\n", |
||||
"print(output)" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "ec2b37af-566e-4b0b-ad4a-8b46cc346e46", |
||||
"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 |
||||
} |
@ -0,0 +1,291 @@
|
||||
{ |
||||
"cells": [ |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"# Project - Airline AI Assistant\n", |
||||
"\n", |
||||
"We'll now bring together what we've learned to make an AI Customer Support assistant for an Airline" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4", |
||||
"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": "747e8786-9da8-4342-b6c9-f5f69c2e22ae", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# Initialization\n", |
||||
"\n", |
||||
"load_dotenv()\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": "0a521d84-d07c-49ab-a0df-d6451499ed97", |
||||
"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": "61a2a15d-b559-4844-b377-6bd5cb4949f6", |
||||
"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": "36bedabf-a0a7-4985-ad8e-07ed6a55a3a4", |
||||
"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": "0696acb1-0b05-4dc2-80d5-771be04f1fb2", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# Let's start by making a useful function\n", |
||||
"\n", |
||||
"ticket_prices = {\"london\": \"$799\", \"paris\": \"$899\", \"tokyo\": \"$1400\", \"berlin\": \"$499\"}\n", |
||||
"ticket_discounts={\"london\":5, \"tokyo\":15}\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\")\n", |
||||
"def get_ticket_discount(destination_city):\n", |
||||
" print(f\"Tool get_ticket_discount called for {destination_city}\")\n", |
||||
" city = destination_city.lower()\n", |
||||
" return ticket_discounts.get(city,0)" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "80ca4e09-6287-4d3f-997d-fa6afbcf6c85", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"get_ticket_price(\"Berlin\")\n", |
||||
"get_ticket_discount(\"Berlin\")" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "4afceded-7178-4c05-8fa6-9f2085e6a344", |
||||
"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", |
||||
"}\n", |
||||
"\n", |
||||
"discount_function = {\n", |
||||
" \"name\": \"get_ticket_discount\",\n", |
||||
" \"description\": \"Get the discount on price of a return ticket to the destination city. Call this whenever you need to know the discount on the ticket price, for example when a customer asks 'Is there a discount on the price on the ticket to this city'\",\n", |
||||
" \"parameters\": {\n", |
||||
" \"type\": \"object\",\n", |
||||
" \"properties\": {\n", |
||||
" \"destination_city\": {\n", |
||||
" \"type\": \"string\",\n", |
||||
" \"description\": \"The discount on price to 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": "bdca8679-935f-4e7f-97e6-e71a4d4f228c", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# And this is included in a list of tools:\n", |
||||
"\n", |
||||
"tools = [{\"type\": \"function\", \"function\": price_function},\n", |
||||
" {\"type\":\"function\", \"function\": discount_function}]\n", |
||||
"tools_functions_map = {\n", |
||||
" \"get_ticket_price\":get_ticket_price,\n", |
||||
" \"get_ticket_discount\":get_ticket_discount\n", |
||||
"}" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "c3d3554f-b4e3-4ce7-af6f-68faa6dd2340", |
||||
"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": "ce9b0744-9c78-408d-b9df-9f6fd9ed78cf", |
||||
"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", |
||||
" tool_responses, city = handle_tool_call(message)\n", |
||||
" messages.append(message)\n", |
||||
" for tool_response in tool_responses:\n", |
||||
" messages.append(tool_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": "b0992986-ea09-4912-a076-8e5603ee631f", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# We have to write that function handle_tool_call:\n", |
||||
"\n", |
||||
"def handle_tool_call(message):\n", |
||||
" tool_calls = message.tool_calls;\n", |
||||
" arguments = json.loads(tool_calls[0].function.arguments)\n", |
||||
" city = arguments.get('destination_city')\n", |
||||
" responses=[]\n", |
||||
" \n", |
||||
" for tool_call in tool_calls:\n", |
||||
" name = tool_call.function.name\n", |
||||
" if name in tools_functions_map:\n", |
||||
" key = \"price\" if \"price\" in name else \"discount\"\n", |
||||
" value = tools_functions_map[name](city)\n", |
||||
" responses.append({\n", |
||||
" \"role\": \"tool\",\n", |
||||
" \"content\": json.dumps({\"destination_city\": city, key : value}),\n", |
||||
" \"tool_call_id\": tool_call.id\n", |
||||
" })\n", |
||||
" return responses, city" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "f4be8a71-b19e-4c2f-80df-f59ff2661f14", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"gr.ChatInterface(fn=chat, type=\"messages\").launch()" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "11c9da69-d0cf-4cf2-a49e-e5669deec47b", |
||||
"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 |
||||
} |
@ -0,0 +1,475 @@
|
||||
{ |
||||
"cells": [ |
||||
{ |
||||
"cell_type": "markdown", |
||||
"id": "ad900e1c-b4a9-4f05-93d5-e364fae208dd", |
||||
"metadata": {}, |
||||
"source": [ |
||||
"# Multimodal Expert Tutor\n", |
||||
"\n", |
||||
"An AI assistant which leverages expertise from other sources for you.\n", |
||||
"\n", |
||||
"Features:\n", |
||||
"- Multimodal\n", |
||||
"- Uses tools\n", |
||||
"- Streams responses\n", |
||||
"- Reads out the responses after streaming\n", |
||||
"- Coverts voice to text during input\n", |
||||
"\n", |
||||
"Scope for Improvement\n", |
||||
"- Read response faster (as streaming starts)\n", |
||||
"- code optimization\n", |
||||
"- UI enhancements\n", |
||||
"- Make it more real time" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": 1, |
||||
"id": "c1070317-3ed9-4659-abe3-828943230e03", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# imports\n", |
||||
"\n", |
||||
"import os\n", |
||||
"import json\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\n", |
||||
"import google.generativeai\n", |
||||
"import anthropic" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "4a456906-915a-4bfd-bb9d-57e505c5093f", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# constants\n", |
||||
"\n", |
||||
"MODEL_GPT = 'gpt-4o-mini'\n", |
||||
"MODEL_CLAUDE = 'claude-3-5-sonnet-20240620'\n", |
||||
"MODEL_GEMINI = 'gemini-1.5-flash'" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "a8d7923c-5f28-4c30-8556-342d7c8497c1", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# set up environment\n", |
||||
"\n", |
||||
"load_dotenv()\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['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "a6fd8538-0be6-4539-8add-00e42133a641", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# Connect to OpenAI, Anthropic and Google\n", |
||||
"\n", |
||||
"openai = OpenAI()\n", |
||||
"\n", |
||||
"claude = anthropic.Anthropic()\n", |
||||
"\n", |
||||
"google.generativeai.configure()" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "852faee9-79aa-4741-a676-4f5145ccccdc", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"import tempfile\n", |
||||
"import subprocess\n", |
||||
"from io import BytesIO\n", |
||||
"from pydub import AudioSegment\n", |
||||
"import time\n", |
||||
"\n", |
||||
"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", |
||||
" 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=\"onyx\", # Also, try replacing onyx with alloy\n", |
||||
" input=message\n", |
||||
" )\n", |
||||
" audio_stream = BytesIO(response.content)\n", |
||||
" audio = AudioSegment.from_file(audio_stream, format=\"mp3\")\n", |
||||
" play_audio(audio)\n", |
||||
"\n", |
||||
"talker(\"Well hi there\")" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "8595807b-8ae2-4e1b-95d9-e8532142e8bb", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# prompts\n", |
||||
"general_prompt = \"Please be as technical as possible with your answers.\\\n", |
||||
"Only answer questions about topics you have expertise in.\\\n", |
||||
"If you do not know something say so.\"\n", |
||||
"\n", |
||||
"additional_prompt_gpt = \"Analyze the user query and determine if the content is primarily related to \\\n", |
||||
"coding, software engineering, data science and LLMs. \\\n", |
||||
"If so please answer it yourself else if it is primarily related to \\\n", |
||||
"physics, chemistry or biology get answers from tool ask_gemini or \\\n", |
||||
"if it belongs to subject related to finance, business or economics get answers from tool ask_claude.\"\n", |
||||
"\n", |
||||
"system_prompt_gpt = \"You are a helpful technical tutor who is an expert in \\\n", |
||||
"coding, software engineering, data science and LLMs.\"+ additional_prompt_gpt + general_prompt\n", |
||||
"system_prompt_gemini = \"You are a helpful technical tutor who is an expert in physics, chemistry and biology.\" + general_prompt\n", |
||||
"system_prompt_claude = \"You are a helpful technical tutor who is an expert in finance, business and economics.\" + general_prompt\n", |
||||
"\n", |
||||
"def get_user_prompt(question):\n", |
||||
" return \"Please give a detailed explanation to the following question: \" + question" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "24d4a313-60b0-4696-b455-6cfef95ad2fe", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"def call_claude(question):\n", |
||||
" result = claude.messages.create(\n", |
||||
" model=MODEL_CLAUDE,\n", |
||||
" max_tokens=200,\n", |
||||
" temperature=0.7,\n", |
||||
" system=system_prompt_claude,\n", |
||||
" messages=[\n", |
||||
" {\"role\": \"user\", \"content\": get_user_prompt(question)},\n", |
||||
" ],\n", |
||||
" )\n", |
||||
" \n", |
||||
" return result.content[0].text" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "cd5d5345-54ab-470b-9b5b-5611a7981458", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"def call_gemini(question):\n", |
||||
" gemini = google.generativeai.GenerativeModel(\n", |
||||
" model_name=MODEL_GEMINI,\n", |
||||
" system_instruction=system_prompt_gemini\n", |
||||
" )\n", |
||||
" response = gemini.generate_content(get_user_prompt(question))\n", |
||||
" response = response.text\n", |
||||
" return response" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "6f74da8f-56d1-405e-bc81-040f5428d296", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# tools and functions\n", |
||||
"\n", |
||||
"def ask_claude(question):\n", |
||||
" print(f\"Tool ask_claude called for {question}\")\n", |
||||
" return call_claude(question)\n", |
||||
"def ask_gemini(question):\n", |
||||
" print(f\"Tool ask_gemini called for {question}\")\n", |
||||
" return call_gemini(question)" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "c469304d-99b4-42ee-ab02-c9216b61594b", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"ask_claude_function = {\n", |
||||
" \"name\": \"ask_claude\",\n", |
||||
" \"description\": \"Get the answer to the question related to a topic this agent is faimiliar with. Call this whenever you need to answer something related to finance, marketing, sales or business in general.For example 'What is gross margin' or 'Explain stock market'\",\n", |
||||
" \"parameters\": {\n", |
||||
" \"type\": \"object\",\n", |
||||
" \"properties\": {\n", |
||||
" \"question_for_topic\": {\n", |
||||
" \"type\": \"string\",\n", |
||||
" \"description\": \"The question which is related to finance, business or economics.\",\n", |
||||
" },\n", |
||||
" },\n", |
||||
" \"required\": [\"question_for_topic\"],\n", |
||||
" \"additionalProperties\": False\n", |
||||
" }\n", |
||||
"}\n", |
||||
"\n", |
||||
"ask_gemini_function = {\n", |
||||
" \"name\": \"ask_gemini\",\n", |
||||
" \"description\": \"Get the answer to the question related to a topic this agent is faimiliar with. Call this whenever you need to answer something related to physics, chemistry or biology.Few examples: 'What is gravity','How do rockets work?', 'What is ATP'\",\n", |
||||
" \"parameters\": {\n", |
||||
" \"type\": \"object\",\n", |
||||
" \"properties\": {\n", |
||||
" \"question_for_topic\": {\n", |
||||
" \"type\": \"string\",\n", |
||||
" \"description\": \"The question which is related to physics, chemistry or biology\",\n", |
||||
" },\n", |
||||
" },\n", |
||||
" \"required\": [\"question_for_topic\"],\n", |
||||
" \"additionalProperties\": False\n", |
||||
" }\n", |
||||
"}" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "73a60096-c49b-401f-bfd3-d1d40f4563d2", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"tools = [{\"type\": \"function\", \"function\": ask_claude_function},\n", |
||||
" {\"type\": \"function\", \"function\": ask_gemini_function}]\n", |
||||
"tools_functions_map = {\n", |
||||
" \"ask_claude\":ask_claude,\n", |
||||
" \"ask_gemini\":ask_gemini\n", |
||||
"}" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "9d54e758-42b2-42f2-a8eb-49c35d44acc6", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"def chat(history):\n", |
||||
" messages = [{\"role\": \"system\", \"content\": system_prompt_gpt}] + history\n", |
||||
" stream = openai.chat.completions.create(model=MODEL_GPT, messages=messages, tools=tools, stream=True)\n", |
||||
" \n", |
||||
" full_response = \"\"\n", |
||||
" history += [{\"role\":\"assistant\", \"content\":full_response}]\n", |
||||
" \n", |
||||
" tool_call_accumulator = \"\" # Accumulator for JSON fragments of tool call arguments\n", |
||||
" tool_call_id = None # Current tool call ID\n", |
||||
" tool_call_function_name = None # Function name\n", |
||||
" tool_calls = [] # List to store complete tool calls\n", |
||||
"\n", |
||||
" for chunk in stream:\n", |
||||
" if chunk.choices[0].delta.content:\n", |
||||
" full_response += chunk.choices[0].delta.content or \"\"\n", |
||||
" history[-1]['content']=full_response\n", |
||||
" yield history\n", |
||||
" \n", |
||||
" if chunk.choices[0].delta.tool_calls:\n", |
||||
" message = chunk.choices[0].delta\n", |
||||
" for tc in chunk.choices[0].delta.tool_calls:\n", |
||||
" if tc.id: # New tool call detected here\n", |
||||
" tool_call_id = tc.id\n", |
||||
" if tool_call_function_name is None:\n", |
||||
" tool_call_function_name = tc.function.name\n", |
||||
" \n", |
||||
" tool_call_accumulator += tc.function.arguments if tc.function.arguments else \"\"\n", |
||||
" \n", |
||||
" # When the accumulated JSON string seems complete then:\n", |
||||
" try:\n", |
||||
" func_args = json.loads(tool_call_accumulator)\n", |
||||
" \n", |
||||
" # Handle tool call and get response\n", |
||||
" tool_response, tool_call = handle_tool_call(tool_call_function_name, func_args, tool_call_id)\n", |
||||
" \n", |
||||
" tool_calls.append(tool_call)\n", |
||||
"\n", |
||||
" # Add tool call and tool response to messages this is required by openAI api\n", |
||||
" messages.append({\n", |
||||
" \"role\": \"assistant\",\n", |
||||
" \"tool_calls\": tool_calls\n", |
||||
" })\n", |
||||
" messages.append(tool_response)\n", |
||||
" \n", |
||||
" # Create new response with full context\n", |
||||
" response = openai.chat.completions.create(\n", |
||||
" model=MODEL_GPT, \n", |
||||
" messages=messages, \n", |
||||
" stream=True\n", |
||||
" )\n", |
||||
" \n", |
||||
" # Reset and accumulate new full response\n", |
||||
" full_response = \"\"\n", |
||||
" for chunk in response:\n", |
||||
" if chunk.choices[0].delta.content:\n", |
||||
" full_response += chunk.choices[0].delta.content or \"\"\n", |
||||
" history[-1]['content'] = full_response\n", |
||||
" yield history\n", |
||||
" \n", |
||||
" # Reset tool call accumulator and related variables\n", |
||||
" tool_call_accumulator = \"\"\n", |
||||
" tool_call_id = None\n", |
||||
" tool_call_function_name = None\n", |
||||
" tool_calls = []\n", |
||||
"\n", |
||||
" except json.JSONDecodeError:\n", |
||||
" # Incomplete JSON; continue accumulating\n", |
||||
" pass\n", |
||||
"\n", |
||||
" # trigger text-to-audio once full response available\n", |
||||
" talker(full_response)" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "515d3774-cc2c-44cd-af9b-768a63ed90dc", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"# We have to write that function handle_tool_call:\n", |
||||
"def handle_tool_call(function_name, arguments, tool_call_id):\n", |
||||
" question = arguments.get('question_for_topic')\n", |
||||
" \n", |
||||
" # Prepare tool call information\n", |
||||
" tool_call = {\n", |
||||
" \"id\": tool_call_id,\n", |
||||
" \"type\": \"function\",\n", |
||||
" \"function\": {\n", |
||||
" \"name\": function_name,\n", |
||||
" \"arguments\": json.dumps(arguments)\n", |
||||
" }\n", |
||||
" }\n", |
||||
" \n", |
||||
" if function_name in tools_functions_map:\n", |
||||
" answer = tools_functions_map[function_name](question)\n", |
||||
" response = {\n", |
||||
" \"role\": \"tool\",\n", |
||||
" \"content\": json.dumps({\"question\": question, \"answer\" : answer}),\n", |
||||
" \"tool_call_id\": tool_call_id\n", |
||||
" }\n", |
||||
"\n", |
||||
" return response, tool_call" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "5d7cc622-8635-4693-afa3-b5bcc2f9a63d", |
||||
"metadata": {}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"def transcribe_audio(audio_file_path):\n", |
||||
" try:\n", |
||||
" audio_file = open(audio_file_path, \"rb\")\n", |
||||
" response = openai.audio.transcriptions.create(model=\"whisper-1\", file=audio_file) \n", |
||||
" return response.text\n", |
||||
" except Exception as e:\n", |
||||
" return f\"An error occurred: {e}\"" |
||||
] |
||||
}, |
||||
{ |
||||
"cell_type": "code", |
||||
"execution_count": null, |
||||
"id": "4ded9b3f-83e1-4971-9714-4894f2982b5a", |
||||
"metadata": { |
||||
"scrolled": true |
||||
}, |
||||
"outputs": [], |
||||
"source": [ |
||||
"with gr.Blocks() as ui:\n", |
||||
" with gr.Row():\n", |
||||
" chatbot = gr.Chatbot(height=500, type=\"messages\", label=\"Multimodal Technical Expert Chatbot\")\n", |
||||
" with gr.Row():\n", |
||||
" entry = gr.Textbox(label=\"Ask our technical expert anything:\")\n", |
||||
" audio_input = gr.Audio(\n", |
||||
" sources=\"microphone\", \n", |
||||
" type=\"filepath\",\n", |
||||
" label=\"Record audio\",\n", |
||||
" editable=False,\n", |
||||
" waveform_options=gr.WaveformOptions(\n", |
||||
" show_recording_waveform=False,\n", |
||||
" ),\n", |
||||
" )\n", |
||||
"\n", |
||||
" # Add event listener for audio stop recording and show text on input area\n", |
||||
" audio_input.stop_recording(\n", |
||||
" fn=transcribe_audio, \n", |
||||
" inputs=audio_input, \n", |
||||
" outputs=entry\n", |
||||
" )\n", |
||||
" \n", |
||||
" with gr.Row():\n", |
||||
" clear = gr.Button(\"Clear\")\n", |
||||
"\n", |
||||
" def do_entry(message, history):\n", |
||||
" history += [{\"role\":\"user\", \"content\":message}]\n", |
||||
" yield \"\", history\n", |
||||
" \n", |
||||
" entry.submit(do_entry, inputs=[entry, chatbot], outputs=[entry,chatbot]).then(\n", |
||||
" chat, inputs=chatbot, 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": null, |
||||
"id": "532cb948-7733-4323-b85f-febfe2631e66", |
||||
"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 |
||||
} |
Binary file not shown.
@ -1,51 +1,72 @@
|
||||
#include <iostream> |
||||
#include <random> |
||||
#include <vector> |
||||
#include <chrono> |
||||
#include <limits> |
||||
#include <iomanip> |
||||
|
||||
// Function to generate random numbers using Mersenne Twister
|
||||
std::mt19937 gen(42); |
||||
using namespace std; |
||||
using namespace chrono; |
||||
|
||||
class LCG { |
||||
private: |
||||
uint64_t value; |
||||
static const uint64_t a = 1664525; |
||||
static const uint64_t c = 1013904223; |
||||
static const uint64_t m = 1ULL << 32; |
||||
|
||||
public: |
||||
LCG(uint64_t seed) : value(seed) {} |
||||
|
||||
uint64_t next() { |
||||
value = (a * value + c) % m; |
||||
return value; |
||||
} |
||||
}; |
||||
|
||||
int64_t max_subarray_sum(int n, uint64_t seed, int min_val, int max_val) { |
||||
LCG lcg(seed); |
||||
vector<int64_t> random_numbers(n); |
||||
for (int i = 0; i < n; ++i) { |
||||
random_numbers[i] = lcg.next() % (max_val - min_val + 1) + min_val; |
||||
} |
||||
|
||||
int64_t max_sum = numeric_limits<int64_t>::min(); |
||||
int64_t current_sum = 0; |
||||
int64_t min_sum = 0; |
||||
|
||||
// Function to calculate maximum subarray sum
|
||||
int max_subarray_sum(int n, int min_val, int max_val) { |
||||
std::uniform_int_distribution<> dis(min_val, max_val); |
||||
int max_sum = std::numeric_limits<int>::min(); |
||||
int current_sum = 0; |
||||
for (int i = 0; i < n; ++i) { |
||||
current_sum += dis(gen); |
||||
if (current_sum > max_sum) { |
||||
max_sum = current_sum; |
||||
} |
||||
if (current_sum < 0) { |
||||
current_sum = 0; |
||||
} |
||||
current_sum += random_numbers[i]; |
||||
max_sum = max(max_sum, current_sum - min_sum); |
||||
min_sum = min(min_sum, current_sum); |
||||
} |
||||
|
||||
return max_sum; |
||||
} |
||||
|
||||
// Function to calculate total maximum subarray sum
|
||||
int total_max_subarray_sum(int n, int initial_seed, int min_val, int max_val) { |
||||
gen.seed(initial_seed); |
||||
int total_sum = 0; |
||||
int64_t total_max_subarray_sum(int n, uint64_t initial_seed, int min_val, int max_val) { |
||||
int64_t total_sum = 0; |
||||
LCG lcg(initial_seed); |
||||
for (int i = 0; i < 20; ++i) { |
||||
total_sum += max_subarray_sum(n, min_val, max_val); |
||||
uint64_t seed = lcg.next(); |
||||
total_sum += max_subarray_sum(n, seed, min_val, max_val); |
||||
} |
||||
return total_sum; |
||||
} |
||||
|
||||
int main() { |
||||
int n = 10000; // Number of random numbers
|
||||
int initial_seed = 42; // Initial seed for the Mersenne Twister
|
||||
int min_val = -10; // Minimum value of random numbers
|
||||
int max_val = 10; // Maximum value of random numbers
|
||||
|
||||
// Timing the function
|
||||
auto start_time = std::chrono::high_resolution_clock::now(); |
||||
int result = total_max_subarray_sum(n, initial_seed, min_val, max_val); |
||||
auto end_time = std::chrono::high_resolution_clock::now(); |
||||
|
||||
std::cout << "Total Maximum Subarray Sum (20 runs): " << result << std::endl; |
||||
std::cout << "Execution Time: " << std::setprecision(6) << std::fixed << std::chrono::duration<double>(end_time - start_time).count() << " seconds" << std::endl; |
||||
const int n = 10000; |
||||
const uint64_t initial_seed = 42; |
||||
const int min_val = -10; |
||||
const int max_val = 10; |
||||
|
||||
auto start_time = high_resolution_clock::now(); |
||||
int64_t result = total_max_subarray_sum(n, initial_seed, min_val, max_val); |
||||
auto end_time = high_resolution_clock::now(); |
||||
|
||||
auto duration = duration_cast<microseconds>(end_time - start_time); |
||||
|
||||
cout << "Total Maximum Subarray Sum (20 runs): " << result << endl; |
||||
cout << "Execution Time: " << fixed << setprecision(6) << duration.count() / 1e6 << " seconds" << endl; |
||||
|
||||
return 0; |
||||
} |
Loading…
Reference in new issue