|
11 | 11 | "metadata": {
|
12 | 12 | "collapsed": false,
|
13 | 13 | "ExecuteTime": {
|
14 |
| - "end_time": "2023-11-28T12:07:45.292065Z", |
15 |
| - "start_time": "2023-11-28T12:07:45.248068Z" |
| 14 | + "end_time": "2023-11-30T11:03:32.009173Z", |
| 15 | + "start_time": "2023-11-30T11:03:30.470410Z" |
16 | 16 | }
|
17 | 17 | },
|
18 | 18 | "id": "1a6bfd1682fcb23f"
|
|
40 | 40 | "metadata": {
|
41 | 41 | "collapsed": false,
|
42 | 42 | "ExecuteTime": {
|
43 |
| - "end_time": "2023-11-28T12:07:46.170766Z", |
44 |
| - "start_time": "2023-11-28T12:07:46.157613Z" |
| 43 | + "end_time": "2023-11-30T11:03:35.629715Z", |
| 44 | + "start_time": "2023-11-30T11:03:35.602754Z" |
45 | 45 | }
|
46 | 46 | },
|
47 | 47 | "id": "49abde692940b09e"
|
|
52 | 52 | "outputs": [
|
53 | 53 | {
|
54 | 54 | "data": {
|
55 |
| - "text/plain": "{'created_timestamp': 1701173269243,\n 'finish_reason': 'stop',\n 'message': {'content': 'Hello! How can I assist you today?',\n 'function_call': None,\n 'role': 'assistant'},\n 'object': 'ChatCompletion'}" |
| 55 | + "text/plain": "ChatCompletion(object='ChatCompletion', finish_reason=<ChatCompletionFinishReason.stop: 'stop'>, message=ChatCompletionAssistantMessage(content='Hello! How can I assist you today?', role=<ChatCompletionRole.assistant: 'assistant'>, function_call=None), created_timestamp=1701342218014)" |
56 | 56 | },
|
57 | 57 | "execution_count": 3,
|
58 | 58 | "metadata": {},
|
|
61 | 61 | ],
|
62 | 62 | "source": [
|
63 | 63 | "# normal \n",
|
64 |
| - "chat_completion = taskingai.inference.chat_completion(\n", |
| 64 | + "chat_completion_result = taskingai.inference.chat_completion(\n", |
65 | 65 | " model_id=model_id,\n",
|
66 | 66 | " messages=[\n",
|
67 | 67 | " SystemMessage(\"You are a professional assistant.\"),\n",
|
68 | 68 | " UserMessage(\"Hi\"),\n",
|
69 | 69 | " ]\n",
|
70 | 70 | ")\n",
|
71 |
| - "chat_completion" |
| 71 | + "chat_completion_result" |
72 | 72 | ],
|
73 | 73 | "metadata": {
|
74 | 74 | "collapsed": false,
|
75 | 75 | "ExecuteTime": {
|
76 |
| - "end_time": "2023-11-28T12:07:49.355234Z", |
77 |
| - "start_time": "2023-11-28T12:07:46.700962Z" |
| 76 | + "end_time": "2023-11-30T11:03:38.108157Z", |
| 77 | + "start_time": "2023-11-30T11:03:36.349445Z" |
78 | 78 | }
|
79 | 79 | },
|
80 | 80 | "id": "43dcc632665f0de4"
|
|
85 | 85 | "outputs": [
|
86 | 86 | {
|
87 | 87 | "data": {
|
88 |
| - "text/plain": "{'created_timestamp': 1701173272255,\n 'finish_reason': 'stop',\n 'message': {'content': \"Of course! Here's another joke for you: Why don't \"\n \"skeletons fight each other? They don't have the guts!\",\n 'function_call': None,\n 'role': 'assistant'},\n 'object': 'ChatCompletion'}" |
| 88 | + "text/plain": "ChatCompletion(object='ChatCompletion', finish_reason=<ChatCompletionFinishReason.stop: 'stop'>, message=ChatCompletionAssistantMessage(content=\"Of course! How about this one: Why don't skeletons fight each other? They don't have the guts!\", role=<ChatCompletionRole.assistant: 'assistant'>, function_call=None), created_timestamp=1701342220370)" |
89 | 89 | },
|
90 | 90 | "execution_count": 4,
|
91 | 91 | "metadata": {},
|
|
94 | 94 | ],
|
95 | 95 | "source": [
|
96 | 96 | "# multi round chat completion\n",
|
97 |
| - "chat_completion = taskingai.inference.chat_completion(\n", |
| 97 | + "chat_completion_result = taskingai.inference.chat_completion(\n", |
98 | 98 | " model_id=model_id,\n",
|
99 | 99 | " messages=[\n",
|
100 | 100 | " SystemMessage(\"You are a professional assistant.\"),\n",
|
|
105 | 105 | " UserMessage(\"That's funny. Can you tell me another one?\"),\n",
|
106 | 106 | " ]\n",
|
107 | 107 | ")\n",
|
108 |
| - "chat_completion" |
| 108 | + "chat_completion_result" |
109 | 109 | ],
|
110 | 110 | "metadata": {
|
111 | 111 | "collapsed": false,
|
112 | 112 | "ExecuteTime": {
|
113 |
| - "end_time": "2023-11-28T12:07:52.367618Z", |
114 |
| - "start_time": "2023-11-28T12:07:50.109888Z" |
| 113 | + "end_time": "2023-11-30T11:03:40.460984Z", |
| 114 | + "start_time": "2023-11-30T11:03:39.092991Z" |
115 | 115 | }
|
116 | 116 | },
|
117 | 117 | "id": "e8933bc07f4b3228"
|
|
122 | 122 | "outputs": [
|
123 | 123 | {
|
124 | 124 | "data": {
|
125 |
| - "text/plain": "{'created_timestamp': 1701173274744,\n 'finish_reason': 'length',\n 'message': {'content': \"Of course! Here's\",\n 'function_call': None,\n 'role': 'assistant'},\n 'object': 'ChatCompletion'}" |
| 125 | + "text/plain": "ChatCompletion(object='ChatCompletion', finish_reason=<ChatCompletionFinishReason.length: 'length'>, message=ChatCompletionAssistantMessage(content=\"Of course! Here's\", role=<ChatCompletionRole.assistant: 'assistant'>, function_call=None), created_timestamp=1701342221823)" |
126 | 126 | },
|
127 | 127 | "execution_count": 5,
|
128 | 128 | "metadata": {},
|
|
131 | 131 | ],
|
132 | 132 | "source": [
|
133 | 133 | "# config max tokens\n",
|
134 |
| - "chat_completion = taskingai.inference.chat_completion(\n", |
| 134 | + "chat_completion_result = taskingai.inference.chat_completion(\n", |
135 | 135 | " model_id=model_id,\n",
|
136 | 136 | " messages=[\n",
|
137 | 137 | " SystemMessage(\"You are a professional assistant.\"),\n",
|
|
145 | 145 | " \"max_tokens\": 5\n",
|
146 | 146 | " }\n",
|
147 | 147 | ")\n",
|
148 |
| - "chat_completion" |
| 148 | + "chat_completion_result" |
149 | 149 | ],
|
150 | 150 | "metadata": {
|
151 | 151 | "collapsed": false,
|
152 | 152 | "ExecuteTime": {
|
153 |
| - "end_time": "2023-11-28T12:07:54.817719Z", |
154 |
| - "start_time": "2023-11-28T12:07:53.137411Z" |
| 153 | + "end_time": "2023-11-30T11:03:41.911247Z", |
| 154 | + "start_time": "2023-11-30T11:03:40.969583Z" |
155 | 155 | }
|
156 | 156 | },
|
157 | 157 | "id": "f7c1b8be2579d9e0"
|
|
164 | 164 | "name": "stdout",
|
165 | 165 | "output_type": "stream",
|
166 | 166 | "text": [
|
167 |
| - "chat_completion = {'created_timestamp': 1701173277776,\n", |
168 |
| - " 'finish_reason': 'function_call',\n", |
169 |
| - " 'message': {'content': None,\n", |
170 |
| - " 'function_call': {'arguments': {'a': 112, 'b': 22},\n", |
171 |
| - " 'name': 'plus_a_and_b'},\n", |
172 |
| - " 'role': 'assistant'},\n", |
173 |
| - " 'object': 'ChatCompletion'}\n", |
| 167 | + "chat_completion_result = object='ChatCompletion' finish_reason=<ChatCompletionFinishReason.function_calls: 'function_call'> message=ChatCompletionAssistantMessage(content=None, role=<ChatCompletionRole.assistant: 'assistant'>, function_call=ChatCompletionFunctionCall(arguments={'a': 112, 'b': 22}, name='plus_a_and_b')) created_timestamp=1701342223979\n", |
174 | 168 | "function name: plus_a_and_b, argument content: {\"a\": 112, \"b\": 22}\n"
|
175 | 169 | ]
|
176 | 170 | }
|
|
195 | 189 | " \"required\": [\"a\", \"b\"]\n",
|
196 | 190 | " },\n",
|
197 | 191 | ")\n",
|
198 |
| - "chat_completion = taskingai.inference.chat_completion(\n", |
| 192 | + "chat_completion_result = taskingai.inference.chat_completion(\n", |
199 | 193 | " model_id=model_id,\n",
|
200 | 194 | " messages=[\n",
|
201 | 195 | " SystemMessage(\"You are a professional assistant.\"),\n",
|
202 | 196 | " UserMessage(\"What is the result of 112 plus 22?\"),\n",
|
203 | 197 | " ],\n",
|
204 | 198 | " functions=[function]\n",
|
205 | 199 | ")\n",
|
206 |
| - "print(f\"chat_completion = {chat_completion}\")\n", |
| 200 | + "print(f\"chat_completion_result = {chat_completion_result}\")\n", |
207 | 201 | "\n",
|
208 |
| - "assistant_function_call_message = chat_completion.message\n", |
| 202 | + "assistant_function_call_message = chat_completion_result.message\n", |
209 | 203 | "fucntion_name = assistant_function_call_message.function_call.name\n",
|
210 | 204 | "argument_content = json.dumps(assistant_function_call_message.function_call.arguments)\n",
|
211 | 205 | "print(f\"function name: {fucntion_name}, argument content: {argument_content}\")"
|
212 | 206 | ],
|
213 | 207 | "metadata": {
|
214 | 208 | "collapsed": false,
|
215 | 209 | "ExecuteTime": {
|
216 |
| - "end_time": "2023-11-28T12:07:57.823570Z", |
217 |
| - "start_time": "2023-11-28T12:07:55.601317Z" |
| 210 | + "end_time": "2023-11-30T11:03:44.068090Z", |
| 211 | + "start_time": "2023-11-30T11:03:42.646835Z" |
218 | 212 | }
|
219 | 213 | },
|
220 | 214 | "id": "2645bdc3df011e7d"
|
|
233 | 227 | "outputs": [
|
234 | 228 | {
|
235 | 229 | "data": {
|
236 |
| - "text/plain": "{'created_timestamp': 1701173282280,\n 'finish_reason': 'stop',\n 'message': {'content': 'The result of 112 plus 22 is 144.',\n 'function_call': None,\n 'role': 'assistant'},\n 'object': 'ChatCompletion'}" |
| 230 | + "text/plain": "ChatCompletion(object='ChatCompletion', finish_reason=<ChatCompletionFinishReason.stop: 'stop'>, message=ChatCompletionAssistantMessage(content='The result of 112 plus 22 is 134.', role=<ChatCompletionRole.assistant: 'assistant'>, function_call=None), created_timestamp=1701342226882)" |
237 | 231 | },
|
238 | 232 | "execution_count": 7,
|
239 | 233 | "metadata": {},
|
|
252 | 246 | " ],\n",
|
253 | 247 | " functions=[function]\n",
|
254 | 248 | ")\n",
|
255 |
| - "chat_completion" |
| 249 | + "chat_completion_result" |
256 | 250 | ],
|
257 | 251 | "metadata": {
|
258 | 252 | "collapsed": false,
|
259 | 253 | "ExecuteTime": {
|
260 |
| - "end_time": "2023-11-28T12:08:02.319026Z", |
261 |
| - "start_time": "2023-11-28T12:08:00.109622Z" |
| 254 | + "end_time": "2023-11-30T11:03:46.969143Z", |
| 255 | + "start_time": "2023-11-30T11:03:46.043885Z" |
262 | 256 | }
|
263 | 257 | },
|
264 | 258 | "id": "9df9a8b9eafa17d9"
|
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "code", |
| 262 | + "execution_count": 14, |
| 263 | + "outputs": [ |
| 264 | + { |
| 265 | + "name": "stdout", |
| 266 | + "output_type": "stream", |
| 267 | + "text": [ |
| 268 | + "1 2 3 4 5 6 7 8 9 10 \n", |
| 269 | + "11 12 13 14 15 16 17 18 19 20 \n", |
| 270 | + "21 22 23 24 25 26 27 28 29 30 \n", |
| 271 | + "31 32 33 34 35 36 37 38 39 40 \n", |
| 272 | + "41 42 43 44 45 46 47 48 49 50 \n", |
| 273 | + "51 52 53 54 55 56 57 58 59 60 \n", |
| 274 | + "61 62 63 64 65 66 67 68 69 70 \n", |
| 275 | + "71 72 73 74 75 76 77 78 79 80 \n", |
| 276 | + "81 82 83 84 85 86 87 88 89 90 \n", |
| 277 | + "91 92 93 94 95 96 97 98 99 100\n", |
| 278 | + " message object: content='1 2 3 4 5 6 7 8 9 10 \\n11 12 13 14 15 16 17 18 19 20 \\n21 22 23 24 25 26 27 28 29 30 \\n31 32 33 34 35 36 37 38 39 40 \\n41 42 43 44 45 46 47 48 49 50 \\n51 52 53 54 55 56 57 58 59 60 \\n61 62 63 64 65 66 67 68 69 70 \\n71 72 73 74 75 76 77 78 79 80 \\n81 82 83 84 85 86 87 88 89 90 \\n91 92 93 94 95 96 97 98 99 100' role=<ChatCompletionRole.assistant: 'assistant'> function_call=None\n" |
| 279 | + ] |
| 280 | + } |
| 281 | + ], |
| 282 | + "source": [ |
| 283 | + "# generate in stream mode\n", |
| 284 | + "chat_completion_result = taskingai.inference.chat_completion(\n", |
| 285 | + " model_id=model_id,\n", |
| 286 | + " messages=[\n", |
| 287 | + " UserMessage(\"Please count from 1 to 100, and add a new line after every 10 numbers.\"),\n", |
| 288 | + " ],\n", |
| 289 | + " stream=True\n", |
| 290 | + ")\n", |
| 291 | + "for item in chat_completion_result:\n", |
| 292 | + " \n", |
| 293 | + " if item.get(\"object\") == \"ChatCompletionChunk\":\n", |
| 294 | + " chunk = ChatCompletionChunk(**item)\n", |
| 295 | + " print(chunk.delta, end=\"\", flush=True)\n", |
| 296 | + " \n", |
| 297 | + " elif item.get(\"object\") == \"ChatCompletion\":\n", |
| 298 | + " result = ChatCompletion(**item)\n", |
| 299 | + " print(f\"\\n message object: {result.message}\")" |
| 300 | + ], |
| 301 | + "metadata": { |
| 302 | + "collapsed": false, |
| 303 | + "ExecuteTime": { |
| 304 | + "end_time": "2023-11-30T11:08:05.624547Z", |
| 305 | + "start_time": "2023-11-30T11:07:58.499361Z" |
| 306 | + } |
| 307 | + }, |
| 308 | + "id": "4f3290f87635152a" |
| 309 | + }, |
| 310 | + { |
| 311 | + "cell_type": "code", |
| 312 | + "execution_count": null, |
| 313 | + "outputs": [], |
| 314 | + "source": [], |
| 315 | + "metadata": { |
| 316 | + "collapsed": false |
| 317 | + }, |
| 318 | + "id": "dab2cb3f84463700" |
265 | 319 | }
|
266 | 320 | ],
|
267 | 321 | "metadata": {
|
|
0 commit comments