feat: run fixed research pipelines within durable budgets
This commit is contained in:
@@ -5,7 +5,7 @@ import json
|
||||
from contextlib import asynccontextmanager
|
||||
from uuid import uuid4
|
||||
|
||||
from pydantic_ai.messages import ToolReturnPart, UserPromptPart
|
||||
from pydantic_ai.messages import ModelResponse, TextPart, ToolCallPart, ToolReturnPart, UserPromptPart
|
||||
from pydantic_ai.models.function import DeltaToolCall, FunctionModel
|
||||
|
||||
from tests.research_fake import research_step
|
||||
@@ -95,6 +95,39 @@ async def fake_stream(messages, info):
|
||||
yield {0: DeltaToolCall(name=name, json_args=json.dumps(args), tool_call_id=uuid4().hex)}
|
||||
|
||||
|
||||
def fake_structured(messages, info):
|
||||
if not info.output_tools:
|
||||
return ModelResponse(parts=[TextPart("READY")])
|
||||
tool = info.output_tools[0]
|
||||
context = next(
|
||||
(json.loads(p.content) for m in reversed(messages) for p in m.parts if isinstance(p, UserPromptPart)),
|
||||
{},
|
||||
)
|
||||
fields = [name for name, kind in context.get("fields", {}).items() if kind == "MATRIX"][:2]
|
||||
template = {
|
||||
"name": "合成流水线模板",
|
||||
"description": "合成模型研究假设",
|
||||
"expression": "rank({field})",
|
||||
"variables": {
|
||||
"field": {"kind": "field", "field_type": "MATRIX", "values": fields or ["TEST_FIN_001"]}
|
||||
},
|
||||
}
|
||||
properties = tool.parameters_json_schema.get("properties", {})
|
||||
if "summary" in properties:
|
||||
data = {"summary": "合成评估建议", "risks": ["仅供验收"], "suggestions": ["继续核实缺失证据"]}
|
||||
elif "input_ids" in properties:
|
||||
data = {
|
||||
"name": "合成特征方案",
|
||||
"hypothesis": context.get("hypothesis", "合成假设"),
|
||||
"input_ids": [i["id"] for i in context.get("inputs", [])],
|
||||
"steps": [],
|
||||
"template": template,
|
||||
}
|
||||
else:
|
||||
data = template
|
||||
return ModelResponse(parts=[ToolCallPart(tool.name, data)])
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def fake_model(config, settings):
|
||||
yield FunctionModel(stream_function=fake_stream, model_name="test-model")
|
||||
yield FunctionModel(function=fake_structured, stream_function=fake_stream, model_name="test-model")
|
||||
|
||||
Reference in New Issue
Block a user