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
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import contextlib
|
|
2
|
-
import typing as ta
|
|
3
|
-
|
|
4
|
-
from omlish import lang
|
|
5
|
-
|
|
6
|
-
from ..... import minichain as mc
|
|
7
|
-
from .types import BackendConfigs
|
|
8
|
-
from .types import BackendName
|
|
9
|
-
from .types import BackendProvider
|
|
10
|
-
from .types import ChatChoicesServiceBackendProvider
|
|
11
|
-
from .types import ChatChoicesStreamServiceBackendProvider
|
|
12
|
-
from .types import ServiceT
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
##
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class _CatalogBackendProvider(BackendProvider[ServiceT], lang.Abstract):
|
|
19
|
-
def __init__(
|
|
20
|
-
self,
|
|
21
|
-
*,
|
|
22
|
-
name: BackendName,
|
|
23
|
-
catalog: 'mc.BackendCatalog',
|
|
24
|
-
configs: BackendConfigs | None = None,
|
|
25
|
-
) -> None:
|
|
26
|
-
super().__init__()
|
|
27
|
-
|
|
28
|
-
self._name = name
|
|
29
|
-
self._catalog = catalog
|
|
30
|
-
self._configs = configs
|
|
31
|
-
|
|
32
|
-
@contextlib.asynccontextmanager
|
|
33
|
-
async def _provide_backend(self, cls: type[ServiceT]) -> ta.AsyncIterator[ServiceT]:
|
|
34
|
-
service: ServiceT
|
|
35
|
-
async with lang.async_maybe_managing(self._catalog.get_backend(
|
|
36
|
-
cls,
|
|
37
|
-
self._name,
|
|
38
|
-
*(self._configs or []),
|
|
39
|
-
)) as service:
|
|
40
|
-
yield service
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
class CatalogChatChoicesServiceBackendProvider(
|
|
44
|
-
_CatalogBackendProvider['mc.ChatChoicesService'],
|
|
45
|
-
ChatChoicesServiceBackendProvider,
|
|
46
|
-
):
|
|
47
|
-
def provide_backend(self) -> ta.AsyncContextManager['mc.ChatChoicesService']:
|
|
48
|
-
return self._provide_backend(mc.ChatChoicesService) # type: ignore[type-abstract]
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
class CatalogChatChoicesStreamServiceBackendProvider(
|
|
52
|
-
_CatalogBackendProvider['mc.ChatChoicesStreamService'],
|
|
53
|
-
ChatChoicesStreamServiceBackendProvider,
|
|
54
|
-
):
|
|
55
|
-
def provide_backend(self) -> ta.AsyncContextManager['mc.ChatChoicesStreamService']:
|
|
56
|
-
return self._provide_backend(mc.ChatChoicesStreamService) # type: ignore[type-abstract]
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
from omlish import inject as inj
|
|
2
|
-
from omlish import lang
|
|
3
|
-
|
|
4
|
-
from .injection import backend_configs
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
with lang.auto_proxy_import(globals()):
|
|
8
|
-
from . import catalog as _catalog
|
|
9
|
-
from . import types as _types
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
##
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def bind_backends(
|
|
16
|
-
*,
|
|
17
|
-
backend: str | None = None,
|
|
18
|
-
) -> inj.Elements:
|
|
19
|
-
els: list[inj.Elemental] = []
|
|
20
|
-
|
|
21
|
-
#
|
|
22
|
-
|
|
23
|
-
els.append(backend_configs().bind_items_provider(singleton=True))
|
|
24
|
-
|
|
25
|
-
#
|
|
26
|
-
|
|
27
|
-
if backend is not None:
|
|
28
|
-
els.append(inj.bind(_types.BackendName, to_const=backend))
|
|
29
|
-
|
|
30
|
-
els.extend([
|
|
31
|
-
inj.bind(_types.ChatChoicesServiceBackendProvider, to_ctor=_catalog.CatalogChatChoicesServiceBackendProvider, singleton=True), # noqa
|
|
32
|
-
inj.bind(_types.ChatChoicesStreamServiceBackendProvider, to_ctor=_catalog.CatalogChatChoicesStreamServiceBackendProvider, singleton=True), # noqa
|
|
33
|
-
])
|
|
34
|
-
|
|
35
|
-
#
|
|
36
|
-
|
|
37
|
-
return inj.as_elements(*els)
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import typing as ta
|
|
2
|
-
|
|
3
|
-
from omlish import inject as inj
|
|
4
|
-
from omlish import lang
|
|
5
|
-
|
|
6
|
-
from ...phases.injection import phase_callbacks
|
|
7
|
-
from ...phases.types import ChatPhase
|
|
8
|
-
from ...phases.types import ChatPhaseCallback
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
with lang.auto_proxy_import(globals()):
|
|
12
|
-
from . import inmemory as _inmemory
|
|
13
|
-
from . import storage as _storage
|
|
14
|
-
from . import types as _types
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
##
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def bind_state(
|
|
21
|
-
*,
|
|
22
|
-
state: ta.Literal['new', 'continue', 'ephemeral'] = 'continue',
|
|
23
|
-
) -> inj.Elements:
|
|
24
|
-
els: list[inj.Elemental] = []
|
|
25
|
-
|
|
26
|
-
if state in ('continue', 'new'):
|
|
27
|
-
els.append(inj.bind(_types.ChatStateManager, to_ctor=_storage.StateStorageChatStateManager, singleton=True))
|
|
28
|
-
|
|
29
|
-
if state == 'new':
|
|
30
|
-
els.append(phase_callbacks().bind_item(to_fn=lang.typed_lambda(cm=_types.ChatStateManager)(
|
|
31
|
-
lambda cm: ChatPhaseCallback(ChatPhase.STARTING, cm.clear_state),
|
|
32
|
-
)))
|
|
33
|
-
|
|
34
|
-
elif state == 'ephemeral':
|
|
35
|
-
els.append(inj.bind(_types.ChatStateManager, to_ctor=_inmemory.InMemoryChatStateManager, singleton=True))
|
|
36
|
-
|
|
37
|
-
else:
|
|
38
|
-
raise TypeError(state)
|
|
39
|
-
|
|
40
|
-
return inj.as_elements(*els)
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import typing as ta
|
|
2
|
-
|
|
3
|
-
from omlish import inject as inj
|
|
4
|
-
from omlish import lang
|
|
5
|
-
|
|
6
|
-
from ...... import minichain as mc
|
|
7
|
-
from ...phases.injection import phase_callbacks
|
|
8
|
-
from ...phases.types import ChatPhase
|
|
9
|
-
from ...phases.types import ChatPhaseCallback
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
with lang.auto_proxy_import(globals()):
|
|
13
|
-
from ..state import types as _state
|
|
14
|
-
from . import interactive as _interactive
|
|
15
|
-
from . import oneshot as _oneshot
|
|
16
|
-
from . import types as _types
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
##
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def bind_user(
|
|
23
|
-
*,
|
|
24
|
-
initial_system_content: ta.Optional['mc.Content'] = None,
|
|
25
|
-
initial_user_content: ta.Optional['mc.Content'] = None,
|
|
26
|
-
interactive: bool = False,
|
|
27
|
-
) -> inj.Elements:
|
|
28
|
-
els: list[inj.Elemental] = []
|
|
29
|
-
|
|
30
|
-
# FIXME: barf
|
|
31
|
-
if initial_system_content is not None:
|
|
32
|
-
async def add_initial_system_content(cm: '_state.ChatStateManager') -> None:
|
|
33
|
-
await cm.extend_chat([mc.SystemMessage(initial_system_content)])
|
|
34
|
-
|
|
35
|
-
els.append(phase_callbacks().bind_item(to_fn=lang.typed_lambda(cm=_state.ChatStateManager)(
|
|
36
|
-
lambda cm: ChatPhaseCallback(ChatPhase.STARTED, lambda: add_initial_system_content(cm)),
|
|
37
|
-
)))
|
|
38
|
-
|
|
39
|
-
if interactive:
|
|
40
|
-
if initial_user_content is not None:
|
|
41
|
-
async def add_initial_user_content(cm: '_state.ChatStateManager') -> None:
|
|
42
|
-
await cm.extend_chat([mc.UserMessage(initial_user_content)])
|
|
43
|
-
|
|
44
|
-
els.append(phase_callbacks().bind_item(to_fn=lang.typed_lambda(cm=_state.ChatStateManager)(
|
|
45
|
-
lambda cm: ChatPhaseCallback(ChatPhase.STARTED, lambda: add_initial_user_content(cm)),
|
|
46
|
-
)))
|
|
47
|
-
|
|
48
|
-
raise NotImplementedError
|
|
49
|
-
|
|
50
|
-
els.append(inj.bind(_types.UserChatInput, to_ctor=_interactive.InteractiveUserChatInput, singleton=True))
|
|
51
|
-
|
|
52
|
-
else:
|
|
53
|
-
if initial_user_content is None:
|
|
54
|
-
raise ValueError('Initial user content is required for non-interactive chat')
|
|
55
|
-
|
|
56
|
-
els.extend([
|
|
57
|
-
inj.bind(_oneshot.OneshotUserChatInputInitialChat, to_const=[mc.UserMessage(initial_user_content)]),
|
|
58
|
-
inj.bind(_types.UserChatInput, to_ctor=_oneshot.OneshotUserChatInput, singleton=True),
|
|
59
|
-
])
|
|
60
|
-
|
|
61
|
-
return inj.as_elements(*els)
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import functools
|
|
2
|
-
import typing as ta
|
|
3
|
-
|
|
4
|
-
from omlish import lang
|
|
5
|
-
|
|
6
|
-
from ...... import minichain as mc
|
|
7
|
-
from .types import UserChatInput
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
##
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class InteractiveUserChatInput(UserChatInput):
|
|
14
|
-
def __init__(
|
|
15
|
-
self,
|
|
16
|
-
string_input: ta.Callable[[], ta.Awaitable[str]] | None = None,
|
|
17
|
-
) -> None:
|
|
18
|
-
super().__init__()
|
|
19
|
-
|
|
20
|
-
if string_input is None:
|
|
21
|
-
string_input = lang.as_async(functools.partial(input, '> '))
|
|
22
|
-
self._string_input = string_input
|
|
23
|
-
|
|
24
|
-
async def get_next_user_messages(self) -> 'mc.UserChat':
|
|
25
|
-
try:
|
|
26
|
-
s = await self._string_input()
|
|
27
|
-
except EOFError:
|
|
28
|
-
return []
|
|
29
|
-
return [mc.UserMessage(s)]
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import typing as ta
|
|
2
|
-
|
|
3
|
-
from ...... import minichain as mc
|
|
4
|
-
from .types import UserChatInput
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
OneshotUserChatInputInitialChat = ta.NewType('OneshotUserChatInputInitialChat', mc.UserChat)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class OneshotUserChatInput(UserChatInput):
|
|
14
|
-
def __init__(
|
|
15
|
-
self,
|
|
16
|
-
initial_chat: OneshotUserChatInputInitialChat,
|
|
17
|
-
) -> None:
|
|
18
|
-
super().__init__()
|
|
19
|
-
|
|
20
|
-
self._pending_chat: mc.UserChat | None = initial_chat
|
|
21
|
-
|
|
22
|
-
async def get_next_user_messages(self) -> 'mc.UserChat':
|
|
23
|
-
ret = self._pending_chat
|
|
24
|
-
self._pending_chat = None
|
|
25
|
-
return ret or []
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import abc
|
|
2
|
-
import typing as ta
|
|
3
|
-
|
|
4
|
-
from omlish import lang
|
|
5
|
-
|
|
6
|
-
from ...... import minichain as mc
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
##
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class UserChatInput(lang.Abstract):
|
|
13
|
-
@abc.abstractmethod
|
|
14
|
-
def get_next_user_messages(self) -> ta.Awaitable['mc.UserChat']:
|
|
15
|
-
raise NotImplementedError
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
from .chat.ai.types import AiChatGenerator
|
|
2
|
-
from .chat.state.types import ChatStateManager
|
|
3
|
-
from .chat.user.types import UserChatInput
|
|
4
|
-
from .phases.manager import ChatPhaseManager
|
|
5
|
-
from .phases.types import ChatPhase
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
##
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class ChatDriver:
|
|
12
|
-
def __init__(
|
|
13
|
-
self,
|
|
14
|
-
*,
|
|
15
|
-
phases: ChatPhaseManager,
|
|
16
|
-
ai_chat_generator: AiChatGenerator,
|
|
17
|
-
user_chat_input: UserChatInput,
|
|
18
|
-
chat_state_manager: ChatStateManager,
|
|
19
|
-
):
|
|
20
|
-
super().__init__()
|
|
21
|
-
|
|
22
|
-
self._phases = phases
|
|
23
|
-
self._ai_chat_generator = ai_chat_generator
|
|
24
|
-
self._user_chat_input = user_chat_input
|
|
25
|
-
self._chat_state_manager = chat_state_manager
|
|
26
|
-
|
|
27
|
-
async def run(self) -> None:
|
|
28
|
-
await self._phases.set_phase(ChatPhase.STARTING)
|
|
29
|
-
await self._phases.set_phase(ChatPhase.STARTED)
|
|
30
|
-
|
|
31
|
-
while True:
|
|
32
|
-
next_user_chat = await self._user_chat_input.get_next_user_messages()
|
|
33
|
-
if not next_user_chat:
|
|
34
|
-
break
|
|
35
|
-
|
|
36
|
-
prev_user_chat = (await self._chat_state_manager.get_state()).chat
|
|
37
|
-
|
|
38
|
-
next_ai_chat = await self._ai_chat_generator.get_next_ai_messages([*prev_user_chat, *next_user_chat])
|
|
39
|
-
|
|
40
|
-
await self._chat_state_manager.extend_chat([*next_user_chat, *next_ai_chat])
|
|
41
|
-
|
|
42
|
-
await self._phases.set_phase(ChatPhase.STOPPING)
|
|
43
|
-
await self._phases.set_phase(ChatPhase.STOPPED)
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import typing as ta
|
|
3
|
-
|
|
4
|
-
from omlish import check
|
|
5
|
-
from omlish import inject as inj
|
|
6
|
-
from omlish import lang
|
|
7
|
-
|
|
8
|
-
from ..... import minichain as mc
|
|
9
|
-
from .injection import bind_tool_context_provider_to_key
|
|
10
|
-
from .injection import tool_catalog_entries
|
|
11
|
-
from .injection import tool_context_providers
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
with lang.auto_proxy_import(globals()):
|
|
15
|
-
from . import confirmation as _confirmation
|
|
16
|
-
from . import execution as _execution
|
|
17
|
-
from . import rendering as _rendering
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
##
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
_TOOL_BINDERS: dict[str, ta.Callable[[], inj.Elements]] = {}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def _tool_binder(name: str) -> ta.Callable[[ta.Callable[[], inj.Elements]], ta.Callable[[], inj.Elements]]:
|
|
27
|
-
def inner(fn):
|
|
28
|
-
check.not_in(name, _TOOL_BINDERS)
|
|
29
|
-
_TOOL_BINDERS[name] = fn
|
|
30
|
-
return fn
|
|
31
|
-
return inner
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
#
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
@_tool_binder('weather')
|
|
38
|
-
def _bind_weather_tool() -> inj.Elements:
|
|
39
|
-
from .weather import WEATHER_TOOL
|
|
40
|
-
|
|
41
|
-
return inj.as_elements(
|
|
42
|
-
tool_catalog_entries().bind_item_consts(WEATHER_TOOL),
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
@_tool_binder('todo')
|
|
47
|
-
def _bind_todo_tools() -> inj.Elements:
|
|
48
|
-
from .....minichain.lib.todo.context import TodoContext
|
|
49
|
-
from .....minichain.lib.todo.tools.read import todo_read_tool
|
|
50
|
-
from .....minichain.lib.todo.tools.write import todo_write_tool
|
|
51
|
-
|
|
52
|
-
return inj.as_elements(
|
|
53
|
-
tool_catalog_entries().bind_item_consts(
|
|
54
|
-
todo_read_tool(),
|
|
55
|
-
todo_write_tool(),
|
|
56
|
-
),
|
|
57
|
-
|
|
58
|
-
inj.bind(TodoContext()),
|
|
59
|
-
bind_tool_context_provider_to_key(TodoContext),
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
@_tool_binder('fs')
|
|
64
|
-
def _bind_fs_tools() -> inj.Elements:
|
|
65
|
-
from .....minichain.lib.fs.context import FsContext
|
|
66
|
-
from .....minichain.lib.fs.tools.ls import ls_tool
|
|
67
|
-
from .....minichain.lib.fs.tools.read import read_tool
|
|
68
|
-
|
|
69
|
-
return inj.as_elements(
|
|
70
|
-
tool_catalog_entries().bind_item_consts(
|
|
71
|
-
ls_tool(),
|
|
72
|
-
read_tool(),
|
|
73
|
-
),
|
|
74
|
-
|
|
75
|
-
inj.bind(FsContext(
|
|
76
|
-
root_dir=os.getcwd(),
|
|
77
|
-
)),
|
|
78
|
-
bind_tool_context_provider_to_key(FsContext),
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
# if tools_config.enable_unsafe_tools_do_not_use_lol:
|
|
83
|
-
# from ...minichain.lib.bash import bash_tool
|
|
84
|
-
# els.append(bind_tool(bash_tool()))
|
|
85
|
-
#
|
|
86
|
-
# from ...minichain.lib.fs.tools.edit import edit_tool
|
|
87
|
-
# els.append(bind_tool(edit_tool()))
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
##
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
def bind_tools(
|
|
94
|
-
*,
|
|
95
|
-
silent: bool = False,
|
|
96
|
-
dangerous_no_confirmation: bool = False,
|
|
97
|
-
enabled_tools: ta.Iterable[str] | None = None,
|
|
98
|
-
) -> inj.Elements:
|
|
99
|
-
els: list[inj.Elemental] = []
|
|
100
|
-
|
|
101
|
-
#
|
|
102
|
-
|
|
103
|
-
els.append(inj.bind(mc.ToolCatalog, singleton=True))
|
|
104
|
-
|
|
105
|
-
#
|
|
106
|
-
|
|
107
|
-
els.append(tool_catalog_entries().bind_items_provider(singleton=True))
|
|
108
|
-
|
|
109
|
-
for etn in check.not_isinstance(enabled_tools or [], str):
|
|
110
|
-
els.append(_TOOL_BINDERS[etn]())
|
|
111
|
-
|
|
112
|
-
#
|
|
113
|
-
|
|
114
|
-
exec_stack = inj.wrapper_binder_helper(_execution.ToolUseExecutor)
|
|
115
|
-
|
|
116
|
-
els.append(exec_stack.push_bind(to_ctor=_execution.ToolUseExecutorImpl, singleton=True))
|
|
117
|
-
|
|
118
|
-
if not silent:
|
|
119
|
-
els.append(exec_stack.push_bind(to_ctor=_rendering.ResultRenderingToolUseExecutor, singleton=True))
|
|
120
|
-
|
|
121
|
-
if dangerous_no_confirmation:
|
|
122
|
-
els.append(exec_stack.push_bind(to_ctor=_rendering.ArgsRenderingToolUseExecutor, singleton=True))
|
|
123
|
-
|
|
124
|
-
els.extend([
|
|
125
|
-
inj.bind(_execution.ToolUseExecutor, to_key=exec_stack.top),
|
|
126
|
-
])
|
|
127
|
-
|
|
128
|
-
#
|
|
129
|
-
|
|
130
|
-
if not dangerous_no_confirmation:
|
|
131
|
-
els.append(inj.bind(_confirmation.ToolExecutionConfirmation, to_ctor=_confirmation.InteractiveToolExecutionConfirmation, singleton=True)) # noqa
|
|
132
|
-
|
|
133
|
-
#
|
|
134
|
-
|
|
135
|
-
els.extend([
|
|
136
|
-
tool_context_providers().bind_items_provider(singleton=True),
|
|
137
|
-
|
|
138
|
-
inj.bind(_execution.ToolContextProvider, to_fn=lang.typed_lambda(tcps=_execution.ToolContextProviders)(
|
|
139
|
-
lambda tcps: _execution.ToolContextProvider(lambda: [tc for tcp in tcps for tc in tcp()]),
|
|
140
|
-
), singleton=True),
|
|
141
|
-
])
|
|
142
|
-
|
|
143
|
-
#
|
|
144
|
-
|
|
145
|
-
return inj.as_elements(*els)
|
|
@@ -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
|
-
)
|