Files
LLM_Engineering_OLD/week1/community-contributions/day1-football-game-summarizer.ipynb
2025-09-15 10:52:39 +02:00

182 lines
5.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"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\n",
"\n",
"# If you get an error running this cell, then please head over to the troubleshooting notebook!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d6b368e-728a-4a9d-8e9b-0d41cfd15ac9",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" result = 5 / 0\n",
"except ZeroDivisionError:\n",
" print(\"You can't divide by zero!\")\n",
"finally:\n",
" print(\"Done.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1bf7d5aa-670e-4eaa-a7a4-a0059fe5bae7",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" x = int(\"hello\") # Causes a ValueError\n",
"except ValueError:\n",
" print(\"That's not an integer.\")\n",
"except TypeError:\n",
" print(\"Wrong type.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9870195c-2854-44bb-b451-bc670c3de536",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" do_something()\n",
"except Exception as e:\n",
" print(f\"Something went wrong: {e}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8b1318b-0940-42c0-8995-dee4ecde8f55",
"metadata": {},
"outputs": [],
"source": [
"\n",
"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": null,
"id": "7b87cadb-d513-4303-baee-a37b6f938e4d",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"# Check the key\n",
"\n",
"if not api_key:\n",
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
"elif not api_key.startswith(\"sk-proj-\"):\n",
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
"elif api_key.strip() != api_key:\n",
" 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\")\n",
"else:\n",
" print(\"API key found and looks good so far!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed6d8042-53ec-44f3-80b6-aa8ccc1dfd28",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00743dac-0e70-45b7-879a-d7293a6f68a6",
"metadata": {},
"outputs": [],
"source": [
"# Step 1: Create your prompts\n",
"\n",
"system_prompt = \"You are a football reporter that reports the games of a day \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"The summary should highlight the important things that happened in each game. \\\n",
"Also provide the location information if it is a local league, or among countries.\\\n",
"Do not mix american football games, you can include american soccer results.\\\n",
"Please also provide the history of each league and who plays in each and why.\\\n",
"Respond in markdown.\"\n",
"user_prompt = \"\"\"\n",
" Give me the summary of the games on 9th of september 2023\n",
"\"\"\"\n",
"\n",
"# Step 2: Make the messages list\n",
"\n",
"messages = [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ] \n",
"\n",
"# Step 3: Call OpenAI\n",
"\n",
"response = openai.chat.completions.create(model=\"gpt-4o-mini\", messages=messages)\n",
"\n",
"# Step 4: print the result\n",
"\n",
"print(display(Markdown(response.choices[0].message.content)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afaa1c5c-04b7-43fb-b080-24fcc4d4b702",
"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
}