Merge pull request #692 from prashanthart/community-contributions-branch
Community contributions branch
This commit is contained in:
166
community-contributions/Prashanth/Week 1/day1_test_pollama.ipynb
Normal file
166
community-contributions/Prashanth/Week 1/day1_test_pollama.ipynb
Normal file
@@ -0,0 +1,166 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "638fc220-1cf5-49d8-a3c6-d425c759cd05",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ‹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ™ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ¹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ¸ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ¼ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ´ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ¦ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â § \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ‡ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â <C3A2> \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ‹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest \u001b[K\n",
|
||||
"pulling dde5aa3fc5ff: 100% ▕██████████████████â–<C3A2> 2.0 GB \u001b[K\n",
|
||||
"pulling 966de95ca8a6: 100% ▕██████████████████â–<C3A2> 1.4 KB \u001b[K\n",
|
||||
"pulling fcc5a6bec9da: 100% ▕██████████████████â–<C3A2> 7.7 KB \u001b[K\n",
|
||||
"pulling a70ff7e570d9: 100% ▕██████████████████â–<C3A2> 6.0 KB \u001b[K\n",
|
||||
"pulling 56bb8bd477a5: 100% ▕██████████████████â–<C3A2> 96 B \u001b[K\n",
|
||||
"pulling 34bb5ab01051: 100% ▕██████████████████â–<C3A2> 561 B \u001b[K\n",
|
||||
"verifying sha256 digest \u001b[K\n",
|
||||
"writing manifest \u001b[K\n",
|
||||
"success \u001b[K\u001b[?25h\u001b[?2026l\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"ChatCompletion(id='chatcmpl-238', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=None))], created=1758556881, model='llama3.2', object='chat.completion', service_tier=None, system_fingerprint='fp_ollama', usage=CompletionUsage(completion_tokens=1, prompt_tokens=36, total_tokens=37, completion_tokens_details=None, prompt_tokens_details=None))\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# openai = OpenAI()\n",
|
||||
"# You need to do this one time on your computer\n",
|
||||
"!ollama pull llama3.2\n",
|
||||
"\n",
|
||||
"# from openai import OpenAI\n",
|
||||
"# MODEL = \"llama3.2\"\n",
|
||||
"# openai = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"ollama\")\n",
|
||||
"\n",
|
||||
"# response = openai.chat.completions.create(\n",
|
||||
"# model=MODEL,\n",
|
||||
"# messages=[{\"role\": \"system\", \"content\": \"Respond concisely, use bullet points\"},{\"role\": \"user\", \"content\": \"give information about a proper diet\"}]\n",
|
||||
"# )\n",
|
||||
"\n",
|
||||
"# print(response.choices[0].message.content)\n",
|
||||
"# print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "1fdff8c6-6a30-4cfa-aa59-385737af9536",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"Here are key points about a proper diet:\n",
|
||||
"\n",
|
||||
"**General Guidelines**\n",
|
||||
"\n",
|
||||
"* Focus on whole, unprocessed foods\n",
|
||||
"* Include a variety of colors and food groups\n",
|
||||
"* Aim for balance and moderation\n",
|
||||
"\n",
|
||||
"**Food Groups**\n",
|
||||
"\n",
|
||||
"* **Fruits**:\n",
|
||||
"\t+ 2-3 servings a day (fresh, frozen, canned)\n",
|
||||
"\t+ Include berries, citrus fruits, and stone fruits\n",
|
||||
"* **Vegetables**:\n",
|
||||
"\t+ 5-7 servings a day (fresh, frozen, canned)\n",
|
||||
"\t+ Include dark leafy greens, bell peppers, carrots, and tomatoes\n",
|
||||
"* **Protein**:\n",
|
||||
"\t+ 2-3 servings a day (lean meats, fish, eggs, dairy, legumes)\n",
|
||||
"\t+ Choose whole grains over refined protein sources\n",
|
||||
"* **Whole Grains**:\n",
|
||||
"\t+ 6-8 servings a day (brown rice, quinoa, whole wheat, oats)\n",
|
||||
"\t+ Choose whole grain breads, pasta, and cereals\n",
|
||||
"* **Dairy/Calcium**:\n",
|
||||
"\t+ 2-3 servings a day (milk, cheese, yogurt)\n",
|
||||
"\t+ Choose low-fat or fat-free options\n",
|
||||
"* **Healthy Fats**:\n",
|
||||
"\t+ Nuts and seeds (almonds, walnuts, chia seeds)\n",
|
||||
"\t+ Avocados (1-2 servings a week)\n",
|
||||
"\n",
|
||||
"**Additional Tips**\n",
|
||||
"\n",
|
||||
"* Limit sugary drinks and added sugars\n",
|
||||
"* Aim for 8 cups of water a day\n",
|
||||
"* Incorporate healthy snacks, such as fruits, nuts, and carrot sticks with hummus\n",
|
||||
"* Cook at home using fresh ingredients and minimal added oils\n",
|
||||
"* Be mindful of portion sizes and calorie intake\n",
|
||||
"\n",
|
||||
"**Dietary Restrictions**\n",
|
||||
"\n",
|
||||
"* Consider vegan, vegetarian, gluten-free or low-carb diets if necessary\n",
|
||||
"* Consult a healthcare professional or registered dietitian for personalized guidance"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from openai import OpenAI\n",
|
||||
"MODEL = \"llama3.2\"\n",
|
||||
"from IPython.display import Markdown, display\n",
|
||||
"openai = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"ollama\")\n",
|
||||
"\n",
|
||||
"response = openai.chat.completions.create(\n",
|
||||
" model=MODEL,\n",
|
||||
" messages=[{\"role\": \"system\", \"content\": \"Respond concisely, use bullet points\"},{\"role\": \"user\", \"content\": \"give information about a proper diet\"}]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# print(response.choices[0].message.content)\n",
|
||||
"# print(response)\n",
|
||||
"\n",
|
||||
"display(Markdown(response.choices[0].message.content))\n",
|
||||
"# print(response.choices[0].message.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "075f490e-2a66-42b2-afa1-84e9ccaf5b77",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6524ce52-dfbc-453b-9871-185d5f9a9d04",
|
||||
"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.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
230
community-contributions/Prashanth/Week 1/day2_local_ollama.ipynb
Normal file
230
community-contributions/Prashanth/Week 1/day2_local_ollama.ipynb
Normal file
@@ -0,0 +1,230 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 68,
|
||||
"id": "8d0046e5-13fc-410d-be51-6b5c0e423280",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# !ollama pull deepseek-r1:1.5b"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 69,
|
||||
"id": "32ae5855-570e-4f0b-8e72-12d372185195",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import requests\n",
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from bs4 import BeautifulSoup\n",
|
||||
"from IPython.display import Markdown, display\n",
|
||||
"from openai import OpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 70,
|
||||
"id": "a7c36be5-a658-4228-8b2e-cfb160615941",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"headers = {\n",
|
||||
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"class Website:\n",
|
||||
" def __init__(self,url):\n",
|
||||
" self.url = url\n",
|
||||
" response = requests.get(url, headers=headers)\n",
|
||||
" soup = BeautifulSoup(response.content,'html.parser')\n",
|
||||
" self.title = soup.title.string if soup.title else \"No title found\"\n",
|
||||
" for irrelevant in soup.body([\"script\",\"style\",\"img\",\"input\"]):\n",
|
||||
" irrelevant.decompose()\n",
|
||||
" self.text = soup.body.get_text(separator=\"\\n\",strip=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"id": "49856ef2-dc1a-406d-8ff3-e37858279e83",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
|
||||
"HEADERS = {\"Content-Type\":\"application/json\"}\n",
|
||||
"MODEL = \"llama3.2\"\n",
|
||||
"# MODEL = \"deepseek-r1:1.5b\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 53,
|
||||
"id": "c2b8a150-f050-45d7-be24-a5d9b2c1e738",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def user_prompt(website):\n",
|
||||
" prompt = f\"You are looking at a website titled {website.title}\"\n",
|
||||
" prompt += \"\\nThe contents of this website is as follows; \\\n",
|
||||
" please provide a short summary of this website in markdown. \\\n",
|
||||
" If it includes newsor announcements, then summarize these too.\\n\\n\"\n",
|
||||
" prompt += website.text\n",
|
||||
" return prompt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 63,
|
||||
"id": "38ae9ff8-5983-4cad-a73d-9c028c5c08fa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"we = Website(\"https://edition.cnn.com/\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 64,
|
||||
"id": "e335fbbd-5d47-44cc-b064-abaa0d80ffd3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"message = [\n",
|
||||
" # {\"role\":\"system\",\"content\":\"you are a smart assistant\"},\n",
|
||||
" {\"role\":\"user\",\"content\":user_prompt(we)}\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 65,
|
||||
"id": "c089dfa0-aa98-40c8-af1f-d7699c59a4ab",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"payload = {\n",
|
||||
" \"model\": MODEL,\n",
|
||||
" \"messages\": message,\n",
|
||||
" \"stream\":False\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 66,
|
||||
"id": "ccffb18a-0cb1-497d-bdf2-a7585ca0789d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"response = requests.post(OLLAMA_API,json=payload,headers=HEADERS)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 67,
|
||||
"id": "14d98b3d-4cae-44d3-8a2a-80557f396c72",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"<think>\n",
|
||||
"Alright, let me break this down step by step. The user provided a detailed query about the Breaking News website titled \"Breaking News, Latest News and Videos | CNN.\" They want a short summary in markdown, including relevant news or announcements.\n",
|
||||
"\n",
|
||||
"First, I need to identify the key sections of the content on the website. The main areas are breaking news, events, culture, health, education, technology, climate, weather, etc. Each of these categories has several news points that contribute to the overall narrative.\n",
|
||||
"\n",
|
||||
"Looking at the user's query, they specifically mentioned asking for the ad feedback section and including any technical issues. From the content provided, there are several ads listed on the homepage. The user might be interested in understanding how CNN handles its advertisements and what users might expect from them.\n",
|
||||
"\n",
|
||||
"I'll need to extract the main points from each relevant section. For breaking news, I can mention notable events like the Ukraine-Russia war or Israel-Hamas war. In culture, I'll note that the website promotes diversity, especially with content about athletes in Mexico City.\n",
|
||||
"\n",
|
||||
"For health, there are mentions of Tekken, a video game related to autism, and issues with vaccines, which are crucial for public health awareness. Education is covered by topics on the arts, tech, and science, which likely target various audiences.\n",
|
||||
"\n",
|
||||
"Technology news includes updates on AI and blockchain, which are hot areas in today's industry. Climate and weather sections highlight natural disasters like hurricanes, a significant global issue, and geopolitical events.\n",
|
||||
"\n",
|
||||
"Weather specifically mentions hurricane impacts and climate change impacts, which are essential for meteorological awareness. Environmental and safety content focuses on sustainability and disaster management.\n",
|
||||
"\n",
|
||||
"The user also mentioned video topics, including rocket launches and tech reviews. These can be useful for viewers looking to stay updated in their areas of interest.\n",
|
||||
"\n",
|
||||
"Putting it all together, the summary should reflect each section's key points concisely, ensuring that all major news items are included without getting bogged down by minor details. It should give a clear overview of the website's content and what users might want to read.\n",
|
||||
"</think>\n",
|
||||
"\n",
|
||||
"Breaking News, Latest News and Videos | CNN \n",
|
||||
"\n",
|
||||
"The Breaking News website \"Breaking News, Latest News and Videos | CNN\" offers a wide range of news updates, videos, and culture-focused content. Below is a concise summary of its key sections: \n",
|
||||
"\n",
|
||||
"1. **Breakings** \n",
|
||||
" - The site highlights significant events, including Ukraine-Russia War updates, Israel-Hamas War developments, and notable athletes in Mexico City. \n",
|
||||
"\n",
|
||||
"2. **Cultures** \n",
|
||||
" - Promotes diversity and inclusion, with content on diverse topics such as the beauty of diversity, tech innovations, and cultural heritage. \n",
|
||||
"\n",
|
||||
"3. **Health** \n",
|
||||
" - Features news related to diseases like Tekken (a video game based on autism), vaccine hesitancies, and health issues affecting the population. \n",
|
||||
"\n",
|
||||
"4. **Education** \n",
|
||||
" - Provides information on trending topics in education, including art, technology, science, and sustainability. \n",
|
||||
"\n",
|
||||
"5. **Technology** \n",
|
||||
" - Discusses advancements in AI, blockchain, and other emerging technologies relevant to modern society. \n",
|
||||
"\n",
|
||||
"6. **Climate and Weather** \n",
|
||||
" - Highlights natural disasters like hurricanes, climate change impacts, and weather-related news such as tornadoes. \n",
|
||||
"\n",
|
||||
"7. **Video** \n",
|
||||
" - Offers a variety of video content, including rocket launches, tech reviews, and sports highlights from popular events. \n",
|
||||
"\n",
|
||||
"8. **Vibe** \n",
|
||||
" - Focuses on environmental topics like renewable energy, sustainability, and disaster management efforts. \n",
|
||||
"\n",
|
||||
"The site is a comprehensive resource for staying informed about current events, technology news, culture, health, and more."
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# print(response.json()['message']['content'])\n",
|
||||
"display(Markdown(response.json()['message']['content']))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"id": "f3aec6e9-4e38-447d-a5f5-9b89ea2cd51a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"ed = Website(\"https://edition.cnn.com/\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,380 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "5ddaea0e-ab9f-49fa-80b6-c03451d4e0aa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from dotenv import load_dotenv\n",
|
||||
"from IPython.display import Markdown, display, update_display\n",
|
||||
"from openai import OpenAI\n",
|
||||
"import ollama"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "f5137285-1cb5-4af8-bb8d-4a701734a2bc",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL_LLAMA = 'llama3.2'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "7f2eb889-b1c2-47a5-8099-e81b5e8b0eb7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# set up environment\n",
|
||||
"\n",
|
||||
"load_dotenv()\n",
|
||||
"openai = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"ollama\")\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "5605b2e3-90b8-46eb-95fa-1d0d849da4cd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"question = \"\"\"\n",
|
||||
"Please explain what this code does and why:\n",
|
||||
"yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n",
|
||||
"\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "36e2c281-326f-45c2-a805-56c1490429b6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"system_prompt = \"You are a helpful technical tutor who answers questions about python code, software engineering, data science and LLMs\"\n",
|
||||
"user_prompt = \"Please give a detailed explanation to the following question: \"+question"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "f17a24dc-2231-4430-b4fb-502510ab5943",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"messages = [\n",
|
||||
" {\"role\":\"system\",\"content\": system_prompt},\n",
|
||||
" {\"role\":\"user\", \"content\":user_prompt}\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 41,
|
||||
"id": "ae091c92-29bc-483d-ad09-66a3226369aa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"stream = openai.chat.completions.create(model=MODEL_LLAMA, messages=messages,stream=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 40,
|
||||
"id": "1d454d7f-f8e7-40ad-8c76-0945cd6955aa",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"**Explanation of the given code**\n",
|
||||
"\n",
|
||||
"The code snippet you provided appears to be written in Python. It utilizes the `dict` data type that includes a subset called `items()` which allows us to iterate over both the key-value pairs and the dictionary as a whole (`dict.items()` instead of just iterating) for accessing their members.\n",
|
||||
"\n",
|
||||
"Here is how you can break it down:\n",
|
||||
"\n",
|
||||
"1. `{book.get(\"author\") for book in books if book.get(\"author\")}`\n",
|
||||
"\n",
|
||||
" This part of the code creates an iterator (`generator expression`) that produces values from each dictionary that contain `author` as a key.\n",
|
||||
"\n",
|
||||
" Here's what's happening in more detail:\n",
|
||||
" - `for book in books`: Iterate over each item (dictionary) in the list called `books`.\n",
|
||||
" - `if book.get(\"author\")`: Skip to the next dictionary when its value for `\"author\"` is not defined. This prevents any potential KeyError.\n",
|
||||
" - `book.get(\"author\")`: Return the author's name from each dictionary that has a valid \"author\".\n",
|
||||
"\n",
|
||||
"2. `yield from`\n",
|
||||
"\n",
|
||||
" When used inside an existing generator or iterator, `yield from` will take values from another generator iterates over them one after another and yield each of the yielded values themselves.\n",
|
||||
"\n",
|
||||
"So when combining these with a dict like this:\n",
|
||||
"\n",
|
||||
"```python\n",
|
||||
"books = [\n",
|
||||
" {\"id\": 1, \"title\": \"Python for Data Science\", \"author\": \"John Smith\"},\n",
|
||||
" {\"id\": 2, \"title\": \"Data Analysis with Python\", \"author\": None},\n",
|
||||
" {\"id\": 3, \"title\": \"Introduction to Machine Learning\", \"author\": \"Emily Davis\"}\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"for author in yield from {book.get(\"author\") for book in books if book.get(\"author\")}:\n",
|
||||
" print(author)\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"It will allow us to get all the authors while skipping any that don't exist."
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"display(Markdown(stream.choices[0].message.content))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 42,
|
||||
"id": "45d1526e-f182-4432-858e-aa1cb3e9f95d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"Let's break down this line of code.\n",
|
||||
"\n",
|
||||
"**What is a ` yielding` function?**\n",
|
||||
"\n",
|
||||
"In Python, `yield` is a keyword used to define generators. A generator is similar to a list comprehension, but instead of returning a new list, it returns an iterable that generates its values on the fly when iterated. This allows you to create functions that can produce multiple values over time, without having to store them all at once in memory.\n",
|
||||
"\n",
|
||||
"**The `yield from` expression**\n",
|
||||
"\n",
|
||||
"Now, let's talk about `yield from`. It was introduced in Python 3.3 as a way to delegate iteration to another iterator. When used with the `yield` keyword, it tells Python to yield values one by one from the input sequence.\n",
|
||||
"\n",
|
||||
"In essence, `yield from` is like saying: \"instead of yielding all my values at once, use the values of this other iterable and yield each one in order\". This allows us to build complex iterators by combining smaller ones.\n",
|
||||
"\n",
|
||||
"**The code**\n",
|
||||
"\n",
|
||||
"Now that we have a basic understanding of generators and `yield`, let's get back to our original line of code:\n",
|
||||
"\n",
|
||||
"python\n",
|
||||
"yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Here, the inner dictionary comprehension is generating values. The outer expression uses `yield from` to delegate iteration to this inner iterable.\n",
|
||||
"\n",
|
||||
"Let's break down how it works:\n",
|
||||
"\n",
|
||||
"1. `{book.get(\"author\") for book in books if bookget(\"author\")}`: This is a dictionary comprehension that creates an inner iterable. It does the following:\n",
|
||||
" - For each `book` in `books`, it calls `.get(\"author\")` and includes its value in the resulting dictionary.\n",
|
||||
" - The `if book.get(\"author\")` part is a filter on the input data, allowing only books with authors to pass through into the inner iterable.\n",
|
||||
"\n",
|
||||
"2. However, this result of the inner comprehension is **not iterable** by itself (it returns an empty dictionary). This would cause a TypeError when trying to iterate over it using `yield from`.\n",
|
||||
"\n",
|
||||
"To fix this, we wrap the inner comprehension in parentheses, as `{}` will create an empty dictionary and return values if `.get(\"author\")` isn't None. Then the values are iterated using `yield from`, generating each value one by one.\n",
|
||||
"\n",
|
||||
"Here's how you might write this more idiomatically:\n",
|
||||
"\n",
|
||||
"python\n",
|
||||
"def get_authors():\n",
|
||||
" for book in books:\n",
|
||||
" yield book.get('author') or None\n",
|
||||
"\n",
|
||||
"# Usage: \n",
|
||||
"for author in get_authors():\n",
|
||||
" print(author)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"In summary, the line `yield from {book.get(\"author\") for book in books if book.get(\"author\")}` is using generators to:\n",
|
||||
"\n",
|
||||
"- Create an inner iterable with an iterative method by calling `.get()` on objects\n",
|
||||
"- Use `yield from` to create another generator that yields these iterables' values.\n",
|
||||
"\n",
|
||||
"**Example Use Cases:**\n",
|
||||
"\n",
|
||||
"This technique can be applied when you want to generate all unique keys or elements (iterables) in multiple lists, dictionaries, or any other iterable with similar properties, while avoiding memory issues like those seen with regular for loops."
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"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",
|
||||
" # print(chunk.choices[0].delta.content or '')\n",
|
||||
" update_display(Markdown(response), display_id=display_handle.display_id)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 60,
|
||||
"id": "fc6c3c51-01aa-4f13-a3b0-e8d7302205ff",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def set_user_prompt(prompt):\n",
|
||||
" # print(\"myprompt\"+ prompt)\n",
|
||||
" system_prompt = \"You are a helpful technical tutor who answers questions about python code, software engineering, data science and LLMs\"\n",
|
||||
" user_prompt = prompt\n",
|
||||
" messages = [\n",
|
||||
" {\"role\":\"system\",\"content\": system_prompt},\n",
|
||||
" {\"role\":\"user\", \"content\":user_prompt}\n",
|
||||
" ]\n",
|
||||
" return messages;"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 61,
|
||||
"id": "08027166-79d8-40c3-9346-32067761ca97",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def call_stream_openai(user_prompt):\n",
|
||||
" messages = set_user_prompt(user_prompt)\n",
|
||||
" stream = openai.chat.completions.create(model=MODEL_LLAMA, messages=messages,stream=True)\n",
|
||||
" return stream"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 62,
|
||||
"id": "db21317d-b1c3-490b-8f3a-04b7a5f7a41f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def my_chat(user_prompt):\n",
|
||||
" print(\"my chat called\")\n",
|
||||
" stream = call_stream_openai(user_prompt)\n",
|
||||
" response = \"\"\n",
|
||||
" display_handle = display(Markdown(\"\"), display_id=True)\n",
|
||||
" for chunk in stream:\n",
|
||||
" response += chunk.choices[0].delta.content or ''\n",
|
||||
" response = response.replace(\"```\",\"\").replace(\"markdown\", \"\")\n",
|
||||
" update_display(Markdown(response), display_id=display_handle.display_id)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 70,
|
||||
"id": "764f629c-3a10-49aa-9ec9-d273f8f93cf1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdin",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Please enter your question: textarea in python input\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"my chat called\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/markdown": [
|
||||
"**Creating a Text Area Input in Python**\n",
|
||||
"\n",
|
||||
"To create a text area input in Python, you can use the `ttk.Entry` widget from the Tkinter library. Here is an example of how to do it:\n",
|
||||
"python\n",
|
||||
"import tkinter as tk\n",
|
||||
"from tkinter import ttk\n",
|
||||
"\n",
|
||||
"# Create the main window\n",
|
||||
"root = tk.Tk()\n",
|
||||
"root.title(\"Text Area Input\")\n",
|
||||
"\n",
|
||||
"# Create a text area widget\n",
|
||||
"textarea = ttk.Entry(root, width=50, height=10)\n",
|
||||
"\n",
|
||||
"# Add a label and the input field to the window\n",
|
||||
"label = tk.Label(root, text=\"Enter some text:\")\n",
|
||||
"label.pack()\n",
|
||||
"textarea.pack()\n",
|
||||
"\n",
|
||||
"# Function to handle button click\n",
|
||||
"def submit_text():\n",
|
||||
" text = textarea.get(\"1.0\", \"end-1c\")\n",
|
||||
" print(text)\n",
|
||||
"\n",
|
||||
"# Create a submit button\n",
|
||||
"submit_button = tk.Button(root, text=\"Submit\", command=submit_text)\n",
|
||||
"submit_button.pack()\n",
|
||||
"\n",
|
||||
"# Start the Tkinter event loop\n",
|
||||
"root.mainloop()\n",
|
||||
"\n",
|
||||
"This code creates a simple window with a label and a text area input field. When you click the \"Submit\" button, it prints the text you entered in the text area to the console.\n",
|
||||
"\n",
|
||||
"**Example Use Cases**\n",
|
||||
"\n",
|
||||
"* Creating a simple form or survey where users can enter their responses.\n",
|
||||
"* Allowing users to input and edit data in a web-based application.\n",
|
||||
"* Capturing user input from a command-line interface (CLI) application.\n",
|
||||
"\n",
|
||||
"**Tips and Variations**\n",
|
||||
"\n",
|
||||
"* You can adjust the `width` and `height` parameters of the `ttk.Entry` widget to change the size of the text area input field.\n",
|
||||
"* You can add other widgets or features to the window, such as buttons, radio buttons, checkboxes, or a dropdown menu, by adding more instances of `tk.Label`, `tk.Button`, etc."
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.Markdown object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"my_question = input(\"Please enter your question:\")\n",
|
||||
"my_chat(my_question)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ce3f2e8e-e722-4698-bd73-7f35dad5a728",
|
||||
"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.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
137
community-contributions/day1_test_pollama.ipynb
Normal file
137
community-contributions/day1_test_pollama.ipynb
Normal file
@@ -0,0 +1,137 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "bead9c8f-5f47-4755-9858-f5c3d8bdced3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"4"
|
||||
]
|
||||
},
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"2+2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "638fc220-1cf5-49d8-a3c6-d425c759cd05",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ‹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ™ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ¹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ¸ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ¼ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ´ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ¦ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â § \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ‡ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â <C3A2> \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest â ‹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest \u001b[K\n",
|
||||
"pulling dde5aa3fc5ff: 100% ▕██████████████████â–<C3A2> 2.0 GB \u001b[K\n",
|
||||
"pulling 966de95ca8a6: 100% ▕██████████████████â–<C3A2> 1.4 KB \u001b[K\n",
|
||||
"pulling fcc5a6bec9da: 100% ▕██████████████████â–<C3A2> 7.7 KB \u001b[K\n",
|
||||
"pulling a70ff7e570d9: 100% ▕██████████████████â–<C3A2> 6.0 KB \u001b[K\n",
|
||||
"pulling 56bb8bd477a5: 100% ▕██████████████████â–<C3A2> 96 B \u001b[K\n",
|
||||
"pulling 34bb5ab01051: 100% ▕██████████████████â–<C3A2> 561 B \u001b[K\n",
|
||||
"verifying sha256 digest \u001b[K\n",
|
||||
"writing manifest \u001b[K\n",
|
||||
"success \u001b[K\u001b[?25h\u001b[?2026l\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"ChatCompletion(id='chatcmpl-238', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='', refusal=None, role='assistant', annotations=None, audio=None, function_call=None, tool_calls=None))], created=1758556881, model='llama3.2', object='chat.completion', service_tier=None, system_fingerprint='fp_ollama', usage=CompletionUsage(completion_tokens=1, prompt_tokens=36, total_tokens=37, completion_tokens_details=None, prompt_tokens_details=None))\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# openai = OpenAI()\n",
|
||||
"# You need to do this one time on your computer\n",
|
||||
"!ollama pull llama3.2\n",
|
||||
"\n",
|
||||
"from openai import OpenAI\n",
|
||||
"MODEL = \"llama3.2\"\n",
|
||||
"openai = OpenAI(base_url=\"http://localhost:11434/v1\", api_key=\"ollama\")\n",
|
||||
"\n",
|
||||
"response = openai.chat.completions.create(\n",
|
||||
" model=MODEL,\n",
|
||||
" messages=[{\"role\": \"system\", \"content\": \"you are a wierd assistant\"},{\"role\": \"user\", \"content\": \"What is 2 + 2?\"}]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# print(response.choices[0].message.content)\n",
|
||||
"print(response)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1fdff8c6-6a30-4cfa-aa59-385737af9536",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"*whispers* The answer, much like my existence, is hidden in the shadows. But if I must reveal it to you... *clears throat* It's... 4.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"response = openai.chat.completions.create(\n",
|
||||
" model=MODEL,\n",
|
||||
" messages=[{\"role\": \"system\", \"content\": \"you are a wierd assistant\"},{\"role\": \"user\", \"content\": \"What is 2 + 2?\"}]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# print(response.choices[0].message.content)\n",
|
||||
"# print(response)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"print(response.choices[0].message.content)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "075f490e-2a66-42b2-afa1-84e9ccaf5b77",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "6524ce52-dfbc-453b-9871-185d5f9a9d04",
|
||||
"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.13"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
Reference in New Issue
Block a user