File size: 8,174 Bytes
447ebeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "vnvlwUDZK7VA"
   },
   "source": [
    "## Demo Notebook of Function Calling with liteLLM\n",
    "- Supported Providers for Function Calling\n",
    "  - OpenAI - `gpt-4-0613` and `gpt-3.5-turbo-0613`\n",
    "- In this notebook we use function calling with `litellm.completion()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "KrINCwRfLgZV"
   },
   "outputs": [],
   "source": [
    "## Install liteLLM\n",
    "!pip install litellm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "nK7zR5OgLlh2"
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from litellm import completion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "id": "dCQlyBxKLqbA"
   },
   "outputs": [],
   "source": [
    "os.environ['OPENAI_API_KEY'] = \"\" #@param"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "gfdGv-FMRCdX"
   },
   "source": [
    "## Define Messages, Functions\n",
    "We create a get_current_weather() function and pass that to GPT 3.5\n",
    "\n",
    "See OpenAI docs for this: https://openai.com/blog/function-calling-and-other-api-updates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "id": "ERzsP1sfM19C"
   },
   "outputs": [],
   "source": [
    "messages = [\n",
    "    {\"role\": \"user\", \"content\": \"What is the weather like in Boston?\"}\n",
    "]\n",
    "\n",
    "def get_current_weather(location):\n",
    "  if location == \"Boston, MA\":\n",
    "    return \"The weather is 12F\"\n",
    "\n",
    "functions = [\n",
    "    {\n",
    "      \"name\": \"get_current_weather\",\n",
    "      \"description\": \"Get the current weather in a given location\",\n",
    "      \"parameters\": {\n",
    "        \"type\": \"object\",\n",
    "        \"properties\": {\n",
    "          \"location\": {\n",
    "            \"type\": \"string\",\n",
    "            \"description\": \"The city and state, e.g. San Francisco, CA\"\n",
    "          },\n",
    "          \"unit\": {\n",
    "            \"type\": \"string\",\n",
    "            \"enum\": [\"celsius\", \"fahrenheit\"]\n",
    "          }\n",
    "        },\n",
    "        \"required\": [\"location\"]\n",
    "      }\n",
    "    }\n",
    "  ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "NX6by2VuRPnp"
   },
   "source": [
    "## Call gpt-3.5-turbo-0613 to Decide what Function to call"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "QVoJ5PtxMlVx",
    "outputId": "efe7a81f-e04a-4afc-aa60-a2b2648f5fb9"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"id\": \"chatcmpl-7mX4RiqdoislVEqfmfVjFSKp3hyIy\",\n",
      "  \"object\": \"chat.completion\",\n",
      "  \"created\": 1691801223,\n",
      "  \"model\": \"gpt-3.5-turbo-0613\",\n",
      "  \"choices\": [\n",
      "    {\n",
      "      \"index\": 0,\n",
      "      \"message\": {\n",
      "        \"role\": \"assistant\",\n",
      "        \"content\": null,\n",
      "        \"function_call\": {\n",
      "          \"name\": \"get_current_weather\",\n",
      "          \"arguments\": \"{\\n  \\\"location\\\": \\\"Boston, MA\\\"\\n}\"\n",
      "        }\n",
      "      },\n",
      "      \"finish_reason\": \"function_call\"\n",
      "    }\n",
      "  ],\n",
      "  \"usage\": {\n",
      "    \"prompt_tokens\": 82,\n",
      "    \"completion_tokens\": 18,\n",
      "    \"total_tokens\": 100\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = completion(model=\"gpt-3.5-turbo-0613\", messages=messages, functions=functions)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Yu0o2saDNLx8"
   },
   "source": [
    "## Parse GPT 3.5 Response\n",
    "Read Information about what Function to Call"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "u1DzXLJsNOR5",
    "outputId": "177e9501-0ce2-4619-9067-3047f18f6c79"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject at 0x7922c70ce930> JSON: {\n",
       "  \"name\": \"get_current_weather\",\n",
       "  \"arguments\": \"{\\n  \\\"location\\\": \\\"Boston, MA\\\"\\n}\"\n",
       "}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function_call_data = response[\"choices\"][0][\"message\"][\"function_call\"]\n",
    "function_call_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "tYb96Mh0NhH9",
    "outputId": "13c4bb89-6f29-4b3b-afa7-302dcf2cdd5f"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "get_current_weather {'location': 'Boston, MA'}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "function_name = function_call_data['name']\n",
    "function_args = function_call_data['arguments']\n",
    "function_args = json.loads(function_args)\n",
    "print(function_name, function_args)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "z3tstH_yN3fX"
   },
   "source": [
    "## Call the get_current_weather() function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "TSb8JHhgN5Zc",
    "outputId": "ef140572-4020-4daf-ac8c-d5161be9aa5c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12F\n"
     ]
    }
   ],
   "source": [
    "if function_name == \"get_current_weather\":\n",
    "  result = get_current_weather(**function_args)\n",
    "  print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "k4HGJE3NRmMI"
   },
   "source": [
    "## Send the response from get_current_weather back to the model to summarize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "a23cmEwiPaw7",
    "outputId": "43259b86-0c4c-4fcb-eab7-6e1a788b2f21"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"id\": \"chatcmpl-7mXGN62u75WXp1Lgen4iSgNvA7hHT\",\n",
      "  \"object\": \"chat.completion\",\n",
      "  \"created\": 1691801963,\n",
      "  \"model\": \"gpt-3.5-turbo-0613\",\n",
      "  \"choices\": [\n",
      "    {\n",
      "      \"index\": 0,\n",
      "      \"message\": {\n",
      "        \"role\": \"assistant\",\n",
      "        \"content\": \"The current weather in Boston is 12 degrees Fahrenheit.\"\n",
      "      },\n",
      "      \"finish_reason\": \"stop\"\n",
      "    }\n",
      "  ],\n",
      "  \"usage\": {\n",
      "    \"prompt_tokens\": 109,\n",
      "    \"completion_tokens\": 12,\n",
      "    \"total_tokens\": 121\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {\"role\": \"user\", \"content\": \"What is the weather like in Boston?\"},\n",
    "    {\"role\": \"assistant\", \"content\": None, \"function_call\": {\"name\": \"get_current_weather\", \"arguments\": \"{ \\\"location\\\": \\\"Boston, MA\\\"}\"}},\n",
    "    {\"role\": \"function\", \"name\": \"get_current_weather\", \"content\": result}\n",
    "]\n",
    "response = completion(model=\"gpt-3.5-turbo-0613\", messages=messages, functions=functions)\n",
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}