Skip to content

Commit aa2c7f1

Browse files
jameszyaoSimsonW
authored andcommitted
feat: add assistant chat
1 parent e3902eb commit aa2c7f1

File tree

5 files changed

+337
-5
lines changed

5 files changed

+337
-5
lines changed

examples/assistant/conversation.ipynb

Lines changed: 329 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,343 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": null,
5+
"execution_count": 11,
66
"id": "initial_id",
77
"metadata": {
8-
"collapsed": true
8+
"collapsed": true,
9+
"ExecuteTime": {
10+
"end_time": "2023-11-28T08:47:28.230279Z",
11+
"start_time": "2023-11-28T08:47:28.205410Z"
12+
}
913
},
1014
"outputs": [],
1115
"source": [
16+
"import time\n",
1217
"import taskingai\n",
1318
"# Load TaskingAI API Key fron environment variable"
1419
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"source": [
24+
"# Chat with Assistant\n",
25+
"\n",
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",
27+
"Then we will start a chat with the assistant."
28+
],
29+
"metadata": {
30+
"collapsed": false
31+
},
32+
"id": "4ca20b4a868dedd8"
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"source": [
37+
"## Create Assistant"
38+
],
39+
"metadata": {
40+
"collapsed": false
41+
},
42+
"id": "5e19ac923d84e898"
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 2,
47+
"outputs": [
48+
{
49+
"name": "stdout",
50+
"output_type": "stream",
51+
"text": [
52+
"created action: {'action_id': 'bFBd2uQJa2wp8ac6kEDLE4yt',\n",
53+
" 'authentication': {'content': None, 'secret': None, 'type': 'none'},\n",
54+
" 'created_timestamp': 1701160841935,\n",
55+
" 'object': 'Action',\n",
56+
" 'schema': {'components': {},\n",
57+
" 'info': {'description': 'API for fetching interesting number facts',\n",
58+
" 'title': 'Numbers API',\n",
59+
" 'version': '1.0.0'},\n",
60+
" 'openapi': '3.0.0',\n",
61+
" 'paths': {'/{number}': {'get': {'parameters': [{'description': 'The '\n",
62+
" 'number '\n",
63+
" 'to '\n",
64+
" 'get '\n",
65+
" 'the '\n",
66+
" 'fact '\n",
67+
" 'for',\n",
68+
" 'in': 'path',\n",
69+
" 'name': 'number',\n",
70+
" 'required': True,\n",
71+
" 'schema': {'type': 'integer'}}],\n",
72+
" 'responses': {'200': {'content': {'text/plain': {'schema': {'type': 'string'}}},\n",
73+
" 'description': 'A '\n",
74+
" 'fact '\n",
75+
" 'about '\n",
76+
" 'the '\n",
77+
" 'number'}},\n",
78+
" 'summary': 'Get fact about a '\n",
79+
" 'number'}}},\n",
80+
" 'security': [],\n",
81+
" 'servers': [{'url': 'http://numbersapi.com'}]}}\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"from taskingai.tool import Action, ActionAuthentication, ActionAuthenticationType\n",
87+
"from typing import List\n",
88+
"\n",
89+
"# create an assistant action\n",
90+
"NUMBERS_API_SCHEMA = {\n",
91+
" \"openapi\": \"3.0.0\",\n",
92+
" \"info\": {\n",
93+
" \"title\": \"Numbers API\",\n",
94+
" \"version\": \"1.0.0\",\n",
95+
" \"description\": \"API for fetching interesting number facts\"\n",
96+
" },\n",
97+
" \"servers\": [\n",
98+
" {\n",
99+
" \"url\": \"http://numbersapi.com\"\n",
100+
" }\n",
101+
" ],\n",
102+
" \"paths\": {\n",
103+
" \"/{number}\": {\n",
104+
" \"get\": {\n",
105+
" \"summary\": \"Get fact about a number\",\n",
106+
" \"parameters\": [\n",
107+
" {\n",
108+
" \"name\": \"number\",\n",
109+
" \"in\": \"path\",\n",
110+
" \"required\": True,\n",
111+
" \"description\": \"The number to get the fact for\",\n",
112+
" \"schema\": {\n",
113+
" \"type\": \"integer\"\n",
114+
" }\n",
115+
" }\n",
116+
" ],\n",
117+
" \"responses\": {\n",
118+
" \"200\": {\n",
119+
" \"description\": \"A fact about the number\",\n",
120+
" \"content\": {\n",
121+
" \"text/plain\": {\n",
122+
" \"schema\": {\n",
123+
" \"type\": \"string\"\n",
124+
" }\n",
125+
" }\n",
126+
" }\n",
127+
" }\n",
128+
" }\n",
129+
" }\n",
130+
" }\n",
131+
" }\n",
132+
"}\n",
133+
"actions: List[Action] = taskingai.tool.bulk_create_actions(\n",
134+
" schema=NUMBERS_API_SCHEMA,\n",
135+
" authentication=ActionAuthentication(\n",
136+
" type=ActionAuthenticationType.NONE,\n",
137+
" )\n",
138+
")\n",
139+
"action = actions[0]\n",
140+
"print(f\"created action: {action}\\n\")"
141+
],
142+
"metadata": {
143+
"collapsed": false,
144+
"ExecuteTime": {
145+
"end_time": "2023-11-28T08:40:42.169559Z",
146+
"start_time": "2023-11-28T08:40:40.544864Z"
147+
}
148+
},
149+
"id": "3b2fda39ba58c5e9"
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 3,
154+
"outputs": [
155+
{
156+
"name": "stdout",
157+
"output_type": "stream",
158+
"text": [
159+
"created assistant: {'assistant_id': 'X5lMxXkQZahUAzsdIUCEidCH',\n",
160+
" 'created_timestamp': 1701160845645,\n",
161+
" 'description': 'A assistant who knows the meaning of various numbers.',\n",
162+
" 'metadata': {'foo': 'bar'},\n",
163+
" 'model_id': 'Gk1145Bl',\n",
164+
" 'name': 'My Assistant',\n",
165+
" 'object': 'Assistant',\n",
166+
" 'retrievals': [],\n",
167+
" 'system_prompt_template': ['You know the meaning of various numbers.',\n",
168+
" \"No matter what the user's language is, you will \"\n",
169+
" 'use the {{langugae}} to explain.'],\n",
170+
" 'tools': [{'id': 'bFBd2uQJa2wp8ac6kEDLE4yt', 'type': 'action'}]}\n"
171+
]
172+
}
173+
],
174+
"source": [
175+
"from taskingai.assistant import Assistant, Chat, AssistantTool, AssistantToolType\n",
176+
"\n",
177+
"# choose an available chat_completion model from your project\n",
178+
"model_id = \"Gk1145Bl\"\n",
179+
"\n",
180+
"assistant: Assistant = taskingai.assistant.create_assistant(\n",
181+
" model_id=model_id,\n",
182+
" name=\"My Assistant\",\n",
183+
" description=\"A assistant who knows the meaning of various numbers.\",\n",
184+
" system_prompt_template=[\n",
185+
" \"You know the meaning of various numbers.\",\n",
186+
" \"No matter what the user's language is, you will use the {{langugae}} to explain.\"\n",
187+
" ],\n",
188+
" tools=[AssistantTool(\n",
189+
" type=AssistantToolType.action,\n",
190+
" id=action.action_id,\n",
191+
" )],\n",
192+
" retrievals=[],\n",
193+
" metadata={\"foo\": \"bar\"},\n",
194+
")\n",
195+
"print(f\"created assistant: {assistant}\\n\")"
196+
],
197+
"metadata": {
198+
"collapsed": false,
199+
"ExecuteTime": {
200+
"end_time": "2023-11-28T08:40:45.894810Z",
201+
"start_time": "2023-11-28T08:40:43.092053Z"
202+
}
203+
},
204+
"id": "3b3df0f232021283"
205+
},
206+
{
207+
"cell_type": "markdown",
208+
"source": [
209+
"## Start a Chat "
210+
],
211+
"metadata": {
212+
"collapsed": false
213+
},
214+
"id": "8e7c1b9461f0a344"
215+
},
216+
{
217+
"cell_type": "code",
218+
"execution_count": 14,
219+
"outputs": [
220+
{
221+
"name": "stdout",
222+
"output_type": "stream",
223+
"text": [
224+
"created chat: SdELbhZeozXgPdr4fXSE7pjG\n"
225+
]
226+
}
227+
],
228+
"source": [
229+
"chat: Chat = taskingai.assistant.create_chat(\n",
230+
" assistant_id=assistant.assistant_id,\n",
231+
")\n",
232+
"print(f\"created chat: {chat.chat_id}\\n\")"
233+
],
234+
"metadata": {
235+
"collapsed": false,
236+
"ExecuteTime": {
237+
"end_time": "2023-11-28T08:49:13.796245Z",
238+
"start_time": "2023-11-28T08:49:11.579401Z"
239+
}
240+
},
241+
"id": "f1e2f0b2af8b1d8d"
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 15,
246+
"outputs": [
247+
{
248+
"name": "stdout",
249+
"output_type": "stream",
250+
"text": [
251+
"User: Hello\n",
252+
"Assistant: Hi there! How can I assist you today?\n",
253+
"User: What is the meaning of 120\n",
254+
"Assistant: The number 120 represents the drop in meters of the world's tallest freefall ride, The Giant Drop, located in Australia.\n",
255+
"User: One more meaning\n",
256+
"Assistant: Another meaning of the number 120 is that it is a file medium format developed by Kodak.\n",
257+
"User: thanks\n",
258+
"Assistant: You're welcome! If you have any more questions, feel free to ask.\n"
259+
]
260+
}
261+
],
262+
"source": [
263+
"from taskingai.assistant import Message\n",
264+
"\n",
265+
"user_input = input(\"User Input: \")\n",
266+
"while user_input != \"q\":\n",
267+
" # create user message\n",
268+
" taskingai.assistant.create_user_message(\n",
269+
" assistant_id=assistant.assistant_id,\n",
270+
" chat_id=chat.chat_id,\n",
271+
" text=user_input,\n",
272+
" )\n",
273+
" print(f\"User: {user_input}\")\n",
274+
" \n",
275+
" # generate assistant response\n",
276+
" assistant_message: Message = taskingai.assistant.generate_assistant_message(\n",
277+
" assistant_id=assistant.assistant_id,\n",
278+
" chat_id=chat.chat_id,\n",
279+
" system_prompt_variables={\n",
280+
" \"language\": \"Spanish\"\n",
281+
" }\n",
282+
" )\n",
283+
" print(f\"Assistant: {assistant_message.content['text']}\")\n",
284+
" time.sleep(2)\n",
285+
" user_input = input(\"User: \")"
286+
],
287+
"metadata": {
288+
"collapsed": false,
289+
"ExecuteTime": {
290+
"end_time": "2023-11-28T08:50:07.890084Z",
291+
"start_time": "2023-11-28T08:49:14.898284Z"
292+
}
293+
},
294+
"id": "b26e30b79b71697a"
295+
},
296+
{
297+
"cell_type": "code",
298+
"execution_count": 18,
299+
"outputs": [
300+
{
301+
"name": "stdout",
302+
"output_type": "stream",
303+
"text": [
304+
"assistant: You're welcome! If you have any more questions, feel free to ask.\n",
305+
"user: thanks\n",
306+
"assistant: Another meaning of the number 120 is that it is a file medium format developed by Kodak.\n",
307+
"user: One more meaning\n",
308+
"assistant: The number 120 represents the drop in meters of the world's tallest freefall ride, The Giant Drop, located in Australia.\n",
309+
"user: What is the meaning of 120\n",
310+
"assistant: Hi there! How can I assist you today?\n",
311+
"user: Hello\n"
312+
]
313+
}
314+
],
315+
"source": [
316+
"# list messages\n",
317+
"messages = taskingai.assistant.list_messages(\n",
318+
" assistant_id=assistant.assistant_id,\n",
319+
" chat_id=chat.chat_id,\n",
320+
")\n",
321+
"for message in messages:\n",
322+
" print(f\"{message.role}: {message.content['text']}\")"
323+
],
324+
"metadata": {
325+
"collapsed": false,
326+
"ExecuteTime": {
327+
"end_time": "2023-11-28T08:50:44.024014Z",
328+
"start_time": "2023-11-28T08:50:41.483107Z"
329+
}
330+
},
331+
"id": "34bae46ba56752bb"
332+
},
333+
{
334+
"cell_type": "code",
335+
"execution_count": null,
336+
"outputs": [],
337+
"source": [],
338+
"metadata": {
339+
"collapsed": false
340+
},
341+
"id": "ed39836bbfdc7a4e"
15342
}
16343
],
17344
"metadata": {

examples/inference/text_embedding.py

Whitespace-only changes.

taskingai/assistant/assistant.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
from typing import Optional, List, Dict
22

33
from taskingai.client.utils import get_assistant_api_instance
4+
from taskingai.client.constants import AssistantToolType, AssistantRetrievalType
45
from taskingai.client.models import Assistant, AssistantRetrieval, AssistantTool
56
from taskingai.client.models import AssistantCreateRequest, AssistantCreateResponse,\
67
AssistantUpdateRequest, AssistantUpdateResponse,\
78
AssistantGetResponse, AssistantListResponse
89

910
__all__ = [
1011
"Assistant",
12+
"AssistantTool",
13+
"AssistantRetrieval",
14+
"AssistantToolType",
15+
"AssistantRetrievalType",
1116
"get_assistant",
1217
"list_assistants",
1318
"create_assistant",

taskingai/assistant/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def generate_assistant_message(
153153

154154
api_instance = get_assistant_api_instance()
155155
body = MessageGenerateRequest(
156-
options=[MessageGenerationResponseOption(stream=False, debug=False)],
156+
options=MessageGenerationResponseOption(stream=False, debug=False),
157157
system_prompt_variables=system_prompt_variables
158158
)
159159
response = api_instance.generate_assistant_message(

taskingai/client/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
CLIENT_VERSION_HEADER = 'X-TaskingAI-Client-Version'
88

99

10-
class ToolType(str, Enum):
10+
class AssistantToolType(str, Enum):
1111
action = "action"
1212
function = "function"
1313

1414

15-
class RetrievalType(str, Enum):
15+
class AssistantRetrievalType(str, Enum):
1616
collection = "collection"
1717

1818

0 commit comments

Comments
 (0)