File size: 2,418 Bytes
bd54294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f0f5f02-c8e9-43a9-853d-12bb3c19dbe8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydantic import BaseModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94244a1e-e55a-4954-885e-4558797c6fe3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.llms import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "641f36c7-0aa3-4146-9840-bfb0d4d78b4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.tools import BaseTool, FunctionTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb20cd13-20fd-4303-acde-b7abe0b48e39",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab4d1a52-84be-492f-8275-3da20d854cb6",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Song(BaseModel):\n",
    "    \"\"\"A song with name and artist\"\"\"\n",
    "\n",
    "    name: str\n",
    "    artist: str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5822b1d-32ef-4b68-8629-a727ff51cd0a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63332a44-9441-4f49-85a2-934e2c55a362",
   "metadata": {},
   "outputs": [],
   "source": [
    "song_fn = FunctionTool.from_defaults(fn=Song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef0d7d67-9855-47ea-8569-7bfb20b03a07",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = OpenAI().complete(\"Generate a song\", tools=[song_fn])\n",
    "tool_calls = response.additional_kwargs[\"tool_calls\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bca4c0b2-5165-4943-af1f-d3168ee88fcd",
   "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.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}