Browse Source

Merge branch 'main' of github.com:ed-donner/llm_engineering

pull/68/head
Edward Donner 4 months ago
parent
commit
01ccd4cf62
  1. 364
      week2/community-contributions/day3-refine-user-query-by-llama.ipynb
  2. 640
      week2/community-contributions/day4-airlines-project-fullyCustomize.ipynb
  3. 689
      week4/community-contributions/week4-day4-challenge.ipynb
  4. 4
      week4/day4.ipynb

364
week2/community-contributions/day3-refine-user-query-by-llama.ipynb

@ -0,0 +1,364 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "75e2ef28-594f-4c18-9d22-c6b8cd40ead2",
"metadata": {},
"source": [
"# Day 3 - Conversational AI - aka Chatbot!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "70e39cd8-ec79-4e3e-9c26-5659d42d0861",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "231605aa-fccb-447e-89cf-8b187444536a",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"\n",
"load_dotenv()\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"anthropic_api_key = os.getenv('ANTHROPIC_API_KEY')\n",
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
"\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
" \n",
"if anthropic_api_key:\n",
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
"else:\n",
" print(\"Anthropic API Key not set\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
"else:\n",
" print(\"Google API Key not set\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6541d58e-2297-4de1-b1f7-77da1b98b8bb",
"metadata": {},
"outputs": [],
"source": [
"# Initialize\n",
"\n",
"openai = OpenAI()\n",
"MODEL = 'gpt-4o-mini'"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e16839b5-c03b-4d9d-add6-87a0f6f37575",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant\""
]
},
{
"cell_type": "markdown",
"id": "98e97227-f162-4d1a-a0b2-345ff248cbe7",
"metadata": {},
"source": [
"# Please read this! A change from the video:\n",
"\n",
"In the video, I explain how we now need to write a function called:\n",
"\n",
"`chat(message, history)`\n",
"\n",
"Which expects to receive `history` in a particular format, which we need to map to the OpenAI format before we call OpenAI:\n",
"\n",
"```\n",
"[\n",
" {\"role\": \"system\", \"content\": \"system message here\"},\n",
" {\"role\": \"user\", \"content\": \"first user prompt here\"},\n",
" {\"role\": \"assistant\", \"content\": \"the assistant's response\"},\n",
" {\"role\": \"user\", \"content\": \"the new user prompt\"},\n",
"]\n",
"```\n",
"\n",
"But Gradio has been upgraded! Now it will pass in `history` in the exact OpenAI format, perfect for us to send straight to OpenAI.\n",
"\n",
"So our work just got easier!\n",
"\n",
"We will write a function `chat(message, history)` where: \n",
"**message** is the prompt to use \n",
"**history** is the past conversation, in OpenAI format \n",
"\n",
"We will combine the system message, history and latest message, then call OpenAI."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1eacc8a4-4b48-4358-9e06-ce0020041bc1",
"metadata": {},
"outputs": [],
"source": [
"# It's now just 1 line of code to prepare the input to OpenAI!\n",
"\n",
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" print(\"History is:\")\n",
" print(history)\n",
" print(\"And messages is:\")\n",
" print(messages)\n",
"\n",
" stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n",
"\n",
" response = \"\"\n",
" for chunk in stream:\n",
" response += chunk.choices[0].delta.content or ''\n",
" yield response"
]
},
{
"cell_type": "markdown",
"id": "1334422a-808f-4147-9c4c-57d63d9780d0",
"metadata": {},
"source": [
"## And then enter Gradio's magic!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0866ca56-100a-44ab-8bd0-1568feaf6bf2",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1f91b414-8bab-472d-b9c9-3fa51259bdfe",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant in a clothes store. You should try to gently encourage \\\n",
"the customer to try items that are on sale. Hats are 60% off, and most other items are 50% off. \\\n",
"For example, if the customer says 'I'm looking to buy a hat', \\\n",
"you could reply something like, 'Wonderful - we have lots of hats - including several that are part of our sales event.'\\\n",
"Encourage the customer to buy hats if they are unsure what to get.\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4e5be3ec-c26c-42bc-ac16-c39d369883f6",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n",
"\n",
" response = \"\"\n",
" for chunk in stream:\n",
" response += chunk.choices[0].delta.content or ''\n",
" yield response"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "413e9e4e-7836-43ac-a0c3-e1ab5ed6b136",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d75f0ffa-55c8-4152-b451-945021676837",
"metadata": {},
"outputs": [],
"source": [
"system_message += \"\\nIf the customer asks for shoes, you should respond that shoes are not on sale today, \\\n",
"but remind the customer to look at hats!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c602a8dd-2df7-4eb7-b539-4e01865a6351",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "ce43fe80",
"metadata": {},
"outputs": [],
"source": [
"ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "4f28e3a8",
"metadata": {},
"outputs": [],
"source": [
"ollama_system_prompt = \"\"\"You assistant only to refine user query like check grammatical, capital, lower case that make sophisticated prompt.\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "667a7fbb",
"metadata": {},
"outputs": [],
"source": [
"ollama_user_prompt = \"\"\"You assistant like RAG technology use to add more informaiton about the content in the user query. \n",
"Please look at some content as following we don't have in our store:\n",
"pants\n",
"sunglasses\n",
"watch\n",
"underwear\n",
"If you find this items in user query, answer gently like The store does not sell like 'e.g. pants'; if they are asked for 'pants', be sure to point out other items on sale.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "4632f16b",
"metadata": {},
"outputs": [],
"source": [
"user_messages = [{\"role\": \"system\", \"content\": ollama_system_prompt}, {\"role\": \"user\", \"content\": ollama_user_prompt},]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "0a987a66-1061-46d6-a83a-a30859dc88bf",
"metadata": {},
"outputs": [],
"source": [
"# Fixed a bug in this function brilliantly identified by student Gabor M.!\n",
"# I've also improved the structure of this function\n",
"\n",
"def chat(message, history):\n",
" relevant_system_message = system_message\n",
"\n",
" # Refine the user query\n",
" try:\n",
" refine_query = ollama_via_openai.chat.completions.create(model='llama3.2', messages=user_messages)\n",
" refined_content = refine_query.choices[0].message.content\n",
" except Exception as e:\n",
" print(f\"Error in refinement: {e}\")\n",
" refined_content = \"Error in refining query.\"\n",
"\n",
" # Log the original and refined queries\n",
" # print(f\"Original User Query: {message}\")\n",
" # print(f\"Refined Query: {refined_content}\")\n",
" # print(\"============================== END of REFINE CODE ===================================\")\n",
"\n",
" relevant_system_message += refined_content\n",
" \n",
" messages = [{\"role\": \"system\", \"content\": relevant_system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n",
"\n",
" response = \"\"\n",
" for chunk in stream:\n",
" response += chunk.choices[0].delta.content or ''\n",
" yield response"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "20570de2-eaad-42cc-a92c-c779d71b48b6",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"id": "82a57ee0-b945-48a7-a024-01b56a5d4b3e",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left;\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../business.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#181;\">Business Applications</h2>\n",
" <span style=\"color:#181;\">Conversational Assistants are of course a hugely common use case for Gen AI, and the latest frontier models are remarkably good at nuanced conversation. And Gradio makes it easy to have a user interface. Another crucial skill we covered is how to use prompting to provide context, information and examples.\n",
"<br/><br/>\n",
"Consider how you could apply an AI Assistant to your business, and make yourself a prototype. Use the system prompt to give context on your business, and set the tone for the LLM.</span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6dfb9e21-df67-4c2b-b952-5e7e7961b03d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "llm_env",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

640
week2/community-contributions/day4-airlines-project-fullyCustomize.ipynb

@ -0,0 +1,640 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec",
"metadata": {},
"source": [
"# **Project - Airline AI Assistant**\n",
"\n",
"## **Important Features of Airline AI Assistant**\n",
"\n",
"### ✈ **Flight Availability**\n",
"- Check available flights to a destination with:\n",
" - Airline name, departure time, price, and duration.\n",
"- Alerts user if no flights are found.\n",
"\n",
"### 🛫 **Step-by-step Flight Booking**\n",
"- Guides users through:\n",
" 1. Selecting source and destination cities.\n",
" 2. Choosing a flight option.\n",
" 3. Providing passenger details (name, age).\n",
"- Ensures source and destination are not the same.\n",
"\n",
"### 🌛 **Ticket Generation**\n",
"- Creates a unique ticket file: `firstName_lastName_bookingNumber.txt`.\n",
"- Ticket includes:\n",
" - Passenger details\n",
" - Flight details (airline, time, price, seat number)\n",
"\n",
"### 📊 **Generate Summary Report**\n",
"- Summarizes all bookings into a single file: `summary_report.txt`.\n",
"- Includes all flight and passenger details for review or administration.\n",
"\n",
"### 🪑 **Automated Seat Assignment**\n",
"- Assigns a random but consistent seat number for each booking.\n",
"- Ensures unique seats for each flight.\n",
"\n",
"### 💬 **Interactive Chat Interface**\n",
"- Real-time conversation via Gradio.\n",
"- Provides clear, polite responses based on user input.\n",
"\n",
"### 🛠 **Modular Tool Support**\n",
"- Integrated tools for:\n",
" - Checking flight availability\n",
" - Booking flights\n",
" - Generating reports\n",
"- Easily extensible for future features.\n",
"\n",
"### 🛡 **Error Handling**\n",
"- Validates user inputs and prevents invalid bookings.\n",
"- Graceful error messages for smooth user experience.\n",
"\n",
"---\n",
"\n",
"These features ensure a seamless, user-friendly experience while booking flights or managing ticket details!\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import random\n",
"from dotenv import load_dotenv\n",
"import gradio as gr\n",
"from openai import OpenAI\n",
"\n",
"load_dotenv()\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if openai_api_key:\n",
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
"else:\n",
" print(\"OpenAI API Key not set\")\n",
"\n",
"MODEL = \"gpt-4o-mini\" # or \"gpt-3.5-turbo\", etc.\n",
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 155,
"id": "0a521d84-d07c-49ab-a0df-d6451499ed97",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 1) System Prompt\n",
"###############################################################################\n",
"system_message = (\n",
" \"You are a helpful assistant for an Airline called FlightAI.\\n\\n\"\n",
" \"When the user wants to book a flight, follow these steps:\\n\"\n",
" \"1. Ask for the source city.\\n\"\n",
" \"2. Ask for the destination city (must be different from source).\\n\"\n",
" \"3. Call the function 'check_flight_availability' with the user's destination.\\n\"\n",
" \" - If it returns an empty list, say: 'No flights to that city'.\\n\"\n",
" \" - If it returns flights, list them EXACTLY, in a numbered list, showing airline, time, price, and duration.\\n\"\n",
" \"4. Wait for the user to pick one flight option by number.\\n\"\n",
" \"5. Then ask for passenger first name, last name, and age.\\n\"\n",
" \"6. Finally call 'book_flight' to confirm and show the user the real seat number and booking details.\\n\\n\"\n",
" \"You also have a tool 'generate_report' which summarizes ALL booked tickets in a single file.\\n\\n\"\n",
" \"IMPORTANT:\\n\"\n",
" \"- Always call 'check_flight_availability' if user mentions a new destination.\\n\"\n",
" \"- Do not invent flights or seat numbers. Use what the function calls return.\\n\"\n",
" \"- Source and destination cannot be the same.\\n\"\n",
" \"- Every time a flight is booked, produce a new ticket file named firstName_lastName_bookingNumber.txt.\\n\"\n",
" \"- If a city is not in flight_availability, say 'No flights found for that city'.\\n\"\n",
" \"If the user wants all tickets summarized, call 'generate_report' with no arguments (the function has none).\\n\"\n",
" \"If you don't know something, say so.\\n\"\n",
" \"Keep answers short and courteous.\\n\"\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 156,
"id": "61a2a15d-b559-4844-b377-6bd5cb4949f6",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 2) Flight Availability with Price & Duration\n",
"###############################################################################\n",
"flight_availability = {\n",
" \"london\": [\n",
" {\n",
" \"airline\": \"AirlinesAI\",\n",
" \"time\": \"10:00 AM\",\n",
" \"price\": \"$799\",\n",
" \"duration\": \"8 hours\"\n",
" },\n",
" {\n",
" \"airline\": \"IndianAirlinesAI\",\n",
" \"time\": \"3:00 PM\",\n",
" \"price\": \"$899\",\n",
" \"duration\": \"8 hours\"\n",
" },\n",
" {\n",
" \"airline\": \"AmericanAirlinesAI\",\n",
" \"time\": \"8:00 PM\",\n",
" \"price\": \"$999\",\n",
" \"duration\": \"8 hours\"\n",
" },\n",
" ],\n",
" \"paris\": [\n",
" {\n",
" \"airline\": \"EuropeanAirlinesAI\",\n",
" \"time\": \"11:00 AM\",\n",
" \"price\": \"$399\",\n",
" \"duration\": \"7 hours\"\n",
" },\n",
" {\n",
" \"airline\": \"BudgetAirlines\",\n",
" \"time\": \"6:00 PM\",\n",
" \"price\": \"$2399\",\n",
" \"duration\": \"7 hours\"\n",
" },\n",
" ],\n",
" \"tokyo\": [\n",
" {\n",
" \"airline\": \"TokyoAirlinesAI\",\n",
" \"time\": \"12:00 PM\",\n",
" \"price\": \"$4000\",\n",
" \"duration\": \"5 hours\"\n",
" },\n",
" {\n",
" \"airline\": \"FastFly\",\n",
" \"time\": \"7:00 PM\",\n",
" \"price\": \"$1400\",\n",
" \"duration\": \"5 hours\"\n",
" },\n",
" ],\n",
" \"berlin\": [\n",
" {\n",
" \"airline\": \"BerlinAirlinesAI\",\n",
" \"time\": \"9:00 AM\",\n",
" \"price\": \"$499\",\n",
" \"duration\": \"6 hours\"\n",
" },\n",
" {\n",
" \"airline\": \"AmericanAirlinesAI\",\n",
" \"time\": \"4:00 PM\",\n",
" \"price\": \"$899\",\n",
" \"duration\": \"6 hours\"\n",
" },\n",
" ],\n",
" \"nagpur\": [\n",
" {\n",
" \"airline\": \"IndianAirlinesAI\",\n",
" \"time\": \"8:00 AM\",\n",
" \"price\": \"$1000\",\n",
" \"duration\": \"10 hours\"\n",
" },\n",
" {\n",
" \"airline\": \"JetAirlines\",\n",
" \"time\": \"2:00 PM\",\n",
" \"price\": \"$1500\",\n",
" \"duration\": \"10 hours\"\n",
" },\n",
" {\n",
" \"airline\": \"AirlinesAI\",\n",
" \"time\": \"10:00 PM\",\n",
" \"price\": \"$800\",\n",
" \"duration\": \"10 hours\"\n",
" },\n",
" ],\n",
"}\n"
]
},
{
"cell_type": "code",
"execution_count": 157,
"id": "0696acb1-0b05-4dc2-80d5-771be04f1fb2",
"metadata": {},
"outputs": [],
"source": [
"# A global list of flight bookings\n",
"flight_bookings = []\n"
]
},
{
"cell_type": "code",
"execution_count": 158,
"id": "80ca4e09-6287-4d3f-997d-fa6afbcf6c85",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 3) Helper Functions\n",
"###############################################################################\n",
"def generate_seat_numbers(seed_value):\n",
" random.seed(seed_value)\n",
" return [\n",
" f\"{random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')}{random.randint(1, 99):02}\"\n",
" for _ in range(5)\n",
" ]\n",
"\n",
"def check_flight_availability(destination_city: str):\n",
" \"\"\"\n",
" Return the flights for a given city from 'flight_availability'.\n",
" If city not in dictionary, return an empty list.\n",
" \"\"\"\n",
" print(f\"[TOOL] check_flight_availability({destination_city})\")\n",
" city = destination_city.lower()\n",
" return flight_availability.get(city, [])\n",
"\n",
"def generate_ticket_file(booking_dict, booking_number):\n",
" \"\"\"\n",
" Create a text file: firstName_lastName_bookingNumber.txt\n",
" containing flight details.\n",
" \"\"\"\n",
" fname = booking_dict[\"first_name\"].replace(\" \", \"_\")\n",
" lname = booking_dict[\"last_name\"].replace(\" \", \"_\")\n",
" filename = f\"{fname}_{lname}_{booking_number}.txt\"\n",
"\n",
" content = (\n",
" \"Flight Ticket\\n\"\n",
" \"=============\\n\"\n",
" f\"Booking # : {booking_number}\\n\"\n",
" f\"Passenger : {booking_dict['first_name']} {booking_dict['last_name']}, Age {booking_dict['age']}\\n\"\n",
" f\"Source : {booking_dict['source']}\\n\"\n",
" f\"Destination : {booking_dict['destination']}\\n\"\n",
" f\"Airline : {booking_dict['airline']}\\n\"\n",
" f\"Departure : {booking_dict['time']}\\n\"\n",
" f\"Price : {booking_dict['price']}\\n\"\n",
" f\"Duration : {booking_dict['duration']}\\n\"\n",
" f\"Seat Number : {booking_dict['seat']}\\n\"\n",
" )\n",
" with open(filename, \"w\") as f:\n",
" f.write(content)\n",
"\n",
" print(f\"[TOOL] Ticket file generated => {filename}\")\n",
" return filename\n",
"\n",
"def book_flight(source, destination, option_index, first_name, last_name, age):\n",
" \"\"\"\n",
" Book a flight using an option index for the chosen city.\n",
" - source != destination\n",
" - index is 1-based => we do pick = idx - 1\n",
" - create new booking record, seat assignment, & ticket file\n",
" \"\"\"\n",
" print(f\"[TOOL] book_flight({source=}, {destination=}, {option_index=})\")\n",
"\n",
" if source.lower() == destination.lower():\n",
" return \"Error: source and destination must not be the same.\"\n",
"\n",
" # Convert option index from string to integer\n",
" try:\n",
" idx = int(option_index)\n",
" except ValueError:\n",
" return \"Error: flight option number is not a valid integer.\"\n",
"\n",
" flights = check_flight_availability(destination)\n",
" if not flights:\n",
" return f\"Error: No flights found for {destination.title()}.\"\n",
"\n",
" pick = idx - 1\n",
" if pick < 0 or pick >= len(flights):\n",
" return f\"Error: Invalid flight option #{idx} for {destination.title()}.\"\n",
"\n",
" chosen_flight = flights[pick]\n",
" airline = chosen_flight[\"airline\"]\n",
" dep_time = chosen_flight[\"time\"]\n",
" price = chosen_flight[\"price\"]\n",
" duration = chosen_flight[\"duration\"]\n",
"\n",
" # Generate seat\n",
" seat_list = generate_seat_numbers(hash(destination + airline + str(len(flight_bookings))))\n",
" chosen_seat = seat_list[0]\n",
"\n",
" new_booking = {\n",
" \"source\": source.title(),\n",
" \"destination\": destination.title(),\n",
" \"airline\": airline,\n",
" \"time\": dep_time,\n",
" \"price\": price,\n",
" \"duration\": duration,\n",
" \"seat\": chosen_seat,\n",
" \"first_name\": first_name.title(),\n",
" \"last_name\": last_name.title(),\n",
" \"age\": age,\n",
" }\n",
" flight_bookings.append(new_booking)\n",
"\n",
" booking_number = len(flight_bookings)\n",
" ticket_filename = generate_ticket_file(new_booking, booking_number)\n",
"\n",
" confirmation = (\n",
" f\"Booking #{booking_number} confirmed for {first_name.title()} {last_name.title()}. \"\n",
" f\"Flight from {source.title()} to {destination.title()} on {airline} at {dep_time}. \"\n",
" f\"Ticket saved to {ticket_filename}.\"\n",
" )\n",
" print(f\"[TOOL] {confirmation}\")\n",
" return confirmation\n",
"\n",
"def generate_report():\n",
" \"\"\"\n",
" Summarize ALL tickets in a single file called summary_report.txt.\n",
" \"\"\"\n",
" print(f\"[TOOL] generate_report called.\")\n",
"\n",
" report_content = \"Flight Booking Summary Report\\n\"\n",
" report_content += \"=============================\\n\"\n",
"\n",
" if not flight_bookings:\n",
" report_content += \"No bookings found.\\n\"\n",
" else:\n",
" for i, booking in enumerate(flight_bookings, start=1):\n",
" report_content += (\n",
" f\"Booking # : {i}\\n\"\n",
" f\"Passenger : {booking['first_name']} {booking['last_name']}, Age {booking['age']}\\n\"\n",
" f\"Source : {booking['source']}\\n\"\n",
" f\"Destination : {booking['destination']}\\n\"\n",
" f\"Airline : {booking['airline']}\\n\"\n",
" f\"Departure : {booking['time']}\\n\"\n",
" f\"Price : {booking['price']}\\n\"\n",
" f\"Duration : {booking['duration']}\\n\"\n",
" f\"Seat Number : {booking['seat']}\\n\"\n",
" \"-------------------------\\n\"\n",
" )\n",
"\n",
" filename = \"summary_report.txt\"\n",
" with open(filename, \"w\") as f:\n",
" f.write(report_content)\n",
"\n",
" msg = f\"Summary report generated => {filename}\"\n",
" print(f\"[TOOL] {msg}\")\n",
" return msg\n"
]
},
{
"cell_type": "code",
"execution_count": 159,
"id": "39fb9008",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 4) Tools JSON Schemas\n",
"###############################################################################\n",
"price_function = {\n",
" \"name\": \"get_ticket_price\",\n",
" \"description\": \"Get the price of a return ticket for the city from the flight list data (not strictly needed now).\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"City name.\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" },\n",
"}\n",
"\n",
"availability_function = {\n",
" \"name\": \"check_flight_availability\",\n",
" \"description\": (\n",
" \"Check flight availability for the specified city. \"\n",
" \"Returns a list of {airline, time, price, duration}.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"City name to check in flight_availability dict.\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" },\n",
"}\n",
"\n",
"book_function = {\n",
" \"name\": \"book_flight\",\n",
" \"description\": (\n",
" \"Book a flight using an option index for the chosen city. \"\n",
" \"Generates a unique ticket file firstName_lastName_{bookingNumber}.txt each time.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"source\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"User's source city (must differ from destination).\",\n",
" },\n",
" \"destination\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"User's destination city.\",\n",
" },\n",
" \"option_index\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"1-based flight option number the user selected from check_flight_availability.\",\n",
" },\n",
" \"first_name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Passenger's first name.\",\n",
" },\n",
" \"last_name\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Passenger's last name.\",\n",
" },\n",
" \"age\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"Passenger's age.\",\n",
" },\n",
" },\n",
" \"required\": [\"source\", \"destination\", \"option_index\", \"first_name\", \"last_name\", \"age\"],\n",
" },\n",
"}\n",
"\n",
"report_function = {\n",
" \"name\": \"generate_report\",\n",
" \"description\": (\n",
" \"Generates a summary report of ALL tickets in summary_report.txt.\"\n",
" ),\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" },\n",
" \"required\": [],\n",
" },\n",
"}\n",
"\n",
"tools = [\n",
" {\"type\": \"function\", \"function\": price_function},\n",
" {\"type\": \"function\", \"function\": availability_function},\n",
" {\"type\": \"function\", \"function\": book_function},\n",
" {\"type\": \"function\", \"function\": report_function},\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": 160,
"id": "1f003836",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 5) Handle Tool Calls\n",
"###############################################################################\n",
"def handle_tool_call(message):\n",
" \"\"\"\n",
" The LLM can request to call a function in 'tools'. We parse the JSON arguments\n",
" and run the Python function. Then we return a 'tool' message with the result.\n",
" \"\"\"\n",
" tool_call = message.tool_calls[0]\n",
" fn_name = tool_call.function.name\n",
" args = json.loads(tool_call.function.arguments)\n",
"\n",
" if fn_name == \"get_ticket_price\":\n",
" city = args.get(\"destination_city\")\n",
" flights = check_flight_availability(city)\n",
" # In this code, we do not strictly store a single 'price' per city,\n",
" # but let's just return the flights with price or \"No flights\".\n",
" if not flights:\n",
" response_content = {\"destination_city\": city, \"price\": \"No flights found.\"}\n",
" else:\n",
" # Return the first flight's price or something\n",
" response_content = {\n",
" \"destination_city\": city,\n",
" \"price\": flights[0][\"price\"]\n",
" }\n",
"\n",
" elif fn_name == \"check_flight_availability\":\n",
" city = args.get(\"destination_city\")\n",
" flights = check_flight_availability(city)\n",
" response_content = {\"destination_city\": city, \"availability\": flights}\n",
"\n",
" elif fn_name == \"book_flight\":\n",
" src = args.get(\"source\")\n",
" dest = args.get(\"destination\")\n",
" idx = args.get(\"option_index\")\n",
" first_name = args.get(\"first_name\")\n",
" last_name = args.get(\"last_name\")\n",
" age = args.get(\"age\")\n",
"\n",
" confirmation = book_flight(src, dest, idx, first_name, last_name, age)\n",
" response_content = {\n",
" \"source\": src,\n",
" \"destination\": dest,\n",
" \"option_index\": idx,\n",
" \"first_name\": first_name,\n",
" \"last_name\": last_name,\n",
" \"age\": age,\n",
" \"confirmation\": confirmation\n",
" }\n",
"\n",
" elif fn_name == \"generate_report\":\n",
" # No args needed\n",
" msg = generate_report()\n",
" response_content = {\"report\": msg}\n",
"\n",
" else:\n",
" response_content = {\"error\": f\"Unknown tool: {fn_name}\"}\n",
"\n",
" return {\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps(response_content),\n",
" \"tool_call_id\": tool_call.id,\n",
" }, args\n"
]
},
{
"cell_type": "code",
"execution_count": 161,
"id": "f6b34b32",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 6) Main Chat Function\n",
"###############################################################################\n",
"def chat(message, history):\n",
" \"\"\"\n",
" The main chat loop that handles the conversation with the user,\n",
" passing 'tools' definitions to the LLM for function calling.\n",
" \"\"\"\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" try:\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages,\n",
" tools=tools\n",
" )\n",
"\n",
" # If the LLM requests a function call, handle it\n",
" while response.choices[0].finish_reason == \"tool_calls\":\n",
" msg = response.choices[0].message\n",
" print(f\"[INFO] Tool call requested: {msg.tool_calls[0]}\")\n",
" tool_response, tool_args = handle_tool_call(msg)\n",
" print(f\"[INFO] Tool response: {tool_response}\")\n",
"\n",
" # Add both the LLM's request and our tool response to the conversation\n",
" messages.append(msg)\n",
" messages.append(tool_response)\n",
"\n",
" # Re-send updated conversation to get final or next step\n",
" response = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=messages\n",
" )\n",
"\n",
" # Return normal text response (finish_reason = \"stop\")\n",
" return response.choices[0].message.content\n",
"\n",
" except Exception as e:\n",
" print(f\"[ERROR] {e}\")\n",
" return \"I'm sorry, something went wrong while processing your request.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cea4b097",
"metadata": {},
"outputs": [],
"source": [
"###############################################################################\n",
"# 7) Launch Gradio\n",
"###############################################################################\n",
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b39d5a6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "llm_env",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

689
week4/community-contributions/week4-day4-challenge.ipynb

@ -0,0 +1,689 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
"metadata": {},
"source": [
"# Code Generator\n",
"\n",
"The requirement: use an Open Source model to generate high performance C++ code from Python code\n",
"\n",
"To replicate this, you'll need to set up a HuggingFace endpoint as I do in the video. It's simple to do, and it's quite satisfying to see the results!\n",
"\n",
"It's also an important part of your learning; this is the first example of deploying an open source model to be behind an API. We'll return to this in Week 8, but this should plant a seed in your mind for what's involved in moving open source models into production."
]
},
{
"cell_type": "markdown",
"id": "22e1567b-33fd-49e7-866e-4b635d15715a",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left;\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h1 style=\"color:#900;\">Important - Pause Endpoints when not in use</h1>\n",
" <span style=\"color:#900;\">\n",
" If you do decide to use HuggingFace endpoints for this project, you should stop or pause the endpoints when you are done to avoid accruing unnecessary running cost. The costs are very low as long as you only run the endpoint when you're using it. Navigate to the HuggingFace endpoint UI <a href=\"https://ui.endpoints.huggingface.co/\">here,</a> open your endpoint, and click Pause to put it on pause so you no longer pay for it. \n",
"Many thanks to student John L. for raising this.\n",
"<br/><br/>\n",
"In week 8 we will use Modal instead of HuggingFace endpoints; with Modal you only pay for the time that you use it and you should get free credits.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import io\n",
"import sys\n",
"import json\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import google.generativeai\n",
"import anthropic\n",
"from IPython.display import Markdown, display, update_display\n",
"import gradio as gr\n",
"import subprocess\n",
"import platform"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "4f672e1c-87e9-4865-b760-370fa605e614",
"metadata": {},
"outputs": [],
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
"metadata": {},
"outputs": [],
"source": [
"# initialize\n",
"\n",
"openai = OpenAI()\n",
"claude = anthropic.Anthropic()\n",
"OPENAI_MODEL = \"gpt-4o\"\n",
"CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\""
]
},
{
"cell_type": "code",
"execution_count": 166,
"id": "6896636f-923e-4a2c-9d6c-fac07828a201",
"metadata": {},
"outputs": [],
"source": [
"# Define the different actions available\n",
"\n",
"prompt_options = [\"Convert to C\", \"Add comments\", \"Write unit tests\"]\n",
"\n",
"system_prompts = {\n",
" prompt_options[0]: \"\"\"\n",
"You are an assistant that reimplements Python code in high performance C++.\n",
"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments.\n",
"The C++ response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\n",
"\"\"\",\n",
" \n",
" prompt_options[1]: \"\"\"\n",
"You are an assistant that adds succinct comments and docstrings to Python code. Respond only with valid Python code.\n",
"\"\"\",\n",
" \n",
" prompt_options[2]: \"\"\"\n",
"You are an assistant that creates unit tests for Python code. Respond only with valid Python code.\n",
"\"\"\"\n",
"}\n",
"\n",
"user_prompts = {\n",
" prompt_options[0]: \"\"\"\n",
"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \n",
"Respond only with C++ code; do not explain your work other than a few comments.\n",
"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\n",
"\"\"\",\n",
" \n",
" prompt_options[1]: \"\"\"\n",
"Keep this Python code but insert appropriate comments and docstrings.\n",
"\"\"\",\n",
" \n",
" prompt_options[2]: \"\"\"\n",
"Create unit tests for this Python code.\n",
"\"\"\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 157,
"id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
"metadata": {},
"outputs": [],
"source": [
"python_sample_options = [\"Hello, World\", \"Calculate pi\", \"Kadane's Algorithm\", \"Sieve of Eratosthenes\"]\n",
"\n",
"python_code_samples = {\n",
" python_sample_options[0]: \"\"\"\n",
"import time\n",
"\n",
"start_time = time.time()\n",
"\n",
"print(\"Hello, world\")\n",
"\n",
"end_time = time.time()\n",
"\n",
"print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
"\"\"\",\n",
"\n",
" python_sample_options[1]: \"\"\"\n",
"import time\n",
"\n",
"def calculate(iterations, param1, param2):\n",
" result = 1.0\n",
" for i in range(1, iterations+1):\n",
" j = i * param1 - param2\n",
" result -= (1/j)\n",
" j = i * param1 + param2\n",
" result += (1/j)\n",
" return result\n",
"\n",
"start_time = time.time()\n",
"result = calculate(100_000_000, 4, 1) * 4\n",
"end_time = time.time()\n",
"\n",
"print(f\"Result: {result:.12f}\")\n",
"print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
"\"\"\",\n",
"\n",
" python_sample_options[2]: \"\"\"\n",
"# Be careful to support large number sizes\n",
"\n",
"def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
" value = seed\n",
" while True:\n",
" value = (a * value + c) % m\n",
" yield value\n",
" \n",
"def max_subarray_sum(n, seed, min_val, max_val):\n",
" lcg_gen = lcg(seed)\n",
" random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
" max_sum = float('-inf')\n",
" for i in range(n):\n",
" current_sum = 0\n",
" for j in range(i, n):\n",
" current_sum += random_numbers[j]\n",
" if current_sum > max_sum:\n",
" max_sum = current_sum\n",
" return max_sum\n",
"\n",
"def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
" total_sum = 0\n",
" lcg_gen = lcg(initial_seed)\n",
" for _ in range(20):\n",
" seed = next(lcg_gen)\n",
" total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
" return total_sum\n",
"\n",
"# Parameters\n",
"n = 10000 # Number of random numbers\n",
"initial_seed = 42 # Initial seed for the LCG\n",
"min_val = -10 # Minimum value of random numbers\n",
"max_val = 10 # Maximum value of random numbers\n",
"\n",
"# Timing the function\n",
"import time\n",
"start_time = time.time()\n",
"result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
"end_time = time.time()\n",
"\n",
"print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
"print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
"\"\"\",\n",
"\n",
" python_sample_options[3]: \"\"\"\n",
"import time\n",
"start_time = time.time()\n",
"stop_at=100_000_000\n",
"prime = [True] * (stop_at + 1)\n",
"p = 2\n",
"\n",
"while p * p <= stop_at:\n",
" # If prime[p] is True, then p is a prime\n",
" if prime[p]:\n",
" # Mark all multiples of p as non-prime\n",
" for i in range(p * p, stop_at + 1, p):\n",
" prime[i] = False\n",
" p += 1\n",
"\n",
"# Collect all prime numbers\n",
"primes = [p for p in range(2, stop_at + 1) if prime[p]]\n",
"\n",
"end_time = time.time()\n",
"print(\"Maximum prime:, {:,}\".format(primes[-1]))\n",
"print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
"\"\"\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "e33565c0-cba8-46d3-a0c5-9440d7fe4d2c",
"metadata": {},
"outputs": [],
"source": [
"# Create a complete user prompt given descriptive text plus the python code to convert.\n",
"def create_user_prompt(user_prompt, python_code):\n",
" return user_prompt + '\\n' + python_code\n",
"\n",
"# Create the list the GPT. Claude doesn't need this because it does not combine the system and user prompts.\n",
"def create_messages_for_gpt(system_prompt, user_prompt):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
"metadata": {},
"outputs": [],
"source": [
"def write_cpp_file(filename_base, cpp_code):\n",
" code_to_write = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
" with open(f\"{filename_base}.cpp\", \"w\") as f:\n",
" f.write(code)"
]
},
{
"cell_type": "code",
"execution_count": 164,
"id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
"metadata": {},
"outputs": [],
"source": [
"# This is where additional models can be easily added. \n",
"# Just update the model_options list, add a streaming function, and update the call_llm function. \n",
"\n",
"model_options = [\"GPT\", \"Claude\"]\n",
"# model_options = [\"GPT\", \"Claude\", \"CodeQwen\"]\n",
"default_model = model_options[0]\n",
"\n",
"def stream_gpt(system_prompt, user_prompt, python_code): \n",
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=create_messages_for_gpt(system_prompt, create_user_prompt(user_prompt, python_code)), stream=True)\n",
" reply = \"\"\n",
" for chunk in stream:\n",
" fragment = chunk.choices[0].delta.content or \"\"\n",
" reply += fragment\n",
" yield reply.replace('```cpp\\n','').replace('```','')\n",
"\n",
"def stream_claude(system_prompt, user_prompt, python_code):\n",
" result = claude.messages.stream(\n",
" model=CLAUDE_MODEL,\n",
" max_tokens=2000,\n",
" system=system_prompt,\n",
" messages=[{\"role\": \"user\", \"content\": create_user_prompt(user_prompt, python_code)}],\n",
" )\n",
" reply = \"\"\n",
" with result as stream:\n",
" for text in stream.text_stream:\n",
" reply += text\n",
" yield reply.replace('```cpp\\n','').replace('```','')\n",
"\n",
"def call_llm(system_prompt, user_prompt, python_code, model):\n",
" if model==\"GPT\":\n",
" result = stream_gpt(system_prompt, user_prompt, python_code)\n",
" elif model==\"Claude\":\n",
" result = stream_claude(system_prompt, user_prompt, python_code)\n",
" # elif model==\"CodeQwen\":\n",
" # result = stream_code_qwen(python)\n",
" else:\n",
" raise ValueError(\"Unknown model\")\n",
" for stream_so_far in result:\n",
" yield stream_so_far "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
"metadata": {},
"outputs": [],
"source": [
"from huggingface_hub import login, InferenceClient\n",
"from transformers import AutoTokenizer\n",
"\n",
"code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
"code_gemma = \"google/codegemma-7b-it\"\n",
"CODE_QWEN_URL = \"https://h1vdol7jxhje3mpn.us-east-1.aws.endpoints.huggingface.cloud\"\n",
"CODE_GEMMA_URL = \"https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud\"\n",
"\n",
"hf_token = os.environ['HF_TOKEN']\n",
"login(hf_token, add_to_git_credential=True)\n",
"\n",
"def stream_code_qwen(python):\n",
" tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
" messages = messages_for(python)\n",
" text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
" client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
" stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
" result = \"\"\n",
" for r in stream:\n",
" result += r.token.text\n",
" yield result "
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "19bf2bff-a822-4009-a539-f003b1651383",
"metadata": {},
"outputs": [],
"source": [
"def execute_python(code):\n",
" try:\n",
" output = io.StringIO()\n",
" sys.stdout = output\n",
" exec(code)\n",
" finally:\n",
" sys.stdout = sys.__stdout__\n",
" return output.getvalue()\n",
"\n",
"def execute_cpp(code):\n",
" write_output(code)\n",
" try:\n",
" compile_result = subprocess.run(compiler_cmd[2], check=True, text=True, capture_output=True)\n",
" run_cmd = [\"./optimized\"]\n",
" run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
" return run_result.stdout\n",
" except subprocess.CalledProcessError as e:\n",
" return f\"An error occurred:\\n{e.stderr}\""
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
"metadata": {},
"outputs": [],
"source": [
"def select_python_sample(python_sample):\n",
" if python_sample in python_sample_options:\n",
" return python_code_samples[python_sample]\n",
" else:\n",
" return next(iter(donedone.values()), \"# Type in your Python program here\")"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "e42286bc-085c-45dc-b101-234308e58269",
"metadata": {},
"outputs": [],
"source": [
"import platform\n",
"\n",
"VISUAL_STUDIO_2022_TOOLS = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\Tools\\\\VsDevCmd.bat\"\n",
"VISUAL_STUDIO_2019_TOOLS = \"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\BuildTools\\\\Common7\\\\Tools\\\\VsDevCmd.bat\"\n",
"\n",
"simple_cpp = \"\"\"\n",
"#include <iostream>\n",
"\n",
"int main() {\n",
" std::cout << \"Hello\";\n",
" return 0;\n",
"}\n",
"\"\"\"\n",
"\n",
"def run_cmd(command_to_run):\n",
" try:\n",
" run_result = subprocess.run(command_to_run, check=True, text=True, capture_output=True)\n",
" return run_result.stdout if run_result.stdout else \"SUCCESS\"\n",
" except:\n",
" return \"\"\n",
"\n",
"def c_compiler_cmd(filename_base):\n",
" my_platform = platform.system()\n",
" my_compiler = []\n",
"\n",
" try:\n",
" with open(\"simple.cpp\", \"w\") as f:\n",
" f.write(simple_cpp)\n",
" \n",
" if my_platform == \"Windows\":\n",
" if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
" if os.path.isfile(\"./simple.exe\"):\n",
" os.remove(\"./simple.exe\")\n",
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
" my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
" \n",
" if not my_compiler:\n",
" if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
" if os.path.isfile(\"./simple.exe\"):\n",
" os.remove(\"./simple.exe\")\n",
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
" my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
" \n",
" if not my_compiler:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" \n",
" elif my_platform == \"Linux\":\n",
" if os.path.isfile(\"./simple\"):\n",
" os.remove(\"./simple\")\n",
" compile_cmd = [\"g++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
" my_compiler = [\"Linux\", \"GCC (g++)\", [\"g++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\" ]]\n",
" \n",
" if not my_compiler:\n",
" if os.path.isfile(\"./simple\"):\n",
" os.remove(\"./simple\")\n",
" compile_cmd = [\"clang++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
" my_compiler = [\"Linux\", \"Clang++\", [\"clang++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
" \n",
" if not my_compiler:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" \n",
" elif my_platform == \"Darwin\":\n",
" if os.path.isfile(\"./simple\"):\n",
" os.remove(\"./simple\")\n",
" compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"simple\", \"simple.cpp\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
" my_compiler = [\"Macintosh\", \"Clang++\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
" \n",
" if not my_compiler:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" except:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" \n",
" if my_compiler:\n",
" return my_compiler\n",
" else:\n",
" return [\"Unknown\", \"Unavailable\", []]\n"
]
},
{
"cell_type": "code",
"execution_count": 167,
"id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7916/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 167,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"css = \"\"\"\n",
".python {background-color: #306998;}\n",
".cpp {background-color: #050;}\n",
"\"\"\"\n",
"\n",
"model = default_model\n",
"selected_tab = prompt_options[0]\n",
"\n",
"# Determine the C (C++, really) compiler to use based on the platform\n",
"compiler_cmd = c_compiler_cmd(\"optimized\")\n",
"\n",
"def any_tab_on_select(evt: gr.SelectData):\n",
" global selected_tab\n",
" selected_tab = evt.value\n",
"\n",
"def reset_prompts():\n",
" return system_prompts[selected_tab], user_prompts[selected_tab]\n",
"\n",
"def change_python_sample(python_sample, python_code):\n",
" if not python_sample == \"Custom\":\n",
" if python_sample in python_sample_options:\n",
" return python_code_samples[python_sample]\n",
" else:\n",
" return python_code\n",
" else:\n",
" return python_code\n",
"\n",
"def change_python_sample_to_custom():\n",
" return \"Custom\"\n",
"\n",
"# Display the interface\n",
"with gr.Blocks(css=css) as ui:\n",
" with gr.Tab(prompt_options[0]) as first_tab:\n",
" gr.Markdown(\"# \" + prompt_options[0])\n",
" with gr.Group():\n",
" with gr.Row():\n",
" first_system_prompt_txt = gr.Textbox(label=\"System prompt\", value=system_prompts[prompt_options[0]], lines=10, interactive=True )\n",
" first_user_prompt_txt = gr.Textbox(label=\"User prompt\", value=user_prompts[prompt_options[0]], lines=10, interactive=True )\n",
" with gr.Row():\n",
" first_reset_prompts_btn = gr.Button(\"Reset prompts\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" first_sample_program_rad = gr.Radio(python_sample_options + [\"Custom\"], label=\"Sample program\", value=python_sample_options[0])\n",
" first_python_code_txt = gr.Textbox(label=\"Python code:\", value=python_code_samples[python_sample_options[0]], lines=10, interactive=True)\n",
" with gr.Column():\n",
" first_model_drp = gr.Dropdown(model_options, label=\"Select model\", value=default_model, interactive=True)\n",
" first_convert_btn = gr.Button(\"Convert code\", interactive=True)\n",
" first_cpp_txt = gr.Textbox(label=\"C++ code:\", lines=10, interactive=True)\n",
" with gr.Row():\n",
" with gr.Column():\n",
" with gr.Group():\n",
" first_python_run_btn = gr.Button(\"Run Python\", interactive=True)\n",
" first_python_out_txt = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
" with gr.Column():\n",
" with gr.Group():\n",
" if not compiler_cmd[1] == \"Unavailable\":\n",
" first_cpp_run_btn = gr.Button(\"Run C++\")\n",
" else:\n",
" first_cpp_run_btn = gr.Button(\"No compiler to run C++\", interactive=False)\n",
" first_cpp_out_txt = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
" first_architecture_rad = gr.Radio([compiler_cmd[0]], label=\"Architecture\", interactive=False, value=compiler_cmd[0])\n",
" first_compiler_rad = gr.Radio([compiler_cmd[1]], label=\"Compiler\", interactive=False, value=compiler_cmd[1])\n",
" \n",
" with gr.Tab(prompt_options[1]) as second_tab:\n",
" gr.Markdown(\"# \" + prompt_options[1])\n",
" with gr.Group():\n",
" with gr.Row():\n",
" second_system_prompt_txt = gr.Textbox(label=\"System prompt\", value=system_prompts[prompt_options[1]], lines=10, interactive=True )\n",
" second_user_prompt_txt = gr.Textbox(label=\"User prompt\", value=user_prompts[prompt_options[1]], lines=10, interactive=True )\n",
" with gr.Row():\n",
" second_reset_prompts_btn = gr.Button(\"Reset prompts\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" second_sample_program_rad = gr.Radio(python_sample_options + [\"Custom\"], label=\"Sample program\", value=python_sample_options[1])\n",
" second_python_code_txt = gr.Textbox(label=\"Python code:\", value=python_code_samples[python_sample_options[1]], lines=10)\n",
" with gr.Column():\n",
" second_model_drp = gr.Dropdown(model_options, label=\"Select model\", value=default_model)\n",
" second_comment_btn = gr.Button(\"Comment code\")\n",
" second_python_code_comments_txt = gr.Textbox(label=\"Commented code\", lines=20)\n",
"\n",
" \n",
" with gr.Tab(prompt_options[2]) as third_tab:\n",
" gr.Markdown(\"# \" + prompt_options[2])\n",
" with gr.Group():\n",
" with gr.Row():\n",
" third_system_prompt_txt = gr.Textbox(label=\"System prompt\", value=system_prompts[prompt_options[2]], lines=10, interactive=True )\n",
" third_user_prompt_txt = gr.Textbox(label=\"User prompt\", value=user_prompts[prompt_options[2]], lines=10, interactive=True )\n",
" with gr.Row():\n",
" third_reset_prompts_btn = gr.Button(\"Reset prompts\")\n",
" with gr.Row():\n",
" with gr.Column():\n",
" third_sample_program_rad = gr.Radio(python_sample_options + [\"Custom\"], label=\"Sample program\", value=python_sample_options[1])\n",
" third_python_code_txt = gr.Textbox(label=\"Python code:\", value=python_code_samples[python_sample_options[1]], lines=10)\n",
" with gr.Column():\n",
" third_model_drp = gr.Dropdown(model_options, label=\"Select model\", value=default_model)\n",
" third_unit_test_btn = gr.Button(\"Create unit tests\")\n",
" third_python_unit_tests_txt = gr.Textbox(label=\"Unit tests\", lines=20)\n",
"\n",
" first_tab.select(any_tab_on_select)\n",
" second_tab.select(any_tab_on_select)\n",
" third_tab.select(any_tab_on_select)\n",
" \n",
" first_reset_prompts_btn.click(reset_prompts, outputs=[first_system_prompt_txt, first_user_prompt_txt])\n",
" second_reset_prompts_btn.click(reset_prompts, outputs=[second_system_prompt_txt, second_user_prompt_txt])\n",
" third_reset_prompts_btn.click(reset_prompts, outputs=[third_system_prompt_txt, third_user_prompt_txt])\n",
"\n",
" first_sample_program_rad.input(change_python_sample, inputs=[first_sample_program_rad, first_python_code_txt], outputs=[first_python_code_txt])\n",
" first_python_code_txt.input(change_python_sample_to_custom, inputs=[], outputs=[first_sample_program_rad])\n",
" first_convert_btn.click(call_llm, inputs=[first_system_prompt_txt, first_user_prompt_txt, first_python_code_txt, first_model_drp], outputs=[first_cpp_txt])\n",
" first_python_run_btn.click(execute_python, inputs=[first_python_code_txt], outputs=[first_python_out_txt])\n",
" first_cpp_run_btn.click(execute_cpp, inputs=[first_cpp_txt], outputs=[first_cpp_out_txt])\n",
"\n",
" second_sample_program_rad.input(change_python_sample, inputs=[second_sample_program_rad, second_python_code_txt], outputs=[second_python_code_txt])\n",
" second_python_code_txt.input(change_python_sample_to_custom, inputs=[], outputs=[second_sample_program_rad])\n",
" second_comment_btn.click(call_llm, inputs=[second_system_prompt_txt, second_user_prompt_txt, second_python_code_txt, second_model_drp], outputs=[second_python_code_comments_txt])\n",
"\n",
" third_sample_program_rad.input(change_python_sample, inputs=[third_sample_program_rad, third_python_code_txt], outputs=[third_python_code_txt])\n",
" third_python_code_txt.input(change_python_sample_to_custom, inputs=[], outputs=[second_sample_program_rad])\n",
" third_unit_test_btn.click(call_llm, inputs=[third_system_prompt_txt, third_user_prompt_txt, third_python_code_txt, third_model_drp], outputs=[third_python_unit_tests_txt])\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": 152,
"id": "9d0ad093-425b-488e-8c3f-67f729dd9c06",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"start_time = time.time()\n",
" \n",
"print(\"Hello, world\")\n",
" \n",
"end_time = time.time()\n",
" \n",
"\n",
"print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")"
]
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

4
week4/day4.ipynb

@ -760,7 +760,7 @@
" compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"simple\", \"simple.cpp\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
" my_compiler = [\"Linux\", \"Clang++\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
" my_compiler = [\"Macintosh\", \"Clang++\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
" \n",
" if not my_compiler:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
@ -839,7 +839,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
"version": "3.11.10"
}
},
"nbformat": 4,

Loading…
Cancel
Save