|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "name": "stdout", |
| 10 | + "output_type": "stream", |
| 11 | + "text": [ |
| 12 | + "{\n", |
| 13 | + " \"id\": \"chatcmpl-a6db1bbb-a128-4c28-88fe-30717ec806b2\",\n", |
| 14 | + " \"object\": \"chat.completion\",\n", |
| 15 | + " \"created\": 1698989577,\n", |
| 16 | + " \"model\": \"gpt-3.5-turbo-0613\",\n", |
| 17 | + " \"choices\": [\n", |
| 18 | + " {\n", |
| 19 | + " \"index\": 0,\n", |
| 20 | + " \"message\": {\n", |
| 21 | + " \"role\": \"assistant\",\n", |
| 22 | + " \"content\": \"The current weather in Boston is sunny with a temperature of 72 degrees\"\n", |
| 23 | + " },\n", |
| 24 | + " \"finish_reason\": \"length\"\n", |
| 25 | + " }\n", |
| 26 | + " ],\n", |
| 27 | + " \"usage\": {\n", |
| 28 | + " \"prompt_tokens\": 135,\n", |
| 29 | + " \"completion_tokens\": 16,\n", |
| 30 | + " \"total_tokens\": 151\n", |
| 31 | + " }\n", |
| 32 | + "}\n" |
| 33 | + ] |
| 34 | + } |
| 35 | + ], |
| 36 | + "source": [ |
| 37 | + "import openai\n", |
| 38 | + "import json\n", |
| 39 | + "\n", |
| 40 | + "openai.api_key = \"sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\" # can be anything\n", |
| 41 | + "openai.api_base = \"http://100.64.159.73:8000/v1\"\n", |
| 42 | + "\n", |
| 43 | + "# Example dummy function hard coded to return the same weather\n", |
| 44 | + "# In production, this could be your backend API or an external API\n", |
| 45 | + "def get_current_weather(location, unit=\"fahrenheit\"):\n", |
| 46 | + " \"\"\"Get the current weather in a given location\"\"\"\n", |
| 47 | + " weather_info = {\n", |
| 48 | + " \"location\": location,\n", |
| 49 | + " \"temperature\": \"72\",\n", |
| 50 | + " \"unit\": unit,\n", |
| 51 | + " \"forecast\": [\"sunny\", \"windy\"],\n", |
| 52 | + " }\n", |
| 53 | + " return json.dumps(weather_info)\n", |
| 54 | + "\n", |
| 55 | + "def run_conversation():\n", |
| 56 | + " # Step 1: send the conversation and available functions to GPT\n", |
| 57 | + " messages = [{\"role\": \"user\", \"content\": \"What's the weather like in Boston?\"}]\n", |
| 58 | + " functions = [\n", |
| 59 | + " {\n", |
| 60 | + " \"name\": \"get_current_weather\",\n", |
| 61 | + " \"description\": \"Get the current weather in a given location\",\n", |
| 62 | + " \"parameters\": {\n", |
| 63 | + " \"type\": \"object\",\n", |
| 64 | + " \"properties\": {\n", |
| 65 | + " \"location\": {\n", |
| 66 | + " \"type\": \"string\",\n", |
| 67 | + " \"description\": \"The city and state, e.g. San Francisco, CA\",\n", |
| 68 | + " },\n", |
| 69 | + " \"unit\": {\"type\": \"string\", \"enum\": [\"celsius\", \"fahrenheit\"]},\n", |
| 70 | + " },\n", |
| 71 | + " \"required\": [\"location\"],\n", |
| 72 | + " },\n", |
| 73 | + " }\n", |
| 74 | + " ]\n", |
| 75 | + " response = openai.ChatCompletion.create(\n", |
| 76 | + " model=\"gpt-3.5-turbo-0613\",\n", |
| 77 | + " messages=messages,\n", |
| 78 | + " functions=functions,\n", |
| 79 | + " function_call=\"auto\", # auto is default, but we'll be explicit\n", |
| 80 | + " )\n", |
| 81 | + " response_message = response[\"choices\"][0][\"message\"]\n", |
| 82 | + "\n", |
| 83 | + " # Step 2: check if GPT wanted to call a function\n", |
| 84 | + " if response_message.get(\"function_call\"):\n", |
| 85 | + " # Step 3: call the function\n", |
| 86 | + " # Note: the JSON response may not always be valid; be sure to handle errors\n", |
| 87 | + " available_functions = {\n", |
| 88 | + " \"get_current_weather\": get_current_weather,\n", |
| 89 | + " } # only one function in this example, but you can have multiple\n", |
| 90 | + " function_name = response_message[\"function_call\"][\"name\"]\n", |
| 91 | + " fuction_to_call = available_functions[function_name]\n", |
| 92 | + " function_args = json.loads(response_message[\"function_call\"][\"arguments\"])\n", |
| 93 | + " function_response = fuction_to_call(\n", |
| 94 | + " location=function_args.get(\"location\"),\n", |
| 95 | + " unit=function_args.get(\"unit\"),\n", |
| 96 | + " )\n", |
| 97 | + "\n", |
| 98 | + " # Step 4: send the info on the function call and function response to GPT\n", |
| 99 | + " messages.append(response_message) # extend conversation with assistant's reply\n", |
| 100 | + " messages.append(\n", |
| 101 | + " {\n", |
| 102 | + " \"role\": \"function\",\n", |
| 103 | + " \"name\": function_name,\n", |
| 104 | + " \"content\": function_response,\n", |
| 105 | + " }\n", |
| 106 | + " ) # extend conversation with function response\n", |
| 107 | + " second_response = openai.ChatCompletion.create(\n", |
| 108 | + " model=\"gpt-3.5-turbo-0613\",\n", |
| 109 | + " messages=messages,\n", |
| 110 | + " ) # get a new response from GPT where it can see the function response\n", |
| 111 | + " return second_response\n", |
| 112 | + " else:\n", |
| 113 | + " print(response)\n", |
| 114 | + " print(\"No function\")\n", |
| 115 | + "\n", |
| 116 | + "print(run_conversation())" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": 2, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [ |
| 124 | + { |
| 125 | + "name": "stdout", |
| 126 | + "output_type": "stream", |
| 127 | + "text": [ |
| 128 | + "name='Jason' age=25\n" |
| 129 | + ] |
| 130 | + } |
| 131 | + ], |
| 132 | + "source": [ |
| 133 | + "from pydantic import BaseModel\n", |
| 134 | + "from instructor import patch\n", |
| 135 | + "\n", |
| 136 | + "patch()\n", |
| 137 | + "\n", |
| 138 | + "class UserDetail(BaseModel):\n", |
| 139 | + " name: str\n", |
| 140 | + " age: int\n", |
| 141 | + "\n", |
| 142 | + "user: UserDetail = openai.ChatCompletion.create(\n", |
| 143 | + " model=\"gpt-3.5-turbo\",\n", |
| 144 | + " response_model=UserDetail,\n", |
| 145 | + " messages=[\n", |
| 146 | + " {\"role\": \"user\", \"content\": \"Extract Jason is 25 years old\"},\n", |
| 147 | + " ]\n", |
| 148 | + ")\n", |
| 149 | + "print(user)" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": 3, |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [ |
| 157 | + { |
| 158 | + "name": "stdout", |
| 159 | + "output_type": "stream", |
| 160 | + "text": [ |
| 161 | + "{\n", |
| 162 | + " \"id\": \"chatcmpl-59bcefad-9df5-4d6b-802c-5537b3e9044e\",\n", |
| 163 | + " \"object\": \"chat.completion\",\n", |
| 164 | + " \"created\": 1698989585,\n", |
| 165 | + " \"model\": \"gpt-3.5-turbo-0613\",\n", |
| 166 | + " \"choices\": [\n", |
| 167 | + " {\n", |
| 168 | + " \"index\": 0,\n", |
| 169 | + " \"message\": {\n", |
| 170 | + " \"role\": \"assistant\",\n", |
| 171 | + " \"content\": \"I don't have up-to-date information on the current weather conditions\"\n", |
| 172 | + " },\n", |
| 173 | + " \"finish_reason\": \"length\"\n", |
| 174 | + " }\n", |
| 175 | + " ],\n", |
| 176 | + " \"usage\": {\n", |
| 177 | + " \"prompt_tokens\": 62,\n", |
| 178 | + " \"completion_tokens\": 16,\n", |
| 179 | + " \"total_tokens\": 78\n", |
| 180 | + " }\n", |
| 181 | + "}\n" |
| 182 | + ] |
| 183 | + } |
| 184 | + ], |
| 185 | + "source": [ |
| 186 | + "response = openai.ChatCompletion.create(\n", |
| 187 | + " model=\"gpt-3.5-turbo-0613\",\n", |
| 188 | + " messages=[\n", |
| 189 | + " {\"role\": \"user\", \"content\": \"What's the weather like in Boston?\"}\n", |
| 190 | + " ]\n", |
| 191 | + ")\n", |
| 192 | + "print(response)" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": null, |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [], |
| 200 | + "source": [] |
| 201 | + } |
| 202 | + ], |
| 203 | + "metadata": { |
| 204 | + "kernelspec": { |
| 205 | + "display_name": "python-3.8.10", |
| 206 | + "language": "python", |
| 207 | + "name": "python3" |
| 208 | + }, |
| 209 | + "language_info": { |
| 210 | + "codemirror_mode": { |
| 211 | + "name": "ipython", |
| 212 | + "version": 3 |
| 213 | + }, |
| 214 | + "file_extension": ".py", |
| 215 | + "mimetype": "text/x-python", |
| 216 | + "name": "python", |
| 217 | + "nbconvert_exporter": "python", |
| 218 | + "pygments_lexer": "ipython3", |
| 219 | + "version": "3.11.5+" |
| 220 | + }, |
| 221 | + "orig_nbformat": 4 |
| 222 | + }, |
| 223 | + "nbformat": 4, |
| 224 | + "nbformat_minor": 2 |
| 225 | +} |
0 commit comments