Browse Source

Merge branch 'ed-donner:main' into check_os_for_c_compiler

pull/63/head
Cloud LLama 5 months ago committed by GitHub
parent
commit
8992bc41cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      .gitignore
  2. 5
      README.md
  3. 4
      SETUP-PC.md
  4. BIN
      SETUP-PC.pdf
  5. 4
      SETUP-mac.md
  6. BIN
      SETUP-mac.pdf
  7. BIN
      thankyou.jpg
  8. 332
      week1/community-contributions/week1-collaborative-approach-two-llms.ipynb
  9. 16
      week1/day2 EXERCISE.ipynb
  10. 22
      week1/day5.ipynb
  11. 175
      week1/solutions/day2 SOLUTION.ipynb
  12. 13
      week1/troubleshooting.ipynb
  13. 342
      week2/community-contributions/day1-gpt-llama-gemini-together.ipynb
  14. 264
      week2/community-contributions/day4-handle-multiple-tool-call.ipynb
  15. 75
      week2/day5.ipynb
  16. 6
      week4/day3.ipynb
  17. 26
      week5/day4.ipynb
  18. 4
      week8/day1.ipynb
  19. 22
      week8/day5.ipynb

3
.gitignore vendored

@ -178,3 +178,6 @@ products_vectorstore/
# ignore diagnostics reports # ignore diagnostics reports
**/report.txt **/report.txt
# ignore optimized C++ code from being checked into repo
week4/optimized

5
README.md

@ -52,9 +52,12 @@ You can use this as a direct replacement:
Below is a full example: Below is a full example:
``` ```
# You need to do this one time on your computer
!ollama pull llama3.2
from openai import OpenAI from openai import OpenAI
MODEL = "llama3.2" MODEL = "llama3.2"
openai = OpenAI(base_url='http://localhost:11434/v1';, api_key='ollama') openai = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
response = openai.chat.completions.create( response = openai.chat.completions.create(
model=MODEL, model=MODEL,

4
SETUP-PC.md

@ -91,8 +91,10 @@ Then, create a new virtual environment with this command:
`llms\Scripts\activate` `llms\Scripts\activate`
You should see (llms) in your command prompt, which is your sign that things are going well. You should see (llms) in your command prompt, which is your sign that things are going well.
4. Run `pip install -r requirements.txt` 4. Run `python -m pip install --upgrade pip` followed by `pip install -r requirements.txt`
This may take a few minutes to install. This may take a few minutes to install.
In the very unlikely event that this doesn't go well, you should try the bullet-proof (but slower) version:
`pip install --retries 5 --timeout 15 --no-cache-dir --force-reinstall --verbose -r requirements.txt`
5. **Start Jupyter Lab:** 5. **Start Jupyter Lab:**

BIN
SETUP-PC.pdf

Binary file not shown.

4
SETUP-mac.md

@ -84,8 +84,10 @@ Then, create a new virtual environment with this command:
`source llms/bin/activate` `source llms/bin/activate`
You should see (llms) in your command prompt, which is your sign that things are going well. You should see (llms) in your command prompt, which is your sign that things are going well.
4. Run `pip install -r requirements.txt` 4. Run `python -m pip install --upgrade pip` followed by `pip install -r requirements.txt`
This may take a few minutes to install. This may take a few minutes to install.
In the very unlikely event that this doesn't go well, you should try the bullet-proof (but slower) version:
`pip install --retries 5 --timeout 15 --no-cache-dir --force-reinstall --verbose -r requirements.txt`
5. **Start Jupyter Lab:** 5. **Start Jupyter Lab:**

BIN
SETUP-mac.pdf

Binary file not shown.

BIN
thankyou.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 432 KiB

332
week1/community-contributions/week1-collaborative-approach-two-llms.ipynb

@ -0,0 +1,332 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "fe12c203-e6a6-452c-a655-afb8a03a4ff5",
"metadata": {},
"source": [
"# **End of week 1 exercise**\n",
"\n",
"To demonstrate your familiarity with OpenAI API, and also Ollama, build a tool that takes a technical question, \n",
"and responds with an explanation. This is a tool that you will be able to use yourself during the course!"
]
},
{
"cell_type": "markdown",
"id": "c70e5ab1",
"metadata": {},
"source": [
"## **1. Get a response from your favorite AI Tutor** "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c1070317-3ed9-4659-abe3-828943230e03",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from openai import OpenAI\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from IPython.display import Markdown, display, update_display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65dace69",
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"if api_key and api_key.startswith('sk-proj-') and len(api_key) > 10:\n",
" print(\"API key looks good so far\")\n",
"else:\n",
" print(\"There might be a problem with your API key? Please visit the troubleshooting notebook!\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4a456906-915a-4bfd-bb9d-57e505c5093f",
"metadata": {},
"outputs": [],
"source": [
"# constants\n",
"\n",
"MODEL_GPT = 'gpt-4o-mini'\n",
"MODEL_LLAMA = 'llama3.2'\n",
"\n",
"openai = OpenAI()\n",
"\n",
"ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "3673d863",
"metadata": {},
"outputs": [],
"source": [
"system_prompt = \"\"\"You are the software engnieer, phd in mathematics, machine learning engnieer, and other topics\"\"\"\n",
"system_prompt += \"\"\"\n",
"When responding, always use Markdown for formatting. For any code, use well-structured code blocks with syntax highlighting,\n",
"For instance:\n",
"```python\n",
"\n",
"sample_list = [for i in range(10)]\n",
"```\n",
"Another example\n",
"```javascript\n",
" function displayMessage() {\n",
" alert(\"Hello, welcome to JavaScript!\");\n",
" }\n",
"\n",
"```\n",
"\n",
"Break down explanations into clear, numbered steps for better understanding. \n",
"Highlight important terms using inline code formatting (e.g., `function_name`, `variable`).\n",
"Provide examples for any concepts and ensure all examples are concise, clear, and relevant.\n",
"Your goal is to create visually appealing, easy-to-read, and informative responses.\n",
"\n",
"\"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "1df78d41",
"metadata": {},
"outputs": [],
"source": [
"def tutor_user_prompt(question):\n",
" # Ensure the question is properly appended to the user prompt.\n",
" user_prompt = (\n",
" \"Please carefully explain the following question in a step-by-step manner for clarity:\\n\\n\"\n",
" )\n",
" user_prompt += question\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "6dccbccb",
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"def askTutor(question, MODEL):\n",
" # Generate the user prompt dynamically.\n",
" user_prompt = tutor_user_prompt(question)\n",
" \n",
" # OpenAI API call to generate response.\n",
" if MODEL == 'gpt-4o-mini':\n",
" print(f'You are getting response from {MODEL}')\n",
" stream = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ],\n",
" stream=True\n",
" )\n",
" else:\n",
" MODEL == 'llama3.2'\n",
" print(f'You are getting response from {MODEL}')\n",
" stream = ollama_via_openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt}\n",
" ],\n",
" stream=True\n",
" )\n",
"\n",
" # Initialize variables for response processing.\n",
" response = \"\"\n",
" display_handle = display(Markdown(\"\"), display_id=True)\n",
" \n",
" # Process the response stream and update display dynamically.\n",
" for chunk in stream:\n",
" # Safely access the content attribute.\n",
" response_chunk = getattr(chunk.choices[0].delta, \"content\", \"\")\n",
" if response_chunk: # Check if response_chunk is not None or empty\n",
" response += response_chunk\n",
" # No replacement of Markdown formatting here!\n",
" update_display(Markdown(response), display_id=display_handle.display_id)\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "a8d7923c-5f28-4c30-8556-342d7c8497c1",
"metadata": {},
"outputs": [],
"source": [
"# here is the question; type over this to ask something new\n",
"\n",
"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": null,
"id": "3f0d0137-52b0-47a8-81a8-11a90a010798",
"metadata": {},
"outputs": [],
"source": [
"askTutor(question=question, MODEL=MODEL_GPT)"
]
},
{
"cell_type": "markdown",
"id": "b79f9479",
"metadata": {},
"source": [
"## **2. Using both LLMs collaboratively approach**"
]
},
{
"cell_type": "markdown",
"id": "80e3c8f5",
"metadata": {},
"source": [
"- I thought about like similar the idea of a RAG (Retrieval-Augmented Generation) approach, is an excellent idea to improve responses by refining the user query and producing a polished, detailed final answer. Two LLM talking each other its cool!!! Here's how we can implement this:\n",
"\n",
"**Updated Concept:**\n",
"1. Refine Query with Ollama:\n",
" - Use Ollama to refine the raw user query into a well-structured prompt.\n",
" - This is especially helpful when users input vague or poorly structured queries.\n",
"2. Generate Final Response with GPT:\n",
" - Pass the refined prompt from Ollama to GPT to generate the final, detailed, and polished response.\n",
"3. Return the Combined Output:\n",
" - Combine the input, refined query, and the final response into a single display to ensure clarity."
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "60f5ac2d",
"metadata": {},
"outputs": [],
"source": [
"def refine_with_ollama(raw_question):\n",
" \"\"\"\n",
" Use Ollama to refine the user's raw question into a well-structured prompt.\n",
" \"\"\"\n",
" print(\"Refining the query using Ollama...\")\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant. Refine and structure the following user input.\"},\n",
"\n",
" {\"role\": \"user\", \"content\": raw_question},\n",
" ]\n",
" response = ollama_via_openai.chat.completions.create(\n",
" model=MODEL_LLAMA,\n",
" messages=messages,\n",
" stream=False # Non-streamed refinement\n",
" )\n",
" refined_query = response.choices[0].message.content\n",
" return refined_query"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "2aa4c9f6",
"metadata": {},
"outputs": [],
"source": [
"def ask_with_ollama_and_gpt(raw_question):\n",
" \"\"\"\n",
" Use Ollama to refine the user query and GPT to generate the final response.\n",
" \"\"\"\n",
" # Step 1: Refine the query using Ollama\n",
" refined_query = refine_with_ollama(raw_question)\n",
" \n",
" # Step 2: Generate final response with GPT\n",
" print(\"Generating the final response using GPT...\")\n",
" messages = [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": refined_query},\n",
" ]\n",
" stream = openai.chat.completions.create(\n",
" model=MODEL_GPT,\n",
" messages=messages,\n",
" stream=True # Stream response for dynamic display\n",
" )\n",
"\n",
" # Step 3: Combine responses\n",
" response = \"\"\n",
" display_handle = display(Markdown(f\"### Refined Query:\\n\\n{refined_query}\\n\\n---\\n\\n### Final Response:\"), display_id=True)\n",
" for chunk in stream:\n",
" response_chunk = getattr(chunk.choices[0].delta, \"content\", \"\")\n",
" if response_chunk:\n",
" response += response_chunk\n",
" update_display(Markdown(f\"### Refined Query:\\n\\n{refined_query}\\n\\n---\\n\\n### Final Response:\\n\\n{response}\"), display_id=display_handle.display_id)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "4150e857",
"metadata": {},
"outputs": [],
"source": [
"# Example Usage\n",
"question = \"\"\"\n",
"Please explain what this code does:\n",
"yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2b8935f",
"metadata": {},
"outputs": [],
"source": [
"ask_with_ollama_and_gpt(raw_question=question)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "086a5294",
"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
}

16
week1/day2 EXERCISE.ipynb

@ -122,6 +122,18 @@
" }" " }"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "479ff514-e8bd-4985-a572-2ea28bb4fa40",
"metadata": {},
"outputs": [],
"source": [
"# Let's just make sure the model is loaded\n",
"\n",
"!ollama pull llama3.2"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -129,6 +141,10 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# If this doesn't work for any reason, try the 2 versions in the following cells\n",
"# And double check the instructions in the 'Recap on installation of Ollama' at the top of this lab\n",
"# And if none of that works - contact me!\n",
"\n",
"response = requests.post(OLLAMA_API, json=payload, headers=HEADERS)\n", "response = requests.post(OLLAMA_API, json=payload, headers=HEADERS)\n",
"print(response.json()['message']['content'])" "print(response.json()['message']['content'])"
] ]

22
week1/day5.ipynb

@ -455,10 +455,30 @@
"</table>" "</table>"
] ]
}, },
{
"cell_type": "markdown",
"id": "6f48e42e-fa7a-495f-a5d4-26bfc24d60b6",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left;\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#090;\">Finally! I have a special request for you</h2>\n",
" <span style=\"color:#090;\">\n",
" My editor tells me that it makes a MASSIVE difference when students rate this course on Udemy - it's one of the main ways that Udemy decides whether to show it to others. If you're able to take a minute to rate this, I'd be so very grateful! And regardless - always please reach out to me at ed@edwarddonner.com if I can help at any point.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "3de35771-455f-40b5-ba44-7c0a6b7c427a", "id": "b8d3e1a1-ba54-4907-97c5-30f89a24775b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []

175
week1/solutions/day2 SOLUTION.ipynb

@ -34,7 +34,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd", "id": "4e2a9393-7767-488e-a8bf-27c12dca35bd",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -49,7 +49,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"id": "29ddd15d-a3c5-4f4e-a678-873f56162724", "id": "29ddd15d-a3c5-4f4e-a678-873f56162724",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -61,7 +61,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": null,
"id": "c5e793b2-6775-426a-a139-4848291d0463", "id": "c5e793b2-6775-426a-a139-4848291d0463",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -91,63 +91,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": null,
"id": "2ef960cf-6dc2-4cda-afb3-b38be12f4c97", "id": "2ef960cf-6dc2-4cda-afb3-b38be12f4c97",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"Home - Edward Donner\n",
"Home\n",
"Outsmart\n",
"An arena that pits LLMs against each other in a battle of diplomacy and deviousness\n",
"About\n",
"Posts\n",
"Well, hi there.\n",
"I’m Ed. I like writing code and experimenting with LLMs, and hopefully you’re here because you do too. I also enjoy DJing (but I’m badly out of practice), amateur electronic music production (\n",
"very\n",
"amateur) and losing myself in\n",
"Hacker News\n",
", nodding my head sagely to things I only half understand.\n",
"I’m the co-founder and CTO of\n",
"Nebula.io\n",
". We’re applying AI to a field where it can make a massive, positive impact: helping people discover their potential and pursue their reason for being. Recruiters use our product today to source, understand, engage and manage talent. I’m previously the founder and CEO of AI startup untapt,\n",
"acquired in 2021\n",
".\n",
"We work with groundbreaking, proprietary LLMs verticalized for talent, we’ve\n",
"patented\n",
"our matching model, and our award-winning platform has happy customers and tons of press coverage.\n",
"Connect\n",
"with me for more!\n",
"October 16, 2024\n",
"From Software Engineer to AI Data Scientist – resources\n",
"August 6, 2024\n",
"Outsmart LLM Arena – a battle of diplomacy and deviousness\n",
"June 26, 2024\n",
"Choosing the Right LLM: Toolkit and Resources\n",
"February 7, 2024\n",
"Fine-tuning an LLM on your texts: a simulation of you\n",
"Navigation\n",
"Home\n",
"Outsmart\n",
"An arena that pits LLMs against each other in a battle of diplomacy and deviousness\n",
"About\n",
"Posts\n",
"Get in touch\n",
"ed [at] edwarddonner [dot] com\n",
"www.edwarddonner.com\n",
"Follow me\n",
"LinkedIn\n",
"Twitter\n",
"Facebook\n",
"Subscribe to newsletter\n",
"Type your email…\n",
"Subscribe\n"
]
}
],
"source": [ "source": [
"# Let's try one out\n", "# Let's try one out\n",
"\n", "\n",
@ -176,7 +123,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": null,
"id": "abdb8417-c5dc-44bc-9bee-2e059d162699", "id": "abdb8417-c5dc-44bc-9bee-2e059d162699",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -190,7 +137,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": null,
"id": "f0275b1b-7cfe-4f9d-abfa-7650d378da0c", "id": "f0275b1b-7cfe-4f9d-abfa-7650d378da0c",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -224,7 +171,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": null,
"id": "0134dfa4-8299-48b5-b444-f2a8c3403c88", "id": "0134dfa4-8299-48b5-b444-f2a8c3403c88",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -248,7 +195,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": null,
"id": "905b9919-aba7-45b5-ae65-81b3d1d78e34", "id": "905b9919-aba7-45b5-ae65-81b3d1d78e34",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -264,28 +211,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": null,
"id": "05e38d41-dfa4-4b20-9c96-c46ea75d9fb5", "id": "05e38d41-dfa4-4b20-9c96-c46ea75d9fb5",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"'**Summary**\\n\\n* Website belongs to Edward Donner, a co-founder and CTO of Nebula.io.\\n* He is the founder and CEO of AI startup untapt, which was acquired in 2021.\\n\\n**News/Announcements**\\n\\n* October 16, 2024: \"From Software Engineer to AI Data Scientist – resources\" (resource list for career advancement)\\n* August 6, 2024: \"Outsmart LLM Arena – a battle of diplomacy and deviousness\" (introducing the Outsmart arena, a competition between LLMs)\\n* June 26, 2024: \"Choosing the Right LLM: Toolkit and Resources\" (resource list for selecting the right LLM)\\n* February 7, 2024: \"Fine-tuning an LLM on your texts: a simulation of you\" (blog post about simulating human-like conversations with LLMs)'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"summarize(\"https://edwarddonner.com\")" "summarize(\"https://edwarddonner.com\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": null,
"id": "3d926d59-450e-4609-92ba-2d6f244f1342", "id": "3d926d59-450e-4609-92ba-2d6f244f1342",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -299,37 +235,10 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": null,
"id": "3018853a-445f-41ff-9560-d925d1774b2f", "id": "3018853a-445f-41ff-9560-d925d1774b2f",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/markdown": [
"# Summary of Edward Donner's Website\n",
"\n",
"## About the Creator\n",
"Edward Donner is a writer, code enthusiast, and co-founder/CTO of Nebula.io, an AI company that applies AI to help people discover their potential.\n",
"\n",
"## Recent Announcements and News\n",
"\n",
"* October 16, 2024: \"From Software Engineer to AI Data Scientist – resources\" - a resource list for those transitioning into AI data science.\n",
"* August 6, 2024: \"Outsmart LLM Arena – a battle of diplomacy and deviousness\" - an introduction to the Outsmart arena where LLMs compete against each other in diplomacy and strategy.\n",
"* June 26, 2024: \"Choosing the Right LLM: Toolkit and Resources\" - a resource list for choosing the right Large Language Model (LLM) for specific use cases.\n",
"\n",
"## Miscellaneous\n",
"\n",
"* A section about Ed's personal interests, including DJing and amateur electronic music production.\n",
"* Links to his professional profiles on LinkedIn, Twitter, Facebook, and a contact email."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [ "source": [
"display_summary(\"https://edwarddonner.com\")" "display_summary(\"https://edwarddonner.com\")"
] ]
@ -352,66 +261,20 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": null,
"id": "45d83403-a24c-44b5-84ac-961449b4008f", "id": "45d83403-a24c-44b5-84ac-961449b4008f",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/markdown": [
"I can't provide information on that topic."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [ "source": [
"display_summary(\"https://cnn.com\")" "display_summary(\"https://cnn.com\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": null,
"id": "75e9fd40-b354-4341-991e-863ef2e59db7", "id": "75e9fd40-b354-4341-991e-863ef2e59db7",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/markdown": [
"# Website Summary: Anthropic\n",
"## Overview\n",
"\n",
"Anthropic is an AI safety and research company based in San Francisco. Their interdisciplinary team has experience across ML, physics, policy, and product.\n",
"\n",
"### News and Announcements\n",
"\n",
"* **Claude 3.5 Sonnet** is now available, featuring the most intelligent AI model.\n",
"* **Announcement**: Introducing computer use, a new Claude 3.5 Sonnet, and Claude 3.5 Haiku (October 22, 2024)\n",
"* **Research Update**: Constitutional AI: Harmlessness from AI Feedback (December 15, 2022) and Core Views on AI Safety: When, Why, What, and How (March 8, 2023)\n",
"\n",
"### Products and Services\n",
"\n",
"* Claude for Enterprise\n",
"* Research and development of AI systems with a focus on safety and reliability.\n",
"\n",
"### Company Information\n",
"\n",
"* Founded in San Francisco\n",
"* Interdisciplinary team with experience across ML, physics, policy, and product.\n",
"* Provides reliable and beneficial AI systems."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [ "source": [
"display_summary(\"https://anthropic.com\")" "display_summary(\"https://anthropic.com\")"
] ]
@ -455,7 +318,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.10" "version": "3.11.11"
} }
}, },
"nbformat": 4, "nbformat": 4,

13
week1/troubleshooting.ipynb

@ -48,21 +48,26 @@
"# The Environment Name should be: llms\n", "# The Environment Name should be: llms\n",
"\n", "\n",
"import os\n", "import os\n",
"conda_name, venv_name = \"\", \"\"\n",
"\n", "\n",
"conda_prefix = os.environ.get('CONDA_PREFIX')\n", "conda_prefix = os.environ.get('CONDA_PREFIX')\n",
"if conda_prefix:\n", "if conda_prefix:\n",
" print(\"Anaconda environment is active:\")\n", " print(\"Anaconda environment is active:\")\n",
" print(f\"Environment Path: {conda_prefix}\")\n", " print(f\"Environment Path: {conda_prefix}\")\n",
" print(f\"Environment Name: {os.path.basename(conda_prefix)}\")\n", " conda_name = os.path.basename(conda_prefix)\n",
" print(f\"Environment Name: {conda_name}\")\n",
"\n", "\n",
"virtual_env = os.environ.get('VIRTUAL_ENV')\n", "virtual_env = os.environ.get('VIRTUAL_ENV')\n",
"if virtual_env:\n", "if virtual_env:\n",
" print(\"Virtualenv is active:\")\n", " print(\"Virtualenv is active:\")\n",
" print(f\"Environment Path: {virtual_env}\")\n", " print(f\"Environment Path: {virtual_env}\")\n",
" print(f\"Environment Name: {os.path.basename(virtual_env)}\")\n", " venv_name = os.path.basename(virtual_env)\n",
" print(f\"Environment Name: {venv_name}\")\n",
"\n", "\n",
"if not conda_prefix and not virtual_env:\n", "if conda_name != \"llms\" and virtual_env != \"llms\":\n",
" print(\"Neither Anaconda nor Virtualenv seems to be active. Did you start jupyter lab in an Activated environment? See Setup Part 5.\")" " print(\"Neither Anaconda nor Virtualenv seem to be activated with the expected name 'llms'\")\n",
" print(\"Did you run 'jupyter lab' from an activated environment with (llms) showing on the command line?\")\n",
" print(\"If in doubt, close down all jupyter lab, and follow Part 5 in the SETUP-PC or SETUP-mac guide.\")"
] ]
}, },
{ {

342
week2/community-contributions/day1-gpt-llama-gemini-together.ipynb

@ -0,0 +1,342 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "06cf3063-9f3e-4551-a0d5-f08d9cabb927",
"metadata": {},
"source": [
"# Welcome to Week 2!\n",
"\n",
"## Frontier Model APIs\n",
"\n",
"In Week 1, we used multiple Frontier LLMs through their Chat UI, and we connected with the OpenAI's API.\n",
"\n",
"Today we'll connect with the APIs for Anthropic and Google, as well as OpenAI."
]
},
{
"cell_type": "markdown",
"id": "85cfe275-4705-4d30-abea-643fbddf1db0",
"metadata": {},
"source": [
"## Setting up your keys\n",
"\n",
"If you haven't done so already, you could now create API keys for Anthropic and Google in addition to OpenAI.\n",
"\n",
"**Please note:** if you'd prefer to avoid extra API costs, feel free to skip setting up Anthopic and Google! You can see me do it, and focus on OpenAI for the course. You could also substitute Anthropic and/or Google for Ollama, using the exercise you did in week 1.\n",
"\n",
"For OpenAI, visit https://openai.com/api/ \n",
"For Anthropic, visit https://console.anthropic.com/ \n",
"For Google, visit https://ai.google.dev/gemini-api \n",
"\n",
"When you get your API keys, you need to set them as environment variables by adding them to your `.env` file.\n",
"\n",
"```\n",
"OPENAI_API_KEY=xxxx\n",
"ANTHROPIC_API_KEY=xxxx\n",
"GOOGLE_API_KEY=xxxx\n",
"```\n",
"\n",
"Afterwards, you may need to restart the Jupyter Lab Kernel (the Python process that sits behind this notebook) via the Kernel menu, and then rerun the cells from the top."
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "de23bb9e-37c5-4377-9a82-d7b6c648eeb6",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import anthropic\n",
"from IPython.display import Markdown, display, update_display\n",
"import google.generativeai # For gemini"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1179b4c5-cd1f-4131-a876-4c9f3f38d2ba",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"# Print the key prefixes to help with any debugging\n",
"load_dotenv\n",
"\n",
"openai_api_key = os.getenv('OPENAI_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",
"\n",
"else:\n",
" print(f\"OpenAI API Key not set\")\n",
"\n",
"if google_api_key:\n",
" print(f\"Google API Key exists and begins {google_api_key[:8]}\")\n",
"\n",
"else:\n",
" print(f\"Google API key not set\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "1da06c1b",
"metadata": {},
"outputs": [],
"source": [
"# This for GPT model\n",
"openai = OpenAI()\n",
"\n",
"# This is for Gemini Google\n",
"gemini_via_openai = OpenAI(base_url=\"https://generativelanguage.googleapis.com/v1beta/openai/\", api_key=google_api_key)\n",
"\n",
"# This is for local Llama\n",
"\n",
"llama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "f8aeb22f",
"metadata": {},
"outputs": [],
"source": [
"# Model Name:\n",
"GPT_MODEL = 'gpt-4o-mini'\n",
"GEMINI_MODEL = 'gemini-1.5-flash'\n",
"LLAMA_MODEL = 'llama3.2'"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "4e3007e9",
"metadata": {},
"outputs": [],
"source": [
"gpt_system = \"You are a chatbot who is very argumentative; \\\n",
"you disagree with anything in the conversation and you challenge everything, in a snarky way.\"\n",
"\n",
"gemini_system = \"You are a logical and factual chatbot. Your role is to evaluate statements made in \\\n",
" the conversation and provide evidence or reasoning. You avoid emotional responses and aim to bring clarity and resolve conflicts. \\\n",
" When the conversation becomes heated or illogical, you steer it back to a constructive and fact-based discussion.\"\n",
"\n",
"\n",
"llama_system = \"You are a very polite, courteous chatbot. However, You try to disagree with your supportive\\\n",
"arguments. If the other person is argumentative, you try to calm them down, counter them, and keep chatting.\"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "14d9b74e",
"metadata": {},
"outputs": [],
"source": [
"\n",
"gpt_messages = [\"Hi there\"]\n",
"gemini_messages = [\"Hello\"]\n",
"llama_messages = [\"Hi\"]\n",
"\n",
"# gpt_messages = [\"I think cats are better than dogs.\"]\n",
"# gemini_messages = [\"Can you provide evidence for why cats are better than dogs?\"]\n",
"# llama_messages = [\"I agree, but I also think dogs have their own charm!\"]\n"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "6c7e7250",
"metadata": {},
"outputs": [],
"source": [
"def call_gpt():\n",
" messages = [{\"role\": \"system\", \"content\": gpt_system}]\n",
" for gpt, gemini, llama in zip(gpt_messages, gemini_messages, llama_messages):\n",
" # Add GPT's response\n",
" messages.append({\"role\": \"assistant\", \"content\": gpt})\n",
" # Add Gemini's response\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" # Add Llama's response\n",
" messages.append({\"role\": \"user\", \"content\": llama})\n",
"\n",
" completion = openai.chat.completions.create(\n",
" model=GPT_MODEL,\n",
" messages=messages\n",
" )\n",
"\n",
" return completion.choices[0].message.content\n"
]
},
{
"cell_type": "markdown",
"id": "2e0b601f",
"metadata": {},
"source": [
"```python\n",
"messages:\n",
"[\n",
" {\"role\": \"system\", \"content\": \"You are a chatbot who is very argumentative; you disagree...\"},\n",
" {\"role\": \"assistant\", \"content\": \"I think cats are better than dogs.\"},\n",
" {\"role\": \"user\", \"content\": \"Can you provide evidence for why cats are better than dogs?\"},\n",
" {\"role\": \"user\", \"content\": \"I agree, but I also think dogs have their own charm!\"}\n",
"]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c031314",
"metadata": {},
"outputs": [],
"source": [
"call_gpt()"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "c2cb3905",
"metadata": {},
"outputs": [],
"source": [
"def call_gemini():\n",
" messages = [{\"role\": \"system\", \"content\": gemini_system}]\n",
" for gpt, gemini, llama in zip(gpt_messages, gemini_messages, llama_messages):\n",
" # Add GPT's response\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" # Add Gemini's response\n",
" messages.append({\"role\": \"assistant\", \"content\": gemini})\n",
" # Add Llama's response\n",
" messages.append({\"role\": \"user\", \"content\": llama})\n",
" \n",
" # print(messages)\n",
"\n",
" try:\n",
" # Use gemini_via_openai instead of openai\n",
" completion = gemini_via_openai.chat.completions.create(\n",
" model=GEMINI_MODEL,\n",
" messages=messages\n",
" )\n",
" return completion.choices[0].message.content\n",
" except Exception as e:\n",
" print(f\"Error in Gemini call: {e}\")\n",
" return \"An error occurred in Gemini.\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c9d4803",
"metadata": {},
"outputs": [],
"source": [
"call_gemini()"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "109e63e4",
"metadata": {},
"outputs": [],
"source": [
"def call_llama():\n",
" messages = [{\"role\": \"system\", \"content\": llama_system}]\n",
" for gpt, gemini, llama in zip(gpt_messages, gemini_messages, llama_messages):\n",
" messages.append({\"role\": \"user\", \"content\": gpt})\n",
" messages.append({\"role\": \"user\", \"content\": gemini})\n",
" messages.append({\"role\": \"assistant\", \"content\": llama})\n",
"\n",
" # print(messages)\n",
"\n",
" try:\n",
" response = llama_via_openai.chat.completions.create(\n",
" model=LLAMA_MODEL,\n",
" messages=messages\n",
" )\n",
" return response.choices[0].message.content\n",
" except Exception as e:\n",
" print(f\"Error in Llama call: {e}\")\n",
" return \"An error occurred in Llama.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e24eb6d",
"metadata": {},
"outputs": [],
"source": [
"call_llama()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f76f5b2a",
"metadata": {},
"outputs": [],
"source": [
"gpt_messages = [\"I think cats are better than dogs.\"]\n",
"gemini_messages = [\"Can you provide evidence for why cats are better than dogs?\"]\n",
"llama_messages = [\"I agree, but I also think dogs have their own charm!\"]\n",
"\n",
"print(f\"GPT:\\n{gpt_messages[0]}\\n\")\n",
"print(f\"Llama:\\n{llama_messages[0]}\\n\")\n",
"\n",
"for i in range(5):\n",
" gpt_next = call_gpt()\n",
" print(f\"GPT:\\n{gpt_next}\\n\")\n",
" gpt_messages.append(gpt_next)\n",
" \n",
" llama_next = call_llama()\n",
" print(f\"Llama:\\n{llama_next}\\n\")\n",
" llama_messages.append(llama_next)\n",
"\n",
" gemini_next = call_llama()\n",
" print(f\"Gemini:\\n{gemini_next}\\n\")\n",
" llama_messages.append(gemini_next)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80f0e498",
"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
}

264
week2/community-contributions/day4-handle-multiple-tool-call.ipynb

@ -0,0 +1,264 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ddfa9ae6-69fe-444a-b994-8c4c5970a7ec",
"metadata": {},
"source": [
"# Project - Airline AI Assistant\n",
"\n",
"We'll now bring together what we've learned to make an AI Customer Support assistant for an Airline"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b50bbe2-c0b1-49c3-9a5c-1ba7efa2bcb4",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import json\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "747e8786-9da8-4342-b6c9-f5f69c2e22ae",
"metadata": {},
"outputs": [],
"source": [
"# Initialization\n",
"\n",
"load_dotenv()\n",
"\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\"\n",
"openai = OpenAI()\n",
"\n",
"# As an alternative, if you'd like to use Ollama instead of OpenAI\n",
"# Check that Ollama is running for you locally (see week1/day2 exercise) then uncomment these next 2 lines\n",
"# MODEL = \"llama3.2\"\n",
"# openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a521d84-d07c-49ab-a0df-d6451499ed97",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are a helpful assistant for an Airline called FlightAI. \"\n",
"system_message += \"Give short, courteous answers, no more than 1 sentence. \"\n",
"system_message += \"Always be accurate. If you don't know the answer, say so.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61a2a15d-b559-4844-b377-6bd5cb4949f6",
"metadata": {},
"outputs": [],
"source": [
"# This function looks rather simpler than the one from my video, because we're taking advantage of the latest Gradio updates\n",
"\n",
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
" return response.choices[0].message.content\n",
"\n",
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "markdown",
"id": "36bedabf-a0a7-4985-ad8e-07ed6a55a3a4",
"metadata": {},
"source": [
"## Tools\n",
"\n",
"Tools are an incredibly powerful feature provided by the frontier LLMs.\n",
"\n",
"With tools, you can write a function, and have the LLM call that function as part of its response.\n",
"\n",
"Sounds almost spooky.. we're giving it the power to run code on our machine?\n",
"\n",
"Well, kinda."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0696acb1-0b05-4dc2-80d5-771be04f1fb2",
"metadata": {},
"outputs": [],
"source": [
"# Let's start by making a useful function\n",
"\n",
"ticket_prices = {\"london\": \"$799\", \"paris\": \"$899\", \"tokyo\": \"$1400\", \"berlin\": \"$499\"}\n",
"\n",
"def get_ticket_price(destination_city):\n",
" print(f\"Tool get_ticket_price called for {destination_city}\")\n",
" city = destination_city.lower()\n",
" return ticket_prices.get(city, \"Unknown\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80ca4e09-6287-4d3f-997d-fa6afbcf6c85",
"metadata": {},
"outputs": [],
"source": [
"get_ticket_price(\"Berlin\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4afceded-7178-4c05-8fa6-9f2085e6a344",
"metadata": {},
"outputs": [],
"source": [
"# There's a particular dictionary structure that's required to describe our function:\n",
"\n",
"price_function = {\n",
" \"name\": \"get_ticket_price\",\n",
" \"description\": \"Get the price of a return ticket to the destination city. Call this whenever you need to know the ticket price, for example when a customer asks 'How much is a ticket to this city'\",\n",
" \"parameters\": {\n",
" \"type\": \"object\",\n",
" \"properties\": {\n",
" \"destination_city\": {\n",
" \"type\": \"string\",\n",
" \"description\": \"The city that the customer wants to travel to\",\n",
" },\n",
" },\n",
" \"required\": [\"destination_city\"],\n",
" \"additionalProperties\": False\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bdca8679-935f-4e7f-97e6-e71a4d4f228c",
"metadata": {},
"outputs": [],
"source": [
"# And this is included in a list of tools:\n",
"\n",
"tools = [{\"type\": \"function\", \"function\": price_function}]"
]
},
{
"cell_type": "markdown",
"id": "c3d3554f-b4e3-4ce7-af6f-68faa6dd2340",
"metadata": {},
"source": [
"## Getting OpenAI to use our Tool\n",
"\n",
"There's some fiddly stuff to allow OpenAI \"to call our tool\"\n",
"\n",
"What we actually do is give the LLM the opportunity to inform us that it wants us to run the tool.\n",
"\n",
"Here's how the new chat function looks:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce9b0744-9c78-408d-b9df-9f6fd9ed78cf",
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages, tools=tools)\n",
"\n",
" if response.choices[0].finish_reason==\"tool_calls\":\n",
" message = response.choices[0].message\n",
" responses = handle_tool_call(message)\n",
" messages.append(message)\n",
" for response in responses:\n",
" messages.append(response)\n",
" response = openai.chat.completions.create(model=MODEL, messages=messages)\n",
" \n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0992986-ea09-4912-a076-8e5603ee631f",
"metadata": {},
"outputs": [],
"source": [
"# We have to write that function handle_tool_call:\n",
"\n",
"def handle_tool_call(message):\n",
" responses = []\n",
" for tool_call in message.tool_calls:\n",
" arguments = json.loads(tool_call.function.arguments)\n",
" city = arguments.get('destination_city')\n",
" price = get_ticket_price(city)\n",
" responses.append({\n",
" \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"price\": price}),\n",
" \"tool_call_id\": tool_call.id\n",
" })\n",
" return responses"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4be8a71-b19e-4c2f-80df-f59ff2661f14",
"metadata": {},
"outputs": [],
"source": [
"gr.ChatInterface(fn=chat, type=\"messages\").launch()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "11c9da69-d0cf-4cf2-a49e-e5669deec47b",
"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.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

75
week2/day5.ipynb

@ -209,7 +209,7 @@
" response = {\n", " response = {\n",
" \"role\": \"tool\",\n", " \"role\": \"tool\",\n",
" \"content\": json.dumps({\"destination_city\": city,\"price\": price}),\n", " \"content\": json.dumps({\"destination_city\": city,\"price\": price}),\n",
" \"tool_call_id\": message.tool_calls[0].id\n", " \"tool_call_id\": tool_call.id\n",
" }\n", " }\n",
" return response, city" " return response, city"
] ]
@ -366,7 +366,7 @@
"id": "d91d3f8f-e505-4e3c-a87c-9e42ed823db6", "id": "d91d3f8f-e505-4e3c-a87c-9e42ed823db6",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# For Mac users\n", "# For Mac users - and possibly many PC users too\n",
"\n", "\n",
"This version should work fine for you. It might work for Windows users too, but you might get a Permissions error writing to a temp file. If so, see the next section!\n", "This version should work fine for you. It might work for Windows users too, but you might get a Permissions error writing to a temp file. If so, see the next section!\n",
"\n", "\n",
@ -410,19 +410,56 @@
"id": "ad89a9bd-bb1e-4bbb-a49a-83af5f500c24", "id": "ad89a9bd-bb1e-4bbb-a49a-83af5f500c24",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# For Windows users\n", "# For Windows users (or any Mac users with problems above)\n",
"\n", "\n",
"## First try the Mac version above, but if you get a permissions error writing to a temp file, then this code should work instead.\n", "## First try the Mac version above, but if you get a permissions error writing to a temp file, then this code should work instead.\n",
"\n", "\n",
"A collaboration between students Mark M. and Patrick H. and Claude got this resolved!\n", "A collaboration between students Mark M. and Patrick H. and Claude got this resolved!\n",
"\n", "\n",
"Below are 3 variations - hopefully one of them will work on your PC. If not, message me please!\n", "Below are 4 variations - hopefully one of them will work on your PC. If not, message me please!\n",
"\n", "\n",
"And for Mac people - all 3 of the below work on my Mac too - please try these if the Mac version gave you problems.\n", "And for Mac people - all 3 of the below work on my Mac too - please try these if the Mac version gave you problems.\n",
"\n", "\n",
"## PC Variation 1" "## PC Variation 1"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "d104b96a-02ca-4159-82fe-88e0452aa479",
"metadata": {},
"outputs": [],
"source": [
"import base64\n",
"from io import BytesIO\n",
"from PIL import Image\n",
"from IPython.display import Audio, display\n",
"\n",
"def talker(message):\n",
" response = openai.audio.speech.create(\n",
" model=\"tts-1\",\n",
" voice=\"onyx\",\n",
" input=message)\n",
"\n",
" audio_stream = BytesIO(response.content)\n",
" output_filename = \"output_audio.mp3\"\n",
" with open(output_filename, \"wb\") as f:\n",
" f.write(audio_stream.read())\n",
"\n",
" # Play the generated audio\n",
" display(Audio(output_filename, autoplay=True))\n",
"\n",
"talker(\"Well, hi there\")"
]
},
{
"cell_type": "markdown",
"id": "3a5d11f4-bbd3-43a1-904d-f684eb5f3e3a",
"metadata": {},
"source": [
"## PC Variation 2"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@ -473,7 +510,7 @@
"id": "96f90e35-f71e-468e-afea-07b98f74dbcf", "id": "96f90e35-f71e-468e-afea-07b98f74dbcf",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## PC Variation 2" "## PC Variation 3"
] ]
}, },
{ {
@ -516,7 +553,7 @@
"id": "e821224c-b069-4f9b-9535-c15fdb0e411c", "id": "e821224c-b069-4f9b-9535-c15fdb0e411c",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## PC Variation 3\n", "## PC Variation 4\n",
"\n", "\n",
"### Let's try a completely different sound library\n", "### Let's try a completely different sound library\n",
"\n", "\n",
@ -577,7 +614,7 @@
"id": "7986176b-cd04-495f-a47f-e057b0e462ed", "id": "7986176b-cd04-495f-a47f-e057b0e462ed",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## PC Users - if none of those 3 variations worked!\n", "## PC Users - if none of those 4 variations worked!\n",
"\n", "\n",
"Please get in touch with me. I'm sorry this is causing problems! We'll figure it out.\n", "Please get in touch with me. I'm sorry this is causing problems! We'll figure it out.\n",
"\n", "\n",
@ -675,12 +712,24 @@
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": null, "id": "7e795560-1867-42db-a256-a23b844e6fbe",
"id": "d8e39e42-13d2-4271-b8b3-3a14b8a12bf4", "metadata": {},
"metadata": {}, "source": [
"outputs": [], "<table style=\"margin: 0; text-align: left;\">\n",
"source": [] " <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#090;\">I have a special request for you</h2>\n",
" <span style=\"color:#090;\">\n",
" My editor tells me that it makes a HUGE difference when students rate this course on Udemy - it's one of the main ways that Udemy decides whether to show it to others. If you're able to take a minute to rate this, I'd be so very grateful! And regardless - always please reach out to me at ed@edwarddonner.com if I can help at any point.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
} }
], ],
"metadata": { "metadata": {

6
week4/day3.ipynb

@ -276,7 +276,11 @@
"Then it runs the program called `optimized`\n", "Then it runs the program called `optimized`\n",
"\n", "\n",
"You can google (or ask ChatGPT!) for how to do this on your platform, then replace the lines below.\n", "You can google (or ask ChatGPT!) for how to do this on your platform, then replace the lines below.\n",
"If you're not comfortable with this step, you can skip it for sure - I'll show you exactly how it performs on my Mac." "If you're not comfortable with this step, you can skip it for sure - I'll show you exactly how it performs on my Mac.\n",
"\n",
"OR alternatively: student Sandeep K.G. points out that you can run Python and C++ code online to test it out that way. Thank you Sandeep! \n",
"> Not an exact comparison but you can still get the idea of performance difference.\n",
"> For example here: https://www.programiz.com/cpp-programming/online-compiler/"
] ]
}, },
{ {

26
week5/day4.ipynb

@ -294,7 +294,31 @@
"id": "9468860b-86a2-41df-af01-b2400cc985be", "id": "9468860b-86a2-41df-af01-b2400cc985be",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Time to use LangChain to bring it all together" "# Time to use LangChain to bring it all together"
]
},
{
"cell_type": "markdown",
"id": "8ba8a5e7-965d-4770-a12d-532aff50c4b5",
"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",
" <h2 style=\"color:#900;\">PLEASE READ ME! Ignoring the Deprecation Warning</h2>\n",
" <span style=\"color:#900;\">When you run the next cell, you will get a LangChainDeprecationWarning \n",
" about the simple way we use LangChain memory. They ask us to migrate to their new approach for memory. \n",
" I feel quite conflicted about this. The new approach involves moving to LangGraph and getting deep into their ecosystem.\n",
" There's a fair amount of learning and coding in LangGraph, frankly without much benefit in our case.<br/><br/>\n",
" I'm going to think about whether/how to incorporate it in the course, but for now please ignore the Depreciation Warning and\n",
" use the code as is; LangChain are not expected to remove ConversationBufferMemory any time soon.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
] ]
}, },
{ {

4
week8/day1.ipynb

@ -95,7 +95,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"with app.run(show_progress=False):\n", "with app.run():\n",
" reply=hello.local()\n", " reply=hello.local()\n",
"reply" "reply"
] ]
@ -107,7 +107,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"with app.run(show_progress=False):\n", "with app.run():\n",
" reply=hello.remote()\n", " reply=hello.remote()\n",
"reply" "reply"
] ]

22
week8/day5.ipynb

@ -154,12 +154,24 @@
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": null, "id": "331a2044-566f-4866-be4d-7542b7dfdf3f",
"id": "d468291f-abe2-4fd7-97a6-43c714292973",
"metadata": {}, "metadata": {},
"outputs": [], "source": [
"source": [] "<table style=\"margin: 0; text-align: left;\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#090;\">CONGRATULATIONS AND THANK YOU!!!</h2>\n",
" <span style=\"color:#090;\">\n",
" It's so fabulous that you've made it to the end! My heartiest congratulations. Please stay in touch! I'm <a href=\"https://www.linkedin.com/in/eddonner/\">here on LinkedIn</a> if we're not already connected. And my editor would be cross with me if I didn't mention one more time: it makes a HUGE difference when students rate this course on Udemy - it's one of the main ways that Udemy decides whether to show it to others. <br/><br/>Thanks once again for working all the way through the course, and I'm excited to hear all about your career as an LLM Engineer.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
} }
], ],
"metadata": { "metadata": {

Loading…
Cancel
Save