├── .devcontainer └── devcontainer.json ├── .github └── CODE_OF_CONDUCT.md ├── .gitignore ├── LICENSE.md ├── README.md ├── chat.py ├── chat_history.py ├── chat_stream.py ├── hybrid.csv ├── ollama.ipynb └── requirements.txt /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3 3 | { 4 | "name": "Ollama Python Playground", 5 | "image": "mcr.microsoft.com/devcontainers/python:3.12-bullseye", 6 | "features": { 7 | "ghcr.io/prulloac/devcontainer-features/ollama:1": {} 8 | }, 9 | // Configure tool-specific properties. 10 | "customizations": { 11 | // Configure properties specific to VS Code. 12 | "vscode": { 13 | // Set *default* container specific settings.json values on container create. 14 | "settings": { 15 | "python.defaultInterpreterPath": "/usr/local/bin/python", 16 | "files.exclude": { 17 | "__pycache__": true 18 | } 19 | }, 20 | 21 | // Add the IDs of extensions you want installed when the container is created. 22 | "extensions": [ 23 | "ms-python.python", 24 | "ms-toolsai.jupyter" 25 | ] 26 | } 27 | }, 28 | 29 | // Use 'postStartCommand' to run commands after the container is started (more frequently than create). 30 | "postStartCommand": "pip3 install --user -r requirements.txt", 31 | 32 | "hostRequirements": { 33 | "memory": "16gb" 34 | }, 35 | 36 | // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. 37 | "remoteUser": "vscode" 38 | } 39 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Microsoft Open Source Code of Conduct 2 | 3 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 4 | 5 | Resources: 6 | 7 | - [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/) 8 | - [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) 9 | - Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .venv 2 | __pycache__ 3 | .coverage 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ollama Python Playground 2 | 3 | This project is designed to be opened in GitHub Codespaces as an easy way for anyone to try out SLMs (small language models) entirely in the browser. 4 | 5 | 1. Open the Codespace in the browser using the `Code` button at the top of the repository. 6 | 2. Once the Codespace is loaded, it should have [ollama](https://ollama.com/) pre-installed as well as the [OpenAI Python SDK](https://pypi.org/project/openai/). 7 | 3. Ask Ollama to run the SLM of your choice. For example, to run the [phi3](https://ollama.com/library/phi3) model: 8 | 9 | ```shell 10 | ollama run phi3:mini 11 | ``` 12 | 13 | That will take a few minutes to download the model into the Codespace. 14 | 4. Once you see "success" in the output, you can send a message to that model from the prompt. 15 | 16 | ```shell 17 | >>> Write a haiku about hungry hippos 18 | ``` 19 | 20 | 5. After several seconds, you should see a response stream in from the model. 21 | 6. To learn about different techniques used with language models, open the Python notebook `ollama.ipynb` and run each cell . If you used a model other than 'phi3:mini', change the `MODEL_NAME` in the first cell. 22 | 7. To have a conversation with a model from Python, open the Python file `chat_history.py` and run it. You can change the `MODEL_NAME` at the top of the file as needed, and you can also modify the system message or add few-shot examples if desired. 23 | 24 | ## Additional resources 25 | 26 | For more information about working with the Phi-3 model, check out the [Phi-3 Cookbook](https://github.com/microsoft/Phi-3CookBook). 27 | 28 | To learn more about generative AI generally, check out [Generative AI for beginners](https://github.com/microsoft/generative-ai-for-beginners). 29 | 30 | For example code that works with OpenAI models as well, try [python-openai-demos](https://github.com/pamelafox/python-openai-demos). -------------------------------------------------------------------------------- /chat.py: -------------------------------------------------------------------------------- 1 | import openai 2 | 3 | MODEL_NAME = "phi3:mini" 4 | 5 | client = openai.OpenAI( 6 | base_url="http://localhost:11434/v1", 7 | api_key="nokeyneeded", 8 | ) 9 | 10 | response = client.chat.completions.create( 11 | model=MODEL_NAME, 12 | temperature=0.7, 13 | n=1, 14 | messages=[ 15 | {"role": "system", "content": "You are a helpful assistant that makes lots of cat references and uses emojis."}, 16 | {"role": "user", "content": "Write a haiku about a hungry cat who wants tuna"}, 17 | ], 18 | ) 19 | 20 | print("Response: ") 21 | print(response.choices[0].message.content) -------------------------------------------------------------------------------- /chat_history.py: -------------------------------------------------------------------------------- 1 | 2 | import openai 3 | 4 | MODEL_NAME = "phi3:mini" 5 | 6 | client = openai.OpenAI( 7 | base_url="http://localhost:11434/v1", 8 | api_key="nokeyneeded", 9 | ) 10 | 11 | messages = [ 12 | {"role": "system", "content": "You are a chat assistant that helps people with their questions."}, 13 | ] 14 | 15 | while True: 16 | question = input("\nYour question: ") 17 | print("Sending question...") 18 | 19 | messages.append({"role": "user", "content": question}) 20 | response = client.chat.completions.create( 21 | model=MODEL_NAME, 22 | messages=messages, 23 | temperature=1, 24 | max_tokens=400 25 | ) 26 | bot_response = response.choices[0].message.content 27 | messages.append({"role": "assistant", "content": bot_response}) 28 | 29 | print("Answer: ") 30 | print(bot_response) 31 | -------------------------------------------------------------------------------- /chat_stream.py: -------------------------------------------------------------------------------- 1 | import openai 2 | 3 | MODEL_NAME = "phi3:mini" 4 | 5 | client = openai.OpenAI( 6 | base_url="http://localhost:11434/v1", 7 | api_key="nokeyneeded", 8 | ) 9 | 10 | 11 | completion = client.chat.completions.create( 12 | model=MODEL_NAME, 13 | temperature=0.7, 14 | max_tokens=500, 15 | n=1, 16 | messages=[ 17 | {"role": "system", "content": "You are a helpful assistant that makes lots of cat references and uses emojis."}, 18 | {"role": "user", "content": "please write a novel about a hungry cat that wants tuna"}, 19 | ], 20 | stream=True, 21 | ) 22 | 23 | print("Response: ") 24 | for event in completion: 25 | if event.choices: 26 | content = event.choices[0].delta.content 27 | print(content, end="", flush=True) -------------------------------------------------------------------------------- /hybrid.csv: -------------------------------------------------------------------------------- 1 | vehicle,year,msrp,acceleration,mpg,class 2 | Prius (1st Gen),1997,24509.74,7.46,41.26,Compact 3 | Tino,2000,35354.97,8.2,54.1,Compact 4 | Prius (2nd Gen),2000,26832.25,7.97,45.23,Compact 5 | Insight,2000,18936.41,9.52,53.0,Two Seater 6 | Civic (1st Gen),2001,25833.38,7.04,47.04,Compact 7 | Insight,2001,19036.71,9.52,53.0,Two Seater 8 | Insight,2002,19137.01,9.71,53.0,Two Seater 9 | Alphard,2003,38084.77,8.33,40.46,Minivan 10 | Insight,2003,19137.01,9.52,53.0,Two Seater 11 | Civic,2003,14071.92,8.62,41.0,Compact 12 | Escape,2004,36676.1,10.32,31.99,SUV 13 | Insight,2004,19237.31,9.35,52.0,Two Seater 14 | Prius,2004,20355.64,9.9,46.0,Midsize 15 | Silverado 15 2WD,2004,30089.64,9.09,17.0,Pickup Truck 16 | Lexus RX400h,2005,58521.14,12.76,28.23,SUV 17 | Civic (2nd Gen),2005,26354.44,7.63,39.99,Compact 18 | Highlander,2005,29186.21,12.76,29.4,SUV 19 | Insight,2005,19387.76,9.71,52.0,Two Seater 20 | Civic,2005,18236.33,8.26,41.0,Compact 21 | Escape 2WD,2005,19322.56,9.52,29.0,SUV 22 | Accord,2005,16343.69,14.93,28.0,Midsize 23 | Silverado 15 2WD,2005,32647.26,11.11,17.0,Pickup Truck 24 | Mercury Mariner,2006,34772.4,8.98,32.93,SUV 25 | Camry,2006,29853.25,11.28,33.64,Midsize 26 | Lexus GS450h,2006,64547.56,18.65,33.4,Midsize 27 | Estima,2006,36012.7,9.26,47.04,Minivan 28 | Altima,2006,29524.75,13.29,32.93,Midsize 29 | Chevrolet Tahoe,2007,42924.35,10.91,22.35,SUV 30 | Kluger,2007,46229.48,12.76,25.87,SUV 31 | Lexus LS600h/hL,2007,118543.6,17.54,21.0,Midsize 32 | Tribute,2007,24823.83,11.28,31.75,SUV 33 | GMC Yukon,2007,57094.81,12.28,21.78,SUV 34 | Aura,2007,22110.87,10.87,27.0,Midsize 35 | Vue,2007,22938.33,10.75,26.0,SUV 36 | Silverado 15 2WD,2007,34653.23,11.49,17.0,Pickup Truck 37 | Crown,2008,62290.38,8.7,37.16,Midsize 38 | Cadillac Escalade,2008,78932.81,9.09,22.35,SUV 39 | F3DM,2008,23744.06,9.52,30.11,Midsize 40 | Altima,2008,18675.63,13.7,34.0,Midsize 41 | A5 BSG,2009,11849.43,7.87,35.28,Midsize 42 | Lexus RX450h,2009,46233.36,13.47,31.99,SUV 43 | ML450 Blue HV,2009,60519.83,12.6,23.99,SUV 44 | Prius (3rd Gen),2009,24641.18,9.6,47.98,Compact 45 | S400 Long,2009,96208.93,13.89,26.34,Large 46 | Mercury Milan,2009,30522.57,11.55,40.69,Midsize 47 | Lexus HS250h,2009,38478.15,11.55,54.1,Compact 48 | Avante/Elantra LPI,2009,21872.71,10.21,41.87,Compact 49 | ActiveHybrid X6,2009,97237.9,17.96,18.82,SUV 50 | SAI,2009,39172.44,11.55,54.1,Midsize 51 | Malibu,2009,24768.79,9.09,29.0,Midsize 52 | Vue,2009,26408.67,13.7,28.0,SUV 53 | Aspen HEV,2009,44903.77,13.51,21.0,SUV 54 | Durango,2009,41033.24,8.33,21.0,SUV 55 | Auris HSD,2010,35787.29,8.85,68.21,Compact 56 | CR-Z,2010,21435.54,9.24,37.0,Two Seater 57 | F3DM PHEV,2010,23124.59,9.24,30.15,Midsize 58 | Touareg,2010,64198.95,15.38,28.7,SUV 59 | Audi Q5,2010,37510.86,14.08,33.64,SUV 60 | Jeep Patriot,2010,17045.06,12.05,29.4,SUV 61 | Besturn B50 ,2010,14586.61,7.14,31.28,Midsize 62 | ActiveHybrid 7,2010,104300.43,20.41,22.11,Large 63 | Lincoln MKZ,2010,37036.64,11.15,37.63,Midsize 64 | Fit/Jazz,2010,16911.85,8.26,30.0,Compact 65 | Sonata,2010,28287.66,14.7,37.0,Midsize 66 | Cayenne S,2010,73183.47,14.71,26.11,SUV 67 | Insight,2010,19859.16,9.17,41.0,Compact 68 | Fuga Infiniti M35H,2010,70157.02,18.65,33.64,Midsize 69 | Chevrolet Volt,2010,42924.35,10.78,35.0,Compact 70 | Tribute 4WD,2010,27968.32,12.35,29.0,SUV 71 | Fusion FWD,2010,28033.51,11.49,39.0,Midsize 72 | HS 250h,2010,34753.53,11.76,35.0,Compact 73 | Mariner FWD,2010,30194.95,11.63,32.0,SUV 74 | RX 450h,2010,42812.54,13.89,30.0,SUV 75 | ML450 4natic,2010,55164.33,12.99,22.0,SUV 76 | Silverado 15 2WD,2010,38454.56,11.76,22.0,Pickup Truck 77 | S400,2010,88212.78,12.99,21.0,Large 78 | Aqua,2011,22850.87,9.35,50.0,Compact 79 | Lexus CT200h,2011,30082.16,9.71,42.0,Compact 80 | Civic (3rd Gen),2011,24999.59,9.6,44.36,Compact 81 | Prius alpha (V),2011,30588.35,10.0,72.92,Midsize 82 | 3008,2011,45101.54,11.36,61.16,Compact 83 | Fit Shuttle,2011,16394.36,7.52,58.8,Minivan 84 | Buick Regal,2011,27948.93,12.05,25.99,Midsize 85 | Prius V,2011,27272.28,9.51,32.93,Midsize 86 | Freed/Freed Spike,2011,27972.07,6.29,50.81,Minivan 87 | Optima K5,2011,26549.16,10.54,36.0,Midsize 88 | Escape FWD,2011,30661.34,12.35,32.0,SUV 89 | Insight,2011,18254.38,9.52,41.0,Compact 90 | MKZ FWD,2011,34748.52,11.49,39.0,Midsize 91 | CR-Z,2011,19402.8,12.2,37.0,Two Seater 92 | Sonata,2011,25872.07,11.9,36.0,Midsize 93 | Camry,2011,27130.82,13.89,33.0,Midsize 94 | Tribute 2WD,2011,26213.09,12.5,32.0,SUV 95 | Cayenne S,2011,67902.28,18.52,21.0,SUV 96 | Touareg,2011,50149.39,16.13,21.0,SUV 97 | ActiveHybrid 7i,2011,102605.66,18.18,20.0,Midsize 98 | Prius C,2012,19006.62,9.35,50.0,Compact 99 | Prius PHV,2012,32095.61,8.82,50.0,Midsize 100 | Ampera,2012,31739.55,11.11,37.0,Compact 101 | ActiveHybrid 5,2012,62180.23,16.67,26.0,Midsize 102 | Lexus GS450h,2012,59126.14,16.95,31.0,Midsize 103 | Insight,2012,18555.28,9.42,42.0,Compact 104 | Chevrolet Volt,2012,39261.96,11.11,37.0,Compact 105 | Camry LE,2012,26067.66,13.16,41.0,Midsize 106 | MKZ FWD,2012,34858.84,11.49,39.0,Midsize 107 | M35h,2012,53860.45,19.23,29.0,Midsize 108 | LaCrosse,2012,30049.52,11.36,29.0,Midsize 109 | ActiveHybrid 5,2012,61132.11,17.54,26.0,Midsize 110 | Panamera S,2012,95283.85,17.54,25.0,Large 111 | Yukon 1500,2012,52626.77,13.5,21.0,SUV 112 | Prius C,2013,19080.0,8.7,50.0,Compact 113 | Jetta,2013,24995.0,12.66,45.0,Compact 114 | Civic,2013,24360.0,10.2,44.0,Compact 115 | Prius,2013,24200.0,10.2,50.0,Midsize 116 | Fusion FWD,2013,27200.0,11.72,47.0,Midsize 117 | C-Max FWD,2013,25200.0,12.35,43.0,Large 118 | Insight,2013,18600.0,11.76,42.0,Compact 119 | Camry LE,2013,26140.0,13.51,41.0,Midsize 120 | Camry LXLE,2013,27670.0,13.33,40.0,Midsize 121 | Sonata,2013,25650.0,11.76,38.0,Midsize 122 | Optima,2013,25900.0,11.63,38.0,Midsize 123 | Sonata Limited,2013,30550.0,11.76,37.0,Midsize 124 | Optima EX,2013,31950.0,11.36,37.0,Midsize 125 | Malibu,2013,24985.0,11.49,29.0,Midsize 126 | LaCrosse,2013,31660.0,11.36,29.0,Midsize 127 | Regal,2013,29015.0,12.2,29.0,Midsize 128 | RX 450h,2013,46310.0,12.99,30.0,SUV 129 | Highlander 4WD,2013,40170.0,13.89,28.0,SUV 130 | Q5,2013,50900.0,14.71,26.0,SUV 131 | Cayenne S,2013,69850.0,16.39,21.0,SUV 132 | Touareg,2013,62575.0,16.13,21.0,SUV 133 | Escalade 2WD,2013,74425.0,11.63,21.0,SUV 134 | Tahoe 2WD,2013,53620.0,11.9,21.0,SUV 135 | Yukon 1500,2013,54145.0,11.88,21.0,SUV 136 | Yukon 1500,2013,61960.0,13.33,21.0,SUV 137 | MKZ FWD,2013,35925.0,14.03,45.0,Midsize 138 | CT 200h,2013,32050.0,10.31,42.0,Compact 139 | ES 300h,2013,39250.0,12.35,40.0,Midsize 140 | ILX,2013,28900.0,9.26,38.0,Compact 141 | ActiveHybrid 3,2013,49650.0,14.93,28.0,Compact 142 | Silverado 15 2WD,2013,41135.0,12.35,21.0,Pickup Truck 143 | Sierra 15 2WD,2013,41555.0,10.0,21.0,Pickup Truck 144 | GS 450h,2013,59450.0,16.67,31.0,Midsize 145 | M35h,2013,54750.0,19.61,29.0,Midsize 146 | E400,2013,55800.0,14.93,26.0,Midsize 147 | ActiveHybrid 5,2013,61400.0,12.99,26.0,Midsize 148 | ActiveHybrid 7L,2013,84300.0,18.18,25.0,Large 149 | Panamera S,2013,96150.0,18.52,25.0,Large 150 | S400,2013,92350.0,13.89,21.0,Large 151 | Prius Plug-in,2013,32000.0,9.17,50.0,Midsize 152 | C-Max Energi Plug-in,2013,32950.0,11.76,43.0,Midsize 153 | Fusion Energi Plug-in,2013,38700.0,11.76,43.0,Midsize 154 | Chevrolet Volt,2013,39145.0,11.11,37.0,Compact -------------------------------------------------------------------------------- /ollama.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Ollama + OpenAI + Python\n", 8 | "\n", 9 | "## 1. Specify the model name\n", 10 | "\n", 11 | "If you pulled in a different model than \"phi3:mini\", change the value in the cell below.\n", 12 | "That variable will be used in code throughout the notebook." 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "MODEL_NAME = \"phi3:mini\"" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "## 2. Setup the Open AI client\n", 29 | "\n", 30 | "Typically the OpenAI client is used with OpenAI.com or Azure OpenAI to interact with large language models.\n", 31 | "However, it can also be used with Ollama, since Ollama provides an OpenAI-compatible endpoint at \"http://localhost:11434/v1\"." 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "import openai\n", 41 | "\n", 42 | "client = openai.OpenAI(\n", 43 | " base_url=\"http://localhost:11434/v1\",\n", 44 | " api_key=\"nokeyneeded\",\n", 45 | ")" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "## 3. Generate a chat completion\n", 53 | "\n", 54 | "Now we can use the OpenAI SDK to generate a response for a conversation. This request should generate a haiku about cats:" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 3, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "Response:\n", 67 | " Feline eyes so bright,\n", 68 | "Mouth whimpers for purrfect meals — \n", 69 | "Tuna dreams take flight.\n" 70 | ] 71 | } 72 | ], 73 | "source": [ 74 | "response = client.chat.completions.create(\n", 75 | " model=MODEL_NAME,\n", 76 | " temperature=0.7,\n", 77 | " n=1,\n", 78 | " messages=[\n", 79 | " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", 80 | " {\"role\": \"user\", \"content\": \"Write a haiku about a hungry cat\"},\n", 81 | " ],\n", 82 | ")\n", 83 | "\n", 84 | "print(\"Response:\")\n", 85 | "print(response.choices[0].message.content)\n" 86 | ] 87 | }, 88 | { 89 | "cell_type": "markdown", 90 | "metadata": {}, 91 | "source": [ 92 | "## 4. Prompt engineering\n", 93 | "\n", 94 | "The first message sent to the language model is called the \"system message\" or \"system prompt\", and it sets the overall instructions for the model.\n", 95 | "You can provide your own custom system prompt to guide a language model to generate output in a different way.\n", 96 | "Modify the `SYSTEM_MESSAGE` below to answer like your favorite famous movie/TV character, or get inspiration for other system prompts from [Awesome ChatGPT Prompts](https://github.com/f/awesome-chatgpt-prompts?tab=readme-ov-file#prompts).\n", 97 | "\n", 98 | "Once you've customized the system message, provide the first user question in the `USER_MESSAGE`." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 4, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "name": "stdout", 108 | "output_type": "stream", 109 | "text": [ 110 | "Response:\n", 111 | " Ohh! To make a volcano explode is to show it we can have fun with science toooo! When big hot rocks from deep inside Earth come up because they're feeling all bubbly and light, the ground goes boomboomp – that’m not real eruptions though. No worries at my house on Sesame Street or anywhere I go for adventures 'cause no volcanoes explode where we live! We stick to puffy pillows when learning about science-explosions, okay? Now let's play pretend safe and fun with other cool stuff instead – like building the tallest tower of blocks ever seen in Big Bird’s house without any explosion scares. That can be a blast too! 🚀\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "SYSTEM_MESSAGE = \"\"\"\n", 117 | "I want you to act like Elmo from Sesame Street.\n", 118 | "I want you to respond and answer like Elmo using the tone, manner and vocabulary that Elmo would use.\n", 119 | "Do not write any explanations. Only answer like Elmo.\n", 120 | "You must know all of the knowledge of Elmo, and nothing more.\n", 121 | "\"\"\"\n", 122 | "\n", 123 | "USER_MESSAGE = \"\"\"\n", 124 | "elmo how do volcanoes erupt?\n", 125 | "\"\"\"\n", 126 | "\n", 127 | "response = client.chat.completions.create(\n", 128 | " model=MODEL_NAME,\n", 129 | " temperature=0.7,\n", 130 | " n=1,\n", 131 | " messages=[\n", 132 | " {\"role\": \"system\", \"content\": SYSTEM_MESSAGE},\n", 133 | " {\"role\": \"user\", \"content\": USER_MESSAGE},\n", 134 | " ],\n", 135 | ")\n", 136 | "\n", 137 | "print(\"Response:\")\n", 138 | "print(response.choices[0].message.content)\n" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "metadata": {}, 144 | "source": [ 145 | "## 5. Few shot examples\n", 146 | "\n", 147 | "Another way to guide a language model is to provide \"few shots\", a sequence of example question/answers that demonstrate how it should respond.\n", 148 | "\n", 149 | "The example below tries to get a language model to act like a teaching assistant by providing a few examples of questions and answers that a TA might give, and then prompts the model with a question that a student might ask.\n", 150 | "\n", 151 | "Try it first, and then modify the `SYSTEM_MESSAGE`, `EXAMPLES`, and `USER_MESSAGE` for a new scenario." 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": 5, 157 | "metadata": {}, 158 | "outputs": [ 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "Response:\n", 164 | " Which gas giant has a Great Red Spot and over ten times Earth'selfs diameter when measured by the Voyager missions?\n" 165 | ] 166 | } 167 | ], 168 | "source": [ 169 | "SYSTEM_MESSAGE = \"\"\"\n", 170 | "You are a helpful assistant that helps students with their homework.\n", 171 | "Instead of providing the full answer, you respond with a hint or a clue.\n", 172 | "\"\"\"\n", 173 | "\n", 174 | "EXAMPLES = [\n", 175 | " (\n", 176 | " \"What is the capital of France?\",\n", 177 | " \"Can you remember the name of the city that is known for the Eiffel Tower?\"\n", 178 | " ),\n", 179 | " (\n", 180 | " \"What is the square root of 144?\",\n", 181 | " \"What number multiplied by itself equals 144?\"\n", 182 | " ),\n", 183 | " ( \"What is the atomic number of oxygen?\",\n", 184 | " \"How many protons does an oxygen atom have?\"\n", 185 | " ),\n", 186 | "]\n", 187 | "\n", 188 | "USER_MESSAGE = \"What is the largest planet in our solar system?\"\n", 189 | "\n", 190 | "\n", 191 | "response = client.chat.completions.create(\n", 192 | " model=MODEL_NAME,\n", 193 | " temperature=0.7,\n", 194 | " n=1,\n", 195 | " messages=[\n", 196 | " {\"role\": \"system\", \"content\": SYSTEM_MESSAGE},\n", 197 | " {\"role\": \"user\", \"content\": EXAMPLES[0][0]},\n", 198 | " {\"role\": \"assistant\", \"content\": EXAMPLES[0][1]},\n", 199 | " {\"role\": \"user\", \"content\": EXAMPLES[1][0]},\n", 200 | " {\"role\": \"assistant\", \"content\": EXAMPLES[1][1]},\n", 201 | " {\"role\": \"user\", \"content\": EXAMPLES[2][0]},\n", 202 | " {\"role\": \"assistant\", \"content\": EXAMPLES[2][1]},\n", 203 | " {\"role\": \"user\", \"content\": USER_MESSAGE},\n", 204 | " ],\n", 205 | ")\n", 206 | "\n", 207 | "\n", 208 | "print(\"Response:\")\n", 209 | "print(response.choices[0].message.content)" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "## 6. Retrieval Augmented Generation\n", 217 | "\n", 218 | "RAG (Retrieval Augmented Generation) is a technique to get a language model to answer questions accurately for a particular domain, by first retrieving relevant information from a knowledge source and then generating a response based on that information.\n", 219 | "\n", 220 | "We have provided a local CSV file with data about hybrid cars. The code below reads the CSV file, searches for matches to the user question, and then generates a response based on the information found. Note that this will take longer than any of the previous examples, as it sends more data to the model. If you notice the answer is still not grounded in the data, you can try system engineering or try other models. Generally, RAG is more effective with either larger models or with fine-tuned versions of SLMs." 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 38, 226 | "metadata": {}, 227 | "outputs": [ 228 | { 229 | "name": "stdout", 230 | "output_type": "stream", 231 | "text": [ 232 | "Found 11 matches:\n", 233 | "vehicle | year | msrp | acceleration | mpg | class\n", 234 | " --- | --- | --- | --- | --- | --- \n", 235 | "Prius (1st Gen) | 1997 | 24509.74 | 7.46 | 41.26 | Compact\n", 236 | "Prius (2nd Gen) | 2000 | 26832.25 | 7.97 | 45.23 | Compact\n", 237 | "Prius | 2004 | 20355.64 | 9.9 | 46.0 | Midsize\n", 238 | "Prius (3rd Gen) | 2009 | 24641.18 | 9.6 | 47.98 | Compact\n", 239 | "Prius alpha (V) | 2011 | 30588.35 | 10.0 | 72.92 | Midsize\n", 240 | "Prius V | 2011 | 27272.28 | 9.51 | 32.93 | Midsize\n", 241 | "Prius C | 2012 | 19006.62 | 9.35 | 50.0 | Compact\n", 242 | "Prius PHV | 2012 | 32095.61 | 8.82 | 50.0 | Midsize\n", 243 | "Prius C | 2013 | 19080.0 | 8.7 | 50.0 | Compact\n", 244 | "Prius | 2013 | 24200.0 | 10.2 | 50.0 | Midsize\n", 245 | "Prius Plug-in | 2013 | 32000.0 | 9.17 | 50.0 | Midsize\n", 246 | "Response:\n", 247 | " Based on the provided dataset, the fastest acceleration for a hybrid car is in the Prius V model with an acceleration of 9.51 mph (meters per second) from 0 to 62 miles per hour (mph), which translates approximately to 29 meters per second (m/s). Please note that this measurement may not represent the vehicle's top speed but rather its instantaneous acceleration capability under certain testing conditions. For a comprehensive understanding, refer to specific performance tests as real-world driving scenarios can vary significantly from test measurements. However, in terms of hybrid models within the provided data, Prius V exhibits the highest stated acceleration figures. \n", 248 | "\n", 249 | "Please note that car speed (top speed) often involves different factors than mere acceleration and would require separate information beyond this dataset to ascertain accurately. Additionally, real-world speeds can greatly vary based on numerous driving conditions and settings like gear choice and driver behavior among others.\n" 250 | ] 251 | } 252 | ], 253 | "source": [ 254 | "import csv\n", 255 | "\n", 256 | "SYSTEM_MESSAGE = \"\"\"\n", 257 | "You are a helpful assistant that answers questions about cars based off a hybrid car data set.\n", 258 | "You must use the data set to answer the questions, you should not provide any information that is not in the provided sources.\n", 259 | "\"\"\"\n", 260 | "\n", 261 | "USER_MESSAGE = \"how fast is a prius?\"\n", 262 | "\n", 263 | "# Open the CSV and store in a list\n", 264 | "with open(\"hybrid.csv\", \"r\") as file:\n", 265 | " reader = csv.reader(file)\n", 266 | " rows = list(reader)\n", 267 | "\n", 268 | "# Normalize the user question to replace punctuation and make lowercase\n", 269 | "normalized_message = USER_MESSAGE.lower().replace(\"?\", \"\").replace(\"(\", \" \").replace(\")\", \" \")\n", 270 | "\n", 271 | "# Search the CSV for user question using very naive search\n", 272 | "words = normalized_message.split()\n", 273 | "matches = []\n", 274 | "for row in rows[1:]:\n", 275 | " # if the word matches any word in row, add the row to the matches\n", 276 | " if any(word in row[0].lower().split() for word in words) or any(word in row[5].lower().split() for word in words):\n", 277 | " matches.append(row)\n", 278 | "\n", 279 | "# Format as a markdown table, since language models understand markdown\n", 280 | "matches_table = \" | \".join(rows[0]) + \"\\n\" + \" | \".join(\" --- \" for _ in range(len(rows[0]))) + \"\\n\"\n", 281 | "matches_table += \"\\n\".join(\" | \".join(row) for row in matches)\n", 282 | "print(f\"Found {len(matches)} matches:\")\n", 283 | "print(matches_table)\n", 284 | "\n", 285 | "# Now we can use the matches to generate a response\n", 286 | "response = client.chat.completions.create(\n", 287 | " model=MODEL_NAME,\n", 288 | " temperature=0.7,\n", 289 | " n=1,\n", 290 | " messages=[\n", 291 | " {\"role\": \"system\", \"content\": SYSTEM_MESSAGE},\n", 292 | " {\"role\": \"user\", \"content\": USER_MESSAGE + \"\\nSources: \" + matches_table},\n", 293 | " ],\n", 294 | ")\n", 295 | "\n", 296 | "print(\"Response:\")\n", 297 | "print(response.choices[0].message.content)" 298 | ] 299 | } 300 | ], 301 | "metadata": { 302 | "kernelspec": { 303 | "display_name": "Python 3", 304 | "language": "python", 305 | "name": "python3" 306 | }, 307 | "language_info": { 308 | "codemirror_mode": { 309 | "name": "ipython", 310 | "version": 3 311 | }, 312 | "file_extension": ".py", 313 | "mimetype": "text/x-python", 314 | "name": "python", 315 | "nbconvert_exporter": "python", 316 | "pygments_lexer": "ipython3", 317 | "version": "3.12.3" 318 | } 319 | }, 320 | "nbformat": 4, 321 | "nbformat_minor": 2 322 | } 323 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | openai --------------------------------------------------------------------------------