From the uDemy course on LLM engineering.
https://www.udemy.com/course/llm-engineering-master-ai-and-large-language-models
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
552 lines
26 KiB
552 lines
26 KiB
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "markdown", |
|
"id": "d15d8294-3328-4e07-ad16-8a03e9bbfdb9", |
|
"metadata": {}, |
|
"source": [ |
|
"# Welcome to your first assignment!\n", |
|
"\n", |
|
"Instructions are below. Please give this a try, and look in the solutions folder if you get stuck (or feel free to ask me!)" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "ada885d9-4d42-4d9b-97f0-74fbbbfe93a9", |
|
"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;\">Just before we get to the assignment --</h2>\n", |
|
" <span style=\"color:#f71;\">I thought I'd take a second to point you at this page of useful resources for the course. This includes links to all the slides.<br/>\n", |
|
" <a href=\"https://edwarddonner.com/2024/11/13/llm-engineering-resources/\">https://edwarddonner.com/2024/11/13/llm-engineering-resources/</a><br/>\n", |
|
" Please keep this bookmarked, and I'll continue to add more useful links there over time.\n", |
|
" </span>\n", |
|
" </td>\n", |
|
" </tr>\n", |
|
"</table>" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "6e9fa1fc-eac5-4d1d-9be4-541b3f2b3458", |
|
"metadata": {}, |
|
"source": [ |
|
"# HOMEWORK EXERCISE ASSIGNMENT\n", |
|
"\n", |
|
"Upgrade the day 1 project to summarize a webpage to use an Open Source model running locally via Ollama rather than OpenAI\n", |
|
"\n", |
|
"You'll be able to use this technique for all subsequent projects if you'd prefer not to use paid APIs.\n", |
|
"\n", |
|
"**Benefits:**\n", |
|
"1. No API charges - open-source\n", |
|
"2. Data doesn't leave your box\n", |
|
"\n", |
|
"**Disadvantages:**\n", |
|
"1. Significantly less power than Frontier Model\n", |
|
"\n", |
|
"## Recap on installation of Ollama\n", |
|
"\n", |
|
"Simply visit [ollama.com](https://ollama.com) and install!\n", |
|
"\n", |
|
"Once complete, the ollama server should already be running locally. \n", |
|
"If you visit: \n", |
|
"[http://localhost:11434/](http://localhost:11434/)\n", |
|
"\n", |
|
"You should see the message `Ollama is running`. \n", |
|
"\n", |
|
"If not, bring up a new Terminal (Mac) or Powershell (Windows) and enter `ollama serve` \n", |
|
"And in another Terminal (Mac) or Powershell (Windows), enter `ollama pull llama3.2` \n", |
|
"Then try [http://localhost:11434/](http://localhost:11434/) again.\n", |
|
"\n", |
|
"If Ollama is slow on your machine, try using `llama3.2:1b` as an alternative. Run `ollama pull llama3.2:1b` from a Terminal or Powershell, and change the code below from `MODEL = \"llama3.2\"` to `MODEL = \"llama3.2:1b\"`" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 1, |
|
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd", |
|
"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": 2, |
|
"id": "29ddd15d-a3c5-4f4e-a678-873f56162724", |
|
"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": 3, |
|
"id": "dac0a679-599c-441f-9bf2-ddc73d35b940", |
|
"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": 4, |
|
"id": "7bb9c624-14f0-4945-a719-8ddb64f66f47", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"payload = {\n", |
|
" \"model\": MODEL,\n", |
|
" \"messages\": messages,\n", |
|
" \"stream\": False\n", |
|
" }" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 5, |
|
"id": "479ff514-e8bd-4985-a572-2ea28bb4fa40", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"\u001b[?25lpulling manifest ⠋ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠙ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠹ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠸ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest ⠼ \u001b[?25h\u001b[?25l\u001b[2K\u001b[1Gpulling manifest \n", |
|
"pulling dde5aa3fc5ff... 100% ▕████████████████▏ 2.0 GB \n", |
|
"pulling 966de95ca8a6... 100% ▕████████████████▏ 1.4 KB \n", |
|
"pulling fcc5a6bec9da... 100% ▕████████████████▏ 7.7 KB \n", |
|
"pulling a70ff7e570d9... 100% ▕████████████████▏ 6.0 KB \n", |
|
"pulling 56bb8bd477a5... 100% ▕████████████████▏ 96 B \n", |
|
"pulling 34bb5ab01051... 100% ▕████████████████▏ 561 B \n", |
|
"verifying sha256 digest \n", |
|
"writing manifest \n", |
|
"success \u001b[?25h\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"# Let's just make sure the model is loaded\n", |
|
"\n", |
|
"!ollama pull llama3.2" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 6, |
|
"id": "42b9f644-522d-4e05-a691-56e7658c0ea9", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Generative AI has numerous business applications across various industries. Here are some examples:\n", |
|
"\n", |
|
"1. **Content Creation**: Generative AI can generate high-quality content such as images, videos, music, and text. This can be used for:\n", |
|
" * Creating social media posts, blog articles, and other marketing materials\n", |
|
" * Generating product descriptions, reviews, and testimonials\n", |
|
" * Producing original artwork, graphics, and designs\n", |
|
"2. **Marketing Automation**: Generative AI can automate repetitive marketing tasks, such as:\n", |
|
" * Personalizing email campaigns and subject lines\n", |
|
" * Creating personalized product recommendations\n", |
|
" * Generating social media posts and ads\n", |
|
"3. **Customer Service**: Generative AI can help customer service teams with tasks like:\n", |
|
" * Responding to customer inquiries and support requests\n", |
|
" * Generating automated responses for FAQs and common issues\n", |
|
" * Providing personalized product recommendations and suggestions\n", |
|
"4. **Sales Forecasting**: Generative AI can analyze historical data and market trends to generate sales forecasts, helping businesses:\n", |
|
" * Predict revenue growth and identify areas of opportunity\n", |
|
" * Optimize pricing strategies and inventory management\n", |
|
" * Make informed decisions about investments and resource allocation\n", |
|
"5. **Product Development**: Generative AI can assist product development teams with tasks like:\n", |
|
" * Generating new product ideas and concepts\n", |
|
" * Designing prototypes and 3D models\n", |
|
" * Creating simulations and testing scenarios\n", |
|
"6. **Financial Analysis**: Generative AI can analyze large datasets to generate insights and predictions, helping businesses:\n", |
|
" * Identify trends and patterns in financial data\n", |
|
" * Predict market fluctuations and economic indicators\n", |
|
" * Optimize investment portfolios and risk management strategies\n", |
|
"7. **Supply Chain Optimization**: Generative AI can help optimize supply chain operations by:\n", |
|
" * Analyzing traffic patterns and predicting demand spikes\n", |
|
" * Identifying optimal routes and inventory levels\n", |
|
" * Optimizing logistics and transportation planning\n", |
|
"8. **Predictive Maintenance**: Generative AI can analyze sensor data to predict equipment failures, helping businesses:\n", |
|
" * Optimize maintenance schedules and reduce downtime\n", |
|
" * Predict and prevent equipment failures\n", |
|
" * Reduce costs and improve overall efficiency\n", |
|
"9. **Customer Segmentation**: Generative AI can help identify high-value customer segments, enabling businesses to:\n", |
|
" * Target specific demographics and interests with personalized marketing campaigns\n", |
|
" * Develop targeted product offerings and services\n", |
|
" * Improve customer retention and loyalty programs\n", |
|
"10. **Intellectual Property Protection**: Generative AI can help protect intellectual property by:\n", |
|
" * Generating unique and original content that avoids copyright infringement\n", |
|
" * Analyzing competitors' content for potential IP risks\n", |
|
" * Providing insights on potential patent applications\n", |
|
"\n", |
|
"These are just a few examples of the many business applications of Generative AI. As the technology continues to evolve, we can expect to see even more innovative uses across various industries.\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"# If this doesn't work for any reason, try the 2 versions in the following cells\n", |
|
"# And double check the instructions in the 'Recap on installation of Ollama' at the top of this lab\n", |
|
"# And if none of that works - contact me!\n", |
|
"\n", |
|
"response = requests.post(OLLAMA_API, json=payload, headers=HEADERS)\n", |
|
"print(response.json()['message']['content'])" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "6a021f13-d6a1-4b96-8e18-4eae49d876fe", |
|
"metadata": {}, |
|
"source": [ |
|
"# Introducing the ollama package\n", |
|
"\n", |
|
"And now we'll do the same thing, but using the elegant ollama python package instead of a direct HTTP call.\n", |
|
"\n", |
|
"Under the hood, it's making the same call as above to the ollama server running at localhost:11434" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 9, |
|
"id": "7745b9c4-57dc-4867-9180-61fa5db55eb8", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Collecting ollama\n", |
|
" Downloading ollama-0.4.7-py3-none-any.whl.metadata (4.7 kB)\n", |
|
"Requirement already satisfied: httpx<0.29,>=0.27 in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from ollama) (0.28.1)\n", |
|
"Requirement already satisfied: pydantic<3.0.0,>=2.9.0 in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from ollama) (2.10.6)\n", |
|
"Requirement already satisfied: anyio in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from httpx<0.29,>=0.27->ollama) (4.8.0)\n", |
|
"Requirement already satisfied: certifi in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from httpx<0.29,>=0.27->ollama) (2024.12.14)\n", |
|
"Requirement already satisfied: httpcore==1.* in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from httpx<0.29,>=0.27->ollama) (1.0.7)\n", |
|
"Requirement already satisfied: idna in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from httpx<0.29,>=0.27->ollama) (3.10)\n", |
|
"Requirement already satisfied: h11<0.15,>=0.13 in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from httpcore==1.*->httpx<0.29,>=0.27->ollama) (0.14.0)\n", |
|
"Requirement already satisfied: annotated-types>=0.6.0 in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.9.0->ollama) (0.7.0)\n", |
|
"Requirement already satisfied: pydantic-core==2.27.2 in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.9.0->ollama) (2.27.2)\n", |
|
"Requirement already satisfied: typing-extensions>=4.12.2 in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.9.0->ollama) (4.12.2)\n", |
|
"Requirement already satisfied: sniffio>=1.1 in /Users/adang4/Documents/devwork/udemy/ai/llm_engineering/venv/lib/python3.11/site-packages (from anyio->httpx<0.29,>=0.27->ollama) (1.3.1)\n", |
|
"Downloading ollama-0.4.7-py3-none-any.whl (13 kB)\n", |
|
"Installing collected packages: ollama\n", |
|
"Successfully installed ollama-0.4.7\n", |
|
"Note: you may need to restart the kernel to use updated packages.\n", |
|
"Generative AI has numerous business applications across various industries, including:\n", |
|
"\n", |
|
"1. **Content Generation**: AI can generate high-quality content such as articles, blog posts, social media posts, and product descriptions, freeing up human writers' time and increasing content volume.\n", |
|
"2. **Image and Video Creation**: Generative AI can create realistic images, videos, and animations for advertising, marketing, and entertainment purposes.\n", |
|
"3. **Chatbots and Virtual Assistants**: AI-powered chatbots can provide 24/7 customer support, answering frequently asked questions, and helping customers with basic queries.\n", |
|
"4. **Personalized Recommendations**: Generative AI can analyze user behavior and preferences to offer personalized product recommendations, increasing sales and improving customer satisfaction.\n", |
|
"5. **Predictive Maintenance**: AI can analyze sensor data from machines and equipment to predict maintenance needs, reducing downtime and increasing overall efficiency.\n", |
|
"6. **Financial Modeling**: Generative AI can create complex financial models, forecast revenue, and identify investment opportunities, helping businesses make informed decisions.\n", |
|
"7. **Marketing Automation**: AI-powered marketing automation platforms can help automate tasks such as email marketing, lead generation, and social media management.\n", |
|
"8. **Influencer Marketing**: Generative AI can analyze social media data to suggest potential influencers for a brand's influencer marketing campaigns.\n", |
|
"9. **Product Design**: AI can generate 3D models of products, reducing the time and cost associated with product design and development.\n", |
|
"10. **Supply Chain Optimization**: Generative AI can analyze supply chain data to optimize inventory levels, predict demand, and identify bottlenecks in logistics.\n", |
|
"\n", |
|
"Some specific business use cases for Generative AI include:\n", |
|
"\n", |
|
"1. **Automated customer service**: Use generative AI to create chatbots that can handle simple customer inquiries.\n", |
|
"2. **Content optimization**: Use generative AI to generate high-quality content that is optimized for search engines.\n", |
|
"3. **Predictive sales forecasting**: Use generative AI to analyze historical data and predict future sales revenue.\n", |
|
"4. **Automated marketing campaigns**: Use generative AI to create personalized marketing campaigns based on customer behavior.\n", |
|
"5. **Product design and development**: Use generative AI to generate 3D models of products, reducing the time and cost associated with product design.\n", |
|
"\n", |
|
"These are just a few examples of the many business applications of Generative AI. As the technology continues to evolve, we can expect to see even more innovative use cases across various industries.\n" |
|
] |
|
} |
|
], |
|
"source": [ |
|
"%pip install ollama\n", |
|
"\n", |
|
"import ollama\n", |
|
"\n", |
|
"response = ollama.chat(model=MODEL, messages=messages)\n", |
|
"print(response['message']['content'])" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "a4704e10-f5fb-4c15-a935-f046c06fb13d", |
|
"metadata": {}, |
|
"source": [ |
|
"## Alternative approach - using OpenAI python library to connect to Ollama" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 10, |
|
"id": "23057e00-b6fc-4678-93a9-6b31cb704bff", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Generative AI has numerous business applications across various industries. Here are some examples:\n", |
|
"\n", |
|
"1. **Content Creation**: Companies can use generative AI to generate content such as blog posts, articles, social media posts, and product descriptions. This can help reduce content creation time and improve consistency.\n", |
|
"2. **Marketing Automation**: Generative AI can be used to create personalized marketing campaigns by generating custom messages, offers, and target audiences based on customer data and preferences.\n", |
|
"3. **Chatbots and Customer Service**: Generative AI-powered chatbots can simulate human-like conversations and provide instant support to customers, helping to improve the overall customer experience.\n", |
|
"4. **Product Design and Development**: Generative AI can be used to generate new product ideas, design concepts, and prototypes, reducing the time and cost associated with traditional product development methods.\n", |
|
"5. **Image and Video Editing**: Companies can use generative AI to edit images and videos automatically, such as removing noise or adjusting lighting and color balance.\n", |
|
"6. **Music Composition**: Generative AI can be used to create music tracks, sound effects, and other audio content, helping musicians and composers to generate new ideas or finish unfinished projects.\n", |
|
"7. **Financial Analysis**: Generative AI can be used to analyze financial data, identify trends, and predict market behavior, helping investors to make more informed decisions.\n", |
|
"8. **Supply Chain Optimization**: Generative AI can be used to optimize supply chain operations by predicting demand, identifying bottlenecks, and suggesting new logistics routes.\n", |
|
"9. **HR Recruitment**: Generative AI can be used to generate job descriptions, interview questions, and candidate feedback, helping human resources teams to streamline the recruitment process.\n", |
|
"10. **Data augmentation**: Generative AI can be used to augment existing datasets, making them more diverse and representative, which can improve the accuracy of machine learning models.\n", |
|
"11. **Recommendation Systems**: Generative AI can be used to generate personalized product recommendations, content suggestions, and travel plans based on customer behavior and preferences.\n", |
|
"12. **Speech Recognition**: Generative AI-powered speech recognition systems can help improve the accuracy of voice-to-text transcriptions, audio transcription, and speech-based interfaces.\n", |
|
"\n", |
|
"Some specific business use cases for generative AI include:\n", |
|
"\n", |
|
"* **Netflix**: Using generative AI to predict user viewing habits and generate personalized content recommendations.\n", |
|
"* **Walmart**: Using generative AI to create personalized marketing campaigns based on customer behavior and preferences.\n", |
|
"* **IBM**: Using generative AI to create personalized healthcare treatment plans for patients.\n", |
|
"* **Microsoft**: Using generative AI to improve the accuracy of speech-to-text transcriptions and audio transcription.\n", |
|
"\n", |
|
"These are just a few examples of the many potential business applications of generative AI. As this technology continues to evolve, we can expect to see even more innovative solutions in the future.\n" |
|
] |
|
} |
|
], |
|
"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": "markdown", |
|
"id": "1622d9bb-5c68-4d4e-9ca4-b492c751f898", |
|
"metadata": {}, |
|
"source": [ |
|
"# NOW the exercise for you\n", |
|
"\n", |
|
"Take the code from day1 and incorporate it here, to build a website summarizer that uses Llama 3.2 running locally instead of OpenAI; use either of the above approaches." |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 14, |
|
"id": "402d5686-4e76-4110-b65a-b3906c35c0a4", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# imports\n", |
|
"\n", |
|
"import requests\n", |
|
"from bs4 import BeautifulSoup\n", |
|
"from IPython.display import Markdown, display\n" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 18, |
|
"id": "b214df93", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# 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", |
|
" \"Content-Type\": \"application/json\"\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)\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", |
|
"def messages_for(website):\n", |
|
" return [\n", |
|
" {\"role\": \"system\", \"content\": system_prompt},\n", |
|
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n", |
|
" ]\n" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 32, |
|
"id": "0f2e95cb", |
|
"metadata": {}, |
|
"outputs": [], |
|
"source": [ |
|
"# Use openai api call to ollama_api\n", |
|
"from openai import OpenAI\n", |
|
"\n", |
|
"# Using ollama python package\n", |
|
"import ollama\n", |
|
"\n", |
|
"\n", |
|
"def summarize(url):\n", |
|
" website = Website(url)\n", |
|
"\n", |
|
" # 1. Use openai api to call ollama api on localhost\n", |
|
" #MODEL = \"llama3.2\"\n", |
|
" #openai_to_ollama = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n", |
|
" #response = openai_to_ollama.chat.completions.create( \n", |
|
" # model=MODEL,\n", |
|
" # messages=messages_for(website)\n", |
|
" #)\n", |
|
" #return response.choices[0].message.content\n", |
|
"\n", |
|
"\n", |
|
" # 2. Use ollama python package to call ollama api on localhost\n", |
|
" MODEL = \"llama3.2\"\n", |
|
" response = ollama.chat(\n", |
|
" model=MODEL,\n", |
|
" messages=messages_for(website)\n", |
|
" )\n", |
|
" print(response['message']['content'])\n", |
|
"\n", |
|
"def display_summary(url):\n", |
|
" summary = summarize(url)\n", |
|
" display(Markdown(summary))\n", |
|
"\n", |
|
"#####\n" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 33, |
|
"id": "110c93bc", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"Here's a summary of the article:\n", |
|
"\n", |
|
"**US Markets Update**\n", |
|
"\n", |
|
"The US stock market closed for the day, with the Dow Jones Industrial Average (DJIA) down 485 points (-1.09%) and the Nasdaq Futures down 436 points (-2.02%). The S&P 500 futures were also lower by 93 points (-1.55%).\n", |
|
"\n", |
|
"**Top Gainers**\n", |
|
"\n", |
|
"* Viavi Solutions Inc. (VIAV): +2.05 (+20.52%)\n", |
|
"* Atlassian Corporation (TEAM): +39.83 (+14.92%)\n", |
|
"* AST SpaceMobile, Inc. (ASTS): +2.03 (+11.15%)\n", |
|
"\n", |
|
"**Top Losers**\n", |
|
"\n", |
|
"* Deckers Outdoor Corporation (DECK): -45.75 (-20.51%)\n", |
|
"* Walgreens Boots Alliance, Inc. (WBA): -1.18 (-10.30%)\n", |
|
"\n", |
|
"**Economic Events**\n", |
|
"\n", |
|
"No major economic events were reported today.\n", |
|
"\n", |
|
"**Cryptocurrency Prices**\n", |
|
"\n", |
|
"The current prices for cryptocurrencies are:\n", |
|
"\n", |
|
"* Ethereum (ETH-USD): 2,817.09 - 322.27 (-10.27%)\n", |
|
"* Bitcoin is not mentioned in this article.\n", |
|
"\n", |
|
"**Stock Market Watchlist**\n", |
|
"\n", |
|
"The watchlist includes stocks such as:\n", |
|
"\n", |
|
"* NVIDIA Corporation (NVDA)\n", |
|
"* Rigetti Computing, Inc. (RGTI)\n", |
|
"* Intel Corporation (INTC)\n", |
|
"* Ford Motor Company (F)\n", |
|
"* Apple Inc. (AAPL)\n", |
|
"\n", |
|
"Please note that this is a summary of the article and not an exhaustive report on the stock market.\n" |
|
] |
|
}, |
|
{ |
|
"data": { |
|
"text/plain": [ |
|
"<IPython.core.display.Markdown object>" |
|
] |
|
}, |
|
"metadata": {}, |
|
"output_type": "display_data" |
|
} |
|
], |
|
"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.\"\n", |
|
"\n", |
|
"display_summary(\"https://finance.yahoo.com\")\n" |
|
] |
|
} |
|
], |
|
"metadata": { |
|
"kernelspec": { |
|
"display_name": "venv", |
|
"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 |
|
}
|
|
|