quantnodes 3.0.0__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.
- QuantNodes/__init__.py +15 -0
- QuantNodes/__main__.py +14 -0
- QuantNodes/agent/__init__.py +158 -0
- QuantNodes/agent/agents/__init__.py +13 -0
- QuantNodes/agent/agents/definition.py +180 -0
- QuantNodes/agent/agents/manager.py +73 -0
- QuantNodes/agent/config/__init__.py +34 -0
- QuantNodes/agent/config/executor.py +958 -0
- QuantNodes/agent/config/loader.py +427 -0
- QuantNodes/agent/config/templates/bollinger_bands.yaml +84 -0
- QuantNodes/agent/config/templates/dual_ma.yaml +72 -0
- QuantNodes/agent/config/templates/empty.yaml +56 -0
- QuantNodes/agent/config/templates/mean_reversion.yaml +47 -0
- QuantNodes/agent/config/templates/mean_reversion_zscore.yaml +90 -0
- QuantNodes/agent/config/templates/momentum.yaml +81 -0
- QuantNodes/agent/config/templates/momentum_breakout.yaml +84 -0
- QuantNodes/agent/config/templates/rsi_strategy.yaml +72 -0
- QuantNodes/agent/config/templates/volume_price.yaml +86 -0
- QuantNodes/agent/config/types.py +156 -0
- QuantNodes/agent/config_mapper.py +293 -0
- QuantNodes/agent/core/__init__.py +19 -0
- QuantNodes/agent/core/dream.py +47 -0
- QuantNodes/agent/core/quant_dream.py +274 -0
- QuantNodes/agent/cron_jobs.py +314 -0
- QuantNodes/agent/nanobot_bridge.py +242 -0
- QuantNodes/agent/permission/__init__.py +30 -0
- QuantNodes/agent/permission/defaults.py +36 -0
- QuantNodes/agent/permission/evaluate.py +41 -0
- QuantNodes/agent/permission/models.py +59 -0
- QuantNodes/agent/permission/service.py +133 -0
- QuantNodes/agent/providers/__init__.py +11 -0
- QuantNodes/agent/providers/base.py +102 -0
- QuantNodes/agent/providers/quantnodes.py +610 -0
- QuantNodes/agent/providers/rate_limiter.py +326 -0
- QuantNodes/agent/providers/registry.py +163 -0
- QuantNodes/agent/skills/__init__.py +20 -0
- QuantNodes/agent/skills/base.py +118 -0
- QuantNodes/agent/skills/bridge.py +73 -0
- QuantNodes/agent/skills/factor/__init__.py +14 -0
- QuantNodes/agent/skills/factor/correlation.py +99 -0
- QuantNodes/agent/skills/factor/group_backtest.py +114 -0
- QuantNodes/agent/skills/factor/ic_analysis.py +106 -0
- QuantNodes/agent/skills/loader.py +107 -0
- QuantNodes/agent/skills/registry.py +105 -0
- QuantNodes/agent/skills/strategy/__init__.py +16 -0
- QuantNodes/agent/skills/strategy/bollinger.py +86 -0
- QuantNodes/agent/skills/strategy/dual_ma.py +82 -0
- QuantNodes/agent/skills/strategy/momentum.py +74 -0
- QuantNodes/agent/skills/strategy/rsi_reversal.py +99 -0
- QuantNodes/agent/skills_quant/__init__.py +14 -0
- QuantNodes/agent/skills_quant/backtest-analyze/SKILL.md +42 -0
- QuantNodes/agent/skills_quant/config-driven/SKILL.md +72 -0
- QuantNodes/agent/skills_quant/factor-research/SKILL.md +40 -0
- QuantNodes/agent/skills_quant/quant-dream/SKILL.md +55 -0
- QuantNodes/agent/skills_quant/risk-management/SKILL.md +45 -0
- QuantNodes/agent/skills_quant/strategy-design/SKILL.md +43 -0
- QuantNodes/agent/templates/__init__.py +4 -0
- QuantNodes/agent/tools/__init__.py +173 -0
- QuantNodes/agent/tools/_workspace.py +51 -0
- QuantNodes/agent/tools/alpha_backtest.py +328 -0
- QuantNodes/agent/tools/alpha_evaluate.py +493 -0
- QuantNodes/agent/tools/backtest.py +226 -0
- QuantNodes/agent/tools/base.py +133 -0
- QuantNodes/agent/tools/code_search.py +207 -0
- QuantNodes/agent/tools/config_backtest.py +401 -0
- QuantNodes/agent/tools/context.py +97 -0
- QuantNodes/agent/tools/dream_skill.py +77 -0
- QuantNodes/agent/tools/echo.py +38 -0
- QuantNodes/agent/tools/factor.py +231 -0
- QuantNodes/agent/tools/file_ops.py +201 -0
- QuantNodes/agent/tools/git_ops.py +190 -0
- QuantNodes/agent/tools/operator_lookup.py +218 -0
- QuantNodes/agent/tools/output_truncation.py +77 -0
- QuantNodes/agent/tools/path_check.py +43 -0
- QuantNodes/agent/tools/pipeline.py +62 -0
- QuantNodes/agent/tools/registry.py +150 -0
- QuantNodes/agent/tools/sandbox.py +62 -0
- QuantNodes/agent/tools/shell_safety.py +63 -0
- QuantNodes/agent/tools/strategy.py +106 -0
- QuantNodes/agent/tools/task.py +171 -0
- QuantNodes/agent/tools/web_fetch.py +142 -0
- QuantNodes/agent/tools/web_search.py +114 -0
- QuantNodes/agent/tools/wiki.py +370 -0
- QuantNodes/agent/utils/__init__.py +11 -0
- QuantNodes/agent/utils/helpers.py +43 -0
- QuantNodes/agent/utils/prompt_templates.py +30 -0
- QuantNodes/agent/workflows/__init__.py +20 -0
- QuantNodes/agent/workflows/implementations/__init__.py +8 -0
- QuantNodes/agent/workflows/implementations/alpha_gpt.py +508 -0
- QuantNodes/agent/workflows/implementations/mcts.py +442 -0
- QuantNodes/agent/workflows/parsers.py +44 -0
- QuantNodes/agent/workflows/registry.py +119 -0
- QuantNodes/agent/workflows/step_agent.py +219 -0
- QuantNodes/agent/workflows/tool.py +198 -0
- QuantNodes/ai/__init__.py +93 -0
- QuantNodes/ai/llm/__init__.py +75 -0
- QuantNodes/ai/llm/base.py +233 -0
- QuantNodes/ai/llm/decorators.py +281 -0
- QuantNodes/ai/llm/gateway.py +571 -0
- QuantNodes/ai/llm/null.py +76 -0
- QuantNodes/ai/llm/openai.py +435 -0
- QuantNodes/ai/optimizer.py +405 -0
- QuantNodes/ai/prompts/__init__.py +229 -0
- QuantNodes/ai/sandbox.py +371 -0
- QuantNodes/ai/sandbox_pandas_bridge.py +150 -0
- QuantNodes/ai/strategy_gen.py +396 -0
- QuantNodes/backtest/__init__.py +64 -0
- QuantNodes/backtest/backtest_node.py +188 -0
- QuantNodes/backtest/broker_node.py +378 -0
- QuantNodes/backtest/config_runner.py +397 -0
- QuantNodes/backtest/config_strategy.py +64 -0
- QuantNodes/backtest/risk_node.py +360 -0
- QuantNodes/backtest/strategy_node.py +268 -0
- QuantNodes/cache_node/__init__.py +19 -0
- QuantNodes/cache_node/base.py +244 -0
- QuantNodes/cache_node/cache_store.py +99 -0
- QuantNodes/cache_node/metadata.py +100 -0
- QuantNodes/cli/__init__.py +109 -0
- QuantNodes/cli/_helpers.py +511 -0
- QuantNodes/cli/command.py +110 -0
- QuantNodes/cli/commands/__init__.py +69 -0
- QuantNodes/cli/commands/agent.py +158 -0
- QuantNodes/cli/commands/alpha.py +951 -0
- QuantNodes/cli/commands/chat.py +38 -0
- QuantNodes/cli/commands/evolve.py +120 -0
- QuantNodes/cli/commands/factor.py +569 -0
- QuantNodes/cli/commands/init.py +190 -0
- QuantNodes/cli/commands/run.py +259 -0
- QuantNodes/cli/commands/serve.py +398 -0
- QuantNodes/cli/commands/version.py +120 -0
- QuantNodes/cli/enhanced.py +146 -0
- QuantNodes/conf_node/__init__.py +37 -0
- QuantNodes/conf_node/base.py +120 -0
- QuantNodes/conf_node/env_config.py +132 -0
- QuantNodes/conf_node/ini_config.py +70 -0
- QuantNodes/conf_node/json_config.py +69 -0
- QuantNodes/conf_node/yaml_config.py +78 -0
- QuantNodes/constants.py +17 -0
- QuantNodes/core/__init__.py +196 -0
- QuantNodes/core/_lookback_helpers.py +49 -0
- QuantNodes/core/ast_parser.py +198 -0
- QuantNodes/core/base.py +61 -0
- QuantNodes/core/cache_manager.py +344 -0
- QuantNodes/core/cache_utils.py +150 -0
- QuantNodes/core/cond_builder.py +53 -0
- QuantNodes/core/config.py +170 -0
- QuantNodes/core/constants.py +48 -0
- QuantNodes/core/control.py +412 -0
- QuantNodes/core/data_preprocessing.py +453 -0
- QuantNodes/core/data_source.py +46 -0
- QuantNodes/core/events.py +178 -0
- QuantNodes/core/evolution/__init__.py +22 -0
- QuantNodes/core/evolution/loop.py +583 -0
- QuantNodes/core/evolution/operators.py +289 -0
- QuantNodes/core/evolution/settings.py +44 -0
- QuantNodes/core/expression.py +841 -0
- QuantNodes/core/feedback/__init__.py +38 -0
- QuantNodes/core/feedback/channels.py +182 -0
- QuantNodes/core/feedback/collector.py +91 -0
- QuantNodes/core/feedback/dataclass.py +239 -0
- QuantNodes/core/feedback/llm_judge.py +138 -0
- QuantNodes/core/knowledge/__init__.py +69 -0
- QuantNodes/core/knowledge/knowledge_base.py +217 -0
- QuantNodes/core/knowledge/lineage_compress.py +196 -0
- QuantNodes/core/knowledge/lineage_expand.py +123 -0
- QuantNodes/core/knowledge/metrics/__init__.py +43 -0
- QuantNodes/core/knowledge/metrics/evaluator.py +176 -0
- QuantNodes/core/knowledge/metrics/metrics.py +220 -0
- QuantNodes/core/knowledge/rag_prompt.py +196 -0
- QuantNodes/core/knowledge/retriever.py +209 -0
- QuantNodes/core/lambda_node.py +81 -0
- QuantNodes/core/monitoring/__init__.py +22 -0
- QuantNodes/core/monitoring/collector.py +292 -0
- QuantNodes/core/monitoring/dashboard.py +365 -0
- QuantNodes/core/node.py +375 -0
- QuantNodes/core/pandas_utils.py +504 -0
- QuantNodes/core/parallel/__init__.py +15 -0
- QuantNodes/core/parallel/worker.py +140 -0
- QuantNodes/core/parallel/worker_process.py +265 -0
- QuantNodes/core/path_utils.py +73 -0
- QuantNodes/core/pipeline.py +328 -0
- QuantNodes/core/plugin.py +135 -0
- QuantNodes/core/quality_gate/__init__.py +32 -0
- QuantNodes/core/quality_gate/complexity.py +94 -0
- QuantNodes/core/quality_gate/consistency.py +26 -0
- QuantNodes/core/quality_gate/node.py +97 -0
- QuantNodes/core/quality_gate/redundancy.py +51 -0
- QuantNodes/core/quality_gate/settings.py +43 -0
- QuantNodes/core/quality_gate/zoo.py +98 -0
- QuantNodes/core/serializable.py +116 -0
- QuantNodes/core/serialization.py +673 -0
- QuantNodes/core/tools.py +333 -0
- QuantNodes/core/trajectory/__init__.py +25 -0
- QuantNodes/core/trajectory/entry.py +116 -0
- QuantNodes/core/trajectory/lineage.py +67 -0
- QuantNodes/core/trajectory/pool.py +211 -0
- QuantNodes/core/trajectory/selector.py +140 -0
- QuantNodes/core/visualization/__init__.py +33 -0
- QuantNodes/core/visualization/builder.py +233 -0
- QuantNodes/core/visualization/gate_breakdown.py +140 -0
- QuantNodes/core/visualization/lineage_dag.py +203 -0
- QuantNodes/core/visualization/metric_distribution.py +125 -0
- QuantNodes/core/visualization/report.py +68 -0
- QuantNodes/database_node/__init__.py +69 -0
- QuantNodes/database_node/base.py +135 -0
- QuantNodes/database_node/clickhouse_node.py +272 -0
- QuantNodes/database_node/csv_node.py +83 -0
- QuantNodes/database_node/duckdb_node.py +86 -0
- QuantNodes/database_node/factory.py +83 -0
- QuantNodes/database_node/mysql_node.py +100 -0
- QuantNodes/database_node/parquet_node.py +75 -0
- QuantNodes/database_node/sqlite_node.py +67 -0
- QuantNodes/factor_node/__init__.py +50 -0
- QuantNodes/factor_node/factor.py +563 -0
- QuantNodes/factor_node/factor_db.py +421 -0
- QuantNodes/factor_node/factor_functions/__init__.py +252 -0
- QuantNodes/factor_node/factor_functions/_helpers.py +358 -0
- QuantNodes/factor_node/factor_functions/_helpers_debug.py +317 -0
- QuantNodes/factor_node/factor_functions/composite_ops.py +136 -0
- QuantNodes/factor_node/factor_functions/math_ops.py +433 -0
- QuantNodes/factor_node/factor_functions/section_ops.py +290 -0
- QuantNodes/factor_node/factor_functions/talib_ops.py +1293 -0
- QuantNodes/factor_node/factor_functions/time_ops.py +535 -0
- QuantNodes/factor_node/factor_operation.py +1115 -0
- QuantNodes/factor_node/factor_table.py +1073 -0
- QuantNodes/factor_node/quant_nodes_object.py +60 -0
- QuantNodes/mcp_server/__init__.py +27 -0
- QuantNodes/mcp_server/__main__.py +4 -0
- QuantNodes/mcp_server/server.py +272 -0
- QuantNodes/methods/__init__.py +28 -0
- QuantNodes/methods/pipeline.py +100 -0
- QuantNodes/methods/sandbox.py +102 -0
- QuantNodes/monitor/__init__.py +27 -0
- QuantNodes/monitor/agent_tools/__init__.py +5 -0
- QuantNodes/monitor/agent_tools/monitor_tool.py +98 -0
- QuantNodes/monitor/agent_tools/schedule_tool.py +98 -0
- QuantNodes/monitor/agent_tools/version_tool.py +133 -0
- QuantNodes/monitor/monitor/__init__.py +6 -0
- QuantNodes/monitor/monitor/alerter.py +60 -0
- QuantNodes/monitor/monitor/collector.py +164 -0
- QuantNodes/monitor/monitor/dashboard.py +115 -0
- QuantNodes/monitor/monitor/drift.py +190 -0
- QuantNodes/monitor/scheduler/__init__.py +4 -0
- QuantNodes/monitor/scheduler/runner.py +133 -0
- QuantNodes/monitor/scheduler/scheduler.py +184 -0
- QuantNodes/monitor/storage/__init__.py +16 -0
- QuantNodes/monitor/storage/models.py +70 -0
- QuantNodes/monitor/storage/repository.py +407 -0
- QuantNodes/monitor/version/__init__.py +4 -0
- QuantNodes/monitor/version/diff.py +81 -0
- QuantNodes/monitor/version/version_manager.py +182 -0
- QuantNodes/operator_node/__init__.py +28 -0
- QuantNodes/operator_node/base.py +97 -0
- QuantNodes/operator_node/query_node.py +129 -0
- QuantNodes/operator_node/sql_builder.py +125 -0
- QuantNodes/operator_node/sql_utils.py +172 -0
- QuantNodes/operator_node/transform.py +130 -0
- QuantNodes/operators/__init__.py +90 -0
- QuantNodes/operators/_engine.py +108 -0
- QuantNodes/operators/composite.py +161 -0
- QuantNodes/operators/composite_dag.py +667 -0
- QuantNodes/operators/composite_dag_ops.py +343 -0
- QuantNodes/operators/composite_dag_pandas_ops.py +382 -0
- QuantNodes/operators/custom.py +408 -0
- QuantNodes/operators/facade.py +164 -0
- QuantNodes/operators/math.py +163 -0
- QuantNodes/operators/proxy.py +29 -0
- QuantNodes/operators/registry.py +144 -0
- QuantNodes/operators/section.py +99 -0
- QuantNodes/operators/talib.py +757 -0
- QuantNodes/operators/templates.py +95 -0
- QuantNodes/operators/time_series.py +136 -0
- QuantNodes/prompts/__init__.py +20 -0
- QuantNodes/prompts/backtest/__init__.py +12 -0
- QuantNodes/prompts/backtest/factor_based.py +86 -0
- QuantNodes/prompts/backtest/standard.py +73 -0
- QuantNodes/prompts/factor/__init__.py +14 -0
- QuantNodes/prompts/factor/correlation.py +77 -0
- QuantNodes/prompts/factor/group_backtest.py +86 -0
- QuantNodes/prompts/factor/ic_analysis.py +91 -0
- QuantNodes/prompts/strategy/__init__.py +18 -0
- QuantNodes/prompts/strategy/market_neutral.py +96 -0
- QuantNodes/prompts/strategy/mean_reversion.py +107 -0
- QuantNodes/prompts/strategy/momentum.py +160 -0
- QuantNodes/prompts/strategy/pairs_trading.py +107 -0
- QuantNodes/prompts/strategy/trend_following.py +96 -0
- QuantNodes/research/README.md +106 -0
- QuantNodes/research/__init__.py +154 -0
- QuantNodes/research/_legacy_3c/__init__.py +61 -0
- QuantNodes/research/_legacy_3c/auto_researcher.py +289 -0
- QuantNodes/research/_legacy_3c/factor_evaluator.py +560 -0
- QuantNodes/research/_legacy_3c/factor_miner.py +318 -0
- QuantNodes/research/_legacy_3c/mcts_search.py +324 -0
- QuantNodes/research/factor_test/__init__.py +25 -0
- QuantNodes/research/factor_test/config.py +184 -0
- QuantNodes/research/factor_test/config_builder.py +276 -0
- QuantNodes/research/factor_test/e2e/data_prep.py +163 -0
- QuantNodes/research/factor_test/e2e/run_evolution_e2e.py +309 -0
- QuantNodes/research/factor_test/evolution_adapter.py +231 -0
- QuantNodes/research/factor_test/feedback_wrapper.py +102 -0
- QuantNodes/research/factor_test/ifind_db/__init__.py +7 -0
- QuantNodes/research/factor_test/ifind_db/fetcher.py +224 -0
- QuantNodes/research/factor_test/ifind_db/ifind_database.py +689 -0
- QuantNodes/research/factor_test/nodes/__init__.py +1 -0
- QuantNodes/research/factor_test/nodes/_base.py +91 -0
- QuantNodes/research/factor_test/nodes/adjust_date_node.py +48 -0
- QuantNodes/research/factor_test/nodes/configs.py +240 -0
- QuantNodes/research/factor_test/nodes/factor_neutralize_node.py +87 -0
- QuantNodes/research/factor_test/nodes/factor_preprocess_node.py +222 -0
- QuantNodes/research/factor_test/nodes/factor_score_node.py +141 -0
- QuantNodes/research/factor_test/nodes/factor_test_report_node.py +153 -0
- QuantNodes/research/factor_test/nodes/group_analyzer_node.py +317 -0
- QuantNodes/research/factor_test/nodes/ic_analyzer_node.py +112 -0
- QuantNodes/research/factor_test/nodes/load_data_node.py +100 -0
- QuantNodes/research/factor_test/nodes/long_short_node.py +93 -0
- QuantNodes/research/factor_test/nodes/neutralizers.py +222 -0
- QuantNodes/research/factor_test/nodes/preprocess_strategies.py +277 -0
- QuantNodes/research/factor_test/nodes/risk_correlation_node.py +112 -0
- QuantNodes/research/factor_test/nodes/sample_pool_filter_node.py +110 -0
- QuantNodes/research/factor_test/nodes/tradability_filter_node.py +92 -0
- QuantNodes/research/factor_test/pipeline_runner.py +305 -0
- QuantNodes/research/factor_test/pipeline_spec.py +216 -0
- QuantNodes/research/factor_test/utils/__init__.py +26 -0
- QuantNodes/research/factor_test/utils/constants.py +86 -0
- QuantNodes/research/factor_test/utils/data_loader.py +141 -0
- QuantNodes/research/factor_test/utils/date_utils.py +232 -0
- QuantNodes/research/factor_test/utils/file_loaders.py +150 -0
- QuantNodes/research/factor_test/utils/labels.py +37 -0
- QuantNodes/research/factor_test/utils/metrics_extractor.py +55 -0
- QuantNodes/research/factor_test/utils/performance_metrics.py +175 -0
- QuantNodes/research/factor_test/utils/safe_load.py +106 -0
- QuantNodes/research/quant_alpha/CHANGELOG.md +80 -0
- QuantNodes/research/quant_alpha/README.md +142 -0
- QuantNodes/research/quant_alpha/__init__.py +45 -0
- QuantNodes/research/quant_alpha/adapters/__init__.py +99 -0
- QuantNodes/research/quant_alpha/adapters/calculator.py +503 -0
- QuantNodes/research/quant_alpha/adapters/expression.py +387 -0
- QuantNodes/research/quant_alpha/alpha101_design/__init__.py +50 -0
- QuantNodes/research/quant_alpha/alpha101_design/few_shot_examples.py +243 -0
- QuantNodes/research/quant_alpha/alpha101_design/philosophy.py +474 -0
- QuantNodes/research/quant_alpha/alpha158_design/__init__.py +63 -0
- QuantNodes/research/quant_alpha/alpha158_design/few_shot_examples.py +219 -0
- QuantNodes/research/quant_alpha/alpha158_design/philosophy.py +240 -0
- QuantNodes/research/quant_alpha/evaluation/__init__.py +47 -0
- QuantNodes/research/quant_alpha/evaluation/baselines/__init__.py +8 -0
- QuantNodes/research/quant_alpha/evaluation/baselines/g1_handcrafted.py +135 -0
- QuantNodes/research/quant_alpha/evaluation/baselines/g2_llm_only.py +269 -0
- QuantNodes/research/quant_alpha/evaluation/baselines/g3_alpha_gpt.py +152 -0
- QuantNodes/research/quant_alpha/evaluation/clickhouse_data_loader.py +227 -0
- QuantNodes/research/quant_alpha/evaluation/contracts.py +376 -0
- QuantNodes/research/quant_alpha/evaluation/evaluators/__init__.py +6 -0
- QuantNodes/research/quant_alpha/evaluation/evaluators/polars_evaluator.py +545 -0
- QuantNodes/research/quant_alpha/evaluation/mock_data_loader.py +226 -0
- QuantNodes/research/quant_alpha/evaluation/runner.py +243 -0
- QuantNodes/research/quant_alpha/llm/__init__.py +38 -0
- QuantNodes/research/quant_alpha/llm/parser.py +681 -0
- QuantNodes/research/quant_alpha/logic_driven_pipeline.py +411 -0
- QuantNodes/research/quant_alpha/logic_mining/__init__.py +74 -0
- QuantNodes/research/quant_alpha/logic_mining/compiler.py +457 -0
- QuantNodes/research/quant_alpha/logic_mining/generator.py +366 -0
- QuantNodes/research/quant_alpha/logic_mining/models.py +252 -0
- QuantNodes/research/quant_alpha/logic_mining/parser.py +287 -0
- QuantNodes/research/quant_alpha/logic_mining/pipelines.py +297 -0
- QuantNodes/research/quant_alpha/logic_mining/sources.py +149 -0
- QuantNodes/research/quant_alpha/mcts/__init__.py +66 -0
- QuantNodes/research/quant_alpha/mcts/cache.py +262 -0
- QuantNodes/research/quant_alpha/mcts/extension_ops.py +320 -0
- QuantNodes/research/quant_alpha/mcts/feedback.py +825 -0
- QuantNodes/research/quant_alpha/mcts/op_prior.py +180 -0
- QuantNodes/research/quant_alpha/mcts/search.py +540 -0
- QuantNodes/research/quant_alpha/mcts/tree.py +201 -0
- QuantNodes/research/quant_alpha/operator_vocab/__init__.py +50 -0
- QuantNodes/research/quant_alpha/operator_vocab/config.py +54 -0
- QuantNodes/research/quant_alpha/operator_vocab/metadata.py +263 -0
- QuantNodes/research/quant_alpha/operator_vocab/vocabulary.py +481 -0
- QuantNodes/research/quant_alpha/pipeline.py +1027 -0
- QuantNodes/research/quant_alpha/types/__init__.py +27 -0
- QuantNodes/research/quant_alpha/types/constants.py +28 -0
- QuantNodes/research/quant_alpha/types/state.py +205 -0
- QuantNodes/research/quant_alpha/workflow/__init__.py +32 -0
- QuantNodes/research/quant_alpha/workflow/alpha_gpt.py +911 -0
- QuantNodes/research/quant_alpha/workflow/alpha_logics.py +416 -0
- QuantNodes/research/quant_alpha/workflow/state.py +27 -0
- QuantNodes/research/report_reproducer.py +485 -0
- QuantNodes/research/wiki.py +1155 -0
- QuantNodes/symbolic/__init__.py +51 -0
- QuantNodes/symbolic/compiler.py +113 -0
- QuantNodes/symbolic/dialect.py +260 -0
- QuantNodes/symbolic/executor.py +147 -0
- QuantNodes/symbolic/expression.py +234 -0
- QuantNodes/symbolic/functions.py +433 -0
- QuantNodes/symbolic/optimizer.py +165 -0
- QuantNodes/ui_node/__init__.py +30 -0
- QuantNodes/ui_node/base.py +222 -0
- quantnodes-3.0.0.dist-info/METADATA +463 -0
- quantnodes-3.0.0.dist-info/RECORD +399 -0
- quantnodes-3.0.0.dist-info/WHEEL +5 -0
- quantnodes-3.0.0.dist-info/entry_points.txt +24 -0
- quantnodes-3.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
"""
|
|
3
|
+
LLM Client 基类
|
|
4
|
+
|
|
5
|
+
提供 LLM 客户端的统一接口。
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from abc import ABC, abstractmethod
|
|
10
|
+
from typing import Any, Dict, List, Optional, Union
|
|
11
|
+
from dataclasses import dataclass
|
|
12
|
+
from enum import Enum
|
|
13
|
+
|
|
14
|
+
import logging
|
|
15
|
+
|
|
16
|
+
from QuantNodes.core.base import QuantNodesError
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MessageRole(str, Enum):
|
|
20
|
+
"""消息角色枚举"""
|
|
21
|
+
SYSTEM = "system"
|
|
22
|
+
USER = "user"
|
|
23
|
+
ASSISTANT = "assistant"
|
|
24
|
+
TOOL = "tool"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class Message:
|
|
29
|
+
"""对话消息"""
|
|
30
|
+
role: MessageRole
|
|
31
|
+
content: str
|
|
32
|
+
name: Optional[str] = None
|
|
33
|
+
tool_calls: Optional[List[Dict[str, Any]]] = None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class ChatCompletion:
|
|
38
|
+
"""聊天补全结果"""
|
|
39
|
+
content: str
|
|
40
|
+
role: MessageRole = MessageRole.ASSISTANT
|
|
41
|
+
finish_reason: Optional[str] = None
|
|
42
|
+
usage: Optional[Dict[str, int]] = None
|
|
43
|
+
tool_calls: Optional[List[Dict[str, Any]]] = None
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class ChatCompletionChunk:
|
|
48
|
+
"""聊天补全流式块"""
|
|
49
|
+
content: str
|
|
50
|
+
finish_reason: Optional[str] = None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class LLMError(QuantNodesError):
|
|
54
|
+
"""LLM 异常基类 (Phase 1.1: 统一异常层次, 继承 QuantNodesError)"""
|
|
55
|
+
code = "LLM_ERROR"
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class RateLimitError(LLMError):
|
|
59
|
+
"""速率限制异常"""
|
|
60
|
+
code = "LLM_RATE_LIMIT"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class AuthenticationError(LLMError):
|
|
64
|
+
"""认证异常"""
|
|
65
|
+
code = "LLM_AUTH"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class APIError(LLMError):
|
|
69
|
+
"""API 异常"""
|
|
70
|
+
code = "LLM_API"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class LLMClientBase(ABC):
|
|
74
|
+
"""
|
|
75
|
+
LLM 客户端基类
|
|
76
|
+
|
|
77
|
+
提供统一的 LLM 调用接口。
|
|
78
|
+
|
|
79
|
+
Subclasses must implement:
|
|
80
|
+
_call_api(): 调用具体的 LLM API
|
|
81
|
+
|
|
82
|
+
Examples:
|
|
83
|
+
>>> client = OpenAIClient(api_key="sk-...")
|
|
84
|
+
>>> response = client.chat([Message(role="user", content="Hello")])
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
def __init__(
|
|
88
|
+
self,
|
|
89
|
+
api_key: Optional[str] = None,
|
|
90
|
+
base_url: Optional[str] = None,
|
|
91
|
+
timeout: int = 60,
|
|
92
|
+
max_retries: int = 3,
|
|
93
|
+
**kwargs
|
|
94
|
+
):
|
|
95
|
+
"""
|
|
96
|
+
初始化 LLM 客户端
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
api_key: API 密钥
|
|
100
|
+
base_url: API 基础 URL
|
|
101
|
+
timeout: 请求超时时间(秒)
|
|
102
|
+
max_retries: 最大重试次数
|
|
103
|
+
**kwargs: 额外配置参数
|
|
104
|
+
"""
|
|
105
|
+
self.api_key = api_key
|
|
106
|
+
self.base_url = base_url
|
|
107
|
+
self.timeout = timeout
|
|
108
|
+
self.max_retries = max_retries
|
|
109
|
+
self.extra_config = kwargs
|
|
110
|
+
self.logger = logging.getLogger(f"llm.{self.__class__.__name__}")
|
|
111
|
+
|
|
112
|
+
@abstractmethod
|
|
113
|
+
def _call_api(
|
|
114
|
+
self,
|
|
115
|
+
messages: List[Message],
|
|
116
|
+
model: Optional[str] = None,
|
|
117
|
+
**kwargs
|
|
118
|
+
) -> ChatCompletion:
|
|
119
|
+
"""
|
|
120
|
+
调用具体的 LLM API
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
messages: 对话消息列表
|
|
124
|
+
model: 模型名称
|
|
125
|
+
**kwargs: 额外参数
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
ChatCompletion 聊天补全结果
|
|
129
|
+
"""
|
|
130
|
+
pass
|
|
131
|
+
|
|
132
|
+
def chat(
|
|
133
|
+
self,
|
|
134
|
+
messages: Union[List[Message], List[Dict[str, str]]],
|
|
135
|
+
model: Optional[str] = None,
|
|
136
|
+
temperature: float = 0.7,
|
|
137
|
+
max_tokens: Optional[int] = None,
|
|
138
|
+
stream: bool = False,
|
|
139
|
+
**kwargs
|
|
140
|
+
) -> Union[ChatCompletion, None]:
|
|
141
|
+
"""
|
|
142
|
+
发送聊天请求
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
messages: 对话消息列表
|
|
146
|
+
model: 模型名称
|
|
147
|
+
temperature: 温度参数
|
|
148
|
+
max_tokens: 最大 token 数
|
|
149
|
+
stream: 是否流式返回
|
|
150
|
+
**kwargs: 额外参数
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
ChatCompletion 或 None(流式模式)
|
|
154
|
+
"""
|
|
155
|
+
normalized_messages = self._normalize_messages(messages)
|
|
156
|
+
return self._call_api(
|
|
157
|
+
normalized_messages, model,
|
|
158
|
+
temperature=temperature, max_tokens=max_tokens,
|
|
159
|
+
stream=stream, **kwargs,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
def _normalize_messages(
|
|
163
|
+
self,
|
|
164
|
+
messages: Union[List[Message], List[Dict[str, str]]]
|
|
165
|
+
) -> List[Message]:
|
|
166
|
+
"""规范化消息格式"""
|
|
167
|
+
normalized = []
|
|
168
|
+
for msg in messages:
|
|
169
|
+
if isinstance(msg, Message):
|
|
170
|
+
normalized.append(msg)
|
|
171
|
+
elif isinstance(msg, dict):
|
|
172
|
+
role = MessageRole(msg.get('role', 'user'))
|
|
173
|
+
normalized.append(Message(
|
|
174
|
+
role=role,
|
|
175
|
+
content=msg.get('content', ''),
|
|
176
|
+
name=msg.get('name'),
|
|
177
|
+
))
|
|
178
|
+
else:
|
|
179
|
+
raise ValueError(f"Invalid message format: {type(msg)}")
|
|
180
|
+
return normalized
|
|
181
|
+
|
|
182
|
+
def chat_stream(
|
|
183
|
+
self,
|
|
184
|
+
messages: Union[List[Message], List[Dict[str, str]]],
|
|
185
|
+
model: Optional[str] = None,
|
|
186
|
+
temperature: float = 0.7,
|
|
187
|
+
max_tokens: Optional[int] = None,
|
|
188
|
+
**kwargs
|
|
189
|
+
):
|
|
190
|
+
"""
|
|
191
|
+
发送聊天请求(流式)
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
messages: 对话消息列表
|
|
195
|
+
model: 模型名称
|
|
196
|
+
temperature: 温度参数
|
|
197
|
+
max_tokens: 最大 token 数
|
|
198
|
+
**kwargs: 额外参数
|
|
199
|
+
|
|
200
|
+
Yields:
|
|
201
|
+
ChatCompletionChunk 流式块
|
|
202
|
+
"""
|
|
203
|
+
normalized_messages = self._normalize_messages(messages)
|
|
204
|
+
for chunk in self._call_api_stream(
|
|
205
|
+
normalized_messages, model,
|
|
206
|
+
temperature=temperature, max_tokens=max_tokens, **kwargs,
|
|
207
|
+
):
|
|
208
|
+
yield chunk
|
|
209
|
+
|
|
210
|
+
def _call_api_stream(
|
|
211
|
+
self,
|
|
212
|
+
messages: List[Message],
|
|
213
|
+
model: Optional[str] = None,
|
|
214
|
+
**kwargs
|
|
215
|
+
):
|
|
216
|
+
"""流式调用 API(子类可重写)"""
|
|
217
|
+
raise NotImplementedError("Streaming not supported by this client")
|
|
218
|
+
|
|
219
|
+
def get_model_list(self) -> List[str]:
|
|
220
|
+
"""获取可用模型列表(子类可重写)"""
|
|
221
|
+
return []
|
|
222
|
+
|
|
223
|
+
def count_tokens(self, text: str) -> int:
|
|
224
|
+
"""估算 token 数量(粗略实现)"""
|
|
225
|
+
return len(text) // 4
|
|
226
|
+
|
|
227
|
+
def count_messages_tokens(self, messages: List[Message]) -> int:
|
|
228
|
+
"""估算消息列表的 token 数量"""
|
|
229
|
+
total = 0
|
|
230
|
+
for msg in messages:
|
|
231
|
+
total += self.count_tokens(msg.content)
|
|
232
|
+
total += 4
|
|
233
|
+
return total
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
"""LLM Client Decorators (Phase 1.1).
|
|
3
|
+
|
|
4
|
+
实现 4 个跨切关注点的装饰器, 可链式组合:
|
|
5
|
+
RetryingLLMClient(LoggingLLMClient(OpenAIClient(...))) # 推荐顺序
|
|
6
|
+
TokenCountingLLMClient(RetryingLLMClient(...))
|
|
7
|
+
CachedLLMClient(RetryingLLMClient(LoggingLLMClient(...)))
|
|
8
|
+
|
|
9
|
+
每个装饰器只关注一件事, 通过组合实现横切关注分离。
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import hashlib
|
|
14
|
+
import logging
|
|
15
|
+
import time
|
|
16
|
+
from collections import OrderedDict
|
|
17
|
+
from typing import Any, Dict, List, Optional
|
|
18
|
+
|
|
19
|
+
from QuantNodes.ai.llm.base import (
|
|
20
|
+
ChatCompletion,
|
|
21
|
+
LLMClientBase,
|
|
22
|
+
Message,
|
|
23
|
+
RateLimitError,
|
|
24
|
+
APIError,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# ============================================================================
|
|
29
|
+
# Decorator 1: RetryingLLMClient
|
|
30
|
+
# ============================================================================
|
|
31
|
+
|
|
32
|
+
class RetryingLLMClient(LLMClientBase):
|
|
33
|
+
"""装饰器: 在 RateLimitError / APIError 时自动重试, 指数退避。
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
inner: 被包装的 LLM 客户端
|
|
37
|
+
max_retries: 最大重试次数 (覆盖 LLMClientBase 字段)
|
|
38
|
+
initial_backoff: 首次重试等待秒数
|
|
39
|
+
backoff_factor: 退避倍数 (e.g. 2.0 → 1s, 2s, 4s, 8s)
|
|
40
|
+
retry_on: 触发重试的异常类型元组, 默认 (RateLimitError, APIError)
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __init__(
|
|
44
|
+
self,
|
|
45
|
+
inner: LLMClientBase,
|
|
46
|
+
max_retries: int = 3,
|
|
47
|
+
initial_backoff: float = 1.0,
|
|
48
|
+
backoff_factor: float = 2.0,
|
|
49
|
+
retry_on: tuple = (RateLimitError, APIError),
|
|
50
|
+
**kwargs: Any,
|
|
51
|
+
) -> None:
|
|
52
|
+
super().__init__(
|
|
53
|
+
api_key=inner.api_key, base_url=inner.base_url,
|
|
54
|
+
timeout=inner.timeout, max_retries=max_retries, **kwargs,
|
|
55
|
+
)
|
|
56
|
+
self.inner = inner
|
|
57
|
+
self.initial_backoff = initial_backoff
|
|
58
|
+
self.backoff_factor = backoff_factor
|
|
59
|
+
self.retry_on = retry_on
|
|
60
|
+
self.total_retries = 0
|
|
61
|
+
|
|
62
|
+
def _call_api(
|
|
63
|
+
self,
|
|
64
|
+
messages: List[Message],
|
|
65
|
+
model: Optional[str] = None,
|
|
66
|
+
**kwargs: Any,
|
|
67
|
+
) -> ChatCompletion:
|
|
68
|
+
backoff = self.initial_backoff
|
|
69
|
+
last_exc: Optional[Exception] = None
|
|
70
|
+
for attempt in range(self.max_retries + 1):
|
|
71
|
+
try:
|
|
72
|
+
return self.inner._call_api(messages, model, **kwargs)
|
|
73
|
+
except self.retry_on as e:
|
|
74
|
+
last_exc = e
|
|
75
|
+
if attempt >= self.max_retries:
|
|
76
|
+
break
|
|
77
|
+
self.total_retries += 1
|
|
78
|
+
self.logger.warning(
|
|
79
|
+
f"LLM call attempt {attempt + 1} failed ({type(e).__name__}: {e}), "
|
|
80
|
+
f"retrying in {backoff:.1f}s..."
|
|
81
|
+
)
|
|
82
|
+
time.sleep(backoff)
|
|
83
|
+
backoff *= self.backoff_factor
|
|
84
|
+
assert last_exc is not None
|
|
85
|
+
raise last_exc
|
|
86
|
+
|
|
87
|
+
def _call_api_stream(self, messages, model=None, **kwargs):
|
|
88
|
+
# 流式不支持重试 (用户体验差), 透传
|
|
89
|
+
yield from self.inner._call_api_stream(messages, model, **kwargs)
|
|
90
|
+
|
|
91
|
+
def get_model_list(self) -> List[str]:
|
|
92
|
+
return self.inner.get_model_list()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# ============================================================================
|
|
96
|
+
# Decorator 2: LoggingLLMClient
|
|
97
|
+
# ============================================================================
|
|
98
|
+
|
|
99
|
+
class LoggingLLMClient(LLMClientBase):
|
|
100
|
+
"""装饰器: 记录每条 LLM 请求/响应 + latency 到 logger.
|
|
101
|
+
|
|
102
|
+
不会修改 prompt 或 response, 仅观察。日志格式:
|
|
103
|
+
[LLMCall] model=foo latency=123ms prompt_tokens=N response_len=M
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
def __init__(self, inner: LLMClientBase, log_level: int = logging.INFO, **kwargs: Any) -> None:
|
|
107
|
+
super().__init__(
|
|
108
|
+
api_key=inner.api_key, base_url=inner.base_url,
|
|
109
|
+
timeout=inner.timeout, max_retries=inner.max_retries, **kwargs,
|
|
110
|
+
)
|
|
111
|
+
self.inner = inner
|
|
112
|
+
self.log_level = log_level
|
|
113
|
+
self.call_log: List[Dict[str, Any]] = []
|
|
114
|
+
|
|
115
|
+
def _call_api(
|
|
116
|
+
self,
|
|
117
|
+
messages: List[Message],
|
|
118
|
+
model: Optional[str] = None,
|
|
119
|
+
**kwargs: Any,
|
|
120
|
+
) -> ChatCompletion:
|
|
121
|
+
start = time.perf_counter()
|
|
122
|
+
prompt_tokens = self.count_messages_tokens(messages)
|
|
123
|
+
try:
|
|
124
|
+
response = self.inner._call_api(messages, model, **kwargs)
|
|
125
|
+
latency_ms = (time.perf_counter() - start) * 1000
|
|
126
|
+
entry = {
|
|
127
|
+
"model": model or "default",
|
|
128
|
+
"latency_ms": latency_ms,
|
|
129
|
+
"prompt_tokens": prompt_tokens,
|
|
130
|
+
"response_len": len(response.content),
|
|
131
|
+
"error": None,
|
|
132
|
+
}
|
|
133
|
+
self.call_log.append(entry)
|
|
134
|
+
self.logger.log(
|
|
135
|
+
self.log_level,
|
|
136
|
+
f"[LLMCall] model={entry['model']} latency={latency_ms:.1f}ms "
|
|
137
|
+
f"prompt_tokens={prompt_tokens} response_len={entry['response_len']}",
|
|
138
|
+
)
|
|
139
|
+
return response
|
|
140
|
+
except Exception as e:
|
|
141
|
+
latency_ms = (time.perf_counter() - start) * 1000
|
|
142
|
+
entry = {
|
|
143
|
+
"model": model or "default",
|
|
144
|
+
"latency_ms": latency_ms,
|
|
145
|
+
"prompt_tokens": prompt_tokens,
|
|
146
|
+
"response_len": 0,
|
|
147
|
+
"error": f"{type(e).__name__}: {e}",
|
|
148
|
+
}
|
|
149
|
+
self.call_log.append(entry)
|
|
150
|
+
self.logger.error(
|
|
151
|
+
f"[LLMCall] model={entry['model']} latency={latency_ms:.1f}ms "
|
|
152
|
+
f"ERROR={entry['error']}"
|
|
153
|
+
)
|
|
154
|
+
raise
|
|
155
|
+
|
|
156
|
+
def _call_api_stream(self, messages, model=None, **kwargs):
|
|
157
|
+
yield from self.inner._call_api_stream(messages, model, **kwargs)
|
|
158
|
+
|
|
159
|
+
def get_model_list(self) -> List[str]:
|
|
160
|
+
return self.inner.get_model_list()
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
# ============================================================================
|
|
164
|
+
# Decorator 3: TokenCountingLLMClient (Phase 1.2)
|
|
165
|
+
# ============================================================================
|
|
166
|
+
|
|
167
|
+
class TokenCountingLLMClient(LLMClientBase):
|
|
168
|
+
"""装饰器: 累计 LLM 调用的 token 消耗, 用于 cost tracking。
|
|
169
|
+
|
|
170
|
+
读取 ChatCompletion.usage.total_tokens (若有), 累加到 self.total_tokens_used。
|
|
171
|
+
同时记录 prompt / completion 各自的累计。
|
|
172
|
+
"""
|
|
173
|
+
|
|
174
|
+
def __init__(self, inner: LLMClientBase, **kwargs: Any) -> None:
|
|
175
|
+
super().__init__(
|
|
176
|
+
api_key=inner.api_key, base_url=inner.base_url,
|
|
177
|
+
timeout=inner.timeout, max_retries=inner.max_retries, **kwargs,
|
|
178
|
+
)
|
|
179
|
+
self.inner = inner
|
|
180
|
+
self.total_tokens_used = 0
|
|
181
|
+
self.total_prompt_tokens = 0
|
|
182
|
+
self.total_completion_tokens = 0
|
|
183
|
+
self.call_count = 0
|
|
184
|
+
|
|
185
|
+
def _call_api(
|
|
186
|
+
self,
|
|
187
|
+
messages: List[Message],
|
|
188
|
+
model: Optional[str] = None,
|
|
189
|
+
**kwargs: Any,
|
|
190
|
+
) -> ChatCompletion:
|
|
191
|
+
response = self.inner._call_api(messages, model, **kwargs)
|
|
192
|
+
self.call_count += 1
|
|
193
|
+
if response.usage:
|
|
194
|
+
pt = response.usage.get("prompt_tokens", 0)
|
|
195
|
+
ct = response.usage.get("completion_tokens", 0)
|
|
196
|
+
tt = response.usage.get("total_tokens", pt + ct)
|
|
197
|
+
self.total_prompt_tokens += pt
|
|
198
|
+
self.total_completion_tokens += ct
|
|
199
|
+
self.total_tokens_used += tt
|
|
200
|
+
return response
|
|
201
|
+
|
|
202
|
+
def _call_api_stream(self, messages, model=None, **kwargs):
|
|
203
|
+
# 流式响应通常无 usage 统计, 透传
|
|
204
|
+
yield from self.inner._call_api_stream(messages, model, **kwargs)
|
|
205
|
+
|
|
206
|
+
def get_model_list(self) -> List[str]:
|
|
207
|
+
return self.inner.get_model_list()
|
|
208
|
+
|
|
209
|
+
def reset(self) -> None:
|
|
210
|
+
self.total_tokens_used = 0
|
|
211
|
+
self.total_prompt_tokens = 0
|
|
212
|
+
self.total_completion_tokens = 0
|
|
213
|
+
self.call_count = 0
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
# ============================================================================
|
|
217
|
+
# Decorator 4: CachedLLMClient (Phase 1.2)
|
|
218
|
+
# ============================================================================
|
|
219
|
+
|
|
220
|
+
class CachedLLMClient(LLMClientBase):
|
|
221
|
+
"""装饰器: 缓存 LLM 响应 (prompt_hash, model) → ChatCompletion。
|
|
222
|
+
|
|
223
|
+
用于:
|
|
224
|
+
- 单元测试中避免重复 API 调用
|
|
225
|
+
- 相同 prompt 的批量处理提速
|
|
226
|
+
- 离线 replay
|
|
227
|
+
|
|
228
|
+
Args:
|
|
229
|
+
inner: 被包装的 LLM 客户端
|
|
230
|
+
max_size: LRU 缓存最大条目数, 默认 128
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
def __init__(self, inner: LLMClientBase, max_size: int = 128, **kwargs: Any) -> None:
|
|
234
|
+
super().__init__(
|
|
235
|
+
api_key=inner.api_key, base_url=inner.base_url,
|
|
236
|
+
timeout=inner.timeout, max_retries=inner.max_retries, **kwargs,
|
|
237
|
+
)
|
|
238
|
+
self.inner = inner
|
|
239
|
+
self.max_size = max_size
|
|
240
|
+
self._cache: "OrderedDict[str, ChatCompletion]" = OrderedDict()
|
|
241
|
+
self.hit_count = 0
|
|
242
|
+
self.miss_count = 0
|
|
243
|
+
|
|
244
|
+
@staticmethod
|
|
245
|
+
def _make_key(messages: List[Message], model: Optional[str], kwargs: Dict[str, Any]) -> str:
|
|
246
|
+
payload = (
|
|
247
|
+
model or "",
|
|
248
|
+
tuple((m.role.value, m.content) for m in messages),
|
|
249
|
+
tuple(sorted(kwargs.items())),
|
|
250
|
+
)
|
|
251
|
+
return hashlib.sha256(repr(payload).encode()).hexdigest()
|
|
252
|
+
|
|
253
|
+
def _call_api(
|
|
254
|
+
self,
|
|
255
|
+
messages: List[Message],
|
|
256
|
+
model: Optional[str] = None,
|
|
257
|
+
**kwargs: Any,
|
|
258
|
+
) -> ChatCompletion:
|
|
259
|
+
key = self._make_key(messages, model, kwargs)
|
|
260
|
+
if key in self._cache:
|
|
261
|
+
self.hit_count += 1
|
|
262
|
+
self._cache.move_to_end(key)
|
|
263
|
+
return self._cache[key]
|
|
264
|
+
self.miss_count += 1
|
|
265
|
+
response = self.inner._call_api(messages, model, **kwargs)
|
|
266
|
+
self._cache[key] = response
|
|
267
|
+
if len(self._cache) > self.max_size:
|
|
268
|
+
self._cache.popitem(last=False)
|
|
269
|
+
return response
|
|
270
|
+
|
|
271
|
+
def _call_api_stream(self, messages, model=None, **kwargs):
|
|
272
|
+
# 流式不缓存
|
|
273
|
+
yield from self.inner._call_api_stream(messages, model, **kwargs)
|
|
274
|
+
|
|
275
|
+
def get_model_list(self) -> List[str]:
|
|
276
|
+
return self.inner.get_model_list()
|
|
277
|
+
|
|
278
|
+
def clear(self) -> None:
|
|
279
|
+
self._cache.clear()
|
|
280
|
+
self.hit_count = 0
|
|
281
|
+
self.miss_count = 0
|