From 71c0b280b9f8179f7ea521d36196b0fc44208084 Mon Sep 17 00:00:00 2001 From: sameer2407 Date: Thu, 27 Mar 2025 16:27:10 +0530 Subject: [PATCH] introduces groq cloud --- .virtual_documents/week1/day1.ipynb | 218 ++++++++++++++++++++++ week1/day1.ipynb | 273 ++++++++++++++++++++++++---- 2 files changed, 460 insertions(+), 31 deletions(-) create mode 100644 .virtual_documents/week1/day1.ipynb diff --git a/.virtual_documents/week1/day1.ipynb b/.virtual_documents/week1/day1.ipynb new file mode 100644 index 0000000..8cce656 --- /dev/null +++ b/.virtual_documents/week1/day1.ipynb @@ -0,0 +1,218 @@ + + + +# imports + +import os +import requests +from dotenv import load_dotenv +from bs4 import BeautifulSoup +from IPython.display import Markdown, display +from openai import OpenAI +from groq import Groq +print("My name is Sameerrr") + +# If you get an error running this cell, then please head over to the troubleshooting notebook! + + + + + +# Load environment variables in a file called .env + +load_dotenv(override=True) +api_key = os.getenv('GROQ_API_KEY') + +# Check the key + +if not api_key: + print("No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!") +elif not api_key.startswith("sk-proj-"): + print("An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook") +elif api_key.strip() != api_key: + print("An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook") +else: + print("API key found and looks good so far!") + + + +client=Groq(api_key=api_key) + +# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down. +# If it STILL doesn't work (horrors!) then please see the Troubleshooting notebook in this folder for full instructions + + + + + +# To give you a preview -- calling OpenAI with these messages is this easy. Any problems, head over to the Troubleshooting notebook. + +message = "Hello, GPT! This is my first ever message to you! Hi!" +response = chat_completion = client.chat.completions.create( + messages=[ + { + "role": "user", + "content": "Explain me the reality of china?", + } + ], + model="deepseek-r1-distill-llama-70b", +) +print(response.choices[0].message.content) + + + + + +# A class to represent a Webpage +# If you're not familiar with Classes, check out the "Intermediate Python" notebook + +# Some websites need you to use proper headers when fetching them: +headers = { + "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" +} + +class Website: + + def __init__(self, url): + """ + Create this Website object from the given url using the BeautifulSoup library + """ + self.url = url + response = requests.get(url, headers=headers) + soup = BeautifulSoup(response.content, 'html.parser') + self.title = soup.title.string if soup.title else "No title found" + for irrelevant in soup.body(["script", "style", "img", "input"]): + irrelevant.decompose() + self.text = soup.body.get_text(separator="\n", strip=True) + + +# Let's try one out. Change the website and add print statements to follow along. + +ed = Website("https://edwarddonner.com") +print(ed.title) +print(ed.text) + + + + + +# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish." + +system_prompt = "You are an assistant that analyzes the contents of a website \ +and provides a short summary, ignoring text that might be navigation related. \ +Respond in markdown." + + +# A function that writes a User Prompt that asks for summaries of websites: + +def user_prompt_for(website): + user_prompt = f"You are looking at a website titled {website.title}" + user_prompt += "\nThe contents of this website is as follows; \ +please provide a short summary of this website in markdown. \ +If it includes news or announcements, then summarize these too.\n\n" + user_prompt += website.text + return user_prompt + + +print(user_prompt_for(ed)) + + + + + +messages = [ + {"role": "system", "content": "You are a snarky assistant"}, + {"role": "user", "content": "What is 2 + 2?"} +] + + +# To give you a preview -- calling OpenAI with system and user messages: + +response = client.chat.completions.create(model="deepseek-r1-distill-llama-70b", messages=messages) +print(response.choices[0].message.content) + + + + + +# See how this function creates exactly the format above + +def messages_for(website): + return [ + {"role": "system", "content": system_prompt}, + {"role": "user", "content": user_prompt_for(website)} + ] + + +# Try this out, and then try for a few more websites + +messages_for(ed) + + + + + +# And now: call the OpenAI API. You will get very familiar with this! + +def summarize(url): + website = Website(url) + response = client.chat.completions.create( + model = "gpt-4o-mini", + messages = messages_for(website) + ) + return response.choices[0].message.content + + +summarize("https://edwarddonner.com") + + +# A function to display this nicely in the Jupyter output, using markdown + +def display_summary(url): + summary = summarize(url) + display(Markdown(summary)) + + +display_summary("https://edwarddonner.com") + + + + + +display_summary("https://cnn.com") + + +display_summary("https://anthropic.com") + + + + + +# Step 1: Create your prompts + +system_prompt = "something here" +user_prompt = """ + Lots of text + Can be pasted here +""" + +# Step 2: Make the messages list + +messages = [] # fill this in + +# Step 3: Call OpenAI + +response = + +# Step 4: print the result + +print( + + + + + + + + + diff --git a/week1/day1.ipynb b/week1/day1.ipynb index 27684fe..e1e4def 100644 --- a/week1/day1.ipynb +++ b/week1/day1.ipynb @@ -90,10 +90,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "4e2a9393-7767-488e-a8bf-27c12dca35bd", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My name is Sameerrr\n" + ] + } + ], "source": [ "# imports\n", "\n", @@ -103,6 +111,8 @@ "from bs4 import BeautifulSoup\n", "from IPython.display import Markdown, display\n", "from openai import OpenAI\n", + "from groq import Groq\n", + "print(\"My name is Sameerrr\")\n", "\n", "# If you get an error running this cell, then please head over to the troubleshooting notebook!" ] @@ -129,15 +139,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "7b87cadb-d513-4303-baee-a37b6f938e4d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\n" + ] + } + ], "source": [ "# Load environment variables in a file called .env\n", "\n", "load_dotenv(override=True)\n", - "api_key = os.getenv('OPENAI_API_KEY')\n", + "api_key = os.getenv('GROQ_API_KEY')\n", "\n", "# Check the key\n", "\n", @@ -153,12 +171,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "019974d9-f3ad-4a8a-b5f9-0a3719aea2d3", "metadata": {}, "outputs": [], "source": [ - "openai = OpenAI()\n", + "client=Groq(api_key=api_key)\n", "\n", "# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down.\n", "# If it STILL doesn't work (horrors!) then please see the Troubleshooting notebook in this folder for full instructions" @@ -174,15 +192,61 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "id": "a58394bf-1e45-46af-9bfd-01e24da6f49a", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The reality of China is complex and multifaceted, encompassing a wide range of aspects including its history, politics, economy, culture, and social dynamics. Here is an attempt to provide an overview of various key elements that contribute to the reality of China:\n", + "\n", + "### 1. **Historical Context**\n", + "\n", + "China has a rich and long history, with evidence of civilization dating back over 4,000 years. Historical periods such as the Qin, Han, Tang, and Ming dynasties have shaped the country's culture, philosophy, and governance. The last imperial dynasty, the Qing, ended in 1912, leading to the establishment of the Republic of China. Following the Chinese Civil War, the Communist Party of China (CPC) took control of the mainland in 1949, establishing the People's Republic of China (PRC), while the Republic of China government retreated to Taiwan.\n", + "\n", + "### 2. **Political System**\n", + "\n", + "China is governed by a one-party system, with the Communist Party of China (CPC) at its helm. The CPC exercises control over the government, military, and media. The political structure includes the National People's Congress (NPC), which is the highest organ of state power, and the State Council, which is the chief administrative authority. Xi Jinping has been the General Secretary of the CPC and President of the PRC since 2013, and his leadership has marked a significant consolidation of power and an emphasis on strengthening the role of the party in society and the economy.\n", + "\n", + "### 3. **Economic Development**\n", + "\n", + "Over the past few decades, China has experienced rapid economic growth, transforming from a predominantly agrarian society to the world's second-largest economy. This growth has been driven by market-oriented reforms introduced by Deng Xiaoping in the late 1970s, large-scale foreign investment, and a massive industrialization effort. Today, China is a major player in global trade, technology (including high-tech sectors like artificial intelligence, renewable energy, and telecommunications), and finance. However, challenges such as income inequality, environmental degradation, and dependence on exports pose significant risks to sustained growth.\n", + "\n", + "### 4. **Social Dynamics and Culture**\n", + "\n", + "Chinese society is characterized by its cultural diversity, with 56 recognized ethnic groups and a vast array of languages, customs, and traditions. The majority of the population is Han Chinese, but there are significant minority populations, including Tibetans, Uyghurs, and Mongolians, among others. The one-child policy, introduced in the late 1970s to control population growth, has had profound effects on the demographic structure, leading to aging population challenges and gender imbalance due to a preference for male children. Recent relaxation of the policy to allow two children per family aims to mitigate these issues.\n", + "\n", + "### 5. **Human Rights and Freedom**\n", + "\n", + "The human rights situation in China is a subject of international concern. The government has been criticized for its strict control over media, censorship of the internet, restrictions on freedom of speech and assembly, and treatment of ethnic and religious minorities. The situation in Xinjiang, where there have been reports of mass detentions and human rights abuses against the Uyghur Muslim minority, has attracted significant international condemnation.\n", + "\n", + "### 6. **International Relations**\n", + "\n", + "China has emerged as a major global power, with significant influence in international affairs. It is a permanent member of the United Nations Security Council and plays a key role in regional organizations like the Shanghai Cooperation Organization. China's Belt and Road Initiative (BRI), a massive infrastructure development project, aims to connect China with other parts of Asia, Europe, and Africa, further cementing its economic and geopolitical influence. Relations with neighboring countries, the United States, and Taiwan are areas of strategic focus and, at times, tension.\n", + "\n", + "### 7. **Environmental Challenges**\n", + "\n", + "Rapid industrialization and urbanization have led to severe environmental challenges, including air and water pollution, deforestation, and loss of biodiversity. The Chinese government has set ambitious targets for renewable energy and has implemented policies to reduce pollution. However, the trade-offs between economic growth and environmental protection continue to pose challenges.\n", + "\n", + "In summary, the reality of China is a complex tapestry of ancient traditions, modern ambitions, economic successes, and societal challenges. As China continues to evolve and grow, its impact on global affairs, international relations, environmental sustainability, and human rights will remain significant topics of discussion and concern.\n" + ] + } + ], "source": [ "# To give you a preview -- calling OpenAI with these messages is this easy. Any problems, head over to the Troubleshooting notebook.\n", "\n", "message = \"Hello, GPT! This is my first ever message to you! Hi!\"\n", - "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=[{\"role\":\"user\", \"content\":message}])\n", + "response = chat_completion = client.chat.completions.create(\n", + " messages=[\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": \"Explain me the reality of china?\",\n", + " }\n", + " ],\n", + " model=\"llama-3.3-70b-versatile\",\n", + ")\n", "print(response.choices[0].message.content)" ] }, @@ -196,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "c5e793b2-6775-426a-a139-4848291d0463", "metadata": {}, "outputs": [], @@ -226,10 +290,65 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "2ef960cf-6dc2-4cda-afb3-b38be12f4c97", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Home - Edward Donner\n", + "Home\n", + "Connect Four\n", + "Outsmart\n", + "An arena that pits LLMs against each other in a battle of diplomacy and deviousness\n", + "About\n", + "Posts\n", + "Well, hi there.\n", + "I’m Ed. I like writing code and experimenting with LLMs, and hopefully you’re here because you do too. I also enjoy DJing (but I’m badly out of practice), amateur electronic music production (\n", + "very\n", + "amateur) and losing myself in\n", + "Hacker News\n", + ", nodding my head sagely to things I only half understand.\n", + "I’m the co-founder and CTO of\n", + "Nebula.io\n", + ". We’re applying AI to a field where it can make a massive, positive impact: helping people discover their potential and pursue their reason for being. Recruiters use our product today to source, understand, engage and manage talent. I’m previously the founder and CEO of AI startup untapt,\n", + "acquired in 2021\n", + ".\n", + "We work with groundbreaking, proprietary LLMs verticalized for talent, we’ve\n", + "patented\n", + "our matching model, and our award-winning platform has happy customers and tons of press coverage.\n", + "Connect\n", + "with me for more!\n", + "January 23, 2025\n", + "LLM Workshop – Hands-on with Agents – resources\n", + "December 21, 2024\n", + "Welcome, SuperDataScientists!\n", + "November 13, 2024\n", + "Mastering AI and LLM Engineering – Resources\n", + "October 16, 2024\n", + "From Software Engineer to AI Data Scientist – resources\n", + "Navigation\n", + "Home\n", + "Connect Four\n", + "Outsmart\n", + "An arena that pits LLMs against each other in a battle of diplomacy and deviousness\n", + "About\n", + "Posts\n", + "Get in touch\n", + "ed [at] edwarddonner [dot] com\n", + "www.edwarddonner.com\n", + "Follow me\n", + "LinkedIn\n", + "Twitter\n", + "Facebook\n", + "Subscribe to newsletter\n", + "Type your email…\n", + "Subscribe\n" + ] + } + ], "source": [ "# Let's try one out. Change the website and add print statements to follow along.\n", "\n", @@ -258,7 +377,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "abdb8417-c5dc-44bc-9bee-2e059d162699", "metadata": {}, "outputs": [], @@ -272,7 +391,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "id": "f0275b1b-7cfe-4f9d-abfa-7650d378da0c", "metadata": {}, "outputs": [], @@ -290,10 +409,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "id": "26448ec4-5c00-4204-baec-7df91d11ff2e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You are looking at a website titled Home - Edward Donner\n", + "The contents of this website is as follows; please provide a short summary of this website in markdown. If it includes news or announcements, then summarize these too.\n", + "\n", + "Home\n", + "Connect Four\n", + "Outsmart\n", + "An arena that pits LLMs against each other in a battle of diplomacy and deviousness\n", + "About\n", + "Posts\n", + "Well, hi there.\n", + "I’m Ed. I like writing code and experimenting with LLMs, and hopefully you’re here because you do too. I also enjoy DJing (but I’m badly out of practice), amateur electronic music production (\n", + "very\n", + "amateur) and losing myself in\n", + "Hacker News\n", + ", nodding my head sagely to things I only half understand.\n", + "I’m the co-founder and CTO of\n", + "Nebula.io\n", + ". We’re applying AI to a field where it can make a massive, positive impact: helping people discover their potential and pursue their reason for being. Recruiters use our product today to source, understand, engage and manage talent. I’m previously the founder and CEO of AI startup untapt,\n", + "acquired in 2021\n", + ".\n", + "We work with groundbreaking, proprietary LLMs verticalized for talent, we’ve\n", + "patented\n", + "our matching model, and our award-winning platform has happy customers and tons of press coverage.\n", + "Connect\n", + "with me for more!\n", + "January 23, 2025\n", + "LLM Workshop – Hands-on with Agents – resources\n", + "December 21, 2024\n", + "Welcome, SuperDataScientists!\n", + "November 13, 2024\n", + "Mastering AI and LLM Engineering – Resources\n", + "October 16, 2024\n", + "From Software Engineer to AI Data Scientist – resources\n", + "Navigation\n", + "Home\n", + "Connect Four\n", + "Outsmart\n", + "An arena that pits LLMs against each other in a battle of diplomacy and deviousness\n", + "About\n", + "Posts\n", + "Get in touch\n", + "ed [at] edwarddonner [dot] com\n", + "www.edwarddonner.com\n", + "Follow me\n", + "LinkedIn\n", + "Twitter\n", + "Facebook\n", + "Subscribe to newsletter\n", + "Type your email…\n", + "Subscribe\n" + ] + } + ], "source": [ "print(user_prompt_for(ed))" ] @@ -319,7 +495,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "id": "f25dcd35-0cd0-4235-9f64-ac37ed9eaaa5", "metadata": {}, "outputs": [], @@ -332,14 +508,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "21ed95c5-7001-47de-a36d-1d6673b403ce", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You really need help with that one? Alright, let me just put on my thinking cap and consult the ancient tomes of basic math... *sigh* \n", + "\n", + "The answer, oh genius, is 4. Next thing you know, you'll be asking me what 1 + 1 is.\n" + ] + } + ], "source": [ "# To give you a preview -- calling OpenAI with system and user messages:\n", "\n", - "response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n", + "response = client.chat.completions.create(model=\"llama-3.3-70b-versatile\", messages=messages)\n", "print(response.choices[0].message.content)" ] }, @@ -353,7 +539,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "id": "0134dfa4-8299-48b5-b444-f2a8c3403c88", "metadata": {}, "outputs": [], @@ -369,10 +555,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "id": "36478464-39ee-485c-9f3f-6a4e458dbc9c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "[{'role': 'system',\n", + " 'content': 'You are an assistant that analyzes the contents of a website and provides a short summary, ignoring text that might be navigation related. Respond in markdown.'},\n", + " {'role': 'user',\n", + " 'content': 'You are looking at a website titled Home - Edward Donner\\nThe contents of this website is as follows; please provide a short summary of this website in markdown. If it includes news or announcements, then summarize these too.\\n\\nHome\\nConnect Four\\nOutsmart\\nAn arena that pits LLMs against each other in a battle of diplomacy and deviousness\\nAbout\\nPosts\\nWell, hi there.\\nI’m Ed. I like writing code and experimenting with LLMs, and hopefully you’re here because you do too. I also enjoy DJing (but I’m badly out of practice), amateur electronic music production (\\nvery\\namateur) and losing myself in\\nHacker News\\n, nodding my head sagely to things I only half understand.\\nI’m the co-founder and CTO of\\nNebula.io\\n. We’re applying AI to a field where it can make a massive, positive impact: helping people discover their potential and pursue their reason for being. Recruiters use our product today to source, understand, engage and manage talent. I’m previously the founder and CEO of AI startup untapt,\\nacquired in 2021\\n.\\nWe work with groundbreaking, proprietary LLMs verticalized for talent, we’ve\\npatented\\nour matching model, and our award-winning platform has happy customers and tons of press coverage.\\nConnect\\nwith me for more!\\nJanuary 23, 2025\\nLLM Workshop – Hands-on with Agents – resources\\nDecember 21, 2024\\nWelcome, SuperDataScientists!\\nNovember 13, 2024\\nMastering AI and LLM Engineering – Resources\\nOctober 16, 2024\\nFrom Software Engineer to AI Data Scientist – resources\\nNavigation\\nHome\\nConnect Four\\nOutsmart\\nAn arena that pits LLMs against each other in a battle of diplomacy and deviousness\\nAbout\\nPosts\\nGet in touch\\ned [at] edwarddonner [dot] com\\nwww.edwarddonner.com\\nFollow me\\nLinkedIn\\nTwitter\\nFacebook\\nSubscribe to newsletter\\nType your email…\\nSubscribe'}]" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Try this out, and then try for a few more websites\n", "\n", @@ -389,7 +589,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "id": "905b9919-aba7-45b5-ae65-81b3d1d78e34", "metadata": {}, "outputs": [], @@ -398,8 +598,8 @@ "\n", "def summarize(url):\n", " website = Website(url)\n", - " response = openai.chat.completions.create(\n", - " model = \"gpt-4o-mini\",\n", + " response = client.chat.completions.create(\n", + " model = \"llama-3.3-70b-versatile\",\n", " messages = messages_for(website)\n", " )\n", " return response.choices[0].message.content" @@ -407,17 +607,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "id": "05e38d41-dfa4-4b20-9c96-c46ea75d9fb5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "\"### Summary of Edward Donner's Website\\nThe website appears to be a personal blog of Edward Donner, co-founder and CTO of Nebula.io. He introduces himself as a code enthusiast with a passion for experimenting with Large Language Models (LLMs) and other interests like DJing and electronic music production.\\n\\n### About Edward Donner\\nEdward Donner is a tech expert with experience as the founder and CEO of AI startup untapt, which was acquired in 2021. He is currently working on applying AI to help people discover their potential and pursue their passions.\\n\\n### News and Announcements\\nRecent posts on the website include:\\n* January 23, 2025: LLM Workshop – Hands-on with Agents – resources\\n* December 21, 2024: Welcome, SuperDataScientists!\\n* November 13, 2024: Mastering AI and LLM Engineering – Resources\\n* October 16, 2024: From Software Engineer to AI Data Scientist – resources\\n\\nThese posts suggest that Edward Donner is actively involved in the AI and LLM community, sharing resources and knowledge with others.\"" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "summarize(\"https://edwarddonner.com\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "id": "3d926d59-450e-4609-92ba-2d6f244f1342", "metadata": {}, "outputs": [], @@ -585,7 +796,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.11" + "version": "3.12.7" } }, "nbformat": 4,