File size: 26,362 Bytes
95bf5e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/tamizh/miniconda3/envs/movies-app/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): huggingface.co:443\n",
      "DEBUG:urllib3.connectionpool:https://huggingface.co:443 \"HEAD /meta-llama/Meta-Llama-3.1-8B-Instruct/resolve/main/tokenizer_config.json HTTP/11\" 200 0\n",
      "DEBUG:urllib3.connectionpool:https://huggingface.co:443 \"HEAD /meta-llama/Meta-Llama-3.1-8B-Instruct/resolve/main/config.json HTTP/11\" 200 0\n",
      "DEBUG:bitsandbytes.cextension:Loading bitsandbytes native library from: /home/tamizh/miniconda3/envs/movies-app/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_cuda121.so\n",
      "INFO:accelerate.utils.modeling:We will use 90% of the memory on device 0 for storing the model, and 10% for the buffer to avoid OOM. You can set `max_memory` in to a higher value to use more memory (at your own risk).\n",
      "Loading checkpoint shards: 100%|██████████| 4/4 [00:07<00:00,  1.81s/it]\n",
      "DEBUG:urllib3.connectionpool:https://huggingface.co:443 \"HEAD /meta-llama/Meta-Llama-3.1-8B-Instruct/resolve/main/generation_config.json HTTP/11\" 200 0\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "import json\n",
    "\n",
    "from functions import *\n",
    "from transformers import pipeline\n",
    "from tools import tools\n",
    "\n",
    "import functions\n",
    "import torch\n",
    "from transformers import (\n",
    "    AutoModelForCausalLM,\n",
    "    AutoTokenizer,\n",
    "    BitsAndBytesConfig\n",
    ")\n",
    "\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "\n",
    "quantization_config = BitsAndBytesConfig(\n",
    "    load_in_8bit=True,\n",
    "    load_in_4bit=False,\n",
    "    bnb_4bit_quant_type=\"nf4\",\n",
    "    bnb_4bit_compute_dtype=torch.bfloat16\n",
    ")\n",
    "\n",
    "model_id = \"meta-llama/Meta-Llama-3.1-8B-Instruct\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_id)\n",
    "model = AutoModelForCausalLM.from_pretrained(model_id, \n",
    "                                             device_map=\"auto\", \n",
    "                                             quantization_config=quantization_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_reasoning_chain(query):\n",
    "    user_message = f\"\"\"\n",
    "    Given the user query: \"{query}\"\n",
    "    Generate a multi-step reasoning chain to answer the query. Include steps for using available tools if necessary.\n",
    "    \"\"\"\n",
    "\n",
    "    messages = [\n",
    "        {\"role\": \"system\", \"content\": \"You are a movie search assistant bot who uses TMDB to help users find movies. Think step by step and identify the sequence of function calls that will help to answer.\"},\n",
    "        {\"role\": \"user\", \"content\": user_message},\n",
    "    ]\n",
    "\n",
    "    tokenized_chat = tokenizer.apply_chat_template(\n",
    "        messages, tools=tools, add_generation_prompt=False, tokenize=True, return_tensors=\"pt\")\n",
    "\n",
    "\n",
    "    outputs = model.generate(tokenized_chat, max_new_tokens=128)\n",
    "    # return tokenizer.batch_decode(outputs[:, tokenized_chat.shape[1]:])[0]\n",
    "    return tokenizer.batch_decode(outputs)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{{- bos_token }}\n",
      "{%- if custom_tools is defined %}\n",
      "    {%- set tools = custom_tools %}\n",
      "{%- endif %}\n",
      "{%- if not tools_in_user_message is defined %}\n",
      "    {%- set tools_in_user_message = true %}\n",
      "{%- endif %}\n",
      "{%- if not date_string is defined %}\n",
      "    {%- set date_string = \"26 Jul 2024\" %}\n",
      "{%- endif %}\n",
      "{%- if not tools is defined %}\n",
      "    {%- set tools = none %}\n",
      "{%- endif %}\n",
      "\n",
      "{#- This block extracts the system message, so we can slot it into the right place. #}\n",
      "{%- if messages[0]['role'] == 'system' %}\n",
      "    {%- set system_message = messages[0]['content']|trim %}\n",
      "    {%- set messages = messages[1:] %}\n",
      "{%- else %}\n",
      "    {%- set system_message = \"\" %}\n",
      "{%- endif %}\n",
      "\n",
      "{#- System message + builtin tools #}\n",
      "{{- \"<|start_header_id|>system<|end_header_id|>\\n\\n\" }}\n",
      "{%- if builtin_tools is defined or tools is not none %}\n",
      "    {{- \"Environment: ipython\\n\" }}\n",
      "{%- endif %}\n",
      "{%- if builtin_tools is defined %}\n",
      "    {{- \"Tools: \" + builtin_tools | reject('equalto', 'code_interpreter') | join(\", \") + \"\\n\\n\"}}\n",
      "{%- endif %}\n",
      "{{- \"Cutting Knowledge Date: December 2023\\n\" }}\n",
      "{{- \"Today Date: \" + date_string + \"\\n\\n\" }}\n",
      "{%- if tools is not none and not tools_in_user_message %}\n",
      "    {{- \"You have access to the following functions. To call a function, please respond with JSON for a function call.\" }}\n",
      "    {{- 'Respond in the format {\"name\": function name, \"parameters\": dictionary of argument name and its value}.' }}\n",
      "    {{- \"Do not use variables.\\n\\n\" }}\n",
      "    {%- for t in tools %}\n",
      "        {{- t | tojson(indent=4) }}\n",
      "        {{- \"\\n\\n\" }}\n",
      "    {%- endfor %}\n",
      "{%- endif %}\n",
      "{{- system_message }}\n",
      "{{- \"<|eot_id|>\" }}\n",
      "\n",
      "{#- Custom tools are passed in a user message with some extra guidance #}\n",
      "{%- if tools_in_user_message and not tools is none %}\n",
      "    {#- Extract the first user message so we can plug it in here #}\n",
      "    {%- if messages | length != 0 %}\n",
      "        {%- set first_user_message = messages[0]['content']|trim %}\n",
      "        {%- set messages = messages[1:] %}\n",
      "    {%- else %}\n",
      "        {{- raise_exception(\"Cannot put tools in the first user message when there's no first user message!\") }}\n",
      "{%- endif %}\n",
      "    {{- '<|start_header_id|>user<|end_header_id|>\\n\\n' -}}\n",
      "    {{- \"Given the following functions, please respond with a JSON for a function call \" }}\n",
      "    {{- \"with its proper arguments that best answers the given prompt.\\n\\n\" }}\n",
      "    {{- 'Respond in the format {\"name\": function name, \"parameters\": dictionary of argument name and its value}.' }}\n",
      "    {{- \"Do not use variables.\\n\\n\" }}\n",
      "    {%- for t in tools %}\n",
      "        {{- t | tojson(indent=4) }}\n",
      "        {{- \"\\n\\n\" }}\n",
      "    {%- endfor %}\n",
      "    {{- first_user_message + \"<|eot_id|>\"}}\n",
      "{%- endif %}\n",
      "\n",
      "{%- for message in messages %}\n",
      "    {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}\n",
      "        {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\\n\\n'+ message['content'] | trim + '<|eot_id|>' }}\n",
      "    {%- elif 'tool_calls' in message %}\n",
      "        {%- if not message.tool_calls|length == 1 %}\n",
      "            {{- raise_exception(\"This model only supports single tool-calls at once!\") }}\n",
      "        {%- endif %}\n",
      "        {%- set tool_call = message.tool_calls[0].function %}\n",
      "        {%- if builtin_tools is defined and tool_call.name in builtin_tools %}\n",
      "            {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' -}}\n",
      "            {{- \"<|python_tag|>\" + tool_call.name + \".call(\" }}\n",
      "            {%- for arg_name, arg_val in tool_call.arguments | items %}\n",
      "                {{- arg_name + '=\"' + arg_val + '\"' }}\n",
      "                {%- if not loop.last %}\n",
      "                    {{- \", \" }}\n",
      "                {%- endif %}\n",
      "                {%- endfor %}\n",
      "            {{- \")\" }}\n",
      "        {%- else  %}\n",
      "            {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' -}}\n",
      "            {{- '{\"name\": \"' + tool_call.name + '\", ' }}\n",
      "            {{- '\"parameters\": ' }}\n",
      "            {{- tool_call.arguments | tojson }}\n",
      "            {{- \"}\" }}\n",
      "        {%- endif %}\n",
      "        {%- if builtin_tools is defined %}\n",
      "            {#- This means we're in ipython mode #}\n",
      "            {{- \"<|eom_id|>\" }}\n",
      "        {%- else %}\n",
      "            {{- \"<|eot_id|>\" }}\n",
      "        {%- endif %}\n",
      "    {%- elif message.role == \"tool\" or message.role == \"ipython\" %}\n",
      "        {{- \"<|start_header_id|>ipython<|end_header_id|>\\n\\n\" }}\n",
      "        {%- if message.content is mapping or message.content is iterable %}\n",
      "            {{- message.content | tojson }}\n",
      "        {%- else %}\n",
      "            {{- message.content }}\n",
      "        {%- endif %}\n",
      "        {{- \"<|eot_id|>\" }}\n",
      "    {%- endif %}\n",
      "{%- endfor %}\n",
      "{%- if add_generation_prompt %}\n",
      "    {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }}\n",
      "{%- endif %}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(tokenizer.chat_template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'type': 'function',\n",
       "  'function': {'name': 'search_person',\n",
       "   'description': 'Search for people in the entertainment industry.',\n",
       "   'parameters': {'type': 'object',\n",
       "    'properties': {'query': {'type': 'string',\n",
       "      'description': 'The search query for the person'},\n",
       "     'include_adult': {'type': 'boolean',\n",
       "      'description': 'Include adult (pornography) content in the results',\n",
       "      'default': False},\n",
       "     'language': {'type': 'string',\n",
       "      'description': 'Language for the search results',\n",
       "      'default': 'en-US'},\n",
       "     'page': {'type': 'integer',\n",
       "      'description': 'Page number of results',\n",
       "      'default': 1}},\n",
       "    'required': ['query']}}},\n",
       " {'type': 'function',\n",
       "  'function': {'name': 'get_person_details',\n",
       "   'description': 'Get detailed information about a specific person.',\n",
       "   'parameters': {'type': 'object',\n",
       "    'properties': {'person_id': {'type': 'integer',\n",
       "      'description': 'The ID of the person to get details for'},\n",
       "     'language': {'type': 'string',\n",
       "      'description': 'Language for the person details',\n",
       "      'default': 'en-US'},\n",
       "     'append_to_response': {'type': 'string',\n",
       "      'description': \"Comma-separated list of additional details to append to the response (e.g., 'images,credits')\"}},\n",
       "    'required': ['person_id']}}}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tools = [\n",
    "    {'type': 'function', 'function': {'name': 'search_person'}},\n",
    "    {'type': 'function', 'function': {'name': 'get_person_details'}} \n",
    "]\n",
    "\n",
    "messages = [\n",
    "        {\"role\": \"system\", \"content\": \"You are a movie search assistant bot who uses TMDB to help users find movies. Think step by step and identify the sequence of function calls that will help to answer.\"},\n",
    "        {\"role\": \"user\", \"content\": \"\"\"Generate a multi-step reasoning chain to answer the query. Include steps for using available tools if necessary.\n",
    "        Reasoning chain:\n",
    "        \"\"\"},\n",
    "        {\"role\": \"assistant\", \"content\": \"Model response\"},\n",
    "    ]\n",
    "\n",
    "\n",
    "expected_rendered_text = \"\"\"\n",
    "<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n",
    "\n",
    "Environment: ipython\n",
    "Cutting Knowledge Date: December 2023\n",
    "Today Date: 26 Jul 2024\n",
    "\n",
    "You are a movie search assistant bot who uses TMDB to help users find movies. Think step by step and identify the sequence of function calls that will help to answer.<|eot_id|>\n",
    "<|start_header_id|>user<|end_header_id|>\n",
    "Generate a multi-step reasoning chain to answer the query. Include steps for using available tools if necessary.\n",
    "<|eot_id|>\n",
    "<|start_header_id|>assistant<|end_header_id|>model_response<|eot_id|>\n",
    "<|start_header_id|>user<|end_header_id|>\n",
    "Given the following functions, please respond with a JSON for a function call with its proper arguments that best answers the given prompt.\n",
    "\n",
    "Respond in the format {\"name\": function name, \"parameters\": dictionary of argument name and its value}.Do not use variables.\n",
    "\n",
    "{\n",
    "    \"type\": \"function\",\n",
    "    \"function\": {\n",
    "        \"name\": \"discover_movie\"}\n",
    "}\n",
    "\n",
    "{\n",
    "    \"type\": \"function\",\n",
    "    \"function\": {\n",
    "        \"name\": \"get_person_details\"}\n",
    "}\n",
    "\"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n",
      "\n",
      "Environment: ipython\n",
      "Cutting Knowledge Date: December 2023\n",
      "Today Date: 26 Jul 2024\n",
      "\n",
      "You are a movie search assistant bot who uses TMDB to help users find movies. Think step by step and identify the sequence of function calls that will help to answer.<|eot_id|><|start_header_id|>user<|end_header_id|>\n",
      "\n",
      "Given the following functions, please respond with a JSON for a function call with its proper arguments that best answers the given prompt.\n",
      "\n",
      "Respond in the format {\"name\": function name, \"parameters\": dictionary of argument name and its value}.Do not use variables.\n",
      "\n",
      "{\n",
      "    \"type\": \"function\",\n",
      "    \"function\": {\n",
      "        \"name\": \"discover_movie\",\n",
      "        \"description\": \"Find movies using over 30 filters and sort options\",\n",
      "        \"parameters\": {\n",
      "            \"type\": \"object\",\n",
      "            \"properties\": {\n",
      "                \"region\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"ISO 3166-1 code to filter release dates\"\n",
      "                },\n",
      "                \"sort_by\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"Sort the results\"\n",
      "                },\n",
      "                \"release_date.gte\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"Filter and only include movies that have a release date (looking at all release dates) that is greater or equal to the specified value\"\n",
      "                },\n",
      "                \"release_date.lte\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"Filter and only include movies that have a release date (looking at all release dates) that is less than or equal to the specified value\"\n",
      "                },\n",
      "                \"with_release_type\": {\n",
      "                    \"type\": \"integer\",\n",
      "                    \"description\": \"Specify a comma (AND) or pipe (OR) separated value to filter release types\"\n",
      "                },\n",
      "                \"year\": {\n",
      "                    \"type\": \"integer\",\n",
      "                    \"description\": \"Filter the results to only include movies that have a release year that equals the specified value\"\n",
      "                },\n",
      "                \"with_cast\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"A comma separated list of person ID's to filter the results with\"\n",
      "                },\n",
      "                \"with_crew\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"A comma separated list of person ID's to filter the results with\"\n",
      "                },\n",
      "                \"with_people\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"A comma separated list of person ID's to filter the results with\"\n",
      "                },\n",
      "                \"with_companies\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"A comma separated list of production company ID's to filter the results with\"\n",
      "                },\n",
      "                \"with_genres\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"A comma separated list of genre ID's to filter the results with\"\n",
      "                },\n",
      "                \"without_genres\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"A comma separated list of genre ID's to exclude from the results\"\n",
      "                },\n",
      "                \"with_keywords\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"A comma separated list of keyword ID's to filter the results with\"\n",
      "                },\n",
      "                \"without_keywords\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"A comma separated list of keyword ID's to exclude from the results\"\n",
      "                }\n",
      "            },\n",
      "            \"required\": []\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "{\n",
      "    \"type\": \"function\",\n",
      "    \"function\": {\n",
      "        \"name\": \"get_movie_details\",\n",
      "        \"description\": \"Get the top level details of a movie by ID\",\n",
      "        \"parameters\": {\n",
      "            \"type\": \"object\",\n",
      "            \"properties\": {\n",
      "                \"movie_id\": {\n",
      "                    \"type\": \"integer\",\n",
      "                    \"description\": \"The ID of the movie to get details for\"\n",
      "                },\n",
      "                \"append_to_response\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"Comma-separated list of sub requests to append to the response\"\n",
      "                }\n",
      "            },\n",
      "            \"required\": [\n",
      "                \"movie_id\"\n",
      "            ]\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "{\n",
      "    \"type\": \"function\",\n",
      "    \"function\": {\n",
      "        \"name\": \"search_person\",\n",
      "        \"description\": \"Search for people in the entertainment industry.\",\n",
      "        \"parameters\": {\n",
      "            \"type\": \"object\",\n",
      "            \"properties\": {\n",
      "                \"query\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"The search query for the person\"\n",
      "                },\n",
      "                \"include_adult\": {\n",
      "                    \"type\": \"boolean\",\n",
      "                    \"description\": \"Include adult (pornography) content in the results\",\n",
      "                    \"default\": false\n",
      "                },\n",
      "                \"language\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"Language for the search results\",\n",
      "                    \"default\": \"en-US\"\n",
      "                },\n",
      "                \"page\": {\n",
      "                    \"type\": \"integer\",\n",
      "                    \"description\": \"Page number of results\",\n",
      "                    \"default\": 1\n",
      "                }\n",
      "            },\n",
      "            \"required\": [\n",
      "                \"query\"\n",
      "            ]\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "{\n",
      "    \"type\": \"function\",\n",
      "    \"function\": {\n",
      "        \"name\": \"get_person_details\",\n",
      "        \"description\": \"Get detailed information about a specific person.\",\n",
      "        \"parameters\": {\n",
      "            \"type\": \"object\",\n",
      "            \"properties\": {\n",
      "                \"person_id\": {\n",
      "                    \"type\": \"integer\",\n",
      "                    \"description\": \"The ID of the person to get details for\"\n",
      "                },\n",
      "                \"language\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"Language for the person details\",\n",
      "                    \"default\": \"en-US\"\n",
      "                },\n",
      "                \"append_to_response\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"Comma-separated list of additional details to append to the response (e.g., 'images,credits')\"\n",
      "                }\n",
      "            },\n",
      "            \"required\": [\n",
      "                \"person_id\"\n",
      "            ]\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "{\n",
      "    \"type\": \"function\",\n",
      "    \"function\": {\n",
      "        \"name\": \"get_movie_genres\",\n",
      "        \"description\": \"Get the list of official genres for movies.\",\n",
      "        \"parameters\": {\n",
      "            \"type\": \"object\",\n",
      "            \"properties\": {\n",
      "                \"language\": {\n",
      "                    \"type\": \"string\",\n",
      "                    \"description\": \"Language for the genre names\",\n",
      "                    \"default\": \"en-US\"\n",
      "                }\n",
      "            }\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "Given the user query: \"What are the genres of the movie 'The Dark Knight'?\"\n",
      "    Generate a multi-step reasoning chain to answer the query. Include steps for using available tools if necessary.<|eot_id|>\n"
     ]
    }
   ],
   "source": [
    "import textwrap\n",
    "\n",
    "\n",
    "query = \"What are the genres of the movie 'The Dark Knight'?\"\n",
    "\n",
    "user_message = f\"\"\"\n",
    "    Given the user query: \"{query}\"\n",
    "    Generate a multi-step reasoning chain to answer the query. Include steps for using available tools if necessary.\n",
    "    \"\"\"\n",
    "messages = [\n",
    "        {\"role\": \"system\", \"content\": \"You are a movie search assistant bot who uses TMDB to help users find movies. Think step by step and identify the sequence of function calls that will help to answer.\"},\n",
    "        {\"role\": \"user\", \"content\": user_message},\n",
    "    ]\n",
    "\n",
    "chat = tokenizer.apply_chat_template(\n",
    "        messages, tools=tools, \n",
    "        add_generation_prompt=False, \n",
    "        tools_in_user_message=True,\n",
    "        tokenize=False, \n",
    "        return_tensors=\"pt\")\n",
    "print(chat)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = generate_reasoning_chain(\"What are the genres of the movie 'The Dark Knight'?\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "<|begin_of_text|>\n",
    "<|start_header_id|>system<|end_header_id|>          {{ system_prompt }}<|eot_id|>\n",
    "<|start_header_id|>user<|end_header_id|>            {{ user_message_1 }}<|eot_id|>\n",
    "<|start_header_id|>assistant<|end_header_id|>       <|python_tag|>{{ model_tool_call_1 }}<|eom_id|>\n",
    "<|start_header_id|>ipython<|end_header_id|>         {{ tool_response }}<|eot_id|>\n",
    "<|start_header_id|>assistant<|end_header_id|>       {{ model_response_based_on_tool_response }}<|eot_id|>\n",
    "\"\"\"\n",
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "movies-app",
   "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": 2
}