Skip to content

Commit 986038c

Browse files
committed
feat: adapt openai tool_calls
1 parent b884e5f commit 986038c

File tree

10 files changed

+143
-202
lines changed

10 files changed

+143
-202
lines changed

examples/assistant/chat_with_assistant.ipynb

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@
175175
"user_input = input(\"User Input: \")\n",
176176
"while user_input != \"q\":\n",
177177
" # create user message\n",
178-
" taskingai.assistant.create_user_message(\n",
178+
" taskingai.assistant.create_message(\n",
179179
" assistant_id=assistant.assistant_id,\n",
180180
" chat_id=chat.chat_id,\n",
181181
" text=user_input,\n",
182182
" )\n",
183183
" print(f\"User: {user_input}\")\n",
184184
" \n",
185185
" # generate assistant response\n",
186-
" assistant_message: Message = taskingai.assistant.generate_assistant_message(\n",
186+
" assistant_message: Message = taskingai.assistant.generate_message(\n",
187187
" assistant_id=assistant.assistant_id,\n",
188188
" chat_id=chat.chat_id,\n",
189189
" system_prompt_variables={\n",
@@ -205,42 +205,33 @@
205205
"execution_count": null,
206206
"outputs": [],
207207
"source": [
208-
"from taskingai.assistant import MessageGenerationLog\n",
209-
"\n",
210208
"user_input = input(\"User Input: \")\n",
211209
"while user_input != \"q\":\n",
212210
" # create user message\n",
213-
" taskingai.assistant.create_user_message(\n",
211+
" taskingai.assistant.create_message(\n",
214212
" assistant_id=assistant.assistant_id,\n",
215213
" chat_id=chat.chat_id,\n",
216214
" text=user_input,\n",
217215
" )\n",
218216
" print(f\"User: {user_input}\")\n",
219217
" \n",
220218
" # generate assistant response\n",
221-
" assistant_message_response = taskingai.assistant.generate_assistant_message(\n",
219+
" assistant_message_response = taskingai.assistant.generate_message(\n",
222220
" assistant_id=assistant.assistant_id,\n",
223221
" chat_id=chat.chat_id,\n",
224222
" system_prompt_variables={\n",
225223
" \"language\": \"English\"\n",
226224
" },\n",
227225
" stream=True,\n",
228-
" debug=True,\n",
229226
" )\n",
230227
" \n",
231228
" print(f\"Assistant:\", end=\" \", flush=True)\n",
232229
" for item in assistant_message_response:\n",
233-
" \n",
234230
" if isinstance(item, MessageChunk):\n",
235231
" print(item.delta, end=\"\", flush=True)\n",
236-
" \n",
237-
" elif isinstance(item, MessageGenerationLog):\n",
238-
" print(f\"\\nlog: {item}\")\n",
239-
" \n",
240232
" elif isinstance(item, Message):\n",
241233
" print(f\"\\nmessage_id: {item.message_id}\")\n",
242234
" \n",
243-
" \n",
244235
" time.sleep(2)\n",
245236
" # quit by input 'q\n",
246237
" user_input = input(\"User: \")"
@@ -270,7 +261,7 @@
270261
},
271262
{
272263
"cell_type": "code",
273-
"execution_count": 15,
264+
"execution_count": null,
274265
"outputs": [],
275266
"source": [
276267
"# delete assistant\n",

examples/inference/chat_completion.ipynb

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
"def plus_a_and_b(a, b):\n",
198198
" return a + b\n",
199199
"\n",
200-
"arguments = function_call_assistant_message.function_call.arguments\n",
200+
"arguments = function_call_assistant_message.function_calls[0].arguments\n",
201201
"function_call_result = plus_a_and_b(**arguments)\n",
202202
"print(f\"function_call_result = {function_call_result}\")"
203203
],
@@ -217,7 +217,7 @@
217217
" messages=[\n",
218218
" UserMessage(\"What is the result of 112 plus 22?\"),\n",
219219
" function_call_assistant_message,\n",
220-
" FunctionMessage(name=\"plus_a_and_b\", content=str(function_call_result))\n",
220+
" FunctionMessage(id=\"FUNCTION_ID\", content=str(function_call_result))\n",
221221
" ],\n",
222222
" functions=[function]\n",
223223
")\n",
@@ -240,26 +240,8 @@
240240
},
241241
{
242242
"cell_type": "code",
243-
"execution_count": 31,
244-
"outputs": [
245-
{
246-
"name": "stdout",
247-
"output_type": "stream",
248-
"text": [
249-
"1 2 3 4 5 6 7 8 9 10\n",
250-
"11 12 13 14 15 16 17 18 19 20\n",
251-
"21 22 23 24 25 26 27 28 29 30\n",
252-
"31 32 33 34 35 36 37 38 39 40\n",
253-
"41 42 43 44 45 46 47 48 49 50\n",
254-
"51 52 53 54 55 56 57 58 59 60\n",
255-
"61 62 63 64 65 66 67 68 69 70\n",
256-
"71 72 73 74 75 76 77 78 79 80\n",
257-
"81 82 83 84 85 86 87 88 89 90\n",
258-
"91 92 93 94 95 96 97 98 99 100\n",
259-
"Finished with reason: stop\n"
260-
]
261-
}
262-
],
243+
"execution_count": null,
244+
"outputs": [],
263245
"source": [
264246
"# generate in stream mode\n",
265247
"chat_completion_result = taskingai.inference.chat_completion(\n",
@@ -278,11 +260,7 @@
278260
" print(f\"\\n\\nFinished with reason: {item.finish_reason}\")"
279261
],
280262
"metadata": {
281-
"collapsed": false,
282-
"ExecuteTime": {
283-
"end_time": "2023-12-12T06:29:54.749165Z",
284-
"start_time": "2023-12-12T06:29:47.420237Z"
285-
}
263+
"collapsed": false
286264
},
287265
"id": "4f3290f87635152a"
288266
},

taskingai/assistant/message.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
"MessageGenerationLog",
2626
"get_message",
2727
"list_messages",
28-
"create_user_message",
28+
"create_message",
2929
"update_message",
30-
"generate_assistant_message",
30+
"generate_message",
3131
"a_get_message",
3232
"a_list_messages",
33-
"a_create_user_message",
33+
"a_create_message",
3434
"a_update_message",
35-
"a_generate_assistant_message",
35+
"a_generate_message",
3636
]
3737

3838

@@ -163,7 +163,7 @@ async def a_get_message(
163163

164164

165165

166-
def create_user_message(
166+
def create_message(
167167
assistant_id: str,
168168
chat_id: str,
169169
text: str,
@@ -194,7 +194,7 @@ def create_user_message(
194194
return message
195195

196196

197-
async def a_create_user_message(
197+
async def a_create_message(
198198
assistant_id: str,
199199
chat_id: str,
200200
text: str,
@@ -284,12 +284,11 @@ async def a_update_message(
284284

285285

286286

287-
def generate_assistant_message(
287+
def generate_message(
288288
assistant_id: str,
289289
chat_id: str,
290290
system_prompt_variables: Optional[Dict] = None,
291291
stream: bool = False,
292-
debug: bool = False,
293292
) -> Union[Message, Stream]:
294293
"""
295294
Generate a message.
@@ -305,11 +304,10 @@ def generate_assistant_message(
305304
body = MessageGenerateRequest(
306305
system_prompt_variables=system_prompt_variables,
307306
stream=stream,
308-
debug=debug,
309307
)
310308

311-
if not stream and not debug:
312-
response = api_instance.generate_assistant_message(
309+
if not stream:
310+
response = api_instance.generate_message(
313311
assistant_id=assistant_id,
314312
chat_id=chat_id,
315313
body=body,
@@ -318,7 +316,7 @@ def generate_assistant_message(
318316
message: Message = Message(**response["data"])
319317
return message
320318
else:
321-
response: Stream = api_instance.generate_assistant_message(
319+
response: Stream = api_instance.generate_message(
322320
assistant_id=assistant_id,
323321
chat_id=chat_id,
324322
body=body,
@@ -327,12 +325,11 @@ def generate_assistant_message(
327325
)
328326
return response
329327

330-
async def a_generate_assistant_message(
328+
async def a_generate_message(
331329
assistant_id: str,
332330
chat_id: str,
333331
system_prompt_variables: Optional[Dict] = None,
334332
stream: bool = False,
335-
debug: bool = False,
336333
) -> Union[Message, AsyncStream]:
337334
"""
338335
Generate a message in async mode.
@@ -348,11 +345,10 @@ async def a_generate_assistant_message(
348345
body = MessageGenerateRequest(
349346
system_prompt_variables=system_prompt_variables,
350347
stream=stream,
351-
debug=debug,
352348
)
353349

354-
if not stream and not debug:
355-
response = await api_instance.generate_assistant_message(
350+
if not stream:
351+
response = await api_instance.generate_message(
356352
assistant_id=assistant_id,
357353
chat_id=chat_id,
358354
body=body,
@@ -361,7 +357,7 @@ async def a_generate_assistant_message(
361357
message: Message = Message(**response["data"])
362358
return message
363359
else:
364-
response: AsyncStream = await api_instance.generate_assistant_message(
360+
response: AsyncStream = await api_instance.generate_message(
365361
assistant_id=assistant_id,
366362
chat_id=chat_id,
367363
body=body,

taskingai/client/api/assistant_api.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def delete_chat_with_http_info(self, assistant_id, chat_id, **kwargs): # noqa:
539539
_request_timeout=params.get('_request_timeout'),
540540
collection_formats=collection_formats)
541541

542-
def generate_assistant_message(self, body, stream, assistant_id, chat_id, **kwargs): # noqa: E501
542+
def generate_message(self, body, stream, assistant_id, chat_id, **kwargs): # noqa: E501
543543
"""Generate assistant message # noqa: E501
544544
545545
Generate a new message with the role of 'assistant'.
@@ -551,15 +551,15 @@ def generate_assistant_message(self, body, stream, assistant_id, chat_id, **kwar
551551
returns the request thread.
552552
"""
553553
kwargs['_return_http_data_only'] = True
554-
return self.generate_assistant_message_with_http_info(body, stream, assistant_id, chat_id, **kwargs) # noqa: E501
554+
return self.generate_message_with_http_info(body, stream, assistant_id, chat_id, **kwargs) # noqa: E501
555555

556-
def generate_assistant_message_with_http_info(self, body, stream, assistant_id, chat_id, **kwargs): # noqa: E501
556+
def generate_message_with_http_info(self, body, stream, assistant_id, chat_id, **kwargs): # noqa: E501
557557
"""Generate assistant message # noqa: E501
558558
559559
Generate a new message with the role of 'assistant'. # noqa: E501
560560
This method makes a synchronous HTTP request by default. To make an
561561
asynchronous HTTP request, please pass async_req=True
562-
>>> thread = api.generate_assistant_message_with_http_info(body, assistant_id, chat_id, async_req=True)
562+
>>> thread = api.generate_message_with_http_info(body, assistant_id, chat_id, async_req=True)
563563
>>> result = thread.get()
564564
565565
:param async_req bool
@@ -582,25 +582,25 @@ def generate_assistant_message_with_http_info(self, body, stream, assistant_id,
582582
if key not in all_params:
583583
raise TypeError(
584584
"Got an unexpected keyword argument '%s'"
585-
" to method generate_assistant_message" % key
585+
" to method generate_message" % key
586586
)
587587
params[key] = val
588588
del params['kwargs']
589589
# verify the required parameter 'body' is set
590590
if ('body' not in params or
591591
params['body'] is None):
592592
raise ValueError(
593-
"Missing the required parameter `body` when calling `generate_assistant_message`") # noqa: E501
593+
"Missing the required parameter `body` when calling `generate_message`") # noqa: E501
594594
# verify the required parameter 'assistant_id' is set
595595
if ('assistant_id' not in params or
596596
params['assistant_id'] is None):
597597
raise ValueError(
598-
"Missing the required parameter `assistant_id` when calling `generate_assistant_message`") # noqa: E501
598+
"Missing the required parameter `assistant_id` when calling `generate_message`") # noqa: E501
599599
# verify the required parameter 'chat_id' is set
600600
if ('chat_id' not in params or
601601
params['chat_id'] is None):
602602
raise ValueError(
603-
"Missing the required parameter `chat_id` when calling `generate_assistant_message`") # noqa: E501
603+
"Missing the required parameter `chat_id` when calling `generate_message`") # noqa: E501
604604

605605
collection_formats = {}
606606

taskingai/client/api/assistant_async_api.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ async def delete_chat_with_http_info(self, assistant_id, chat_id, **kwargs): #
540540
collection_formats=collection_formats)
541541

542542

543-
async def generate_assistant_message(self, body, stream, assistant_id, chat_id, **kwargs): # noqa: E501
543+
async def generate_message(self, body, stream, assistant_id, chat_id, **kwargs): # noqa: E501
544544
"""Generate assistant message # noqa: E501
545545
546546
Generate a new message with the role of 'assistant'.
@@ -552,16 +552,16 @@ async def generate_assistant_message(self, body, stream, assistant_id, chat_id,
552552
returns the request thread.
553553
"""
554554
kwargs['_return_http_data_only'] = True
555-
return await self.generate_assistant_message_with_http_info(body, stream, assistant_id, chat_id, **kwargs) # noqa: E501
555+
return await self.generate_message_with_http_info(body, stream, assistant_id, chat_id, **kwargs) # noqa: E501
556556

557557

558-
async def generate_assistant_message_with_http_info(self, body, stream, assistant_id, chat_id, **kwargs): # noqa: E501
558+
async def generate_message_with_http_info(self, body, stream, assistant_id, chat_id, **kwargs): # noqa: E501
559559
"""Generate assistant message # noqa: E501
560560
561561
Generate a new message with the role of 'assistant'. # noqa: E501
562562
This method makes a synchronous HTTP request by default. To make an
563563
asynchronous HTTP request, please pass async_req=True
564-
>>> thread = api.generate_assistant_message_with_http_info(body, assistant_id, chat_id, async_req=True)
564+
>>> thread = api.generate_message_with_http_info(body, assistant_id, chat_id, async_req=True)
565565
>>> result = thread.get()
566566
567567
:param async_req bool
@@ -584,25 +584,25 @@ async def generate_assistant_message_with_http_info(self, body, stream, assistan
584584
if key not in all_params:
585585
raise TypeError(
586586
"Got an unexpected keyword argument '%s'"
587-
" to method generate_assistant_message" % key
587+
" to method generate_message" % key
588588
)
589589
params[key] = val
590590
del params['kwargs']
591591
# verify the required parameter 'body' is set
592592
if ('body' not in params or
593593
params['body'] is None):
594594
raise ValueError(
595-
"Missing the required parameter `body` when calling `generate_assistant_message`") # noqa: E501
595+
"Missing the required parameter `body` when calling `generate_message`") # noqa: E501
596596
# verify the required parameter 'assistant_id' is set
597597
if ('assistant_id' not in params or
598598
params['assistant_id'] is None):
599599
raise ValueError(
600-
"Missing the required parameter `assistant_id` when calling `generate_assistant_message`") # noqa: E501
600+
"Missing the required parameter `assistant_id` when calling `generate_message`") # noqa: E501
601601
# verify the required parameter 'chat_id' is set
602602
if ('chat_id' not in params or
603603
params['chat_id'] is None):
604604
raise ValueError(
605-
"Missing the required parameter `chat_id` when calling `generate_assistant_message`") # noqa: E501
605+
"Missing the required parameter `chat_id` when calling `generate_message`") # noqa: E501
606606

607607
collection_formats = {}
608608

0 commit comments

Comments
 (0)