|
2 | 2 | "cells": [
|
3 | 3 | {
|
4 | 4 | "cell_type": "code",
|
| 5 | +"execution_count": null, |
5 | 6 | "id": "initial_id",
|
6 | 7 | "metadata": {
|
7 | 8 | "collapsed": true
|
8 | 9 | },
|
| 10 | +"outputs": [], |
9 | 11 | "source": [
|
10 | 12 | "import time\n",
|
11 | 13 | "import taskingai\n",
|
12 | 14 | "# Load TaskingAI API Key from environment variable"
|
13 |
| -], |
14 |
| -"outputs": [], |
15 |
| -"execution_count": null |
| 15 | +] |
16 | 16 | },
|
17 | 17 | {
|
18 | 18 | "cell_type": "markdown",
|
| 19 | +"id": "4ca20b4a868dedd8", |
| 20 | +"metadata": { |
| 21 | +"collapsed": false |
| 22 | +}, |
19 | 23 | "source": [
|
20 | 24 | "# TaskingAI: Chat with Assistant Example\n",
|
21 | 25 | "\n",
|
22 | 26 | "In this example, we will first create an assistant who knows the meaning of various numbers and will explain it in certain language.\n",
|
23 | 27 | "Then we will start a chat with the assistant."
|
24 |
| -], |
25 |
| -"metadata": { |
26 |
| -"collapsed": false |
27 |
| -}, |
28 |
| -"id": "4ca20b4a868dedd8" |
| 28 | +] |
29 | 29 | },
|
30 | 30 | {
|
31 | 31 | "cell_type": "markdown",
|
32 |
| -"source": [ |
33 |
| -"## Create Assistant" |
34 |
| -], |
| 32 | +"id": "5e19ac923d84e898", |
35 | 33 | "metadata": {
|
36 | 34 | "collapsed": false
|
37 | 35 | },
|
38 |
| -"id": "5e19ac923d84e898" |
| 36 | +"source": [ |
| 37 | +"## Create Assistant" |
| 38 | +] |
39 | 39 | },
|
40 | 40 | {
|
41 | 41 | "cell_type": "code",
|
42 |
| -"source": [ |
43 |
| -"from taskingai.tool import Action, ActionAuthentication, ActionAuthenticationType\n", |
44 |
| -"from typing import List\n", |
45 |
| -"\n", |
46 |
| -"# create an assistant action\n", |
47 |
| -"NUMBERS_API_SCHEMA = {\n", |
48 |
| -" \"openapi\": \"3.0.0\",\n", |
49 |
| -" \"info\": {\n", |
50 |
| -" \"title\": \"Numbers API\",\n", |
51 |
| -" \"version\": \"1.0.0\",\n", |
52 |
| -" \"description\": \"API for fetching interesting number facts\"\n", |
53 |
| -" },\n", |
54 |
| -" \"servers\": [\n", |
55 |
| -" {\n", |
56 |
| -" \"url\": \"http://numbersapi.com\"\n", |
57 |
| -" }\n", |
58 |
| -" ],\n", |
59 |
| -" \"paths\": {\n", |
60 |
| -" \"/{number}\": {\n", |
61 |
| -" \"get\": {\n", |
62 |
| -" \"description\": \"Get a fact about a number\",\n", |
63 |
| -" \"operationId\": \"getNumberFact\",\n", |
64 |
| -" \"parameters\": [\n", |
65 |
| -" {\n", |
66 |
| -" \"name\": \"number\",\n", |
67 |
| -" \"in\": \"path\",\n", |
68 |
| -" \"required\": True,\n", |
69 |
| -" \"description\": \"The number to get the fact for\",\n", |
70 |
| -" \"schema\": {\n", |
71 |
| -" \"type\": \"integer\"\n", |
72 |
| -" }\n", |
73 |
| -" }\n", |
74 |
| -" ],\n", |
75 |
| -" \"responses\": {\n", |
76 |
| -" \"200\": {\n", |
77 |
| -" \"description\": \"A fact about the number\",\n", |
78 |
| -" \"content\": {\n", |
79 |
| -" \"text/plain\": {\n", |
80 |
| -" \"schema\": {\n", |
81 |
| -" \"type\": \"string\"\n", |
82 |
| -" }\n", |
83 |
| -" }\n", |
84 |
| -" }\n", |
85 |
| -" }\n", |
86 |
| -" }\n", |
87 |
| -" }\n", |
88 |
| -" }\n", |
89 |
| -" }\n", |
90 |
| -"}\n", |
91 |
| -"actions: List[Action] = taskingai.tool.bulk_create_actions(\n", |
92 |
| -" openapi_schema=NUMBERS_API_SCHEMA,\n", |
93 |
| -" authentication=ActionAuthentication(\n", |
94 |
| -" type=ActionAuthenticationType.NONE,\n", |
95 |
| -" )\n", |
96 |
| -")\n", |
97 |
| -"action = actions[0]\n", |
98 |
| -"print(f\"created action: {action}\\n\")" |
99 |
| -], |
| 42 | +"execution_count": null, |
| 43 | +"id": "3b3df0f232021283", |
100 | 44 | "metadata": {
|
101 | 45 | "collapsed": false
|
102 | 46 | },
|
103 |
| -"id": "3b2fda39ba58c5e9", |
104 | 47 | "outputs": [],
|
105 |
| -"execution_count": null |
106 |
| -}, |
107 |
| -{ |
108 |
| -"cell_type": "code", |
109 | 48 | "source": [
|
110 | 49 | "from taskingai.assistant import Assistant, Chat, ToolRef, ToolType\n",
|
111 | 50 | "from taskingai.assistant.memory import AssistantMessageWindowMemory\n",
|
|
118 | 57 | " name=\"My Assistant\",\n",
|
119 | 58 | " description=\"A assistant who knows the meaning of various numbers.\",\n",
|
120 | 59 | " memory=AssistantMessageWindowMemory(\n",
|
121 |
| -" max_messages=20,\n", |
122 | 60 | " max_tokens=1000\n",
|
123 | 61 | " ),\n",
|
124 | 62 | " system_prompt_template=[\n",
|
|
127 | 65 | " ],\n",
|
128 | 66 | " tools=[\n",
|
129 | 67 | " ToolRef(\n",
|
130 |
| -" type=ToolType.ACTION,\n", |
131 |
| -" id=action.action_id,\n", |
| 68 | +" type=ToolType.PLUGIN,\n", |
| 69 | +" id=\"open_weather/get_hourly_forecast\",\n", |
132 | 70 | " )\n",
|
133 | 71 | " ],\n",
|
134 | 72 | " retrievals=[],\n",
|
135 | 73 | " metadata={\"k\": \"v\"},\n",
|
136 | 74 | ")\n",
|
137 | 75 | "print(f\"created assistant: {assistant}\\n\")"
|
138 |
| -], |
139 |
| -"metadata": { |
140 |
| -"collapsed": false |
141 |
| -}, |
142 |
| -"id": "3b3df0f232021283", |
143 |
| -"outputs": [], |
144 |
| -"execution_count": null |
| 76 | +] |
145 | 77 | },
|
146 | 78 | {
|
147 | 79 | "cell_type": "markdown",
|
148 |
| -"source": [ |
149 |
| -"## Start a Chat " |
150 |
| -], |
| 80 | +"id": "8e7c1b9461f0a344", |
151 | 81 | "metadata": {
|
152 | 82 | "collapsed": false
|
153 | 83 | },
|
154 |
| -"id": "8e7c1b9461f0a344" |
| 84 | +"source": [ |
| 85 | +"## Start a Chat " |
| 86 | +] |
155 | 87 | },
|
156 | 88 | {
|
157 | 89 | "cell_type": "code",
|
| 90 | +"execution_count": null, |
| 91 | +"id": "f1e2f0b2af8b1d8d", |
| 92 | +"metadata": { |
| 93 | +"collapsed": false |
| 94 | +}, |
| 95 | +"outputs": [], |
158 | 96 | "source": [
|
159 | 97 | "chat: Chat = taskingai.assistant.create_chat(\n",
|
160 | 98 | " assistant_id=assistant.assistant_id,\n",
|
161 | 99 | ")\n",
|
162 | 100 | "print(f\"created chat: {chat.chat_id}\\n\")"
|
163 |
| -], |
| 101 | +] |
| 102 | +}, |
| 103 | +{ |
| 104 | +"cell_type": "code", |
| 105 | +"execution_count": null, |
| 106 | +"id": "b26e30b79b71697a", |
164 | 107 | "metadata": {
|
165 | 108 | "collapsed": false
|
166 | 109 | },
|
167 |
| -"id": "f1e2f0b2af8b1d8d", |
168 | 110 | "outputs": [],
|
169 |
| -"execution_count": null |
170 |
| -}, |
171 |
| -{ |
172 |
| -"cell_type": "code", |
173 | 111 | "source": [
|
174 | 112 | "from taskingai.assistant import Message, MessageChunk\n",
|
175 | 113 | "user_input = input(\"User Input: \")\n",
|
|
181 | 119 | " text=user_input,\n",
|
182 | 120 | " )\n",
|
183 | 121 | " print(f\"User: {user_input}\")\n",
|
184 |
| -" \n", |
| 122 | +"\n", |
185 | 123 | " # generate assistant response\n",
|
186 | 124 | " assistant_message: Message = taskingai.assistant.generate_message(\n",
|
187 | 125 | " assistant_id=assistant.assistant_id,\n",
|
|
194 | 132 | " time.sleep(2)\n",
|
195 | 133 | " # quit by input 'q\n",
|
196 | 134 | " user_input = input(\"User: \")"
|
197 |
| -], |
| 135 | +] |
| 136 | +}, |
| 137 | +{ |
| 138 | +"cell_type": "code", |
| 139 | +"execution_count": null, |
| 140 | +"id": "c7d73e0b138e3eba", |
198 | 141 | "metadata": {
|
199 | 142 | "collapsed": false
|
200 | 143 | },
|
201 |
| -"id": "b26e30b79b71697a", |
202 | 144 | "outputs": [],
|
203 |
| -"execution_count": null |
204 |
| -}, |
205 |
| -{ |
206 |
| -"cell_type": "code", |
207 | 145 | "source": [
|
208 | 146 | "user_input = input(\"User Input: \")\n",
|
209 | 147 | "while user_input.strip() and user_input != \"q\":\n",
|
|
214 | 152 | " text=user_input,\n",
|
215 | 153 | " )\n",
|
216 | 154 | " print(f\"User: {user_input} ({user_message.message_id})\")\n",
|
217 |
| -" \n", |
| 155 | +"\n", |
218 | 156 | " # generate assistant response\n",
|
219 | 157 | " assistant_message_response = taskingai.assistant.generate_message(\n",
|
220 | 158 | " assistant_id=assistant.assistant_id,\n",
|
|
224 | 162 | " },\n",
|
225 | 163 | " stream=True,\n",
|
226 | 164 | " )\n",
|
227 |
| -" \n", |
228 |
| -" print(f\"Assistant:\", end=\" \", flush=True)\n", |
| 165 | +"\n", |
| 166 | +" print(\"Assistant:\", end=\" \", flush=True)\n", |
229 | 167 | " for item in assistant_message_response:\n",
|
230 | 168 | " if isinstance(item, MessageChunk):\n",
|
231 | 169 | " print(item.delta, end=\"\", flush=True)\n",
|
232 | 170 | " elif isinstance(item, Message):\n",
|
233 | 171 | " print(f\" ({item.message_id})\")\n",
|
234 |
| -" \n", |
| 172 | +"\n", |
235 | 173 | " time.sleep(2)\n",
|
236 | 174 | " # quit by input 'q\n",
|
237 | 175 | " user_input = input(\"User: \")"
|
238 |
| -], |
239 |
| -"metadata": { |
240 |
| -"collapsed": false |
241 |
| -}, |
242 |
| -"id": "c7d73e0b138e3eba", |
| 176 | +] |
| 177 | +}, |
| 178 | +{ |
| 179 | +"cell_type": "code", |
| 180 | +"execution_count": null, |
| 181 | +"id": "3a67261c", |
| 182 | +"metadata": {}, |
243 | 183 | "outputs": [],
|
244 |
| -"execution_count": null |
| 184 | +"source": [ |
| 185 | +"# clean chat context\n", |
| 186 | +"taskingai.assistant.clean_chat_context(\n", |
| 187 | +" assistant_id=assistant.assistant_id,\n", |
| 188 | +" chat_id=chat.chat_id,\n", |
| 189 | +")" |
| 190 | +] |
245 | 191 | },
|
246 | 192 | {
|
247 | 193 | "cell_type": "code",
|
| 194 | +"execution_count": null, |
| 195 | +"outputs": [], |
248 | 196 | "source": [
|
249 | 197 | "# list messages\n",
|
250 | 198 | "messages = taskingai.assistant.list_messages(\n",
|
|
258 | 206 | "metadata": {
|
259 | 207 | "collapsed": false
|
260 | 208 | },
|
261 |
| -"id": "e94e3adb0d15373b", |
262 |
| -"outputs": [], |
263 |
| -"execution_count": null |
| 209 | +"id": "e94e3adb0d15373b" |
264 | 210 | },
|
265 | 211 | {
|
266 | 212 | "cell_type": "code",
|
| 213 | +"execution_count": null, |
| 214 | +"outputs": [], |
267 | 215 | "source": [
|
268 | 216 | "# delete assistant\n",
|
269 | 217 | "taskingai.assistant.delete_assistant(\n",
|
|
273 | 221 | "metadata": {
|
274 | 222 | "collapsed": false
|
275 | 223 | },
|
276 |
| -"id": "ed39836bbfdc7a4e", |
277 |
| -"outputs": [], |
278 |
| -"execution_count": null |
| 224 | +"id": "ed39836bbfdc7a4e" |
279 | 225 | }
|
280 | 226 | ],
|
281 | 227 | "metadata": {
|
|
287 | 233 | "language_info": {
|
288 | 234 | "codemirror_mode": {
|
289 | 235 | "name": "ipython",
|
290 |
| -"version": 2 |
| 236 | +"version": 3 |
291 | 237 | },
|
292 | 238 | "file_extension": ".py",
|
293 | 239 | "mimetype": "text/x-python",
|
294 | 240 | "name": "python",
|
295 | 241 | "nbconvert_exporter": "python",
|
296 |
| -"pygments_lexer": "ipython2", |
297 |
| -"version": "2.7.6" |
| 242 | +"pygments_lexer": "ipython3", |
| 243 | +"version": "3.10.14" |
298 | 244 | }
|
299 | 245 | },
|
300 | 246 | "nbformat": 4,
|
|
0 commit comments