Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

Block 1: LLM Factory API probe - module-level async complete()/stream()

whf99798ade00e1
diff --git a/.hermes_block1_spec.md b/.hermes_block1_spec.md
new file mode 100644
index 000000000..d3fd29d21
--- /dev/null
+++ b/.hermes_block1_spec.md
@@ -0,0 +1,57 @@
+# Block 1: LLM Factory 探测
+
+探测 DeepTutor 的 LLM Factory 接口,输出签名和用法给后续 Block 使用。
+
+## 操作
+
+1. 读取 `deeptutor/services/llm/factory.py`,理解 LLMFactory 的:
+ - 实例化方式(单例?每次 new?构造函数参数?)
+ - 主要调用方法(`chat()`, `complete()`, `generate()`?)
+ - 方法签名(参数类型、返回值类型)
+ - 如何传入 system prompt 和 user message
+
+2. 读取 `deeptutor/services/llm/__init__.py`,看公开导出了什么
+
+3. 确认 import 路径和最小调用示例
+
+## 输出要求
+
+在项目根目录创建 `.hermes_llm_factory_api.md`,写入:
+
+```markdown
+# LLM Factory API
+
+## Import
+```python
+from deeptutor.services.llm.factory import <正确的类名>
+```
+
+## Instantiation
+```python
+<实例化代码>
+```
+
+## Call Method
+```python
+<调用代码,包含 system prompt 和 user message>
+```
+
+## Return Type
+<返回值类型和格式>
+```
+
+## 验收
+
+```bash
+# 验证 .hermes_llm_factory_api.md 存在且包含必要信息
+python -c "
+import os
+path = r'D:\Project\DeepTutor\.hermes_llm_factory_api.md'
+assert os.path.exists(path), 'File not found'
+content = open(path).read()
+assert 'Import' in content
+assert 'Instantiation' in content
+assert 'Call Method' in content
+print('BLOCK 1 COMPLETE')
+"
+```
diff --git a/.hermes_llm_factory_api.md b/.hermes_llm_factory_api.md
new file mode 100644
index 000000000..1e28a4435
--- /dev/null
+++ b/.hermes_llm_factory_api.md
@@ -0,0 +1,84 @@
+# LLM Factory API
+
+## Import
+```python
+from deeptutor.services.llm import complete, stream
+```
+
+## Instantiation
+No instantiation needed. `complete` and `stream` are module-level async functions that auto-resolve config from `get_llm_config()` (global settings). Pass overrides as kwargs.
+
+## Call Method
+```python
+# Non-streaming completion
+response = await complete(
+ prompt="Explain photosynthesis",
+ system_prompt="You are a helpful tutor.",
+)
+
+# Streaming completion
+async for chunk in stream(
+ prompt="Explain photosynthesis",
+ system_prompt="You are a helpful tutor.",
+):
+ print(chunk, end="")
+
+# With explicit overrides (model, api_key, base_url, binding)
+response = await complete(
+ prompt="Hello",
+ system_prompt="You are helpful.",
+ model="gpt-4o",
+ api_key="sk-...",
+ base_url="https://api.openai.com/v1",
+ binding="openai",
+)
+
+# With pre-built messages list (bypasses prompt/system_prompt)
+messages = [
+ {"role": "system", "content": "You are helpful."},
+ {"role": "user", "content": "Hello"},
+]
+response = await complete(prompt="", system_prompt="", messages=messages)
+```
+
+## Function Signatures
+```python
+async def complete(
+ prompt: str,
+ system_prompt: str = "You are a helpful assistant.",
+ model: str | None = None,
+ api_key: str | None = None,
+ base_url: str | None = None,
+ api_version: str | None = None,
+ binding: str | None = None,
+ messages: list[dict[str, Any]] | None = None,
+ max_retries: int = DEFAULT_MAX_RETRIES,
+ retry_delay: float = DEFAULT_RETRY_DELAY,
+ exponential_backoff: bool = DEFAULT_EXPONENTIAL_BACKOFF,
+ **kwargs: Any, # extra_headers, reasoning_effort, image_data, response_format
+) -> str
+
+async def stream(
+ prompt: str,
+ system_prompt: str = "You are a helpful assistant.",
+ model: str | None = None,
+ api_key: str | None = None,
+ base_url: str | None = None,
+ api_version: str | None = None,
+ binding: str | None = None,
+ messages: list[dict[str, Any]] | None = None,
+ max_retries: int = DEFAULT_MAX_RETRIES,
+ retry_delay: float = DEFAULT_RETRY_DELAY,
+ exponential_backoff: bool = DEFAULT_EXPONENTIAL_BACKOFF,
+ **kwargs: Any,
+) -> AsyncGenerator[str, None]
+```
+
+## Return Type
+- `complete()` returns `str` (the full response content).
+- `stream()` returns `AsyncGenerator[str, None]` yielding content chunks as strings.
+
+## Notes
+- `LLMFactory` class exists but is a thin legacy shim — only has `get_provider(config)`. Prefer `complete()`/`stream()`.
+- Config resolution: if `model`+`api_key`+(`base_url` or `binding`) are all passed, they form an explicit config; otherwise falls back to global `get_llm_config()`.
+- Provider routing is automatic based on model name, URL patterns, or explicit `binding` string.