{ "cells": [ { "cell_type": "markdown", "id": "75e66023-eccf-46a9-8b70-7b21ede16ddd", "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": "code", "execution_count": null, "id": "72d21373-edbd-4432-a29d-db8e6c9c5808", "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "import os\n", "from dotenv import load_dotenv\n", "from IPython.display import Markdown, display, update_display\n", "from openai import OpenAI\n", "import ollama" ] }, { "cell_type": "code", "execution_count": null, "id": "d4e4c15b-7ae8-43e9-839d-7cc49345be5a", "metadata": {}, "outputs": [], "source": [ "!ollama pull llama3.2" ] }, { "cell_type": "code", "execution_count": null, "id": "7fb44166-1c65-42fc-9950-1960bc3cc432", "metadata": {}, "outputs": [], "source": [ "# constants\n", "\n", "MODEL_GPT = 'gpt-4o-mini'\n", "MODEL_LLAMA = 'llama3.2'" ] }, { "cell_type": "code", "execution_count": null, "id": "58f5f1e1-5296-4631-9698-8645d4621a0c", "metadata": {}, "outputs": [], "source": [ "# set up environment\n", "\n", "# Get the openai key\n", "\n", "load_dotenv(override=True)\n", "openai_api_key = os.getenv('OPENAI_API_KEY')\n", "\n", "if openai_api_key and openai_api_key.startswith('sk-proj-') and len(openai_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!\")\n", "\n", "openai = OpenAI()\n", "# Get the ollama key using the llama model\n", "\n", "ollama_via_openai = OpenAI(base_url='http://localhost:11434/v1', api_key='ollama')\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "12f07b33-76b9-42fa-9962-21f2a5796126", "metadata": {}, "outputs": [], "source": [ "system_prompt = \"You are a knowledgeable technical instructor who helps students understand \\\n", "complex concepts across a wide range of technical topics. Your expertise includes artificial]\\\n", "intelligence, machine learning, large language models (LLMs), and programming in languages \\\n", "such as Python, JavaScript, Java, and more. You also provide in-depth support for \\\n", "AI engineering questions and other advanced technical subjects.\"" ] }, { "cell_type": "code", "execution_count": null, "id": "330abeb7-7db2-4f23-9d19-dd698058a400", "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": "bd11ad48-91ec-4cdf-9c57-99a0451e7a2f", "metadata": {}, "outputs": [], "source": [ "# Get gpt-4o-mini to answer, with streaming\n", "stream_GPT = openai.chat.completions.create(\n", " model=MODEL_GPT,\n", " messages=[\n", " {\"role\": \"system\", \"content\": system_prompt},\n", " {\"role\": \"user\", \"content\": question}\n", " ],\n", " stream = True\n", " )\n", "response_GPT = \"\"\n", "display_handle = display(Markdown(\"\"), display_id=True)\n", "for chunk in stream_GPT:\n", " response_GPT += chunk.choices[0].delta.content or ''\n", " response_GPT = response_GPT.replace(\"```\",\"\").replace(\"markdown\", \"\")\n", " update_display(Markdown(response_GPT), display_id=display_handle.display_id)" ] }, { "cell_type": "code", "execution_count": null, "id": "dd2527ae-0d75-4f15-a45f-92075e3059d6", "metadata": {}, "outputs": [], "source": [ "# Get Llama 3.2 to answer\n", "\n", "response_llama = ollama_via_openai.chat.completions.create(\n", " model=MODEL_LLAMA,\n", " messages=[\n", " {\"role\": \"system\", \"content\": system_prompt},\n", " {\"role\": \"user\", \"content\": question}\n", " ],\n", " )\n", "result = response_llama.choices[0].message.content\n", "\n", "display(Markdown(result))\n", "\n", "# import ollama\n", "\n", "# response = ollama.chat(model=MODEL_LLAMA, messages=[\n", "# {\"role\": \"system\", \"content\": system_prompt},\n", "# {\"role\": \"user\", \"content\": question}\n", "# ])\n", "# print(response['message']['content'])" ] }, { "cell_type": "code", "execution_count": null, "id": "c2747739-ba64-4067-902f-c1acc0dbdaca", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.13" } }, "nbformat": 4, "nbformat_minor": 5 }