ommlds 0.0.0.dev466__py3-none-any.whl → 0.0.0.dev512__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.
- ommlds/.omlish-manifests.json +404 -31
- ommlds/README.md +11 -0
- ommlds/__about__.py +21 -12
- ommlds/_hacks/__init__.py +4 -0
- ommlds/_hacks/funcs.py +110 -0
- ommlds/_hacks/names.py +158 -0
- ommlds/_hacks/params.py +73 -0
- ommlds/_hacks/patches.py +0 -3
- ommlds/backends/anthropic/protocol/__init__.py +13 -1
- ommlds/backends/anthropic/protocol/_dataclasses.py +1625 -0
- ommlds/backends/anthropic/protocol/sse/events.py +2 -0
- ommlds/backends/anthropic/protocol/types.py +5 -7
- ommlds/backends/cerebras/__init__.py +7 -0
- ommlds/backends/cerebras/_dataclasses.py +4254 -0
- ommlds/backends/cerebras/_marshal.py +24 -0
- ommlds/backends/cerebras/clients.py +9 -0
- ommlds/backends/cerebras/protocol.py +310 -0
- ommlds/backends/google/protocol/__init__.py +13 -0
- ommlds/backends/google/protocol/_dataclasses.py +5997 -0
- ommlds/backends/google/protocol/types.py +6 -8
- ommlds/backends/groq/__init__.py +7 -0
- ommlds/backends/groq/_dataclasses.py +3901 -0
- ommlds/backends/groq/_marshal.py +23 -0
- ommlds/backends/groq/clients.py +9 -0
- ommlds/backends/groq/protocol.py +247 -0
- ommlds/{huggingface.py → backends/huggingface/cache.py} +1 -6
- ommlds/backends/huggingface/cli.py +208 -0
- ommlds/backends/llamacpp/logging.py +4 -1
- ommlds/backends/mlx/caching.py +7 -3
- ommlds/backends/mlx/cli.py +10 -7
- ommlds/backends/mlx/generation.py +18 -16
- ommlds/backends/mlx/limits.py +10 -6
- ommlds/backends/mlx/loading.py +7 -4
- ommlds/backends/ollama/__init__.py +7 -0
- ommlds/backends/ollama/_dataclasses.py +3940 -0
- ommlds/backends/ollama/cli.py +36 -0
- ommlds/backends/ollama/protocol.py +201 -0
- ommlds/backends/openai/protocol/__init__.py +15 -1
- ommlds/backends/openai/protocol/_common.py +3 -5
- ommlds/backends/openai/protocol/_dataclasses.py +7708 -0
- ommlds/backends/tavily/__init__.py +7 -0
- ommlds/backends/tavily/_dataclasses.py +1734 -0
- ommlds/backends/tavily/protocol.py +299 -0
- ommlds/backends/tinygrad/models/llama3/__init__.py +22 -14
- ommlds/backends/torch/backends.py +1 -1
- ommlds/backends/transformers/__init__.py +14 -0
- ommlds/backends/transformers/filecache.py +109 -0
- ommlds/backends/transformers/streamers.py +73 -0
- ommlds/cli/__init__.py +7 -0
- ommlds/cli/_dataclasses.py +3835 -0
- ommlds/cli/asyncs.py +30 -0
- ommlds/cli/backends/catalog.py +88 -0
- ommlds/cli/backends/configs.py +9 -0
- ommlds/cli/backends/inject.py +100 -42
- ommlds/cli/{sessions/chat/backends → backends}/injection.py +1 -1
- ommlds/cli/backends/meta.py +82 -0
- ommlds/cli/{sessions/chat/backends → backends}/types.py +11 -1
- ommlds/cli/{sessions/chat/content → content}/messages.py +2 -2
- ommlds/cli/{sessions/chat/content → content}/strings.py +1 -1
- ommlds/cli/inject.py +17 -8
- ommlds/cli/inputs/asyncs.py +32 -0
- ommlds/cli/inputs/sync.py +75 -0
- ommlds/cli/main.py +346 -114
- ommlds/cli/rendering/configs.py +9 -0
- ommlds/cli/{sessions/chat/rendering → rendering}/inject.py +4 -5
- ommlds/cli/{sessions/chat/rendering → rendering}/markdown.py +1 -1
- ommlds/cli/{sessions/chat/rendering → rendering}/raw.py +1 -1
- ommlds/cli/{sessions/chat/rendering → rendering}/types.py +7 -1
- ommlds/cli/secrets.py +22 -0
- ommlds/cli/sessions/base.py +1 -10
- ommlds/cli/sessions/chat/configs.py +13 -30
- ommlds/cli/sessions/chat/drivers/ai/configs.py +13 -0
- ommlds/cli/sessions/chat/drivers/ai/events.py +57 -0
- ommlds/cli/sessions/chat/{chat → drivers}/ai/inject.py +15 -12
- ommlds/cli/sessions/chat/{chat → drivers}/ai/rendering.py +8 -8
- ommlds/cli/sessions/chat/{chat → drivers}/ai/services.py +5 -5
- ommlds/cli/sessions/chat/{chat → drivers}/ai/tools.py +4 -8
- ommlds/cli/sessions/chat/{chat → drivers}/ai/types.py +10 -1
- ommlds/cli/sessions/chat/drivers/configs.py +25 -0
- ommlds/cli/sessions/chat/drivers/events/inject.py +27 -0
- ommlds/cli/sessions/chat/drivers/events/injection.py +14 -0
- ommlds/cli/sessions/chat/drivers/events/manager.py +16 -0
- ommlds/cli/sessions/chat/drivers/events/types.py +44 -0
- ommlds/cli/sessions/chat/drivers/impl.py +50 -0
- ommlds/cli/sessions/chat/drivers/inject.py +70 -0
- ommlds/cli/sessions/chat/drivers/state/configs.py +13 -0
- ommlds/cli/sessions/chat/drivers/state/ids.py +25 -0
- ommlds/cli/sessions/chat/drivers/state/inject.py +83 -0
- ommlds/cli/sessions/chat/{chat → drivers}/state/inmemory.py +1 -6
- ommlds/cli/sessions/chat/{chat → drivers}/state/storage.py +18 -12
- ommlds/cli/sessions/chat/{chat → drivers}/state/types.py +11 -6
- ommlds/cli/sessions/chat/drivers/tools/configs.py +22 -0
- ommlds/cli/sessions/chat/drivers/tools/confirmation.py +44 -0
- ommlds/cli/sessions/chat/drivers/tools/errorhandling.py +39 -0
- ommlds/cli/sessions/chat/{tools → drivers/tools}/execution.py +3 -4
- ommlds/cli/sessions/chat/drivers/tools/fs/__init__.py +0 -0
- ommlds/cli/sessions/chat/drivers/tools/fs/configs.py +12 -0
- ommlds/cli/sessions/chat/drivers/tools/fs/inject.py +35 -0
- ommlds/cli/sessions/chat/drivers/tools/inject.py +83 -0
- ommlds/cli/sessions/chat/{tools → drivers/tools}/injection.py +20 -5
- ommlds/cli/sessions/chat/{tools → drivers/tools}/rendering.py +3 -3
- ommlds/cli/sessions/chat/drivers/tools/todo/__init__.py +0 -0
- ommlds/cli/sessions/chat/drivers/tools/todo/configs.py +12 -0
- ommlds/cli/sessions/chat/drivers/tools/todo/inject.py +31 -0
- ommlds/cli/sessions/chat/drivers/tools/weather/__init__.py +0 -0
- ommlds/cli/sessions/chat/drivers/tools/weather/configs.py +12 -0
- ommlds/cli/sessions/chat/drivers/tools/weather/inject.py +22 -0
- ommlds/cli/sessions/chat/{tools/weather.py → drivers/tools/weather/tools.py} +1 -1
- ommlds/cli/sessions/chat/drivers/types.py +31 -0
- ommlds/cli/sessions/chat/drivers/user/__init__.py +0 -0
- ommlds/cli/sessions/chat/drivers/user/configs.py +14 -0
- ommlds/cli/sessions/chat/drivers/user/inject.py +41 -0
- ommlds/cli/sessions/chat/facades/__init__.py +0 -0
- ommlds/cli/sessions/chat/facades/commands/__init__.py +0 -0
- ommlds/cli/sessions/chat/facades/commands/base.py +83 -0
- ommlds/cli/sessions/chat/facades/commands/configs.py +9 -0
- ommlds/cli/sessions/chat/facades/commands/inject.py +41 -0
- ommlds/cli/sessions/chat/facades/commands/injection.py +15 -0
- ommlds/cli/sessions/chat/facades/commands/manager.py +59 -0
- ommlds/cli/sessions/chat/facades/commands/simple.py +34 -0
- ommlds/cli/sessions/chat/facades/commands/types.py +13 -0
- ommlds/cli/sessions/chat/facades/configs.py +11 -0
- ommlds/cli/sessions/chat/facades/facade.py +26 -0
- ommlds/cli/sessions/chat/facades/inject.py +35 -0
- ommlds/cli/sessions/chat/facades/ui.py +34 -0
- ommlds/cli/sessions/chat/inject.py +10 -49
- ommlds/cli/sessions/chat/interfaces/__init__.py +0 -0
- ommlds/cli/sessions/chat/interfaces/bare/__init__.py +0 -0
- ommlds/cli/sessions/chat/interfaces/bare/configs.py +15 -0
- ommlds/cli/sessions/chat/interfaces/bare/inject.py +69 -0
- ommlds/cli/sessions/chat/interfaces/bare/interactive.py +49 -0
- ommlds/cli/sessions/chat/interfaces/bare/oneshot.py +21 -0
- ommlds/cli/sessions/chat/{tools/confirmation.py → interfaces/bare/tools.py} +3 -22
- ommlds/cli/sessions/chat/interfaces/base.py +13 -0
- ommlds/cli/sessions/chat/interfaces/configs.py +11 -0
- ommlds/cli/sessions/chat/interfaces/inject.py +29 -0
- ommlds/cli/sessions/chat/interfaces/textual/__init__.py +0 -0
- ommlds/cli/sessions/chat/interfaces/textual/app.py +429 -0
- ommlds/cli/sessions/chat/interfaces/textual/configs.py +11 -0
- ommlds/cli/sessions/chat/interfaces/textual/facades.py +19 -0
- ommlds/cli/sessions/chat/interfaces/textual/inject.py +111 -0
- ommlds/cli/sessions/chat/interfaces/textual/inputhistory.py +174 -0
- ommlds/cli/sessions/chat/interfaces/textual/interface.py +24 -0
- ommlds/cli/sessions/chat/interfaces/textual/styles/__init__.py +29 -0
- ommlds/cli/sessions/chat/interfaces/textual/styles/input.tcss +53 -0
- ommlds/cli/sessions/chat/interfaces/textual/styles/markdown.tcss +7 -0
- ommlds/cli/sessions/chat/interfaces/textual/styles/messages.tcss +167 -0
- ommlds/cli/sessions/chat/interfaces/textual/tools.py +38 -0
- ommlds/cli/sessions/chat/interfaces/textual/widgets/__init__.py +0 -0
- ommlds/cli/sessions/chat/interfaces/textual/widgets/input.py +70 -0
- ommlds/cli/sessions/chat/interfaces/textual/widgets/messages.py +207 -0
- ommlds/cli/sessions/chat/session.py +8 -13
- ommlds/cli/sessions/completion/configs.py +5 -6
- ommlds/cli/sessions/completion/inject.py +15 -2
- ommlds/cli/sessions/completion/session.py +10 -18
- ommlds/cli/sessions/configs.py +10 -0
- ommlds/cli/sessions/embedding/configs.py +5 -6
- ommlds/cli/sessions/embedding/inject.py +15 -2
- ommlds/cli/sessions/embedding/session.py +10 -18
- ommlds/cli/sessions/inject.py +15 -15
- ommlds/cli/state/storage.py +8 -2
- ommlds/minichain/__init__.py +217 -60
- ommlds/minichain/_dataclasses.py +20640 -0
- ommlds/minichain/_typedvalues.py +15 -8
- ommlds/minichain/backends/catalogs/base.py +20 -1
- ommlds/minichain/backends/catalogs/simple.py +2 -2
- ommlds/minichain/backends/catalogs/strings.py +13 -10
- ommlds/minichain/backends/impls/anthropic/chat.py +28 -5
- ommlds/minichain/backends/impls/anthropic/names.py +3 -3
- ommlds/minichain/backends/impls/anthropic/protocol.py +2 -2
- ommlds/minichain/backends/impls/anthropic/stream.py +23 -18
- ommlds/minichain/backends/impls/cerebras/__init__.py +0 -0
- ommlds/minichain/backends/impls/cerebras/chat.py +82 -0
- ommlds/minichain/backends/impls/cerebras/names.py +45 -0
- ommlds/minichain/backends/impls/cerebras/protocol.py +143 -0
- ommlds/minichain/backends/impls/cerebras/stream.py +114 -0
- ommlds/minichain/backends/impls/duckduckgo/search.py +5 -1
- ommlds/minichain/backends/impls/dummy/__init__.py +0 -0
- ommlds/minichain/backends/impls/dummy/chat.py +69 -0
- ommlds/minichain/backends/impls/google/chat.py +20 -84
- ommlds/minichain/backends/impls/google/names.py +6 -0
- ommlds/minichain/backends/impls/google/protocol.py +105 -0
- ommlds/minichain/backends/impls/google/search.py +10 -5
- ommlds/minichain/backends/impls/google/stream.py +64 -142
- ommlds/minichain/backends/impls/google/tools.py +2 -2
- ommlds/minichain/backends/impls/groq/__init__.py +0 -0
- ommlds/minichain/backends/impls/groq/chat.py +77 -0
- ommlds/minichain/backends/impls/groq/names.py +48 -0
- ommlds/minichain/backends/impls/groq/protocol.py +143 -0
- ommlds/minichain/backends/impls/groq/stream.py +114 -0
- ommlds/minichain/backends/impls/huggingface/repos.py +1 -5
- ommlds/minichain/backends/impls/llamacpp/chat.py +15 -3
- ommlds/minichain/backends/impls/llamacpp/completion.py +7 -3
- ommlds/minichain/backends/impls/llamacpp/stream.py +38 -19
- ommlds/minichain/backends/impls/mistral.py +9 -2
- ommlds/minichain/backends/impls/mlx/chat.py +100 -23
- ommlds/minichain/backends/impls/ollama/__init__.py +0 -0
- ommlds/minichain/backends/impls/ollama/chat.py +193 -0
- ommlds/minichain/backends/impls/ollama/protocol.py +144 -0
- ommlds/minichain/backends/impls/openai/chat.py +14 -7
- ommlds/minichain/backends/impls/openai/completion.py +9 -2
- ommlds/minichain/backends/impls/openai/embedding.py +9 -2
- ommlds/minichain/backends/impls/openai/format.py +117 -115
- ommlds/minichain/backends/impls/openai/names.py +33 -5
- ommlds/minichain/backends/impls/openai/stream.py +61 -70
- ommlds/minichain/backends/impls/sentencepiece/tokens.py +9 -6
- ommlds/minichain/backends/impls/tavily.py +66 -0
- ommlds/minichain/backends/impls/tinygrad/chat.py +17 -14
- ommlds/minichain/backends/impls/tokenizers/tokens.py +9 -6
- ommlds/minichain/backends/impls/transformers/sentence.py +5 -2
- ommlds/minichain/backends/impls/transformers/tokens.py +9 -6
- ommlds/minichain/backends/impls/transformers/transformers.py +139 -20
- ommlds/minichain/backends/strings/parsing.py +2 -2
- ommlds/minichain/backends/strings/resolving.py +7 -2
- ommlds/minichain/chat/choices/stream/__init__.py +0 -0
- ommlds/minichain/chat/{stream → choices/stream}/adapters.py +7 -7
- ommlds/minichain/chat/choices/stream/joining.py +31 -0
- ommlds/minichain/chat/choices/stream/services.py +45 -0
- ommlds/minichain/chat/choices/stream/types.py +43 -0
- ommlds/minichain/chat/content.py +42 -0
- ommlds/minichain/chat/messages.py +46 -42
- ommlds/minichain/chat/stream/_marshal.py +4 -4
- ommlds/minichain/chat/stream/joining.py +56 -43
- ommlds/minichain/chat/stream/services.py +15 -15
- ommlds/minichain/chat/stream/types.py +17 -24
- ommlds/minichain/chat/templating.py +3 -3
- ommlds/minichain/content/__init__.py +20 -3
- ommlds/minichain/content/_marshal.py +181 -55
- ommlds/minichain/content/code.py +26 -0
- ommlds/minichain/content/composite.py +28 -0
- ommlds/minichain/content/content.py +27 -0
- ommlds/minichain/content/dynamic.py +12 -0
- ommlds/minichain/content/emphasis.py +27 -0
- ommlds/minichain/content/images.py +2 -2
- ommlds/minichain/content/json.py +2 -2
- ommlds/minichain/content/link.py +13 -0
- ommlds/minichain/content/markdown.py +12 -0
- ommlds/minichain/content/metadata.py +10 -0
- ommlds/minichain/content/namespaces.py +8 -0
- ommlds/minichain/content/placeholders.py +10 -9
- ommlds/minichain/content/quote.py +26 -0
- ommlds/minichain/content/raw.py +49 -0
- ommlds/minichain/content/recursive.py +12 -0
- ommlds/minichain/content/resources.py +22 -0
- ommlds/minichain/content/section.py +26 -0
- ommlds/minichain/content/sequence.py +17 -3
- ommlds/minichain/content/standard.py +32 -0
- ommlds/minichain/content/tag.py +28 -0
- ommlds/minichain/content/templates.py +13 -0
- ommlds/minichain/content/text.py +2 -2
- ommlds/minichain/content/transform/__init__.py +0 -0
- ommlds/minichain/content/transform/json.py +55 -0
- ommlds/minichain/content/transform/markdown.py +8 -0
- ommlds/minichain/content/transform/materialize.py +59 -0
- ommlds/minichain/content/transform/metadata.py +16 -0
- ommlds/minichain/content/transform/namespaces.py +20 -0
- ommlds/minichain/content/transform/placeholders.py +60 -0
- ommlds/minichain/content/{prepare.py → transform/prepare.py} +10 -15
- ommlds/minichain/content/transform/recursive.py +54 -0
- ommlds/minichain/content/transform/resources.py +58 -0
- ommlds/minichain/content/transform/standard.py +43 -0
- ommlds/minichain/content/{transforms → transform}/stringify.py +1 -7
- ommlds/minichain/content/transform/strings.py +33 -0
- ommlds/minichain/content/transform/templates.py +25 -0
- ommlds/minichain/content/transform/types.py +18 -0
- ommlds/minichain/content/transform/visitors.py +38 -0
- ommlds/minichain/content/visitors.py +218 -0
- ommlds/minichain/http/__init__.py +0 -0
- ommlds/minichain/http/stream.py +195 -0
- ommlds/minichain/lib/fs/tools/read.py +1 -1
- ommlds/minichain/lib/fs/tools/recursivels/rendering.py +1 -1
- ommlds/minichain/lib/fs/tools/recursivels/running.py +1 -1
- ommlds/minichain/lib/todo/tools/write.py +2 -1
- ommlds/minichain/lib/todo/types.py +1 -1
- ommlds/minichain/llms/types.py +4 -0
- ommlds/minichain/metadata.py +56 -2
- ommlds/minichain/models/configs.py +2 -2
- ommlds/minichain/models/names.py +2 -0
- ommlds/minichain/registries/globals.py +18 -4
- ommlds/minichain/resources.py +49 -3
- ommlds/minichain/search.py +1 -1
- ommlds/minichain/services/README.md +154 -0
- ommlds/minichain/services/__init__.py +6 -2
- ommlds/minichain/services/_marshal.py +46 -10
- ommlds/minichain/services/_origclasses.py +11 -0
- ommlds/minichain/services/_typedvalues.py +8 -3
- ommlds/minichain/services/requests.py +73 -3
- ommlds/minichain/services/responses.py +73 -3
- ommlds/minichain/services/services.py +9 -0
- ommlds/minichain/standard.py +8 -0
- ommlds/minichain/stream/services.py +43 -17
- ommlds/minichain/text/applypatch.py +2 -1
- ommlds/minichain/text/toolparsing/llamacpp/types.py +1 -1
- ommlds/minichain/tokens/specials.py +1 -1
- ommlds/minichain/tools/execution/catalog.py +2 -2
- ommlds/minichain/tools/execution/errorhandling.py +36 -0
- ommlds/minichain/tools/execution/errors.py +2 -2
- ommlds/minichain/tools/execution/executors.py +1 -1
- ommlds/minichain/tools/fns.py +1 -1
- ommlds/minichain/tools/jsonschema.py +2 -2
- ommlds/minichain/tools/reflect.py +11 -7
- ommlds/minichain/tools/types.py +16 -19
- ommlds/minichain/vectors/_marshal.py +1 -1
- ommlds/minichain/vectors/embeddings.py +1 -1
- ommlds/minichain/wrappers/__init__.py +7 -0
- ommlds/minichain/wrappers/firstinwins.py +144 -0
- ommlds/minichain/wrappers/instrument.py +146 -0
- ommlds/minichain/wrappers/retry.py +168 -0
- ommlds/minichain/wrappers/services.py +98 -0
- ommlds/minichain/wrappers/stream.py +57 -0
- ommlds/nanochat/LICENSE +21 -0
- ommlds/nanochat/__init__.py +0 -0
- ommlds/nanochat/rustbpe/LICENSE +21 -0
- ommlds/nanochat/rustbpe/README.md +10 -0
- ommlds/nanochat/tokenizers.py +440 -0
- ommlds/specs/__init__.py +0 -0
- ommlds/specs/mcp/__init__.py +0 -0
- ommlds/specs/mcp/_marshal.py +23 -0
- ommlds/specs/mcp/clients.py +146 -0
- ommlds/specs/mcp/protocol.py +369 -0
- ommlds/tools/git.py +84 -64
- ommlds/tools/ocr.py +1 -1
- ommlds/wiki/analyze.py +2 -2
- ommlds/wiki/models.py +4 -4
- ommlds/wiki/text/mfh.py +9 -9
- ommlds/wiki/utils/xml.py +5 -5
- {ommlds-0.0.0.dev466.dist-info → ommlds-0.0.0.dev512.dist-info}/METADATA +28 -21
- ommlds-0.0.0.dev512.dist-info/RECORD +534 -0
- {ommlds-0.0.0.dev466.dist-info → ommlds-0.0.0.dev512.dist-info}/WHEEL +1 -1
- ommlds/cli/backends/standard.py +0 -20
- ommlds/cli/sessions/chat/backends/catalog.py +0 -56
- ommlds/cli/sessions/chat/backends/inject.py +0 -37
- ommlds/cli/sessions/chat/chat/state/inject.py +0 -40
- ommlds/cli/sessions/chat/chat/user/inject.py +0 -61
- ommlds/cli/sessions/chat/chat/user/interactive.py +0 -29
- ommlds/cli/sessions/chat/chat/user/oneshot.py +0 -25
- ommlds/cli/sessions/chat/chat/user/types.py +0 -15
- ommlds/cli/sessions/chat/driver.py +0 -43
- ommlds/cli/sessions/chat/tools/inject.py +0 -145
- ommlds/minichain/backends/impls/openai/format2.py +0 -210
- ommlds/minichain/content/materialize.py +0 -196
- ommlds/minichain/content/simple.py +0 -47
- ommlds/minichain/content/transforms/base.py +0 -46
- ommlds/minichain/content/transforms/interleave.py +0 -70
- ommlds/minichain/content/transforms/squeeze.py +0 -72
- ommlds/minichain/content/transforms/strings.py +0 -24
- ommlds/minichain/content/types.py +0 -43
- ommlds/minichain/stream/wrap.py +0 -62
- ommlds-0.0.0.dev466.dist-info/RECORD +0 -376
- /ommlds/{cli/sessions/chat/backends → backends/huggingface}/__init__.py +0 -0
- /ommlds/cli/{sessions/chat/chat → content}/__init__.py +0 -0
- /ommlds/cli/{sessions/chat/chat/ai → inputs}/__init__.py +0 -0
- /ommlds/cli/{sessions/chat/chat/state → rendering}/__init__.py +0 -0
- /ommlds/cli/sessions/chat/{chat/user → drivers}/__init__.py +0 -0
- /ommlds/cli/sessions/chat/{content → drivers/ai}/__init__.py +0 -0
- /ommlds/cli/sessions/chat/{chat → drivers}/ai/injection.py +0 -0
- /ommlds/cli/sessions/chat/{phases → drivers/events}/__init__.py +0 -0
- /ommlds/cli/sessions/chat/{rendering → drivers/phases}/__init__.py +0 -0
- /ommlds/cli/sessions/chat/{phases → drivers/phases}/inject.py +0 -0
- /ommlds/cli/sessions/chat/{phases → drivers/phases}/injection.py +0 -0
- /ommlds/cli/sessions/chat/{phases → drivers/phases}/manager.py +0 -0
- /ommlds/cli/sessions/chat/{phases → drivers/phases}/types.py +0 -0
- /ommlds/cli/sessions/chat/{tools → drivers/state}/__init__.py +0 -0
- /ommlds/{minichain/content/transforms → cli/sessions/chat/drivers/tools}/__init__.py +0 -0
- {ommlds-0.0.0.dev466.dist-info → ommlds-0.0.0.dev512.dist-info}/entry_points.txt +0 -0
- {ommlds-0.0.0.dev466.dist-info → ommlds-0.0.0.dev512.dist-info}/licenses/LICENSE +0 -0
- {ommlds-0.0.0.dev466.dist-info → ommlds-0.0.0.dev512.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
|
|
3
|
+
from omlish import check
|
|
4
|
+
|
|
5
|
+
from .....backends.ollama import protocol as pt
|
|
6
|
+
from ....chat.choices.services import ChatChoicesResponse
|
|
7
|
+
from ....chat.choices.stream.types import AiChoiceDeltas
|
|
8
|
+
from ....chat.choices.types import AiChoice
|
|
9
|
+
from ....chat.messages import AiMessage
|
|
10
|
+
from ....chat.messages import AnyAiMessage
|
|
11
|
+
from ....chat.messages import Chat
|
|
12
|
+
from ....chat.messages import SystemMessage
|
|
13
|
+
from ....chat.messages import ToolUseMessage
|
|
14
|
+
from ....chat.messages import ToolUseResultMessage
|
|
15
|
+
from ....chat.messages import UserMessage
|
|
16
|
+
from ....chat.stream.types import AiDelta
|
|
17
|
+
from ....chat.stream.types import ContentAiDelta
|
|
18
|
+
from ....chat.stream.types import ToolUseAiDelta
|
|
19
|
+
from ....chat.tools.types import Tool
|
|
20
|
+
from ....content.transform.prepare import prepare_content_str
|
|
21
|
+
from ....tools.jsonschema import build_tool_spec_params_json_schema
|
|
22
|
+
from ....tools.types import ToolUse
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
##
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def build_ol_request_messages(chat: Chat) -> list[pt.Message]:
|
|
29
|
+
ol_msgs: list[pt.Message] = []
|
|
30
|
+
|
|
31
|
+
for _, g in itertools.groupby(chat, lambda mc_m: isinstance(mc_m, AnyAiMessage)):
|
|
32
|
+
mc_msgs = list(g)
|
|
33
|
+
|
|
34
|
+
if isinstance(mc_msgs[0], AnyAiMessage):
|
|
35
|
+
tups: list[tuple[AiMessage | None, list[ToolUseMessage]]] = []
|
|
36
|
+
for mc_msg in mc_msgs:
|
|
37
|
+
if isinstance(mc_msg, AiMessage):
|
|
38
|
+
tups.append((mc_msg, []))
|
|
39
|
+
|
|
40
|
+
elif isinstance(mc_msg, ToolUseMessage):
|
|
41
|
+
if not tups:
|
|
42
|
+
tups.append((None, []))
|
|
43
|
+
tups[-1][1].append(mc_msg)
|
|
44
|
+
|
|
45
|
+
else:
|
|
46
|
+
raise TypeError(mc_msg)
|
|
47
|
+
|
|
48
|
+
for mc_ai_msg, mc_tu_msgs in tups:
|
|
49
|
+
ol_msgs.append(pt.Message(
|
|
50
|
+
role='assistant',
|
|
51
|
+
content=check.isinstance(mc_ai_msg.c, str) if mc_ai_msg is not None else None,
|
|
52
|
+
tool_calls=[
|
|
53
|
+
pt.Message.ToolCall(
|
|
54
|
+
function=pt.Message.ToolCall.Function(
|
|
55
|
+
name=mc_tu_msg.tu.name,
|
|
56
|
+
arguments=mc_tu_msg.tu.args,
|
|
57
|
+
),
|
|
58
|
+
id=check.not_none(mc_tu_msg.tu.id),
|
|
59
|
+
)
|
|
60
|
+
for mc_tu_msg in mc_tu_msgs
|
|
61
|
+
] if mc_tu_msgs else None,
|
|
62
|
+
))
|
|
63
|
+
|
|
64
|
+
else:
|
|
65
|
+
for mc_msg in mc_msgs:
|
|
66
|
+
if isinstance(mc_msg, SystemMessage):
|
|
67
|
+
ol_msgs.append(pt.Message(
|
|
68
|
+
role='system',
|
|
69
|
+
content=check.isinstance(mc_msg.c, str),
|
|
70
|
+
))
|
|
71
|
+
|
|
72
|
+
elif isinstance(mc_msg, UserMessage):
|
|
73
|
+
ol_msgs.append(pt.Message(
|
|
74
|
+
role='user',
|
|
75
|
+
content=check.isinstance(mc_msg.c, str),
|
|
76
|
+
))
|
|
77
|
+
|
|
78
|
+
elif isinstance(mc_msg, ToolUseResultMessage):
|
|
79
|
+
ol_msgs.append(pt.Message(
|
|
80
|
+
role='tool',
|
|
81
|
+
tool_name=mc_msg.tur.name,
|
|
82
|
+
content=check.isinstance(mc_msg.tur.c, str),
|
|
83
|
+
))
|
|
84
|
+
|
|
85
|
+
else:
|
|
86
|
+
raise TypeError(mc_msg)
|
|
87
|
+
|
|
88
|
+
return ol_msgs
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def build_ol_request_tool(t: Tool) -> pt.Tool:
|
|
92
|
+
return pt.Tool(
|
|
93
|
+
function=pt.Tool.Function(
|
|
94
|
+
name=check.not_none(t.spec.name),
|
|
95
|
+
description=prepare_content_str(t.spec.desc) if t.spec.desc is not None else None,
|
|
96
|
+
parameters=build_tool_spec_params_json_schema(t.spec),
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def build_mc_choices_response(ol_resp: pt.ChatResponse) -> ChatChoicesResponse:
|
|
102
|
+
ol_msg = ol_resp.message
|
|
103
|
+
|
|
104
|
+
lst: list[AnyAiMessage] = []
|
|
105
|
+
|
|
106
|
+
if ol_msg.role in (None, 'assistant'):
|
|
107
|
+
if ol_msg.content is not None:
|
|
108
|
+
lst.append(AiMessage(
|
|
109
|
+
check.isinstance(ol_msg.content, str),
|
|
110
|
+
))
|
|
111
|
+
|
|
112
|
+
for ol_tc in ol_msg.tool_calls or []:
|
|
113
|
+
lst.append(ToolUseMessage(ToolUse(
|
|
114
|
+
id=ol_tc.id,
|
|
115
|
+
name=ol_tc.function.name,
|
|
116
|
+
args=ol_tc.function.arguments,
|
|
117
|
+
)))
|
|
118
|
+
|
|
119
|
+
else:
|
|
120
|
+
raise ValueError(ol_msg)
|
|
121
|
+
|
|
122
|
+
return ChatChoicesResponse([AiChoice(lst)])
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def build_mc_ai_choice_deltas(ol_resp: pt.ChatResponse) -> AiChoiceDeltas:
|
|
126
|
+
ol_msg = ol_resp.message
|
|
127
|
+
|
|
128
|
+
if ol_msg.role in (None, 'assistant'):
|
|
129
|
+
lst: list[AiDelta] = []
|
|
130
|
+
|
|
131
|
+
if ol_msg.content is not None:
|
|
132
|
+
lst.append(ContentAiDelta(ol_msg.content))
|
|
133
|
+
|
|
134
|
+
for tc in ol_msg.tool_calls or []:
|
|
135
|
+
lst.append(ToolUseAiDelta(
|
|
136
|
+
id=tc.id,
|
|
137
|
+
name=check.not_none(tc.function.name),
|
|
138
|
+
args=tc.function.arguments,
|
|
139
|
+
))
|
|
140
|
+
|
|
141
|
+
return AiChoiceDeltas(lst)
|
|
142
|
+
|
|
143
|
+
else:
|
|
144
|
+
raise ValueError(ol_msg)
|
|
@@ -26,9 +26,9 @@ from ....chat.choices.services import static_check_is_chat_choices_service
|
|
|
26
26
|
from ....models.configs import ModelName
|
|
27
27
|
from ....standard import ApiKey
|
|
28
28
|
from ....standard import DefaultOptions
|
|
29
|
-
from .
|
|
30
|
-
from .
|
|
31
|
-
from .names import
|
|
29
|
+
from .format import OpenaiChatRequestHandler
|
|
30
|
+
from .format import build_mc_choices_response
|
|
31
|
+
from .names import CHAT_MODEL_NAMES
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
##
|
|
@@ -40,11 +40,17 @@ from .names import MODEL_NAMES
|
|
|
40
40
|
# )
|
|
41
41
|
@static_check_is_chat_choices_service
|
|
42
42
|
class OpenaiChatChoicesService:
|
|
43
|
-
DEFAULT_MODEL_NAME: ta.ClassVar[ModelName] = ModelName(check.not_none(
|
|
43
|
+
DEFAULT_MODEL_NAME: ta.ClassVar[ModelName] = ModelName(check.not_none(CHAT_MODEL_NAMES.default))
|
|
44
44
|
|
|
45
|
-
def __init__(
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
*configs: ApiKey | ModelName | DefaultOptions,
|
|
48
|
+
http_client: http.AsyncHttpClient | None = None,
|
|
49
|
+
) -> None:
|
|
46
50
|
super().__init__()
|
|
47
51
|
|
|
52
|
+
self._http_client = http_client
|
|
53
|
+
|
|
48
54
|
with tv.consume(*configs) as cc:
|
|
49
55
|
self._model_name = cc.pop(self.DEFAULT_MODEL_NAME)
|
|
50
56
|
self._api_key = ApiKey.pop_secret(cc, env='OPENAI_API_KEY')
|
|
@@ -60,7 +66,7 @@ class OpenaiChatChoicesService:
|
|
|
60
66
|
*request.options,
|
|
61
67
|
override=True,
|
|
62
68
|
),
|
|
63
|
-
model=
|
|
69
|
+
model=CHAT_MODEL_NAMES.resolve(self._model_name.v),
|
|
64
70
|
mandatory_kwargs=dict(
|
|
65
71
|
stream=False,
|
|
66
72
|
),
|
|
@@ -68,13 +74,14 @@ class OpenaiChatChoicesService:
|
|
|
68
74
|
|
|
69
75
|
raw_request = msh.marshal(rh.oai_request())
|
|
70
76
|
|
|
71
|
-
http_response = http.
|
|
77
|
+
http_response = await http.async_request(
|
|
72
78
|
'https://api.openai.com/v1/chat/completions',
|
|
73
79
|
headers={
|
|
74
80
|
http.consts.HEADER_CONTENT_TYPE: http.consts.CONTENT_TYPE_JSON,
|
|
75
81
|
http.consts.HEADER_AUTH: http.consts.format_bearer_auth_header(check.not_none(self._api_key).reveal()),
|
|
76
82
|
},
|
|
77
83
|
data=json.dumps(raw_request).encode('utf-8'),
|
|
84
|
+
client=self._http_client,
|
|
78
85
|
)
|
|
79
86
|
|
|
80
87
|
raw_response = json.loads(check.not_none(http_response.data).decode('utf-8'))
|
|
@@ -23,9 +23,15 @@ from ....standard import ApiKey
|
|
|
23
23
|
class OpenaiCompletionService:
|
|
24
24
|
DEFAULT_MODEL_NAME: ta.ClassVar[str] = 'gpt-3.5-turbo-instruct'
|
|
25
25
|
|
|
26
|
-
def __init__(
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
*configs: Config,
|
|
29
|
+
http_client: http.AsyncHttpClient | None = None,
|
|
30
|
+
) -> None:
|
|
27
31
|
super().__init__()
|
|
28
32
|
|
|
33
|
+
self._http_client = http_client
|
|
34
|
+
|
|
29
35
|
with tv.consume(*configs) as cc:
|
|
30
36
|
self._api_key = ApiKey.pop_secret(cc, env='OPENAI_API_KEY')
|
|
31
37
|
|
|
@@ -41,13 +47,14 @@ class OpenaiCompletionService:
|
|
|
41
47
|
stream=False,
|
|
42
48
|
)
|
|
43
49
|
|
|
44
|
-
raw_response = http.
|
|
50
|
+
raw_response = await http.async_request(
|
|
45
51
|
'https://api.openai.com/v1/completions',
|
|
46
52
|
headers={
|
|
47
53
|
http.consts.HEADER_CONTENT_TYPE: http.consts.CONTENT_TYPE_JSON,
|
|
48
54
|
http.consts.HEADER_AUTH: http.consts.format_bearer_auth_header(check.not_none(self._api_key).reveal()),
|
|
49
55
|
},
|
|
50
56
|
data=json.dumps(raw_request).encode('utf-8'),
|
|
57
|
+
client=self._http_client,
|
|
51
58
|
)
|
|
52
59
|
|
|
53
60
|
response = json.loads(check.not_none(raw_response.data).decode('utf-8'))
|
|
@@ -22,9 +22,15 @@ from ....vectors.types import Vector
|
|
|
22
22
|
class OpenaiEmbeddingService:
|
|
23
23
|
model = 'text-embedding-3-small'
|
|
24
24
|
|
|
25
|
-
def __init__(
|
|
25
|
+
def __init__(
|
|
26
|
+
self,
|
|
27
|
+
*configs: Config,
|
|
28
|
+
http_client: http.AsyncHttpClient | None = None,
|
|
29
|
+
) -> None:
|
|
26
30
|
super().__init__()
|
|
27
31
|
|
|
32
|
+
self._http_client = http_client
|
|
33
|
+
|
|
28
34
|
with tv.consume(*configs) as cc:
|
|
29
35
|
self._api_key = ApiKey.pop_secret(cc, env='OPENAI_API_KEY')
|
|
30
36
|
|
|
@@ -34,13 +40,14 @@ class OpenaiEmbeddingService:
|
|
|
34
40
|
input=check.isinstance(request.v, str),
|
|
35
41
|
)
|
|
36
42
|
|
|
37
|
-
raw_response = http.
|
|
43
|
+
raw_response = await http.async_request(
|
|
38
44
|
'https://api.openai.com/v1/embeddings',
|
|
39
45
|
headers={
|
|
40
46
|
http.consts.HEADER_CONTENT_TYPE: http.consts.CONTENT_TYPE_JSON,
|
|
41
47
|
http.consts.HEADER_AUTH: http.consts.format_bearer_auth_header(check.not_none(self._api_key).reveal()),
|
|
42
48
|
},
|
|
43
49
|
data=json.dumps(raw_request).encode('utf-8'),
|
|
50
|
+
client=self._http_client,
|
|
44
51
|
)
|
|
45
52
|
|
|
46
53
|
response = json.loads(check.not_none(raw_response.data).decode('utf-8'))
|
|
@@ -2,33 +2,33 @@ import typing as ta
|
|
|
2
2
|
|
|
3
3
|
from omlish import cached
|
|
4
4
|
from omlish import check
|
|
5
|
-
from omlish import lang
|
|
6
5
|
from omlish import typedvalues as tv
|
|
7
6
|
from omlish.formats import json
|
|
8
7
|
|
|
8
|
+
from .....backends.openai import protocol as pt
|
|
9
9
|
from ....chat.choices.services import ChatChoicesResponse
|
|
10
10
|
from ....chat.choices.types import AiChoice
|
|
11
|
+
from ....chat.choices.types import AiChoices
|
|
11
12
|
from ....chat.choices.types import ChatChoicesOptions
|
|
12
|
-
from ....chat.messages import AiChat
|
|
13
13
|
from ....chat.messages import AiMessage
|
|
14
14
|
from ....chat.messages import AnyAiMessage
|
|
15
15
|
from ....chat.messages import Chat
|
|
16
|
-
from ....chat.messages import Message
|
|
17
16
|
from ....chat.messages import SystemMessage
|
|
18
17
|
from ....chat.messages import ToolUseMessage
|
|
19
18
|
from ....chat.messages import ToolUseResultMessage
|
|
20
19
|
from ....chat.messages import UserMessage
|
|
21
|
-
from ....chat.stream.types import
|
|
22
|
-
from ....chat.stream.types import
|
|
23
|
-
from ....chat.stream.types import
|
|
20
|
+
from ....chat.stream.types import AiDelta
|
|
21
|
+
from ....chat.stream.types import ContentAiDelta
|
|
22
|
+
from ....chat.stream.types import PartialToolUseAiDelta
|
|
24
23
|
from ....chat.tools.types import Tool
|
|
25
24
|
from ....content.json import JsonContent
|
|
26
|
-
from ....content.prepare import prepare_content_str
|
|
25
|
+
from ....content.transform.prepare import prepare_content_str
|
|
26
|
+
from ....llms.types import MaxCompletionTokens
|
|
27
27
|
from ....llms.types import MaxTokens
|
|
28
28
|
from ....llms.types import Temperature
|
|
29
29
|
from ....llms.types import TokenUsage
|
|
30
30
|
from ....llms.types import TokenUsageOutput
|
|
31
|
-
from ....tools.jsonschema import
|
|
31
|
+
from ....tools.jsonschema import build_tool_spec_params_json_schema
|
|
32
32
|
from ....tools.types import ToolSpec
|
|
33
33
|
from ....tools.types import ToolUse
|
|
34
34
|
from ....types import Option
|
|
@@ -37,61 +37,116 @@ from ....types import Option
|
|
|
37
37
|
##
|
|
38
38
|
|
|
39
39
|
|
|
40
|
-
def
|
|
41
|
-
|
|
40
|
+
def build_oai_request_msgs(mc_chat: Chat) -> ta.Sequence[pt.ChatCompletionMessage]:
|
|
41
|
+
oai_msgs: list[pt.ChatCompletionMessage] = []
|
|
42
42
|
|
|
43
|
-
for
|
|
44
|
-
if isinstance(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
content=m.c,
|
|
43
|
+
for mc_msg in mc_chat:
|
|
44
|
+
if isinstance(mc_msg, SystemMessage):
|
|
45
|
+
oai_msgs.append(pt.SystemChatCompletionMessage(
|
|
46
|
+
content=check.isinstance(mc_msg.c, str),
|
|
48
47
|
))
|
|
49
48
|
|
|
50
|
-
elif isinstance(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
content=check.isinstance(m.c, (str, None)),
|
|
49
|
+
elif isinstance(mc_msg, AiMessage):
|
|
50
|
+
oai_msgs.append(pt.AssistantChatCompletionMessage(
|
|
51
|
+
content=check.isinstance(mc_msg.c, (str, None)),
|
|
54
52
|
))
|
|
55
53
|
|
|
56
|
-
elif isinstance(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
arguments=check.not_none(m.tu.raw_args),
|
|
64
|
-
name=m.tu.name,
|
|
65
|
-
),
|
|
66
|
-
type='function',
|
|
54
|
+
elif isinstance(mc_msg, ToolUseMessage):
|
|
55
|
+
oai_msgs.append(pt.AssistantChatCompletionMessage(
|
|
56
|
+
tool_calls=[pt.AssistantChatCompletionMessage.ToolCall(
|
|
57
|
+
id=check.not_none(mc_msg.tu.id),
|
|
58
|
+
function=pt.AssistantChatCompletionMessage.ToolCall.Function(
|
|
59
|
+
arguments=check.not_none(mc_msg.tu.raw_args),
|
|
60
|
+
name=mc_msg.tu.name,
|
|
67
61
|
),
|
|
68
|
-
],
|
|
62
|
+
)],
|
|
69
63
|
))
|
|
70
64
|
|
|
71
|
-
elif isinstance(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
content=prepare_content_str(m.c),
|
|
65
|
+
elif isinstance(mc_msg, UserMessage):
|
|
66
|
+
oai_msgs.append(pt.UserChatCompletionMessage(
|
|
67
|
+
content=prepare_content_str(mc_msg.c),
|
|
75
68
|
))
|
|
76
69
|
|
|
77
|
-
elif isinstance(
|
|
70
|
+
elif isinstance(mc_msg, ToolUseResultMessage):
|
|
78
71
|
tc: str
|
|
79
|
-
if isinstance(
|
|
80
|
-
tc =
|
|
81
|
-
elif isinstance(
|
|
82
|
-
tc = json.dumps_compact(
|
|
72
|
+
if isinstance(mc_msg.tur.c, str):
|
|
73
|
+
tc = mc_msg.tur.c
|
|
74
|
+
elif isinstance(mc_msg.tur.c, JsonContent):
|
|
75
|
+
tc = json.dumps_compact(mc_msg.tur.c)
|
|
83
76
|
else:
|
|
84
|
-
raise TypeError(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
tool_call_id=m.tur.id,
|
|
77
|
+
raise TypeError(mc_msg.tur.c)
|
|
78
|
+
oai_msgs.append(pt.ToolChatCompletionMessage(
|
|
79
|
+
tool_call_id=check.not_none(mc_msg.tur.id),
|
|
88
80
|
content=tc,
|
|
89
81
|
))
|
|
90
82
|
|
|
91
83
|
else:
|
|
92
|
-
raise TypeError(
|
|
84
|
+
raise TypeError(mc_msg)
|
|
93
85
|
|
|
94
|
-
return
|
|
86
|
+
return oai_msgs
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
#
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def build_mc_ai_choice(oai_choice: pt.ChatCompletionResponseChoice) -> AiChoice:
|
|
93
|
+
cur: list[AnyAiMessage] = []
|
|
94
|
+
|
|
95
|
+
oai_msg = oai_choice.message
|
|
96
|
+
|
|
97
|
+
if (oai_c := oai_msg.content) is not None:
|
|
98
|
+
cur.append(AiMessage(check.isinstance(oai_c, str)))
|
|
99
|
+
|
|
100
|
+
for oai_tc in oai_msg.tool_calls or []:
|
|
101
|
+
cur.append(ToolUseMessage(ToolUse(
|
|
102
|
+
id=oai_tc.id,
|
|
103
|
+
name=oai_tc.function.name,
|
|
104
|
+
args=json.loads(oai_tc.function.arguments or '{}'),
|
|
105
|
+
raw_args=oai_tc.function.arguments,
|
|
106
|
+
)))
|
|
107
|
+
|
|
108
|
+
return AiChoice(cur)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def build_mc_ai_choices(oai_resp: pt.ChatCompletionResponse) -> AiChoices:
|
|
112
|
+
return [
|
|
113
|
+
build_mc_ai_choice(oai_choice)
|
|
114
|
+
for oai_choice in oai_resp.choices
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def build_mc_choices_response(oai_resp: pt.ChatCompletionResponse) -> ChatChoicesResponse:
|
|
119
|
+
return ChatChoicesResponse(
|
|
120
|
+
build_mc_ai_choices(oai_resp),
|
|
121
|
+
|
|
122
|
+
tv.TypedValues(
|
|
123
|
+
*([TokenUsageOutput(TokenUsage(
|
|
124
|
+
input=tu.prompt_tokens,
|
|
125
|
+
output=tu.completion_tokens,
|
|
126
|
+
total=tu.total_tokens,
|
|
127
|
+
))] if (tu := oai_resp.usage) is not None else []),
|
|
128
|
+
),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def build_mc_ai_delta(delta: pt.ChatCompletionChunkChoiceDelta) -> AiDelta:
|
|
133
|
+
if delta.content is not None:
|
|
134
|
+
check.state(not delta.tool_calls)
|
|
135
|
+
return ContentAiDelta(delta.content)
|
|
136
|
+
|
|
137
|
+
elif delta.tool_calls is not None:
|
|
138
|
+
check.state(delta.content is None)
|
|
139
|
+
tc = check.single(delta.tool_calls)
|
|
140
|
+
tc_fn = check.not_none(tc.function)
|
|
141
|
+
return PartialToolUseAiDelta(
|
|
142
|
+
id=tc.id,
|
|
143
|
+
name=tc_fn.name,
|
|
144
|
+
raw_args=tc_fn.arguments,
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
else:
|
|
148
|
+
# FIXME: no
|
|
149
|
+
return ContentAiDelta('')
|
|
95
150
|
|
|
96
151
|
|
|
97
152
|
##
|
|
@@ -112,22 +167,15 @@ class OpenaiChatRequestHandler:
|
|
|
112
167
|
self._model = model
|
|
113
168
|
self._mandatory_kwargs = mandatory_kwargs
|
|
114
169
|
|
|
115
|
-
ROLES_MAP: ta.ClassVar[ta.Mapping[type[Message], str]] = {
|
|
116
|
-
SystemMessage: 'system',
|
|
117
|
-
UserMessage: 'user',
|
|
118
|
-
AiMessage: 'assistant',
|
|
119
|
-
ToolUseMessage: 'assistant',
|
|
120
|
-
ToolUseResultMessage: 'tool',
|
|
121
|
-
}
|
|
122
|
-
|
|
123
170
|
DEFAULT_OPTIONS: ta.ClassVar[tv.TypedValues[Option]] = tv.TypedValues[Option](
|
|
124
|
-
Temperature(0.),
|
|
125
|
-
MaxTokens(1024),
|
|
171
|
+
# Temperature(0.),
|
|
172
|
+
# MaxTokens(1024),
|
|
126
173
|
)
|
|
127
174
|
|
|
128
175
|
_OPTION_KWARG_NAMES_MAP: ta.ClassVar[ta.Mapping[str, type[ChatChoicesOptions]]] = dict(
|
|
129
176
|
temperature=Temperature,
|
|
130
177
|
max_tokens=MaxTokens,
|
|
178
|
+
max_completion_tokens=MaxCompletionTokens,
|
|
131
179
|
)
|
|
132
180
|
|
|
133
181
|
class _ProcessedOptions(ta.NamedTuple):
|
|
@@ -137,8 +185,8 @@ class OpenaiChatRequestHandler:
|
|
|
137
185
|
@cached.function
|
|
138
186
|
def _process_options(self) -> _ProcessedOptions:
|
|
139
187
|
kwargs: dict = dict(
|
|
140
|
-
temperature=0,
|
|
141
|
-
max_tokens=1024,
|
|
188
|
+
# temperature=0,
|
|
189
|
+
# max_tokens=1024,
|
|
142
190
|
)
|
|
143
191
|
|
|
144
192
|
tools_by_name: dict[str, ToolSpec] = {}
|
|
@@ -162,72 +210,26 @@ class OpenaiChatRequestHandler:
|
|
|
162
210
|
)
|
|
163
211
|
|
|
164
212
|
@cached.function
|
|
165
|
-
def
|
|
213
|
+
def oai_request(self) -> pt.ChatCompletionRequest:
|
|
166
214
|
po = self._process_options()
|
|
167
215
|
|
|
168
|
-
tools = [
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
216
|
+
tools: list[pt.ChatCompletionRequestTool] = [
|
|
217
|
+
pt.ChatCompletionRequestTool(
|
|
218
|
+
function=pt.ChatCompletionRequestTool.Function(
|
|
219
|
+
name=check.not_none(ts.name),
|
|
220
|
+
description=prepare_content_str(ts.desc) if ts.desc is not None else None,
|
|
221
|
+
parameters=build_tool_spec_params_json_schema(ts),
|
|
222
|
+
),
|
|
172
223
|
)
|
|
173
224
|
for ts in po.tools_by_name.values()
|
|
174
225
|
]
|
|
175
226
|
|
|
176
|
-
return
|
|
227
|
+
return pt.ChatCompletionRequest(
|
|
177
228
|
model=self._model,
|
|
178
|
-
messages=
|
|
229
|
+
messages=build_oai_request_msgs(self._chat),
|
|
179
230
|
top_p=1,
|
|
180
|
-
|
|
231
|
+
tools=tools or None,
|
|
181
232
|
frequency_penalty=0.0,
|
|
182
233
|
presence_penalty=0.0,
|
|
183
234
|
**po.kwargs,
|
|
184
235
|
)
|
|
185
|
-
|
|
186
|
-
def build_ai_chat(self, message: ta.Mapping[str, ta.Any]) -> AiChat:
|
|
187
|
-
out: list[AnyAiMessage] = []
|
|
188
|
-
if (c := message.get('content')) is not None:
|
|
189
|
-
out.append(AiMessage(c))
|
|
190
|
-
for tc in message.get('tool_calls', []):
|
|
191
|
-
out.append(ToolUseMessage(
|
|
192
|
-
ToolUse(
|
|
193
|
-
id=tc['id'],
|
|
194
|
-
name=tc['function']['name'],
|
|
195
|
-
args=json.loads(tc['function']['arguments'] or '{}'),
|
|
196
|
-
raw_args=tc['function']['arguments'],
|
|
197
|
-
),
|
|
198
|
-
))
|
|
199
|
-
return out
|
|
200
|
-
|
|
201
|
-
def build_response(self, raw_response: ta.Mapping[str, ta.Any]) -> ChatChoicesResponse:
|
|
202
|
-
return ChatChoicesResponse(
|
|
203
|
-
[
|
|
204
|
-
AiChoice(self.build_ai_chat(choice['message']))
|
|
205
|
-
for choice in raw_response['choices']
|
|
206
|
-
],
|
|
207
|
-
|
|
208
|
-
tv.TypedValues(
|
|
209
|
-
*([TokenUsageOutput(TokenUsage(
|
|
210
|
-
input=tu['prompt_tokens'],
|
|
211
|
-
output=tu['completion_tokens'],
|
|
212
|
-
total=tu['total_tokens'],
|
|
213
|
-
))] if (tu := raw_response.get('usage')) is not None else []),
|
|
214
|
-
),
|
|
215
|
-
)
|
|
216
|
-
|
|
217
|
-
def build_ai_choice_delta(self, delta: ta.Mapping[str, ta.Any]) -> AiChoiceDelta:
|
|
218
|
-
if (c := delta.get('content')) is not None:
|
|
219
|
-
check.state(not delta.get('tool_calls'))
|
|
220
|
-
return ContentAiChoiceDelta(c)
|
|
221
|
-
|
|
222
|
-
elif (tcs := delta.get('tool_calls')) is not None: # noqa
|
|
223
|
-
check.state(delta.get('content') is None)
|
|
224
|
-
tc = check.single(tcs)
|
|
225
|
-
tc_fn = tc['function']
|
|
226
|
-
return PartialToolUseAiChoiceDelta(
|
|
227
|
-
id=tc.get('id'),
|
|
228
|
-
name=tc_fn.get('name'),
|
|
229
|
-
raw_args=tc_fn.get('arguments'),
|
|
230
|
-
)
|
|
231
|
-
|
|
232
|
-
else:
|
|
233
|
-
raise ValueError(delta)
|
|
@@ -30,10 +30,14 @@ _GPT_MODEL_NAMES = [
|
|
|
30
30
|
'gpt-5-chat-latest',
|
|
31
31
|
'gpt-5-mini',
|
|
32
32
|
'gpt-5-nano',
|
|
33
|
+
|
|
34
|
+
'gpt-5.1',
|
|
35
|
+
|
|
36
|
+
'gpt-5.2',
|
|
33
37
|
]
|
|
34
38
|
|
|
35
39
|
|
|
36
|
-
|
|
40
|
+
CHAT_MODEL_NAMES = ModelNameCollection(
|
|
37
41
|
default='gpt',
|
|
38
42
|
aliases={
|
|
39
43
|
**{
|
|
@@ -46,8 +50,8 @@ MODEL_NAMES = ModelNameCollection(
|
|
|
46
50
|
for n in _GPT_MODEL_NAMES
|
|
47
51
|
},
|
|
48
52
|
|
|
49
|
-
'gpt': 'gpt-
|
|
50
|
-
'gpt-mini': 'gpt-
|
|
53
|
+
'gpt': 'gpt-5.2',
|
|
54
|
+
'gpt-mini': 'gpt-5-mini',
|
|
51
55
|
|
|
52
56
|
#
|
|
53
57
|
|
|
@@ -61,11 +65,35 @@ MODEL_NAMES = ModelNameCollection(
|
|
|
61
65
|
|
|
62
66
|
|
|
63
67
|
# @omlish-manifest
|
|
64
|
-
|
|
68
|
+
_CHAT_BACKEND_STRINGS_MANIFEST = BackendStringsManifest(
|
|
65
69
|
[
|
|
66
70
|
'ChatChoicesService',
|
|
67
71
|
'ChatChoicesStreamService',
|
|
68
72
|
],
|
|
69
73
|
'openai',
|
|
70
|
-
model_names=
|
|
74
|
+
model_names=CHAT_MODEL_NAMES,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
##
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# @omlish-manifest
|
|
82
|
+
_COMPLETION_BACKEND_STRINGS_MANIFEST = BackendStringsManifest(
|
|
83
|
+
[
|
|
84
|
+
'CompletionService',
|
|
85
|
+
],
|
|
86
|
+
'openai',
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
##
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# @omlish-manifest
|
|
94
|
+
_EMBEDDING_BACKEND_STRINGS_MANIFEST = BackendStringsManifest(
|
|
95
|
+
[
|
|
96
|
+
'EmbeddingService',
|
|
97
|
+
],
|
|
98
|
+
'openai',
|
|
71
99
|
)
|