0829.1.1.mov
0829.1.mov
A sophisticated AI research agent that combines Schema-Guided Reasoning (SGR) with OpenAI Function Calls to create a natural, interpretable, and powerful research workflow with persistent context memory.
Traditional agents either use pure function calls (losing reasoning transparency) or structured output with local execution (missing natural LLM behavior). This agent combines the best of both worlds with persistent memory across sessions.
- Reasoning as a Tool -
generate_reasoningfunction call - Controlled via
tool_choice="generate_reasoning" - Structured Output for explicit reasoning analysis
- Model explains what to do and why
- Pure analytical thinking without tool execution
- Full transparency into decision-making process
- Native OpenAI Function Calls with
tool_choice="auto" - Model naturally chooses appropriate tools based on reasoning
- Preserves LLM's natural conversation flow
- No disruption to chat template or message structure
- Task Summaries - automatically created after each completed task
- Session History - remembers previous requests and actions across tasks
- File Memory - tracks created and modified files
- Anti-Forgetting - context is preserved between different user queries
- Web Search - Internet research via Tavily API
- Report Generation - Comprehensive reports with citations
- Date/Time Awareness - Gets current date for time-sensitive queries
- Adaptive Planning - Real-time strategy adjustment
- Read Files - Analyze local file content
- Create Files - Generate new files with specified content
- Update Files - Modify existing files (append, prepend, replace)
- File Memory - Remembers all created files across sessions
- List Directories - Browse file structure with tree view
- Create Directories - Build new folder structures
- Recursive Exploration - Deep directory analysis
- Clarification - Asks questions when requests are unclear
- Simple Answers - Quick responses without formal reports
- Multi-language Support - Russian and English
- Context Awareness - References previous conversations
- Task History - "What did I ask before?"
- Action Memory - "What files did we create?"
- Continuous Context - No information loss between tasks
- Smart Summaries - Efficient context compression
- Both phases use native OpenAI function calling
- Phase 1:
tool_choice="generate_reasoning"- forced reasoning - Phase 2:
tool_choice="auto"- natural tool selection - Maintains proper chat message flow throughout
- Model decides tool usage naturally within OpenAI's framework
- Every decision is explicitly reasoned
- Clear explanation of why each action is taken
- Transparent thought process at each step
- Easy debugging and understanding
- Cross-session continuity - remembers previous interactions
- Task summaries - compact history storage
- File tracking - knows what was created/modified
- Context integration - seamlessly uses previous information
- Real-time adaptation based on new information
- Context-aware decision making
- Anti-cycling mechanisms to prevent loops
- Dynamic re-planning when needed
βββ sgr_agent.py # π― Main orchestration engine
βββ models.py # π Pydantic models for type safety
βββ tool_schemas.py # π οΈ OpenAI function schemas
βββ executors.py # β‘ Tool execution logic
βββ prompts.yaml # π¬ System prompts configuration
βββ config.yaml.example # βοΈ Configuration template
βββ requirements.txt # π¦ Python dependencies
βββ gui_app.py # π Chainlit web interface
βββ api_server.py # π FastAPI OpenAI-compatible server
βββ test_openai_client.py # π§ͺ OpenAI client test script
βββ start_api.sh # π API server startup script
βββ API_README.md # π API server documentation
graph TD
A[User Query] --> B[Load Previous Context]
B --> C[Phase 1: SGR Analysis]
C --> D[Structured Output Call]
D --> E[ReasoningStep Model]
E --> F{Validation}
F -->|Pass| G[Phase 2: Tool Execution]
F -->|Fail| C
G --> H[Function Calls Auto]
H --> I[Local Tool Execution]
I --> J[Update Context]
J --> K{Task Complete?}
K -->|No| C
K -->|Yes| L[Create Task Summary]
L --> M[Save to Global Context]
M --> N[Task Completion]
generate_reasoning- Analyze situation and plan next stepsclarification- Ask clarifying questions when request is unclearsimple_answer- Provide quick, direct answers
web_search- Search the internet for informationcreate_report- Generate comprehensive reports with citationsget_current_datetime- Get current date and time
read_local_file- Read content from local filescreate_local_file- Create new files with specified contentupdate_local_file- Modify existing files (append, prepend, replace)
list_directory- Show contents of directories (supports tree view)create_directory- Create new directories (with user confirmation)
report_completion- Mark tasks as completed
pip install -r requirements.txtor
pip install uv
uv syncexport OPENAI_API_KEY="your-openai-key"
export TAVILY_API_KEY="your-tavily-key"Or create config.yaml:
openai:
api_key: "your-openai-key"
model: "gpt-4o"
temperature: 0.3
tavily:
api_key: "your-tavily-key"
execution:
max_rounds: 8
max_searches_total: 6python sgr_agent.pychainlit run gui_app.py -wThe web interface will be available at http://localhost:8000
Web Interface Features:
- π Beautiful chat interface
- π Real-time progress tracking
- π Formatted reports and results
- π¨ Visual feedback for all operations
- π± Mobile-friendly design
- π Auto-reload during development (with
-wflag)
π Enter research task: Find current Bitcoin price
π§ Analysis: Need current date for accurate pricing
π Getting current date: 2025-08-29
π Search: 'Bitcoin price 29 August 2025'
π¬ Answer: Bitcoin is trading at $166,912 (projected)
π Enter research task: Create a Python script for data analysis
π§ Analysis: User wants Python script creation
π Creating file: data_analysis.py
β
File created with data processing functions
π Enter research task: What did I ask before?
π§ Analysis: Checking previous session history
π Previous actions:
- Request: 'Find Bitcoin price' β Actions: web search, simple answer
- Request: 'Create Python script' β Actions: file creation
π¬ Answer: You previously asked about Bitcoin price and Python script creation
OPENAI_API_KEY: Your OpenAI API keyTAVILY_API_KEY: Your Tavily search API keyOPENAI_MODEL: Model to use (default: gpt-4o)MAX_ROUNDS: Maximum research rounds (default: 8)MAX_SEARCHES_TOTAL: Maximum searches per session (default: 6)
Edit prompts.yaml to customize system prompts:
structured_output_reasoning:
template: |
You are a reasoning module...
# Customize reasoning instructions
outer_system:
template: |
You are an expert researcher...
# Customize main system promptThe agent maintains memory across sessions through:
- Task Summaries - Each completed task creates a structured summary
- History Integration - Previous actions are loaded into new conversations
- File Tracking - All created/modified files are remembered
- Smart Context - Relevant history is automatically included
- Prevents repetitive clarification requests
- Detects and breaks reasoning loops
- Ensures forward progress on tasks
- Automatic language detection from user input
- Consistent language usage throughout responses
- Russian and English support
- Graceful handling of API failures
- Structured output validation with fallbacks
- Context preservation during errors
Session 1:
User: "Research Tesla Model S pricing"
Agent: Creates comprehensive report β Saves to context
Session 2:
User: "What did I research before?"
Agent: "You researched Tesla Model S pricing and created a report"
Session 3:
User: "Now compare with BMW i7"
Agent: References previous Tesla research β Creates comparison
The project includes a FastAPI-based API server that provides full OpenAI API compatibility with streaming support and SGR integration.
- π OpenAI API Compatibility - Drop-in replacement for OpenAI API
- π Streaming Support - Real-time streaming with
stream: true - π§ SGR Integration - Full Schema-Guided Reasoning capabilities
- π Auto-documentation - Swagger/ReDoc documentation
# Install dependencies
pip install -r requirements.txt
# Start API server
python api_server.pyThe API server will be available at:
- API Endpoint: http://localhost:8000/v1/chat/completions
- Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
from openai import OpenAI
# Point to local API
client = OpenAI(
api_key="dummy-key", base_url="http://localhost:8000/v1" # Not used by local API
)
# Same API calls as OpenAI
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Research Python"}],
stream=False,
)The project includes a modern web interface built with Chainlit that provides a superior user experience compared to the console version.
- Interactive Chat - Natural conversation flow with the AI agent
- Beautiful UI - Modern, responsive design that works on all devices
- Real-time Updates - Watch the agent's reasoning and tool execution live
- Rich Content - Formatted reports, code blocks, and structured data display
- Progress Tracking - Visual indicators for search progress and tool execution
- Session Management - Automatic context preservation between conversations
- Error Handling - Graceful error display and recovery options
# Install chainlit if not already installed
pip install chainlit>=1.0.0
# Start the web interface with auto-reload
chainlit run gui_app.py -w
# Interface will be available at http://localhost:8000| Feature | Console | Web Interface |
|---|---|---|
| User Experience | Text-based | Rich, interactive |
| Visual Feedback | Limited | Comprehensive |
| Progress Tracking | Basic | Real-time with animations |
| Report Display | Plain text | Formatted with syntax highlighting |
| Mobile Support | No | Yes |
| Multi-session | Manual | Automatic |
| File Operations | Text output | Visual file browser |
The web interface automatically uses the same configuration as the console version:
- API keys from
config.yamlor environment variables - Same tool set and capabilities
- Identical reasoning and memory systems
- Fork the repository
- Create a feature branch
- Make your changes with type hints and tests
- Update documentation as needed
- Submit a pull request
MIT License - see LICENSE file for details.
Built with β€οΈ for transparent, powerful AI research automation with persistent memory