sr-harness 1.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.
Files changed (154) hide show
  1. sr_harness/README.md +102 -0
  2. sr_harness/__init__.py +31 -0
  3. sr_harness/_vendor/README.zh.md +13 -0
  4. sr_harness/_vendor/__init__.py +0 -0
  5. sr_harness/_vendor/llmsr_bench/__init__.py +1 -0
  6. sr_harness/_vendor/llmsr_bench/algorithms/__init__.py +32 -0
  7. sr_harness/_vendor/llmsr_bench/algorithms/codex/__init__.py +747 -0
  8. sr_harness/_vendor/llmsr_bench/algorithms/codex/call_tool_template.py +63 -0
  9. sr_harness/_vendor/llmsr_bench/algorithms/codex/readme_template.md +83 -0
  10. sr_harness/_vendor/llmsr_bench/algorithms/codex/utils.py +41 -0
  11. sr_harness/_vendor/llmsr_bench/algorithms/functionevolve.py +288 -0
  12. sr_harness/_vendor/llmsr_bench/algorithms/linear.py +37 -0
  13. sr_harness/_vendor/llmsr_bench/algorithms/my_igsr.py +414 -0
  14. sr_harness/_vendor/llmsr_bench/algorithms/my_pysr.py +102 -0
  15. sr_harness/_vendor/llmsr_bench/algorithms/polynomial.py +57 -0
  16. sr_harness/_vendor/llmsr_bench/algorithms/pysr.py +105 -0
  17. sr_harness/_vendor/llmsr_bench/algorithms/sr_harness.py +212 -0
  18. sr_harness/_vendor/llmsr_bench/algorithms/sr_scientist.py +425 -0
  19. sr_harness/_vendor/llmsr_bench/core.py +81 -0
  20. sr_harness/agents/__init__.py +14 -0
  21. sr_harness/agents/agent.py +136 -0
  22. sr_harness/agents/data_preparation_agent.py +315 -0
  23. sr_harness/agents/evaluator_construction_agent.py +317 -0
  24. sr_harness/agents/sr_agent.py +1376 -0
  25. sr_harness/agents/sr_agent_interactive.py +682 -0
  26. sr_harness/api/__init__.py +24 -0
  27. sr_harness/api/base_api.py +180 -0
  28. sr_harness/api/deepseek_api.py +102 -0
  29. sr_harness/api/gemini_api.py +102 -0
  30. sr_harness/api/lmstudio_api.py +161 -0
  31. sr_harness/api/manual_api.py +60 -0
  32. sr_harness/api/openai_api.py +349 -0
  33. sr_harness/api/openrouter_api.py +296 -0
  34. sr_harness/api/siliconflow_api.py +199 -0
  35. sr_harness/cli/README.zh.md +5 -0
  36. sr_harness/cli/__init__.py +78 -0
  37. sr_harness/cli/benchmark.py +705 -0
  38. sr_harness/cli/run.py +142 -0
  39. sr_harness/cli/synthetic.py +369 -0
  40. sr_harness/cli/tool.py +194 -0
  41. sr_harness/core/__init__.py +40 -0
  42. sr_harness/core/api.py +73 -0
  43. sr_harness/core/context.py +319 -0
  44. sr_harness/core/context_data.py +458 -0
  45. sr_harness/core/search.py +668 -0
  46. sr_harness/core/tool.py +59 -0
  47. sr_harness/evaluator/__init__.py +15 -0
  48. sr_harness/evaluator/default_evaluator.py +90 -0
  49. sr_harness/evaluator/graph_evaluator.py +99 -0
  50. sr_harness/evaluator/load_custom_evaluator.py +170 -0
  51. sr_harness/evaluator/utils/__init__.py +342 -0
  52. sr_harness/parser/__init__.py +12 -0
  53. sr_harness/parser/base_parser.py +103 -0
  54. sr_harness/parser/json_parser.py +110 -0
  55. sr_harness/parser/openai_parser.py +72 -0
  56. sr_harness/parser/text_parser.py +231 -0
  57. sr_harness/parser/xml_parser.py +18 -0
  58. sr_harness/runtime/__init__.py +24 -0
  59. sr_harness/runtime/interaction_manager.py +559 -0
  60. sr_harness/runtime/model_router.py +120 -0
  61. sr_harness/skills/__init__.py +1 -0
  62. sr_harness/skills/discover-symbolic-laws/SKILL.md +343 -0
  63. sr_harness/skills/skill_manager.py +289 -0
  64. sr_harness/skills/sr-harness-engine-graph-syntax/SKILL.md +181 -0
  65. sr_harness/skills/sr-harness-engine-syntax/SKILL.md +96 -0
  66. sr_harness/tools/README.md +160 -0
  67. sr_harness/tools/__init__.py +50 -0
  68. sr_harness/tools/base_tool.py +907 -0
  69. sr_harness/tools/call_llm.py +36 -0
  70. sr_harness/tools/call_pysr.py +274 -0
  71. sr_harness/tools/call_sindy.py +275 -0
  72. sr_harness/tools/code_executor.py +810 -0
  73. sr_harness/tools/constant_fit.py +225 -0
  74. sr_harness/tools/create_skill.py +293 -0
  75. sr_harness/tools/edit_skill.py +133 -0
  76. sr_harness/tools/edit_tool.py +113 -0
  77. sr_harness/tools/eic.py +320 -0
  78. sr_harness/tools/evaluate_code.py +356 -0
  79. sr_harness/tools/evaluate_formula.py +98 -0
  80. sr_harness/tools/harmonic_interaction_fit.py +155 -0
  81. sr_harness/tools/model_test.py +36 -0
  82. sr_harness/tools/nd2.py +383 -0
  83. sr_harness/tools/polynomial_fit.py +385 -0
  84. sr_harness/tools/power_law_fit.py +320 -0
  85. sr_harness/tools/predict_property.py +398 -0
  86. sr_harness/tools/rational_fit.py +301 -0
  87. sr_harness/tools/read_pdf.py +114 -0
  88. sr_harness/tools/read_skill.py +119 -0
  89. sr_harness/tools/read_source.py +209 -0
  90. sr_harness/tools/relationship_analysis.py +329 -0
  91. sr_harness/tools/sr4mdl.py +345 -0
  92. sr_harness/tools/statistics_analysis.py +205 -0
  93. sr_harness/tools/subagent.py +196 -0
  94. sr_harness/tools/validate_context_data.py +114 -0
  95. sr_harness/tools/validate_evaluator.py +67 -0
  96. sr_harness/tools/web_research.py +191 -0
  97. sr_harness/tools/workspace_code_executor.py +501 -0
  98. sr_harness/tools/workspace_shell.py +1123 -0
  99. sr_harness/utils/__init__.py +34 -0
  100. sr_harness/utils/attr_dict.py +49 -0
  101. sr_harness/utils/auto_gpu.py +81 -0
  102. sr_harness/utils/classproperty.py +13 -0
  103. sr_harness/utils/constant_optimizer.py +242 -0
  104. sr_harness/utils/df_to_3line.py +48 -0
  105. sr_harness/utils/factory_mixin.py +138 -0
  106. sr_harness/utils/fix_parser.py +64 -0
  107. sr_harness/utils/format_confusion_matrix.py +75 -0
  108. sr_harness/utils/format_pareto_front.py +129 -0
  109. sr_harness/utils/lazy_loader.py +35 -0
  110. sr_harness/utils/log_exception.py +12 -0
  111. sr_harness/utils/logger.py +268 -0
  112. sr_harness/utils/metrics.py +30 -0
  113. sr_harness/utils/model_store.py +154 -0
  114. sr_harness/utils/nn/__init__.py +3 -0
  115. sr_harness/utils/nn/gnn.py +113 -0
  116. sr_harness/utils/nn/positional_encoding.py +22 -0
  117. sr_harness/utils/parse_json_with_template.py +154 -0
  118. sr_harness/utils/plot.py +369 -0
  119. sr_harness/utils/render_markdown.py +23 -0
  120. sr_harness/utils/render_python.py +18 -0
  121. sr_harness/utils/sanitize_filename.py +6 -0
  122. sr_harness/utils/save_args.py +77 -0
  123. sr_harness/utils/serialize.py +35 -0
  124. sr_harness/utils/symbolic_acc.py +291 -0
  125. sr_harness/utils/tag2ansi.py +170 -0
  126. sr_harness/utils/timing.py +283 -0
  127. sr_harness/utils/utils.py +44 -0
  128. sr_harness/web/__init__.py +2 -0
  129. sr_harness/web/app.py +287 -0
  130. sr_harness/web/conversations.py +478 -0
  131. sr_harness/web/demo_data.py +139 -0
  132. sr_harness/web/platform.py +1007 -0
  133. sr_harness/web/session.py +1624 -0
  134. sr_harness/web/static/favicon.svg +4 -0
  135. sr_harness/web/static/index.html +2962 -0
  136. sr_harness/web/static/platform.html +1331 -0
  137. sr_harness-1.0.0.dist-info/METADATA +165 -0
  138. sr_harness-1.0.0.dist-info/RECORD +154 -0
  139. sr_harness-1.0.0.dist-info/WHEEL +5 -0
  140. sr_harness-1.0.0.dist-info/entry_points.txt +2 -0
  141. sr_harness-1.0.0.dist-info/licenses/LICENSE +7 -0
  142. sr_harness-1.0.0.dist-info/top_level.txt +2 -0
  143. sr_harness_engine/README.zh.md +282 -0
  144. sr_harness_engine/__init__.py +104 -0
  145. sr_harness_engine/analysis.py +127 -0
  146. sr_harness_engine/context.py +37 -0
  147. sr_harness_engine/desugar.py +90 -0
  148. sr_harness_engine/evaluation.py +422 -0
  149. sr_harness_engine/expression.py +530 -0
  150. sr_harness_engine/indexed_evaluation.py +838 -0
  151. sr_harness_engine/optimize.py +166 -0
  152. sr_harness_engine/parser.py +281 -0
  153. sr_harness_engine/render.py +132 -0
  154. sr_harness_engine/tree.py +162 -0
sr_harness/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # SRHarness 框架说明
2
+
3
+ > SRHarness 的长篇使用文档和 API Reference:[`docs/index.md`](../../docs/index.md)
4
+
5
+ ## SRAgent 整体架构
6
+
7
+ SRAgent 是一个基于 LLM 的符号回归 Agent。其核心是一个 **"请求 LLM → 解析工具调用 → 调用工具 → 格式化消息"** 的循环,循环的 pipeline 由四个参数控制:
8
+
9
+ ```
10
+ for R in 1..max_restart_loop: # best-solution restart 次数
11
+ for C in 1..global_width: # 独立对话分支数量
12
+ for L in 1..max_refinement_depth: # 每个分支的迭代轮数
13
+ for K in 1..local_sample_size: # 每轮 LLM 重复采样次数
14
+ ... (单次迭代)
15
+ ```
16
+
17
+ - **R (Restart)**:外层重启循环。每轮重启会用历史最优结果构建新的初始 prompt,引导 LLM 在已有成果上继续搜索。
18
+ - **C (Conversation branch)**:独立对话分支,每个分支从相同的初始 prompt 出发独立探索。
19
+ - **L (Refinement step)**:单个分支内的对话迭代,每轮包含一次完整的 prompt 构建、LLM 请求、工具调用、buffer 更新。
20
+ - **K (Local sample)**:单次 LLM 请求的重复采样次数,产生多个候选响应,产生最佳结果的响应会被追加到 buffer 中,其余结果中不涉及公式评估的工具调用结果也会被追加到 buffer 中以供后续参考。
21
+
22
+ ## SRAgent 核心组件
23
+
24
+ `Agent` 抽象基类负责 API、Parser、共享工具上下文和工具执行机制。
25
+ `DataPreparationAgent` 基于它维护独立、可持续的资料整理对话;`SRAgent` 基于它维护
26
+ R-C-L-K 符号回归搜索,`SRAgentInteractive` 则在同一个搜索循环上增加人工控制和事件。
27
+ 这些 Agent 可以共享一个 `AgentContext`,但不会混用各自的对话 Buffer。
28
+
29
+ `AgentContext`:内存中的权威研究上下文。它保存完整结构化数据、目标和自变量、变量
30
+ 描述、来源、工作区、当前训练/验证划分以及单调递增的数据版本。工具继续通过 Mapping
31
+ 接口读取上下文。数据准备 Agent 只在工作区生成或验证 `context.data/`;`InteractiveSession` 在受控边界加载新版本。运行中的交互式
32
+ SRAgent 只在安全迭代边界刷新划分并把变量变化写入原有对话。
33
+
34
+ SRAgent 在运行时组织以下核心组件:
35
+
36
+ `tools: List[BaseTool]`:可用的工具实例列表。每个工具接收调用参数,返回一个 `ToolCallResult`。
37
+ - 工具的详细开发指南见 [`tools/README.md`](tools/README.md)。
38
+ - 工具的调用流程如下:
39
+ 1. 初始化 SRAgent 时,工具被实例化并拿到运行时上下文(数据、目标变量等)。
40
+ 2. LLM 产生工具调用(名称 + 参数)
41
+ 3. Agent 根据名称在 `tools` 中查找对应工具实例
42
+ 4. 调用 `tool(**params)`,返回 `ToolCallResult`(包含 `ok`、`result`、`result_str`、`meta_data`)
43
+
44
+ `api: BaseAPI`:LLM API 实例,负责与 LLM 服务交互。
45
+ - 调用方式如下:
46
+ ```python
47
+ llm_result = self.api(messages, n=local_sample_size)
48
+ ```
49
+ - `messages` 是对话消息列表,格式为 `[{'role': 'system', 'content': ...}, {'role': 'user', 'content': ...}, ...]`。
50
+ - `llm_result` 是一个 `APICallResult` 对象,可迭代获取 LLM 的响应:
51
+ ```python
52
+ for content, tool_calls, message in llm_result:
53
+ # content: LLM 响应中的文字内容
54
+ # tool_calls: 从响应中解析出的工具调用列表 (List[ToolCall])
55
+ # - 文本解析模式 (text/json): 从 content 中解析
56
+ # - OpenAI 模式: 从 message 的 tool_calls 字段直接提取
57
+ # message: 可直接追加到 messages 中以延续对话的消息字典
58
+ ```
59
+ - 迭代完成后,`llm_result` 中保留用量统计:
60
+ ```python
61
+ llm_result.usage # {'token': {...}, 'price': {...}}
62
+ ```
63
+
64
+ ## SRAgent 单次迭代流程
65
+
66
+ 每一轮 (L) 的执行步骤如下:
67
+
68
+ 1. **构建 Prompt**:根据当前 buffer(对话历史)构建 messages。
69
+ 2. **请求 LLM**:调用 `api(messages)`,得到 K 个候选响应。
70
+ 3. **执行工具**:解析每个响应中的 tool_calls,查找并调用对应工具,得到结果列表。
71
+ 4. **记录搜索节点**:将当前 Prompt、响应、工具结果、父节点关系和用量写入 `SearchRunState`。
72
+ 5. **收集候选公式**:验证 `is_candidate=True` 的结果,并按配置的排名指标更新候选集合。
73
+ 6. **更新 Buffer**:选择产生最优候选的分支,将其 message 和工具结果追加到 buffer;其它分支中不涉及公式评估的工具结果也会追加,避免丢失有用信息。
74
+ 7. **日志与终止检查**:打印当前最优结果、工具调用统计和 token/费用用量,并判断是否结束搜索。
75
+
76
+ ## 目录结构
77
+
78
+ ```
79
+ src/sr_harness/
80
+ ├── agents/
81
+ │ ├── agent.py # Agent 抽象基类与共享工具执行机制
82
+ │ ├── data_preparation_agent.py # 持久化的数据准备 Agent
83
+ │ ├── sr_agent.py # SRAgent 主类,包含 run() 主循环和各个步骤方法
84
+ │ └── sr_agent_interactive.py # 交互式 SRAgent
85
+ ├── api/ # BaseAPI 与各 Provider API
86
+ │ ├── base_api.py
87
+ │ └── *_api.py # OpenAI、DeepSeek、Gemini 等实现
88
+ ├── core/ # APICallResult、ToolCall 和搜索状态等核心结构
89
+ │ ├── api.py
90
+ │ ├── context.py # AgentContext 共享研究上下文
91
+ │ ├── search.py
92
+ │ └── tool.py
93
+ ├── interaction/ # 终端与 Web 交互管理器
94
+ ├── runtime/ # 模型路由与交互控制器
95
+ ├── parser/ # 工具调用解析器
96
+ │ ├── base_parser.py # BaseParser 基类
97
+ │ ├── *_parser.py # 具体解析器实现 (TextParser, JSONParser, ...)
98
+ ├── tools/ # BaseTool 与具体科学工具
99
+ ├── skills/ # 运行时技能管理
100
+ ├── web/ # Web 服务与会话适配
101
+ └── utils/ # 通用工具函数
102
+ ```
sr_harness/__init__.py ADDED
@@ -0,0 +1,31 @@
1
+ from .agents.agent import Agent
2
+ from .agents.data_preparation_agent import DataPreparationAgent
3
+ from .agents.evaluator_construction_agent import EvaluatorConstructionAgent
4
+ from .agents.sr_agent import SRAgent
5
+ from .agents.sr_agent_interactive import SRAgentInteractive
6
+ from .core import AgentContext, ToolCall
7
+ from .evaluator import DefaultEvaluator, GraphEvaluator, load_custom_evaluator
8
+ from .runtime import InteractionManager, SRInteractionManager
9
+ from . import api
10
+ from . import tools
11
+ from . import utils
12
+ from . import parser
13
+
14
+ __all__ = [
15
+ "Agent",
16
+ "AgentContext",
17
+ "DataPreparationAgent",
18
+ "DefaultEvaluator",
19
+ "EvaluatorConstructionAgent",
20
+ "GraphEvaluator",
21
+ "InteractionManager",
22
+ "SRAgent",
23
+ "SRAgentInteractive",
24
+ "SRInteractionManager",
25
+ "ToolCall",
26
+ "api",
27
+ "load_custom_evaluator",
28
+ "parser",
29
+ "tools",
30
+ "utils",
31
+ ]
@@ -0,0 +1,13 @@
1
+ 本目录用于存放被 `sr_harness` 私有化的第三方源码。
2
+
3
+ 具体而言,sr_harness 需要利用第三方的代码和/或工具,尽管绝大多数工具可以通过 `pip install` 等方式安装,某些代码可能:
4
+ 1. 不存在合理的包结构、无法通过常规方式安装;
5
+ 2. 需要经过适当修改或包装以适应 SRHarness 的调用;
6
+ 我们将这类代码作为 `sr_harness` 的内部实现细节,安装到这一目录中。
7
+
8
+ 这一目录中的依赖应使用 `sr_harness._vendor` 作为唯一导入入口,例如:
9
+ ```python
10
+ import sr_harness._vendor.vendor_package
11
+ ```
12
+ 对于可以安装的第三方库,优先使用正常依赖声明和/或 `pip install`,不要将它放入这一目录。
13
+ 不要让同一份源码同时支持 `import vendor_package` 和 `import sr_harness._vendor.vendor_package` 两种入口,否则 Python 会把它们加载成两套模块对象,可能导致 `isinstance`、类身份比较、注册表和缓存失效。
File without changes
@@ -0,0 +1 @@
1
+ from .core import SEDTask, SRResult, Problem
@@ -0,0 +1,32 @@
1
+ """
2
+ 符号回归算法集合
3
+ """
4
+
5
+ import importlib
6
+ from pathlib import Path
7
+
8
+ # 算法目录
9
+ ALGORITHMS_DIR = Path(__file__).parent
10
+
11
+ def get_algorithm(name: str):
12
+ """获取指定算法的 run 函数"""
13
+ module = importlib.import_module(f".{name}", __name__)
14
+ return getattr(module, "run")
15
+
16
+
17
+ def get_update_parser(name: str):
18
+ """获取指定算法的 update_parser 函数"""
19
+ module = importlib.import_module(f".{name}", __name__)
20
+ return getattr(module, "update_parser", None)
21
+
22
+
23
+ def list_algorithms():
24
+ """列出所有可用的算法"""
25
+ algorithms = []
26
+ for p in ALGORITHMS_DIR.glob("*.py"):
27
+ if p.stem != "__init__":
28
+ algorithms.append(p.stem)
29
+ for p in ALGORITHMS_DIR.iterdir():
30
+ if p.is_dir() and (p / "__init__.py").exists() and not p.name.startswith("__"):
31
+ algorithms.append(p.name)
32
+ return sorted(algorithms)