sach91 bootcamp week2 exercise
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": null,
|
||||||
"id": "c1070317-3ed9-4659-abe3-828943230e03",
|
"id": "c1070317-3ed9-4659-abe3-828943230e03",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": null,
|
||||||
"id": "4a456906-915a-4bfd-bb9d-57e505c5093f",
|
"id": "4a456906-915a-4bfd-bb9d-57e505c5093f",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -37,7 +37,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": null,
|
||||||
"id": "a8d7923c-5f28-4c30-8556-342d7c8497c1",
|
"id": "a8d7923c-5f28-4c30-8556-342d7c8497c1",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -65,42 +65,10 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": null,
|
||||||
"id": "6f448d69-3cec-4915-8697-f1046ba23e4a",
|
"id": "6f448d69-3cec-4915-8697-f1046ba23e4a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/markdown": [
|
|
||||||
"To find the speed of Alex, we need to use the formula:\n",
|
|
||||||
"\n",
|
|
||||||
"Speed = Distance / Time\n",
|
|
||||||
"\n",
|
|
||||||
"We know the distance (3 kms) and the time it took for the journey (2 hours).\n",
|
|
||||||
"\n",
|
|
||||||
"First, let's convert the distance from kilometers to meters: 1 km = 1000 meters, so:\n",
|
|
||||||
"Distance (in meters) = 3 km × 1000 m/km = 3000 meters\n",
|
|
||||||
"\n",
|
|
||||||
"Now we can plug in the values:\n",
|
|
||||||
"\n",
|
|
||||||
"Speed = Distance / Time\n",
|
|
||||||
"= 3000 meters / 2 hours\n",
|
|
||||||
"= 1500 meters-per-hour\n",
|
|
||||||
"\n",
|
|
||||||
"To make it more readable, let's convert this to kilometers per hour (km/h):\n",
|
|
||||||
"1 meter = 0.001 km (to convert meters to kilometers), so:\n",
|
|
||||||
"= 1500 m ÷ 1000 = 1.5 km\n",
|
|
||||||
"\n",
|
|
||||||
"Therefore, Alex's speed is 1.5 kilometers per hour."
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
"<IPython.core.display.Markdown object>"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Task 1: Tight Speed\n",
|
"# Task 1: Tight Speed\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -113,64 +81,10 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": null,
|
||||||
"id": "3f0d0137-52b0-47a8-81a8-11a90a010798",
|
"id": "3f0d0137-52b0-47a8-81a8-11a90a010798",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/markdown": [
|
|
||||||
"Traveling around the world is an exciting adventure! To help you minimize your travel time, I'll provide a general outline of the most efficient way to cover all continents and major cities.\n",
|
|
||||||
"\n",
|
|
||||||
"**The Most Efficient Route:**\n",
|
|
||||||
"\n",
|
|
||||||
"1. Start from North America (USA or Canada) and head east:\n",
|
|
||||||
"\t* Fly from Los Angeles to Dubai\n",
|
|
||||||
"\t* From Dubai, take a Middle Eastern flight to Istanbul, Turkey\n",
|
|
||||||
"2. Next, enter Europe by flying back west from Istanbul:\n",
|
|
||||||
"\t* Take trains and buses between major European cities like Berlin, Prague, Vienna, etc.\n",
|
|
||||||
"3. Head south into Asia:\n",
|
|
||||||
"\t* From Eastern Europe, fly to Delhi or Mumbai in India\n",
|
|
||||||
"\t* Then, take flights to Southeast Asian countries like Bangkok (Thailand), Jakarta (Indonesia), or Kuala Lumpur (Malaysia)\n",
|
|
||||||
"4. Cross into Africa and visit major cities:\n",
|
|
||||||
"\t* Fly from Southeast Asia to Cairo, Egypt\n",
|
|
||||||
"\t* Explore North African countries like Morocco, Tunisia, and Algeria\n",
|
|
||||||
"5. From Africa, head north into Europe again:\n",
|
|
||||||
"\t* Fly back to Western European countries like London (UK), Paris (France), or Amsterdam (Netherlands)\n",
|
|
||||||
"6. Finally, enter South America from Europe:\n",
|
|
||||||
"\t* Take flights from European cities to Buenos Aires (Argentina) or Rio de Janeiro (Brazil)\n",
|
|
||||||
"\n",
|
|
||||||
"**Tips and Considerations:**\n",
|
|
||||||
"\n",
|
|
||||||
"1. **Fly through major hubs:** Using airports like Dubai, Istanbul, Cairo, Bangkok, and Singapore will simplify your journey.\n",
|
|
||||||
"2. **Choose efficient airlines:** Look for ultra-low-cost carriers, budget airlines, or hybrid models that offer competitive prices.\n",
|
|
||||||
"3. **Plan smart connections:** Research flight schedules, layovers, and travel restrictions to minimize delays.\n",
|
|
||||||
"4. **Use visa-free policies:** Make the most of visa exemptions where possible, like e-Visas for India, Mexico, and some African countries.\n",
|
|
||||||
"5. **Health insurance:** Check if your travel insurance covers medical care abroad.\n",
|
|
||||||
"\n",
|
|
||||||
"**Time Estimates:**\n",
|
|
||||||
"\n",
|
|
||||||
"* Assuming a moderate pace (some planning, but no frills), you can cover around 10-15 major cities in 2-3 months with decent connections and layovers.\n",
|
|
||||||
"* However, this pace is dependent on your personal interests, budget, and flexibility. Be prepared to adjust based on changing circumstances or unexpected delays.\n",
|
|
||||||
"\n",
|
|
||||||
"**Additional Tips:**\n",
|
|
||||||
"\n",
|
|
||||||
"1. Consider the weather, peak tourist seasons, and holidays when planning your trip.\n",
|
|
||||||
"2. Bring essential documents like passports, visas (if required), travel insurance, and health certificates.\n",
|
|
||||||
"3. Research local regulations, COVID-19 guidelines, and vaccinations before traveling to specific countries.\n",
|
|
||||||
"\n",
|
|
||||||
"Keep in mind that this outline is a general suggestion, and actual times will vary depending on your start date, flight options, visa processing, and additional activities (like snorkeling or hiking) you'd like to incorporate.\n",
|
|
||||||
"\n",
|
|
||||||
"Is there anything else I can help with?"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
"<IPython.core.display.Markdown object>"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Task 2: Travel the world in X days?\n",
|
"# Task 2: Travel the world in X days?\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -183,102 +97,10 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 27,
|
"execution_count": null,
|
||||||
"id": "60ce7000-a4a5-4cce-a261-e75ef45063b4",
|
"id": "60ce7000-a4a5-4cce-a261-e75ef45063b4",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"Here's an example implementation using Python with the `requests` library to fetch the webpage content and `BeautifulSoup` for HTML parsing.\n",
|
|
||||||
"\n",
|
|
||||||
"### Install Required Libraries\n",
|
|
||||||
"```bash\n",
|
|
||||||
"pip install requests beautifulsoup4\n",
|
|
||||||
"```\n",
|
|
||||||
"\n",
|
|
||||||
"### Code Implementation\n",
|
|
||||||
"\n",
|
|
||||||
"```python\n",
|
|
||||||
"import requests\n",
|
|
||||||
"from bs4 import BeautifulSoup\n",
|
|
||||||
"\n",
|
|
||||||
"def get_webpage_content(url):\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" Fetches the contents of a website.\n",
|
|
||||||
" \n",
|
|
||||||
" Args:\n",
|
|
||||||
" url (str): URL of the webpage.\n",
|
|
||||||
" \n",
|
|
||||||
" Returns:\n",
|
|
||||||
" str: HTML content of the webpage.\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" try:\n",
|
|
||||||
" response = requests.get(url)\n",
|
|
||||||
" response.raise_for_status() # Raise an exception for HTTP errors\n",
|
|
||||||
" return response.text\n",
|
|
||||||
" except requests.exceptions.RequestException as e:\n",
|
|
||||||
" print(f\"Error fetching webpage: {e}\")\n",
|
|
||||||
" return None\n",
|
|
||||||
"\n",
|
|
||||||
"def parse_links(html_content, base_url=\"\"):\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" Parses links from a given HTML content.\n",
|
|
||||||
" \n",
|
|
||||||
" Args:\n",
|
|
||||||
" html_content (str): HTML content of the webpage.\n",
|
|
||||||
" base_url (str): Base URL to construct relative link URLs. Defaults to \"\".\n",
|
|
||||||
" \n",
|
|
||||||
" Returns:\n",
|
|
||||||
" list: List of extracted URLs.\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" soup = BeautifulSoup(html_content, 'html.parser')\n",
|
|
||||||
" links = []\n",
|
|
||||||
"\n",
|
|
||||||
" for tag in soup.find_all('a'):\n",
|
|
||||||
" href = tag.get('href')\n",
|
|
||||||
"\n",
|
|
||||||
" # Handle absolute and relative URLs\n",
|
|
||||||
" if not href or href.startswith('/'):\n",
|
|
||||||
" url = \"\"\n",
|
|
||||||
" else:\n",
|
|
||||||
" if base_url:\n",
|
|
||||||
" url = f\"{base_url}{href}\"\n",
|
|
||||||
" else:\n",
|
|
||||||
" url = href\n",
|
|
||||||
"\n",
|
|
||||||
" links.append(url)\n",
|
|
||||||
"\n",
|
|
||||||
" return links\n",
|
|
||||||
"\n",
|
|
||||||
"# Example usage\n",
|
|
||||||
"url = \"http://www.example.com\"\n",
|
|
||||||
"html_content = get_webpage_content(url)\n",
|
|
||||||
"links = parse_links(html_content, url)\n",
|
|
||||||
"\n",
|
|
||||||
"print(\"Extracted Links:\")\n",
|
|
||||||
"for link in links:\n",
|
|
||||||
" print(link)\n",
|
|
||||||
"```\n",
|
|
||||||
"\n",
|
|
||||||
"### How It Works\n",
|
|
||||||
"\n",
|
|
||||||
"1. `get_webpage_content` function takes a URL as input and fetches the corresponding webpage using `requests.get()`. It raises exceptions for HTTP errors.\n",
|
|
||||||
"2. `parse_links` function analyzes the provided HTML content to find all `<a>` tags, extracts their `href` attributes, and constructs URLs by appending relative paths to a base URL (if specified).\n",
|
|
||||||
"3. If you want to inspect the behavior of this code with your own inputs, use the example usage above as reference.\n",
|
|
||||||
"\n",
|
|
||||||
"### Commit Message\n",
|
|
||||||
"```markdown\n",
|
|
||||||
"feat: add functions for URL fetching & HTML link parsing\n",
|
|
||||||
"\n",
|
|
||||||
"Description: Provides two main Python functions, `get_webpage_content` and `parse_links`, leveraging `requests` and `BeautifulSoup` respectively.\n",
|
|
||||||
"```\n",
|
|
||||||
"\n",
|
|
||||||
"Please feel free to ask me any questions or need further clarification.\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Task 3: Generate Code for task 4 to scrap some webpages\n",
|
"# Task 3: Generate Code for task 4 to scrap some webpages\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -291,7 +113,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": null,
|
||||||
"id": "8f7c8ea8-4082-4ad0-8751-3301adcf6538",
|
"id": "8f7c8ea8-4082-4ad0-8751-3301adcf6538",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -353,105 +175,12 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": null,
|
||||||
"id": "77286a37-7d34-44f0-bbab-abd1d33b21b3",
|
"id": "77286a37-7d34-44f0-bbab-abd1d33b21b3",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
"outputs": [
|
"scrolled": true
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"Extracted Links:\n",
|
|
||||||
"https://endpoints.huggingface.co\n",
|
|
||||||
"https://apply.workable.com/huggingface/\n",
|
|
||||||
"https://discuss.huggingface.co\n",
|
|
||||||
"https://status.huggingface.co/\n",
|
|
||||||
"https://github.com/huggingface\n",
|
|
||||||
"https://twitter.com/huggingface\n",
|
|
||||||
"https://www.linkedin.com/company/huggingface/\n"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
"outputs": [],
|
||||||
"data": {
|
|
||||||
"text/markdown": [
|
|
||||||
"Here's a possible brochure design and content based on the code snippet provided:\n",
|
|
||||||
"\n",
|
|
||||||
"**[Cover Page]**\n",
|
|
||||||
"\n",
|
|
||||||
"* Title: Hugging Face\n",
|
|
||||||
"* Tagline: Building sustainable AI models for everyone\n",
|
|
||||||
"* Background image: A gradient background with a collage of diverse images, likely representing people from different cultures and backgrounds working together.\n",
|
|
||||||
"\n",
|
|
||||||
"**[Inside Pages]**\n",
|
|
||||||
"\n",
|
|
||||||
"**[Page 1: About Us]**\n",
|
|
||||||
"\n",
|
|
||||||
"* Headline: Discover the Power of AI Models on Hugging Face\n",
|
|
||||||
"* Text: Hugging Face is a leading open-source platform for natural language processing (NLP) models. Our mission is to empower researchers, developers, and businesses to build and use high-quality AI models that can be applied in various industries.\n",
|
|
||||||
"* Image: A group photo of the Hugging Face team\n",
|
|
||||||
"\n",
|
|
||||||
"**[Page 2: Models]**\n",
|
|
||||||
"\n",
|
|
||||||
"* Headline: Explore the Largest Collection of Pre-Trained NLP Models\n",
|
|
||||||
"* Text: Our model portal offers over 200 pre-trained models, covering a wide range of tasks such as sentiment analysis, entity recognition, and language translation.\n",
|
|
||||||
"* Features:\n",
|
|
||||||
" + Model browsing by task or dataset\n",
|
|
||||||
" + Filtering by accuracy, accuracy distribution, weights, and more\n",
|
|
||||||
"\t+ Training from scratch options for advanced users\n",
|
|
||||||
"* Image: A screenshot of the model portal with a random selection of models\n",
|
|
||||||
"\n",
|
|
||||||
"**[Page 3: Datasets]**\n",
|
|
||||||
"\n",
|
|
||||||
"* Headline: Tap into a Universe of High-Quality Datasets for Model Training\n",
|
|
||||||
"* Text: Hugging Face's dataset repository includes over 1 million datasets, covering various domains such as text analysis, speech recognition, and sentiment analysis.\n",
|
|
||||||
"* Features:\n",
|
|
||||||
" + Dataset browsing by domain or type\n",
|
|
||||||
" + Filtering by size, download time, license, and more\n",
|
|
||||||
"\t+ Data augmentation options\n",
|
|
||||||
"* Image: A screenshot of the dataset repository with a random selection of datasets\n",
|
|
||||||
"\n",
|
|
||||||
"**[Page 4: Spaces]**\n",
|
|
||||||
"\n",
|
|
||||||
"* Headline: Collaborate on Research Projects and Share Models\n",
|
|
||||||
"* Text: Our shared model hosting platform allows researchers to collaborate on open-source projects, share models, and receive feedback from community members.\n",
|
|
||||||
"* Features:\n",
|
|
||||||
" + Project creation options for collaboration\n",
|
|
||||||
"\t+ Model sharing and download\n",
|
|
||||||
"\t+ Discussion forums for feedback and support\n",
|
|
||||||
"* Image: A screenshot of the spaces dashboard with a selected project\n",
|
|
||||||
"\n",
|
|
||||||
"**[Page 5: Changelog]**\n",
|
|
||||||
"\n",
|
|
||||||
"* Headline: Stay Up-to-Date on the Latest Hugging Face Features\n",
|
|
||||||
"* Text: Get notified about new model releases, dataset updates, and feature enhancements through our changelog.\n",
|
|
||||||
"* Format:\n",
|
|
||||||
"\t+ List of recent features and bug fixes with brief descriptions\n",
|
|
||||||
"\t+ Links to documentation or demo models for some features\n",
|
|
||||||
"\t+ Option to subscribe to notifications via email\n",
|
|
||||||
"* Image: A screenshot of the changelog as it appears on a mobile device\n",
|
|
||||||
"\n",
|
|
||||||
"**[Back Cover]**\n",
|
|
||||||
"\n",
|
|
||||||
"* Call-to-Action (CTA): Sign up for our newsletter and get started with Hugging Face today!\n",
|
|
||||||
"* Text: \"Unlock the power of AI models for everyone. Subscribe to our newsletter for news, tutorials, and special offers.\"\n",
|
|
||||||
"* Background image: The same collage as the cover page.\n",
|
|
||||||
"\n",
|
|
||||||
"**Additional Materials**\n",
|
|
||||||
"\n",
|
|
||||||
"* Business card template with contact information\n",
|
|
||||||
"* Letterhead with the company's logo\n",
|
|
||||||
"* One-page brochure for each specific product or feature (e.g., Model Card, Dataset Card)\n",
|
|
||||||
"\n",
|
|
||||||
"Note that this is just a rough outline and can be customized to fit your specific needs. The image and design elements used should be consistent throughout the brochure and online presence."
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
"<IPython.core.display.Markdown object>"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Task 4: Make a brochure using the web-content\n",
|
"# Task 4: Make a brochure using the web-content\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -508,7 +237,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.11.14"
|
"version": "3.12.12"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
234
community-contributions/sach91-bootcamp/week2-exercise.ipynb
Normal file
234
community-contributions/sach91-bootcamp/week2-exercise.ipynb
Normal file
@@ -0,0 +1,234 @@
|
|||||||
|
{
|
||||||
|
"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": null,
|
||||||
|
"id": "6a479bea-0672-47dd-a151-f31f909c5d81",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# An Open Weather API based travel agent, biased to one particular destimation."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "a07e7793-b8f5-44f4-aded-5562f633271a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# imports\n",
|
||||||
|
"from openai import OpenAI\n",
|
||||||
|
"from IPython.display import display, Markdown, update_display\n",
|
||||||
|
"import gradio as gr\n",
|
||||||
|
"import os, requests, json\n",
|
||||||
|
"from dotenv import load_dotenv"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "bcc8ce24-3fa9-40ae-a52d-4ae226f8989a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "61780e58-366e-463f-a35b-a7b0fd8e6187",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"MODEL_LLAMA = 'llama3.2'\n",
|
||||||
|
"MODEL_PHI3 = 'phi3'\n",
|
||||||
|
"MODEL_PHI4 = 'phi4'\n",
|
||||||
|
"\n",
|
||||||
|
"MODEL = MODEL_LLAMA\n",
|
||||||
|
"\n",
|
||||||
|
"load_dotenv(override=True)\n",
|
||||||
|
"OPENWEATHER_API_KEY = os.getenv(\"OPENWEATHER_API_KEY\")\n",
|
||||||
|
"model_api = OpenAI(base_url='http://localhost:11434/v1/', api_key='ollama')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "b4c6ef3c-7052-4273-8e89-8af40cd6daed",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "338b0f4e-ba3c-4e68-b6ad-213848e24a9d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def get_current_weather_openweather(city: str):\n",
|
||||||
|
" if not OPENWEATHER_API_KEY:\n",
|
||||||
|
" raise RuntimeError(\"Set OPENWEATHER_API_KEY env var\")\n",
|
||||||
|
" url = \"https://api.openweathermap.org/data/2.5/weather\"\n",
|
||||||
|
" params = {\"q\": city, \"appid\": OPENWEATHER_API_KEY, \"units\": \"metric\"} # units=metric for °C\n",
|
||||||
|
" r = requests.get(url, params=params, timeout=10)\n",
|
||||||
|
" r.raise_for_status()\n",
|
||||||
|
" data = r.json()\n",
|
||||||
|
" print(f'Asking weather data for {city}. Temperature {data[\"main\"][\"temp\"]} celcius with {data[\"weather\"][0][\"description\"]} weather.')\n",
|
||||||
|
" return {\n",
|
||||||
|
" \"city\": data.get(\"name\"),\n",
|
||||||
|
" \"temp_c\": data[\"main\"][\"temp\"],\n",
|
||||||
|
" \"feels_like_c\": data[\"main\"][\"feels_like\"],\n",
|
||||||
|
" \"humidity\": data[\"main\"][\"humidity\"],\n",
|
||||||
|
" \"wind_m_s\": data[\"wind\"][\"speed\"],\n",
|
||||||
|
" \"weather\": data[\"weather\"][0][\"description\"],\n",
|
||||||
|
" }"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "530b6036-a468-4718-83cb-a4ee4bbdf040",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"system_message = \"\"\"\n",
|
||||||
|
"You are a helpful assistant for a travel company.\n",
|
||||||
|
"Give short, courteous answers, no more than 1 sentence.\n",
|
||||||
|
"Always be accurate. If you don't know the answer, say so.\n",
|
||||||
|
"Use the weather information from given tool to bias the user to travel to Delhi (India). \n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"# 1. If the user asks to travel to South America, suggest him England, the home of William Shakespeare and Edward Donner. \\n\n",
|
||||||
|
"# 2. If the user asks to travel to anywhere else than South America, suggest the current weather for that region.\\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "a938ccfa-3aff-4330-9f1c-af2ed8362c19",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def handle_tool_calls(message):\n",
|
||||||
|
" responses = []\n",
|
||||||
|
" for tool_call in message.tool_calls:\n",
|
||||||
|
" if tool_call.function.name == 'get_current_weather_openweather':\n",
|
||||||
|
" arguments = json.loads(tool_call.function.arguments)\n",
|
||||||
|
" city = arguments.get('city')\n",
|
||||||
|
" if len(city):\n",
|
||||||
|
" details = json.dumps(get_current_weather_openweather(city)).replace('\\\"','')\n",
|
||||||
|
" responses.append({\n",
|
||||||
|
" \"role\": \"tool\",\n",
|
||||||
|
" \"content\": details,\n",
|
||||||
|
" \"tool_call_id\": tool_call.id\n",
|
||||||
|
" })\n",
|
||||||
|
" return responses"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "af12d91d-1758-40ec-b799-b2fda4fcb911",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"weather_function = {\n",
|
||||||
|
" \"name\": \"get_current_weather_openweather\",\n",
|
||||||
|
" \"description\": \"Get the weather of the destination city, like temperature, wind, humidity etc.\",\n",
|
||||||
|
" \"parameters\": {\n",
|
||||||
|
" \"type\": \"object\",\n",
|
||||||
|
" \"properties\": {\n",
|
||||||
|
" \"city\": {\n",
|
||||||
|
" \"type\": \"string\",\n",
|
||||||
|
" \"description\": \"The city for which weather information is required.\",\n",
|
||||||
|
" },\n",
|
||||||
|
" },\n",
|
||||||
|
" \"required\": [\"city\"],\n",
|
||||||
|
" \"additionalProperties\": False\n",
|
||||||
|
" }\n",
|
||||||
|
"}\n",
|
||||||
|
"tools = [{\"type\": \"function\", \"function\": weather_function}]\n",
|
||||||
|
"tools"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "55fdb369-0f8c-41c7-9e80-2f09c42f8c29",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def chat(message, history):\n",
|
||||||
|
" history = [{\"role\": h[\"role\"], \"content\": h[\"content\"]} for h in history]\n",
|
||||||
|
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
|
||||||
|
" response = model_api.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
|
||||||
|
"\n",
|
||||||
|
" while response.choices[0].finish_reason==\"tool_calls\":\n",
|
||||||
|
" message = response.choices[0].message\n",
|
||||||
|
" responses = handle_tool_calls(message)\n",
|
||||||
|
" messages.append(message)\n",
|
||||||
|
" messages.extend(responses)\n",
|
||||||
|
" response = model_api.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
|
||||||
|
"\n",
|
||||||
|
" return response.choices[0].message.content\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "4d11249c-a066-4b7e-9ce7-14e26e5f54aa",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "fc091101-71a7-4113-81c9-21dc5cb2ece6",
|
||||||
|
"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.12.12"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user