Skip to content

Commit e2b7e3a

Browse files
jameszyaoSimsonW
authored andcommitted
feat: auto cast stream dict to model
1 parent bc9b055 commit e2b7e3a

File tree

5 files changed

+254
-124
lines changed

5 files changed

+254
-124
lines changed

examples/assistant/chat_with_assistant.ipynb

Lines changed: 153 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 1,
66
"id": "initial_id",
77
"metadata": {
88
"collapsed": true,
9-
"is_executing": true
9+
"ExecuteTime": {
10+
"end_time": "2023-11-30T11:56:53.238031Z",
11+
"start_time": "2023-11-30T11:56:53.081526Z"
12+
}
1013
},
1114
"outputs": [],
1215
"source": [
@@ -46,36 +49,7 @@
4649
"name": "stdout",
4750
"output_type": "stream",
4851
"text": [
49-
"created action: {'action_id': 'bFBd2uQJa2wp8ac6kEDLE4yt',\n",
50-
" 'authentication': {'content': None, 'secret': None, 'type': 'none'},\n",
51-
" 'created_timestamp': 1701160841935,\n",
52-
" 'object': 'Action',\n",
53-
" 'schema': {'components': {},\n",
54-
" 'info': {'description': 'API for fetching interesting number facts',\n",
55-
" 'title': 'Numbers API',\n",
56-
" 'version': '1.0.0'},\n",
57-
" 'openapi': '3.0.0',\n",
58-
" 'paths': {'/{number}': {'get': {'parameters': [{'description': 'The '\n",
59-
" 'number '\n",
60-
" 'to '\n",
61-
" 'get '\n",
62-
" 'the '\n",
63-
" 'fact '\n",
64-
" 'for',\n",
65-
" 'in': 'path',\n",
66-
" 'name': 'number',\n",
67-
" 'required': True,\n",
68-
" 'schema': {'type': 'integer'}}],\n",
69-
" 'responses': {'200': {'content': {'text/plain': {'schema': {'type': 'string'}}},\n",
70-
" 'description': 'A '\n",
71-
" 'fact '\n",
72-
" 'about '\n",
73-
" 'the '\n",
74-
" 'number'}},\n",
75-
" 'summary': 'Get fact about a '\n",
76-
" 'number'}}},\n",
77-
" 'security': [],\n",
78-
" 'servers': [{'url': 'http://numbersapi.com'}]}}\n"
52+
"created action: object='Action' action_id='bFBd4I6YoHOiIgEnTpICAgC8' schema={'info': {'title': 'Numbers API', 'version': '1.0.0', 'description': 'API for fetching interesting number facts'}, 'paths': {'/{number}': {'get': {'summary': 'Get fact about a number', 'responses': {'200': {'content': {'text/plain': {'schema': {'type': 'string'}}}, 'description': 'A fact about the number'}}, 'parameters': [{'in': 'path', 'name': 'number', 'schema': {'type': 'integer'}, 'required': True, 'description': 'The number to get the fact for'}]}}}, 'openapi': '3.0.0', 'servers': [{'url': 'http://numbersapi.com'}], 'security': [], 'components': {}} authentication=ActionAuthentication(type=<ActionAuthenticationType.none: 'none'>, secret=None, content=None) created_timestamp=1701345415792\n"
7953
]
8054
}
8155
],
@@ -139,8 +113,8 @@
139113
"metadata": {
140114
"collapsed": false,
141115
"ExecuteTime": {
142-
"end_time": "2023-11-28T08:40:42.169559Z",
143-
"start_time": "2023-11-28T08:40:40.544864Z"
116+
"end_time": "2023-11-30T11:56:55.907860Z",
117+
"start_time": "2023-11-30T11:56:54.639041Z"
144118
}
145119
},
146120
"id": "3b2fda39ba58c5e9"
@@ -153,18 +127,7 @@
153127
"name": "stdout",
154128
"output_type": "stream",
155129
"text": [
156-
"created assistant: {'assistant_id': 'X5lMxXkQZahUAzsdIUCEidCH',\n",
157-
" 'created_timestamp': 1701160845645,\n",
158-
" 'description': 'A assistant who knows the meaning of various numbers.',\n",
159-
" 'metadata': {'foo': 'bar'},\n",
160-
" 'model_id': 'Gk1145Bl',\n",
161-
" 'name': 'My Assistant',\n",
162-
" 'object': 'Assistant',\n",
163-
" 'retrievals': [],\n",
164-
" 'system_prompt_template': ['You know the meaning of various numbers.',\n",
165-
" \"No matter what the user's language is, you will \"\n",
166-
" 'use the {{langugae}} to explain.'],\n",
167-
" 'tools': [{'id': 'bFBd2uQJa2wp8ac6kEDLE4yt', 'type': 'action'}]}\n"
130+
"created assistant: object='Assistant' assistant_id='X5lMUSFLWzESDvTmy0SwOx0v' model_id='Gk1145Bl' name='My Assistant' description='A assistant who knows the meaning of various numbers.' system_prompt_template=['You know the meaning of various numbers.', \"No matter what the user's language is, you will use the {{langugae}} to explain.\"] tools=[AssistantTool(type=<AssistantToolType.action: 'action'>, id='bFBd4I6YoHOiIgEnTpICAgC8')] retrievals=[] metadata={'foo': 'bar'} created_timestamp=1701345417694\n"
168131
]
169132
}
170133
],
@@ -194,8 +157,8 @@
194157
"metadata": {
195158
"collapsed": false,
196159
"ExecuteTime": {
197-
"end_time": "2023-11-28T08:40:45.894810Z",
198-
"start_time": "2023-11-28T08:40:43.092053Z"
160+
"end_time": "2023-11-30T11:56:57.786789Z",
161+
"start_time": "2023-11-30T11:56:56.560889Z"
199162
}
200163
},
201164
"id": "3b3df0f232021283"
@@ -212,13 +175,13 @@
212175
},
213176
{
214177
"cell_type": "code",
215-
"execution_count": 14,
178+
"execution_count": 4,
216179
"outputs": [
217180
{
218181
"name": "stdout",
219182
"output_type": "stream",
220183
"text": [
221-
"created chat: SdELbhZeozXgPdr4fXSE7pjG\n"
184+
"created chat: SdELqszTR3IXt51rBj0svkSq\n"
222185
]
223186
}
224187
],
@@ -231,33 +194,43 @@
231194
"metadata": {
232195
"collapsed": false,
233196
"ExecuteTime": {
234-
"end_time": "2023-11-28T08:49:13.796245Z",
235-
"start_time": "2023-11-28T08:49:11.579401Z"
197+
"end_time": "2023-11-30T11:56:59.106482Z",
198+
"start_time": "2023-11-30T11:56:58.723727Z"
236199
}
237200
},
238201
"id": "f1e2f0b2af8b1d8d"
239202
},
240203
{
241204
"cell_type": "code",
242-
"execution_count": 15,
205+
"execution_count": 5,
243206
"outputs": [
244207
{
245208
"name": "stdout",
246209
"output_type": "stream",
247210
"text": [
248-
"User: Hello\n",
249-
"Assistant: Hi there! How can I assist you today?\n",
250-
"User: What is the meaning of 120\n",
251-
"Assistant: The number 120 represents the drop in meters of the world's tallest freefall ride, The Giant Drop, located in Australia.\n",
252-
"User: One more meaning\n",
253-
"Assistant: Another meaning of the number 120 is that it is a file medium format developed by Kodak.\n",
211+
"User: hi\n",
212+
"Assistant: Hello! How can I assist you today?\n",
213+
"User: what is the meaning of 12\n",
214+
"Assistant: The number 12 is often associated with various meanings depending on the context. Here are some interpretations:\n",
215+
"\n",
216+
"1. Numerology: In numerology, 12 is considered a powerful number associated with completion, harmony, and a sense of wholeness. It is often seen as a symbol of perfection, balance, and divine guidance.\n",
217+
"\n",
218+
"2. Time: There are 12 months in a year, and this is based on the approximate time it takes for the moon to complete its cycle around the Earth. Additionally, there are 12 hours on a clock face, both in the morning and in the evening.\n",
219+
"\n",
220+
"3. Mathematics: 12 is divisible by 1, 2, 3, 4, 6, and itself, making it a highly composite number. It also has a prime factorization of 2^2 × 3, which is significant in number theory.\n",
221+
"\n",
222+
"4. Religion and Mythology: In many religious beliefs and mythologies, there are groups or sets of 12. For instance, there are 12 disciples of Jesus in Christianity, 12 signs of the zodiac in astrology, and 12 Olympian gods in Greek mythology.\n",
223+
"\n",
224+
"5. Measurement: The duodecimal numerical system, based on the number 12, was historically used in some cultures for counting and measurements. This system is still present in modern times in certain contexts, such as inches in a foot (12 inches) and the number of members in a jury (often 12).\n",
225+
"\n",
226+
"These are just a few examples of the meaning associated with the number 12. The interpretation can vary depending on the cultural, historical, or personal perspectives.\n",
254227
"User: thanks\n",
255228
"Assistant: You're welcome! If you have any more questions, feel free to ask.\n"
256229
]
257230
}
258231
],
259232
"source": [
260-
"from taskingai.assistant import Message\n",
233+
"from taskingai.assistant import Message, MessageChunk\n",
261234
"\n",
262235
"user_input = input(\"User Input: \")\n",
263236
"while user_input != \"q\":\n",
@@ -274,7 +247,7 @@
274247
" assistant_id=assistant.assistant_id,\n",
275248
" chat_id=chat.chat_id,\n",
276249
" system_prompt_variables={\n",
277-
" \"language\": \"Spanish\"\n",
250+
" \"language\": \"English\"\n",
278251
" }\n",
279252
" )\n",
280253
" print(f\"Assistant: {assistant_message.content.text}\")\n",
@@ -284,28 +257,114 @@
284257
"metadata": {
285258
"collapsed": false,
286259
"ExecuteTime": {
287-
"end_time": "2023-11-28T08:50:07.890084Z",
288-
"start_time": "2023-11-28T08:49:14.898284Z"
260+
"end_time": "2023-11-30T11:57:37.676466Z",
261+
"start_time": "2023-11-30T11:57:00.619700Z"
289262
}
290263
},
291264
"id": "b26e30b79b71697a"
292265
},
293266
{
294267
"cell_type": "code",
295-
"execution_count": 18,
268+
"execution_count": 6,
296269
"outputs": [
297270
{
298271
"name": "stdout",
299272
"output_type": "stream",
300273
"text": [
274+
"User: what about 127\n",
275+
"Assistant: The number 127 is the atomic number of Unbiseptium, an element that has not yet been discovered. In the periodic table of elements, each unique element has an atomic number that corresponds to the number of protons in its nucleus. However, as of now, no element with 127 protons has been confirmed or named. Therefore, 127 represents a theoretical element that scientists have not yet observed or identified.\n",
276+
"message_id: Mah1zSgBZpDuLvpEnVhEmWZy\n",
277+
"User: bye\n",
278+
"Assistant: Goodbye! If you have any more questions in the future, feel free to ask. Have a great day!\n",
279+
"message_id: Mah1uIUcePTL7gvPjzl6JtpM\n"
280+
]
281+
}
282+
],
283+
"source": [
284+
"# response in stream\n",
285+
"\n",
286+
"user_input = input(\"User Input: \")\n",
287+
"while user_input != \"q\":\n",
288+
" # create user message\n",
289+
" taskingai.assistant.create_user_message(\n",
290+
" assistant_id=assistant.assistant_id,\n",
291+
" chat_id=chat.chat_id,\n",
292+
" text=user_input,\n",
293+
" )\n",
294+
" print(f\"User: {user_input}\")\n",
295+
" \n",
296+
" # generate assistant response\n",
297+
" assistant_message_response = taskingai.assistant.generate_assistant_message(\n",
298+
" assistant_id=assistant.assistant_id,\n",
299+
" chat_id=chat.chat_id,\n",
300+
" system_prompt_variables={\n",
301+
" \"language\": \"English\"\n",
302+
" },\n",
303+
" stream=True,\n",
304+
" )\n",
305+
" \n",
306+
" print(f\"Assistant:\", end=\" \", flush=True)\n",
307+
" for item in assistant_message_response:\n",
308+
" \n",
309+
" if isinstance(item, MessageChunk):\n",
310+
" print(item.delta, end=\"\", flush=True)\n",
311+
" \n",
312+
" elif isinstance(item, Message):\n",
313+
" print(f\"\\nmessage_id: {item.message_id}\")\n",
314+
" \n",
315+
" \n",
316+
" time.sleep(2)\n",
317+
" user_input = input(\"User: \")"
318+
],
319+
"metadata": {
320+
"collapsed": false,
321+
"ExecuteTime": {
322+
"end_time": "2023-11-30T11:58:24.864156Z",
323+
"start_time": "2023-11-30T11:57:58.379023Z"
324+
}
325+
},
326+
"id": "c7d73e0b138e3eba"
327+
},
328+
{
329+
"cell_type": "code",
330+
"execution_count": 8,
331+
"outputs": [
332+
{
333+
"name": "stdout",
334+
"output_type": "stream",
335+
"text": [
336+
"assistant: Goodbye! If you have any more questions in the future, feel free to ask. Have a great day!\n",
337+
"----------------------------------------------------------------------------------------------------\n",
338+
"user: bye\n",
339+
"----------------------------------------------------------------------------------------------------\n",
340+
"assistant: The number 127 is the atomic number of Unbiseptium, an element that has not yet been discovered. In the periodic table of elements, each unique element has an atomic number that corresponds to the number of protons in its nucleus. However, as of now, no element with 127 protons has been confirmed or named. Therefore, 127 represents a theoretical element that scientists have not yet observed or identified.\n",
341+
"----------------------------------------------------------------------------------------------------\n",
342+
"user: what about 127\n",
343+
"----------------------------------------------------------------------------------------------------\n",
301344
"assistant: You're welcome! If you have any more questions, feel free to ask.\n",
345+
"----------------------------------------------------------------------------------------------------\n",
302346
"user: thanks\n",
303-
"assistant: Another meaning of the number 120 is that it is a file medium format developed by Kodak.\n",
304-
"user: One more meaning\n",
305-
"assistant: The number 120 represents the drop in meters of the world's tallest freefall ride, The Giant Drop, located in Australia.\n",
306-
"user: What is the meaning of 120\n",
307-
"assistant: Hi there! How can I assist you today?\n",
308-
"user: Hello\n"
347+
"----------------------------------------------------------------------------------------------------\n",
348+
"assistant: The number 12 is often associated with various meanings depending on the context. Here are some interpretations:\n",
349+
"\n",
350+
"1. Numerology: In numerology, 12 is considered a powerful number associated with completion, harmony, and a sense of wholeness. It is often seen as a symbol of perfection, balance, and divine guidance.\n",
351+
"\n",
352+
"2. Time: There are 12 months in a year, and this is based on the approximate time it takes for the moon to complete its cycle around the Earth. Additionally, there are 12 hours on a clock face, both in the morning and in the evening.\n",
353+
"\n",
354+
"3. Mathematics: 12 is divisible by 1, 2, 3, 4, 6, and itself, making it a highly composite number. It also has a prime factorization of 2^2 × 3, which is significant in number theory.\n",
355+
"\n",
356+
"4. Religion and Mythology: In many religious beliefs and mythologies, there are groups or sets of 12. For instance, there are 12 disciples of Jesus in Christianity, 12 signs of the zodiac in astrology, and 12 Olympian gods in Greek mythology.\n",
357+
"\n",
358+
"5. Measurement: The duodecimal numerical system, based on the number 12, was historically used in some cultures for counting and measurements. This system is still present in modern times in certain contexts, such as inches in a foot (12 inches) and the number of members in a jury (often 12).\n",
359+
"\n",
360+
"These are just a few examples of the meaning associated with the number 12. The interpretation can vary depending on the cultural, historical, or personal perspectives.\n",
361+
"----------------------------------------------------------------------------------------------------\n",
362+
"user: what is the meaning of 12\n",
363+
"----------------------------------------------------------------------------------------------------\n",
364+
"assistant: Hello! How can I assist you today?\n",
365+
"----------------------------------------------------------------------------------------------------\n",
366+
"user: hi\n",
367+
"----------------------------------------------------------------------------------------------------\n"
309368
]
310369
}
311370
],
@@ -316,17 +375,37 @@
316375
" chat_id=chat.chat_id,\n",
317376
")\n",
318377
"for message in messages:\n",
319-
" print(f\"{message.role}: {message.content.text}\")"
378+
" print(f\"{message.role}: {message.content.text}\")\n",
379+
" print(\"-\"*100)"
320380
],
321381
"metadata": {
322382
"collapsed": false,
323383
"ExecuteTime": {
324-
"end_time": "2023-11-28T08:50:44.024014Z",
325-
"start_time": "2023-11-28T08:50:41.483107Z"
384+
"end_time": "2023-11-30T11:58:52.694600Z",
385+
"start_time": "2023-11-30T11:58:51.370902Z"
326386
}
327387
},
328388
"id": "34bae46ba56752bb"
329389
},
390+
{
391+
"cell_type": "code",
392+
"execution_count": 9,
393+
"outputs": [],
394+
"source": [
395+
"# delete assistant\n",
396+
"taskingai.assistant.delete_assistant(\n",
397+
" assistant_id=assistant.assistant_id,\n",
398+
")"
399+
],
400+
"metadata": {
401+
"collapsed": false,
402+
"ExecuteTime": {
403+
"end_time": "2023-11-30T11:59:08.821200Z",
404+
"start_time": "2023-11-30T11:59:07.561480Z"
405+
}
406+
},
407+
"id": "ed39836bbfdc7a4e"
408+
},
330409
{
331410
"cell_type": "code",
332411
"execution_count": null,
@@ -335,7 +414,7 @@
335414
"metadata": {
336415
"collapsed": false
337416
},
338-
"id": "ed39836bbfdc7a4e"
417+
"id": "325965f154e14f42"
339418
}
340419
],
341420
"metadata": {

0 commit comments

Comments
 (0)