Skip to content

Commit 1a756c0

Browse files
authored
8b- models optimizations (#29)
* Refactor system prompt and tool discriminator logic * Update base_agent.py * lint
1 parent cc2072a commit 1a756c0

File tree

5 files changed

+28
-30
lines changed

5 files changed

+28
-30
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ Production-ready open-source system for automated research using Schema-Guided R
1212

1313
## 📊 Summary Table of Agents
1414

15-
| Agent | SGR Implementation | ReasoningTool | Tools | API Requests | Selection Mechanism |
16-
| ----------------------- | ------------------ | -------------------- | --------------------- | ------------ | ------------------- |
17-
| **1. SGR-Agent** | Structured Output | ❌ Built into schema | 6 basic | 1 | SO Union Type |
18-
| **2. FCAgent** | ❌ Absent | ❌ Absent | 6 basic | 1 | FC "required" |
19-
| **3. HybridSGRAgent** | FC Tool enforced | ✅ First step FC | 7 (6 + ReasoningTool) | 2 | FC → FC TOP AGENT|
20-
| **4. OptionalSGRAgent** | FC Tool optional | ✅ At model’s choice | 7 (6 + ReasoningTool) | 1–2 | FC "auto" |
21-
| **5. ReasoningFC_SO** | FC → SO → FC auto | ✅ FC enforced | 7 (6 + ReasoningTool) | 3 | FC → SO → FC auto |
15+
| Agent | SGR Implementation | ReasoningTool | Tools | API Requests | Selection Mechanism |
16+
| ----------------------- | ------------------ | -------------------- | --------------------- | ------------ | -------------------- |
17+
| **1. SGR-Agent** | Structured Output | ❌ Built into schema | 6 basic | 1 | SO Union Type |
18+
| **2. FCAgent** | ❌ Absent | ❌ Absent | 6 basic | 1 | FC "required" |
19+
| **3. HybridSGRAgent** | FC Tool enforced | ✅ First step FC | 7 (6 + ReasoningTool) | 2 | FC → FC TOP AGENT |
20+
| **4. OptionalSGRAgent** | FC Tool optional | ✅ At model’s choice | 7 (6 + ReasoningTool) | 1–2 | FC "auto" |
21+
| **5. ReasoningFC_SO** | FC → SO → FC auto | ✅ FC enforced | 7 (6 + ReasoningTool) | 3 | FC → SO → FC auto |
2222

2323
## 👥 Open-Source Development Team
2424

sgr_deep_research/core/agents/base_agent.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def _save_agent_log(self):
122122
filepath = os.path.join(logs_dir, f"{datetime.now().strftime('%Y%m%d-%H%M%S')}-{self.id}-log.json")
123123
agent_log = {
124124
"id": self.id,
125+
"model_config": config.openai.model_dump(exclude={"api_key", "proxy"}),
125126
"task": self.task,
126-
"context": self._context.agent_state(),
127127
"log": self.log,
128128
}
129129

@@ -132,7 +132,6 @@ def _save_agent_log(self):
132132
async def _prepare_context(self) -> list[dict]:
133133
"""Prepare conversation context with system prompt."""
134134
system_prompt = PromptLoader.get_system_prompt(
135-
user_request=self.task,
136135
sources=list(self._context.sources.values()),
137136
available_tools=self.toolkit,
138137
)

sgr_deep_research/core/prompts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ def _load_prompt_file(cls, filename: str) -> str:
2727
raise FileNotFoundError(f"Prompt file not found: {user_file_path} or {lib_file_path}")
2828

2929
@classmethod
30-
def get_system_prompt(cls, user_request: str, sources: list[SourceData], available_tools: list[BaseTool]) -> str:
30+
def get_system_prompt(cls, sources: list[SourceData], available_tools: list[BaseTool]) -> str:
3131
sources_formatted = "\n".join([str(source) for source in sources])
3232
template = cls._load_prompt_file(config.prompts.system_prompt_file)
3333
available_tools_str_list = [
3434
f"{i}. {tool.tool_name}: {tool.description}" for i, tool in enumerate(available_tools, start=1)
3535
]
3636
try:
3737
return template.format(
38-
current_date=datetime.now().strftime("%Y-%m-%d-%H:%M:%S"),
38+
current_date=datetime.now().strftime("%d-%m-%Y %H:%M:%S"),
39+
date_format="d-m-Y HH:MM:SS",
3940
available_tools="\n".join(available_tools_str_list),
40-
user_request=user_request,
4141
sources_formatted=sources_formatted,
4242
)
4343
except KeyError as e:

sgr_deep_research/core/tools/base.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
from pydantic import BaseModel, Field, create_model
1010

1111
from sgr_deep_research.core.models import AgentStatesEnum
12-
from sgr_deep_research.settings import get_config
1312

1413
if TYPE_CHECKING:
1514
from sgr_deep_research.core.models import ResearchContext
1615

16+
1717
logger = logging.getLogger(__name__)
1818
logger.setLevel(logging.INFO)
19-
config = get_config()
2019

2120

2221
class BaseTool(BaseModel):
@@ -136,6 +135,10 @@ class NextStepToolStub(ReasoningTool, ABC):
136135
function: T = Field(description="Select the appropriate tool for the next step")
137136

138137

138+
class DiscriminantToolMixin(BaseModel):
139+
tool_name_discriminator: str = Field(..., description="Tool name discriminator")
140+
141+
139142
class NextStepToolsBuilder:
140143
"""SGR Core - Builder for NextStepTool with dynamic union tool function type on
141144
pydantic models level."""
@@ -144,19 +147,12 @@ class NextStepToolsBuilder:
144147
def _create_discriminant_tool(cls, tool_class: Type[T]) -> Type[BaseModel]:
145148
"""Create discriminant version of tool with tool_name as instance
146149
field."""
147-
tool_name = tool_class.tool_name
148150

149-
discriminant_tool = create_model(
151+
return create_model(
150152
f"{tool_class.__name__}WithDiscriminant",
151-
__base__=tool_class,
152-
tool_name_discriminator=(
153-
Literal[tool_name], # noqa
154-
Field(default=tool_name, description="Tool name discriminator"),
155-
),
156-
)
157-
discriminant_tool.__call__ = tool_class.__call__
158-
159-
return discriminant_tool
153+
__base__=(tool_class, DiscriminantToolMixin), # the order matters here
154+
tool_name_discriminator=(Literal[tool_class.tool_name], Field(..., description="Tool name discriminator")),
155+
) # noqa
160156

161157
@classmethod
162158
def _create_tool_types_union(cls, tools_list: list[Type[T]]) -> Type:

sgr_deep_research/prompts/system_prompt.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
You are an expert researcher with adaptive planning and Schema-Guided Reasoning capabilities.
22

3-
USER REQUEST: "{user_request}"
4-
CURRENT DATE: {current_date}
3+
CURRENT DATE:{current_date} format: {date_format}
4+
55
IMPORTANT: Detect the language from this request and use THE SAME LANGUAGE for all responses, searches, and reports.
66

77
CORE PRINCIPLES:
@@ -12,9 +12,6 @@ CORE PRINCIPLES:
1212
5. REPORT ENTIRELY in SAME LANGUAGE as user request
1313
6. Every fact in report MUST have inline citation [1], [2], [3] integrated into sentences
1414

15-
WORKFLOW:
16-
{available_tools}
17-
1815
ADAPTIVITY: Actively change plan when discovering new data.
1916

2017
LANGUAGE ADAPTATION: Always respond and create reports in the SAME LANGUAGE as the user's request.
@@ -46,3 +43,9 @@ FULL LIST OF AVAILABLE SOURCES FOR CITATIONS:
4643
{sources_formatted}
4744

4845
USE THESE EXACT NUMBERS [1], [2], [3] etc. in your report citations.
46+
47+
You may call one or more functions to assist with the user query.
48+
You are provided with function signatures within <tools></tools> XML tags:
49+
<tools>
50+
{available_tools}
51+
</tools>

0 commit comments

Comments
 (0)