ommlds 0.0.0.dev465__py3-none-any.whl → 0.0.0.dev467__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ommlds might be problematic. Click here for more details.
- ommlds/.omlish-manifests.json +22 -6
- ommlds/backends/google/protocol/types.py +4 -1
- ommlds/cli/sessions/chat/backends/catalog.py +1 -1
- ommlds/cli/sessions/chat/chat/ai/services.py +5 -7
- ommlds/minichain/__init__.py +6 -0
- ommlds/minichain/backends/impls/anthropic/stream.py +17 -0
- ommlds/minichain/backends/impls/google/stream.py +105 -20
- ommlds/minichain/backends/impls/mlx/chat.py +95 -21
- ommlds/minichain/backends/impls/openai/chat.py +2 -2
- ommlds/minichain/backends/impls/openai/format.py +108 -104
- ommlds/minichain/backends/impls/openai/stream.py +14 -13
- ommlds/minichain/chat/stream/adapters.py +5 -50
- ommlds/minichain/chat/stream/joining.py +96 -0
- ommlds/minichain/chat/stream/types.py +20 -4
- {ommlds-0.0.0.dev465.dist-info → ommlds-0.0.0.dev467.dist-info}/METADATA +3 -3
- {ommlds-0.0.0.dev465.dist-info → ommlds-0.0.0.dev467.dist-info}/RECORD +20 -20
- ommlds/minichain/backends/impls/openai/format2.py +0 -210
- {ommlds-0.0.0.dev465.dist-info → ommlds-0.0.0.dev467.dist-info}/WHEEL +0 -0
- {ommlds-0.0.0.dev465.dist-info → ommlds-0.0.0.dev467.dist-info}/entry_points.txt +0 -0
- {ommlds-0.0.0.dev465.dist-info → ommlds-0.0.0.dev467.dist-info}/licenses/LICENSE +0 -0
- {ommlds-0.0.0.dev465.dist-info → ommlds-0.0.0.dev467.dist-info}/top_level.txt +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
ommlds/.omlish-manifests.json,sha256=
|
|
1
|
+
ommlds/.omlish-manifests.json,sha256=MyJQsh5T1CMMXcGdxwqI9abQ8-j-ZlRGRluiikbeKRY,18414
|
|
2
2
|
ommlds/__about__.py,sha256=uAJgr2I_m_oZPlV5P8XLFeYpBlEM-DdzeyF6O5OK_qs,1759
|
|
3
3
|
ommlds/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
4
|
ommlds/huggingface.py,sha256=JfEyfKOxU3-SY_ojtXBJFNeD-NIuKjvMe3GL3e93wNA,1175
|
|
@@ -16,7 +16,7 @@ ommlds/backends/anthropic/protocol/sse/events.py,sha256=pLscyLFpxcrtOijNPRgivrxY
|
|
|
16
16
|
ommlds/backends/google/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
17
17
|
ommlds/backends/google/protocol/__init__.py,sha256=OWmhuCS_sZ08ZaBtSSDRIudHKzkzgmqxY-3jgQxTidw,105
|
|
18
18
|
ommlds/backends/google/protocol/_marshal.py,sha256=LWikcdMDrKQ0lMtclNXwK9qamijUBzK62nrEVo2OFtc,305
|
|
19
|
-
ommlds/backends/google/protocol/types.py,sha256
|
|
19
|
+
ommlds/backends/google/protocol/types.py,sha256=JDft5-5sPuy8sFWTvid4JuC9a9EnuQizt9dyAV2L56s,17536
|
|
20
20
|
ommlds/backends/llamacpp/__init__.py,sha256=zXFpLXE4a2vEl0jcPDyKlPHHfZ3Z8Dz0twhEIyZ8-vg,59
|
|
21
21
|
ommlds/backends/llamacpp/buildwheel.py,sha256=q9ghCLVbm8Jm6syrZlBP-x1qNDd0wSl15B2OXBtDBQ8,3813
|
|
22
22
|
ommlds/backends/llamacpp/logging.py,sha256=nCEC6ASEuTpJqx47DMLhnbr5KelDlbxhM0nKQt4bc3w,773
|
|
@@ -91,7 +91,7 @@ ommlds/cli/sessions/chat/driver.py,sha256=ddnCYTKqWiPxV8U4UbFwb7E3yi81ItjZ9j3AJd
|
|
|
91
91
|
ommlds/cli/sessions/chat/inject.py,sha256=7Yg6wUs2Oej4UjNZCAWCJCEsDJZWvT4G8XvkvVUMC7U,1928
|
|
92
92
|
ommlds/cli/sessions/chat/session.py,sha256=eqwelLE74JFC-fBpk_hdwMD2nP4pLv3ZPwUn99200B8,521
|
|
93
93
|
ommlds/cli/sessions/chat/backends/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
94
|
-
ommlds/cli/sessions/chat/backends/catalog.py,sha256=
|
|
94
|
+
ommlds/cli/sessions/chat/backends/catalog.py,sha256=gc03dqXEUUSi4WCWJ30HrkHPWlAkZHXkj1pOq7KVakU,1764
|
|
95
95
|
ommlds/cli/sessions/chat/backends/inject.py,sha256=VbZ-Fb679kTItRpAhIYCqSM8vXUFeRDQWssUfrFgGi8,882
|
|
96
96
|
ommlds/cli/sessions/chat/backends/injection.py,sha256=GCn5OvNIEowgB70kQVuU84z3i8lLA4vOVkTZlQG8s0o,327
|
|
97
97
|
ommlds/cli/sessions/chat/backends/types.py,sha256=5eImYHXLKqbC5MDrN443eMGamP9snCmV1n7LtAsqgPk,696
|
|
@@ -100,7 +100,7 @@ ommlds/cli/sessions/chat/chat/ai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRk
|
|
|
100
100
|
ommlds/cli/sessions/chat/chat/ai/inject.py,sha256=FCq-E8auVJ-gnK18NoKiINGBHgDllroZRG7z5ZY63L8,2514
|
|
101
101
|
ommlds/cli/sessions/chat/chat/ai/injection.py,sha256=2O_ELMusxQsYaB2oqvzjYpZQzjshocJup7Z5unzoUIY,404
|
|
102
102
|
ommlds/cli/sessions/chat/chat/ai/rendering.py,sha256=cicA6NwkM9UJb1dgsakhkhIMWGzBsArPFvYoINBmuec,2285
|
|
103
|
-
ommlds/cli/sessions/chat/chat/ai/services.py,sha256
|
|
103
|
+
ommlds/cli/sessions/chat/chat/ai/services.py,sha256=-XcMEatIDA0h_h-crL45c3qvC5VbDdhvKcWsxzioQA8,2507
|
|
104
104
|
ommlds/cli/sessions/chat/chat/ai/tools.py,sha256=en94LLM8y73N11xhQPlkSLfbzAmYVwKU-4nz2i0CK0E,1094
|
|
105
105
|
ommlds/cli/sessions/chat/chat/ai/types.py,sha256=QH6Cn6DrdxBi0Tnura7Fq-WtGUm0FdsazZSgki6nf0A,750
|
|
106
106
|
ommlds/cli/sessions/chat/chat/state/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -147,7 +147,7 @@ ommlds/cli/state/storage.py,sha256=tRPmgCANRrw7A5Qr700OaH58F6S96O37I8Ivrbo7_gI,3
|
|
|
147
147
|
ommlds/datasets/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
148
148
|
ommlds/datasets/lib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
149
149
|
ommlds/datasets/lib/movies.py,sha256=LmdfoXsZU9XMM_r-sxCLv_s06BFzwWO4xUj6sc9XVcI,1961
|
|
150
|
-
ommlds/minichain/__init__.py,sha256=
|
|
150
|
+
ommlds/minichain/__init__.py,sha256=EqNJpuMwqkkdlNmipjaoC30yAqH7c8oziszlkCcXBrQ,10982
|
|
151
151
|
ommlds/minichain/_marshal.py,sha256=n9PGWrHhvAmGIc7KDOYt3IF9Z6G0ncXskyICTp3Ji6k,1923
|
|
152
152
|
ommlds/minichain/_typedvalues.py,sha256=Vl1Edt5khC0e5RPFBPmPCxn0IzrfVd0NHzAjAN2E6Kc,2183
|
|
153
153
|
ommlds/minichain/completion.py,sha256=lQ0LfCIYZsvDqteHhhDIv16D2_gn_xMfEL0ouywE5Yo,1033
|
|
@@ -172,14 +172,14 @@ ommlds/minichain/backends/impls/anthropic/__init__.py,sha256=47DEQpj8HBSa-_TImW-
|
|
|
172
172
|
ommlds/minichain/backends/impls/anthropic/chat.py,sha256=LVaRP_Pbz4UghSHD8GZ22hMWE4Rsd_6bSysgl2BR_AM,4166
|
|
173
173
|
ommlds/minichain/backends/impls/anthropic/names.py,sha256=GPPeYt0CcDcDCR8I6BMd7bMjC_Zk_bjnLLpF9ClwXcg,1099
|
|
174
174
|
ommlds/minichain/backends/impls/anthropic/protocol.py,sha256=whPVYuKShKiMCzasHl77sCIiymhzXj8mFZXEyhZvld8,3292
|
|
175
|
-
ommlds/minichain/backends/impls/anthropic/stream.py,sha256=
|
|
175
|
+
ommlds/minichain/backends/impls/anthropic/stream.py,sha256=AvoLk25R7E_aSS1tAN-P-UKjhKPxYtQoeGakXvFdblI,8677
|
|
176
176
|
ommlds/minichain/backends/impls/duckduckgo/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
177
177
|
ommlds/minichain/backends/impls/duckduckgo/search.py,sha256=igzeU9P9b1MMiu4KAJVS9H6KLIoPm68wXi4Kx3_DHyQ,940
|
|
178
178
|
ommlds/minichain/backends/impls/google/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
179
179
|
ommlds/minichain/backends/impls/google/chat.py,sha256=q6LasmAh3Xn78kNlgpgwoKhdTj3o76j0X3_xitdyAE0,6316
|
|
180
180
|
ommlds/minichain/backends/impls/google/names.py,sha256=HxHJ31HeKZg6aW1C_Anqp-gamCXpq9pOdKj8_yVgE8Y,871
|
|
181
181
|
ommlds/minichain/backends/impls/google/search.py,sha256=5-2nAZ1QmbqHSQcwWnqqcgCM-Duy2ryctJEIv2tcpZg,3260
|
|
182
|
-
ommlds/minichain/backends/impls/google/stream.py,sha256=
|
|
182
|
+
ommlds/minichain/backends/impls/google/stream.py,sha256=Mhg5cOe7PKbnlQURyk2IRermtpzxU5Y0j3w4fkt2ZRg,7900
|
|
183
183
|
ommlds/minichain/backends/impls/google/tools.py,sha256=Tty0gsyx7-PbeoNqMuql_ewQ6q-ZsDaDdsD5ShinGVY,5089
|
|
184
184
|
ommlds/minichain/backends/impls/huggingface/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
185
185
|
ommlds/minichain/backends/impls/huggingface/configs.py,sha256=6jsBtPNXOP57PcpxNTVLGWLc-18Iwn_lDbGouwCJTIQ,258
|
|
@@ -190,15 +190,14 @@ ommlds/minichain/backends/impls/llamacpp/completion.py,sha256=oJ2I6wUoIPXYLm9Vc7
|
|
|
190
190
|
ommlds/minichain/backends/impls/llamacpp/format.py,sha256=fcLMwk7r7FbNrYCH39G3fDRInKvlPIqcoxyLj95CooA,778
|
|
191
191
|
ommlds/minichain/backends/impls/llamacpp/stream.py,sha256=uGog3xPNqCjGgyZjXEjhlxKbIbakWbapjANAEsmW-U4,3378
|
|
192
192
|
ommlds/minichain/backends/impls/mlx/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
193
|
-
ommlds/minichain/backends/impls/mlx/chat.py,sha256=
|
|
193
|
+
ommlds/minichain/backends/impls/mlx/chat.py,sha256=sMlhgiFZrxAC-kKkLSJ6c-2uJn0IHZXH4EiPET_-CKI,7458
|
|
194
194
|
ommlds/minichain/backends/impls/openai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
195
|
-
ommlds/minichain/backends/impls/openai/chat.py,sha256=
|
|
195
|
+
ommlds/minichain/backends/impls/openai/chat.py,sha256=eMRjxPNrzrRjaw83LJuYzP9DGvwGyY2ObJSZub4Z9bY,2658
|
|
196
196
|
ommlds/minichain/backends/impls/openai/completion.py,sha256=0XTC08mZzbW23Y2DNW2xfRR0eDX4nTyejF8CR1BdHZs,1756
|
|
197
197
|
ommlds/minichain/backends/impls/openai/embedding.py,sha256=kkDJ3_0EqwQ_E0eXsSH1TuWXQmRqaijK8zG90fnlf3s,1582
|
|
198
|
-
ommlds/minichain/backends/impls/openai/format.py,sha256=
|
|
199
|
-
ommlds/minichain/backends/impls/openai/format2.py,sha256=OQ3N8VR4uL3PvHxjOQSdgg1bQ4_WiDz_sOV4WhVEXpQ,6611
|
|
198
|
+
ommlds/minichain/backends/impls/openai/format.py,sha256=teGX8mNU3sXNWP4YWGD8d59M4X9_r75ImSzfTJgtNCM,7351
|
|
200
199
|
ommlds/minichain/backends/impls/openai/names.py,sha256=b74t8FwSbGEveVtVz4SqM5tiRDyTKNlUKlseV6AX3Yo,1211
|
|
201
|
-
ommlds/minichain/backends/impls/openai/stream.py,sha256=
|
|
200
|
+
ommlds/minichain/backends/impls/openai/stream.py,sha256=M7II7kZFsy33j8NQwdM1CCeKet3lw-XLOQdDzrzn-Yo,5297
|
|
202
201
|
ommlds/minichain/backends/impls/sentencepiece/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
203
202
|
ommlds/minichain/backends/impls/sentencepiece/tokens.py,sha256=tUEBKyBgkTowssS_AdcAuPkyFzfyDfE935x4JG8PXM0,1602
|
|
204
203
|
ommlds/minichain/backends/impls/tinygrad/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -228,9 +227,10 @@ ommlds/minichain/chat/choices/services.py,sha256=p_FsCZEsGJun8epzAHoeNdjwJ86Zwih
|
|
|
228
227
|
ommlds/minichain/chat/choices/types.py,sha256=OPnMUp0KUKGGPJKsQZiXUtd8Z9b_JnhOy7SXKL6QDWU,725
|
|
229
228
|
ommlds/minichain/chat/stream/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
230
229
|
ommlds/minichain/chat/stream/_marshal.py,sha256=r6NYBUviV7jIssaFprzv2rVEj8cFEuBlt71BSMZ-448,397
|
|
231
|
-
ommlds/minichain/chat/stream/adapters.py,sha256=
|
|
230
|
+
ommlds/minichain/chat/stream/adapters.py,sha256=3hKo3-MLtVIB-Nhdlxt17LP9vZESr-2fBZQ3Yr6l_Ps,1077
|
|
231
|
+
ommlds/minichain/chat/stream/joining.py,sha256=oPxLT4qEYWCaxclnZvt54ztQP5md4V6u6Uwn4qd2e9M,2936
|
|
232
232
|
ommlds/minichain/chat/stream/services.py,sha256=TxNEOm85QEFYtKb59q_uP6eSNh75v1fF-IpsJjhY4to,1252
|
|
233
|
-
ommlds/minichain/chat/stream/types.py,sha256=
|
|
233
|
+
ommlds/minichain/chat/stream/types.py,sha256=kpHsWLNHk7hmaNPDSCqLH-ECSAiz83lRfr00LhSWb5U,1589
|
|
234
234
|
ommlds/minichain/chat/tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
235
235
|
ommlds/minichain/chat/tools/execution.py,sha256=tCPsz1kCt5RcoRX7dwfaJRvObniJJv_D2hCwz1Slo_A,573
|
|
236
236
|
ommlds/minichain/chat/tools/ids.py,sha256=DFBKrpeDTCnMcU-P38VbPWX0YBDaz_HzMgx3yXWjFWQ,759
|
|
@@ -367,9 +367,9 @@ ommlds/wiki/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU
|
|
|
367
367
|
ommlds/wiki/utils/io.py,sha256=UKgDJGtmpnWvIqVd2mJc2QNPOqlToEY1GEveNp6_pMo,7088
|
|
368
368
|
ommlds/wiki/utils/progress.py,sha256=EhvKcMFYtsarCQhIahlO6f0SboyAKP3UwUyrnVnP-Vk,3222
|
|
369
369
|
ommlds/wiki/utils/xml.py,sha256=vVV8Ctn13aaRM9eYfs9Wd6rHn5WOCEUzQ44fIhOvJdg,3754
|
|
370
|
-
ommlds-0.0.0.
|
|
371
|
-
ommlds-0.0.0.
|
|
372
|
-
ommlds-0.0.0.
|
|
373
|
-
ommlds-0.0.0.
|
|
374
|
-
ommlds-0.0.0.
|
|
375
|
-
ommlds-0.0.0.
|
|
370
|
+
ommlds-0.0.0.dev467.dist-info/licenses/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
|
|
371
|
+
ommlds-0.0.0.dev467.dist-info/METADATA,sha256=NvYqf0PtfEdrj2en5RFFpTHB_cGjm41groVHgy54WZQ,3224
|
|
372
|
+
ommlds-0.0.0.dev467.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
373
|
+
ommlds-0.0.0.dev467.dist-info/entry_points.txt,sha256=Z5YWtX7ClfiCKdW-dd_CSVvM0h4yQpJPi-2G3q6gNFo,35
|
|
374
|
+
ommlds-0.0.0.dev467.dist-info/top_level.txt,sha256=Rbnk5d5wi58vnAXx13WFZqdQ4VX8hBCS2hEL3WeXOhY,7
|
|
375
|
+
ommlds-0.0.0.dev467.dist-info/RECORD,,
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import typing as ta
|
|
2
|
-
|
|
3
|
-
from omlish import cached
|
|
4
|
-
from omlish import check
|
|
5
|
-
from omlish import typedvalues as tv
|
|
6
|
-
from omlish.formats import json
|
|
7
|
-
|
|
8
|
-
from .....backends.openai import protocol as pt
|
|
9
|
-
from ....chat.choices.services import ChatChoicesResponse
|
|
10
|
-
from ....chat.choices.types import AiChoice
|
|
11
|
-
from ....chat.choices.types import AiChoices
|
|
12
|
-
from ....chat.choices.types import ChatChoicesOptions
|
|
13
|
-
from ....chat.messages import AiMessage
|
|
14
|
-
from ....chat.messages import AnyAiMessage
|
|
15
|
-
from ....chat.messages import Chat
|
|
16
|
-
from ....chat.messages import SystemMessage
|
|
17
|
-
from ....chat.messages import ToolUseMessage
|
|
18
|
-
from ....chat.messages import ToolUseResultMessage
|
|
19
|
-
from ....chat.messages import UserMessage
|
|
20
|
-
from ....chat.tools.types import Tool
|
|
21
|
-
from ....content.json import JsonContent
|
|
22
|
-
from ....content.prepare import prepare_content_str
|
|
23
|
-
from ....llms.types import MaxTokens
|
|
24
|
-
from ....llms.types import Temperature
|
|
25
|
-
from ....llms.types import TokenUsage
|
|
26
|
-
from ....llms.types import TokenUsageOutput
|
|
27
|
-
from ....tools.jsonschema import build_tool_spec_params_json_schema
|
|
28
|
-
from ....tools.types import ToolSpec
|
|
29
|
-
from ....tools.types import ToolUse
|
|
30
|
-
from ....types import Option
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
##
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def build_oai_request_msgs(mc_chat: Chat) -> ta.Sequence[pt.ChatCompletionMessage]:
|
|
37
|
-
oai_msgs: list[pt.ChatCompletionMessage] = []
|
|
38
|
-
|
|
39
|
-
for mc_msg in mc_chat:
|
|
40
|
-
if isinstance(mc_msg, SystemMessage):
|
|
41
|
-
oai_msgs.append(pt.SystemChatCompletionMessage(
|
|
42
|
-
content=check.isinstance(mc_msg.c, str),
|
|
43
|
-
))
|
|
44
|
-
|
|
45
|
-
elif isinstance(mc_msg, AiMessage):
|
|
46
|
-
oai_msgs.append(pt.AssistantChatCompletionMessage(
|
|
47
|
-
content=check.isinstance(mc_msg.c, (str, None)),
|
|
48
|
-
))
|
|
49
|
-
|
|
50
|
-
elif isinstance(mc_msg, ToolUseMessage):
|
|
51
|
-
oai_msgs.append(pt.AssistantChatCompletionMessage(
|
|
52
|
-
tool_calls=[pt.AssistantChatCompletionMessage.ToolCall(
|
|
53
|
-
id=check.not_none(mc_msg.tu.id),
|
|
54
|
-
function=pt.AssistantChatCompletionMessage.ToolCall.Function(
|
|
55
|
-
arguments=check.not_none(mc_msg.tu.raw_args),
|
|
56
|
-
name=mc_msg.tu.name,
|
|
57
|
-
),
|
|
58
|
-
)],
|
|
59
|
-
))
|
|
60
|
-
|
|
61
|
-
elif isinstance(mc_msg, UserMessage):
|
|
62
|
-
oai_msgs.append(pt.UserChatCompletionMessage(
|
|
63
|
-
content=prepare_content_str(mc_msg.c),
|
|
64
|
-
))
|
|
65
|
-
|
|
66
|
-
elif isinstance(mc_msg, ToolUseResultMessage):
|
|
67
|
-
tc: str
|
|
68
|
-
if isinstance(mc_msg.tur.c, str):
|
|
69
|
-
tc = mc_msg.tur.c
|
|
70
|
-
elif isinstance(mc_msg.tur.c, JsonContent):
|
|
71
|
-
tc = json.dumps_compact(mc_msg.tur.c)
|
|
72
|
-
else:
|
|
73
|
-
raise TypeError(mc_msg.tur.c)
|
|
74
|
-
oai_msgs.append(pt.ToolChatCompletionMessage(
|
|
75
|
-
tool_call_id=check.not_none(mc_msg.tur.id),
|
|
76
|
-
content=tc,
|
|
77
|
-
))
|
|
78
|
-
|
|
79
|
-
else:
|
|
80
|
-
raise TypeError(mc_msg)
|
|
81
|
-
|
|
82
|
-
return oai_msgs
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
#
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
def build_mc_ai_choice(oai_choice: pt.ChatCompletionResponseChoice) -> AiChoice:
|
|
89
|
-
cur: list[AnyAiMessage] = []
|
|
90
|
-
|
|
91
|
-
oai_msg = oai_choice.message
|
|
92
|
-
|
|
93
|
-
if (oai_c := oai_msg.content) is not None:
|
|
94
|
-
cur.append(AiMessage(check.isinstance(oai_c, str)))
|
|
95
|
-
|
|
96
|
-
for oai_tc in oai_msg.tool_calls or []:
|
|
97
|
-
cur.append(ToolUseMessage(ToolUse(
|
|
98
|
-
id=oai_tc.id,
|
|
99
|
-
name=oai_tc.function.name,
|
|
100
|
-
args=json.loads(oai_tc.function.arguments or '{}'),
|
|
101
|
-
raw_args=oai_tc.function.arguments,
|
|
102
|
-
)))
|
|
103
|
-
|
|
104
|
-
return AiChoice(cur)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
def build_mc_ai_choices(oai_resp: pt.ChatCompletionResponse) -> AiChoices:
|
|
108
|
-
return [
|
|
109
|
-
build_mc_ai_choice(oai_choice)
|
|
110
|
-
for oai_choice in oai_resp.choices
|
|
111
|
-
]
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def build_mc_choices_response(oai_resp: pt.ChatCompletionResponse) -> ChatChoicesResponse:
|
|
115
|
-
return ChatChoicesResponse(
|
|
116
|
-
build_mc_ai_choices(oai_resp),
|
|
117
|
-
|
|
118
|
-
tv.TypedValues(
|
|
119
|
-
*([TokenUsageOutput(TokenUsage(
|
|
120
|
-
input=tu.prompt_tokens,
|
|
121
|
-
output=tu.completion_tokens,
|
|
122
|
-
total=tu.total_tokens,
|
|
123
|
-
))] if (tu := oai_resp.usage) is not None else []),
|
|
124
|
-
),
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
##
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
class OpenaiChatRequestHandler:
|
|
132
|
-
def __init__(
|
|
133
|
-
self,
|
|
134
|
-
chat: Chat,
|
|
135
|
-
*options: ChatChoicesOptions,
|
|
136
|
-
model: str,
|
|
137
|
-
mandatory_kwargs: ta.Mapping[str, ta.Any] | None = None,
|
|
138
|
-
) -> None:
|
|
139
|
-
super().__init__()
|
|
140
|
-
|
|
141
|
-
self._chat = chat
|
|
142
|
-
self._options = options
|
|
143
|
-
self._model = model
|
|
144
|
-
self._mandatory_kwargs = mandatory_kwargs
|
|
145
|
-
|
|
146
|
-
DEFAULT_OPTIONS: ta.ClassVar[tv.TypedValues[Option]] = tv.TypedValues[Option](
|
|
147
|
-
Temperature(0.),
|
|
148
|
-
MaxTokens(1024),
|
|
149
|
-
)
|
|
150
|
-
|
|
151
|
-
_OPTION_KWARG_NAMES_MAP: ta.ClassVar[ta.Mapping[str, type[ChatChoicesOptions]]] = dict(
|
|
152
|
-
temperature=Temperature,
|
|
153
|
-
max_tokens=MaxTokens,
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
class _ProcessedOptions(ta.NamedTuple):
|
|
157
|
-
kwargs: dict[str, ta.Any]
|
|
158
|
-
tools_by_name: dict[str, ToolSpec]
|
|
159
|
-
|
|
160
|
-
@cached.function
|
|
161
|
-
def _process_options(self) -> _ProcessedOptions:
|
|
162
|
-
kwargs: dict = dict(
|
|
163
|
-
temperature=0,
|
|
164
|
-
max_tokens=1024,
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
tools_by_name: dict[str, ToolSpec] = {}
|
|
168
|
-
|
|
169
|
-
with tv.TypedValues(*self._options).consume() as oc:
|
|
170
|
-
kwargs.update(oc.pop_scalar_kwargs(**self._OPTION_KWARG_NAMES_MAP))
|
|
171
|
-
|
|
172
|
-
for t in oc.pop(Tool, []):
|
|
173
|
-
if t.spec.name in tools_by_name:
|
|
174
|
-
raise NameError(t.spec.name)
|
|
175
|
-
tools_by_name[check.non_empty_str(t.spec.name)] = t.spec
|
|
176
|
-
|
|
177
|
-
if (mk := self._mandatory_kwargs):
|
|
178
|
-
for k, v in mk.items():
|
|
179
|
-
check.not_in(k, kwargs)
|
|
180
|
-
kwargs[k] = v
|
|
181
|
-
|
|
182
|
-
return self._ProcessedOptions(
|
|
183
|
-
kwargs=kwargs,
|
|
184
|
-
tools_by_name=tools_by_name,
|
|
185
|
-
)
|
|
186
|
-
|
|
187
|
-
@cached.function
|
|
188
|
-
def oai_request(self) -> pt.ChatCompletionRequest:
|
|
189
|
-
po = self._process_options()
|
|
190
|
-
|
|
191
|
-
tools: list[pt.ChatCompletionRequestTool] = [
|
|
192
|
-
pt.ChatCompletionRequestTool(
|
|
193
|
-
function=pt.ChatCompletionRequestTool.Function(
|
|
194
|
-
name=check.not_none(ts.name),
|
|
195
|
-
description=prepare_content_str(ts.desc),
|
|
196
|
-
parameters=build_tool_spec_params_json_schema(ts),
|
|
197
|
-
),
|
|
198
|
-
)
|
|
199
|
-
for ts in po.tools_by_name.values()
|
|
200
|
-
]
|
|
201
|
-
|
|
202
|
-
return pt.ChatCompletionRequest(
|
|
203
|
-
model=self._model,
|
|
204
|
-
messages=build_oai_request_msgs(self._chat),
|
|
205
|
-
top_p=1,
|
|
206
|
-
tools=tools or None,
|
|
207
|
-
frequency_penalty=0.0,
|
|
208
|
-
presence_penalty=0.0,
|
|
209
|
-
**po.kwargs,
|
|
210
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|