How vivenx/ai-agent-for-leasing works
1. Top-level shape
- The repo is a single Python service split into two layers: a FastAPI HTTP application in api/ and a reusable domain package in leasing_analyzer/.
- The api layer is only the delivery boundary: it serves the UI, exposes REST endpoints, and delegates all business work to the package.
2. HTTP/UI boundary
- api/main.py is the main runtime entrypoint.
- It serves:
- GET / for the local web UI from api/templates/index.html
- static assets from api/static/
- GET /health
- analysis endpoints under /api/*
- The frontend is a lightweight browser client (script.js, style.css) that calls the backend; there is no separate SPA framework.
3. Core analysis pipeline
- leasing_analyzer/services/pipeline.py is the orchestration layer for the main “analyze an item” flow.
- It wires together search, fetching, parsing, market aggregation, and optional AI assistance.
- This is the central subsystem to place in the diagram because most requests eventually pass through it.
4. Search and page acquisition
- leasing_analyzer/services/search.py builds search queries and collects candidate result URLs, likely via Serper/Google.
- leasing_analyzer/services/fetcher.py retrieves target pages using Selenium, which makes browser availability part of the runtime architecture.
- This layer depends on external websites and browser automation, so it is the main operational bottleneck and failure point for market analysis.
5. Parsing and normalization
- leasing_analyzer/parsing/ contains site-specific and generic parsers:
- avito.py for Avito-specific logic
- basic.py and base.py for generic parsing contracts
- content_cleaner.py and helpers.py for text cleanup and normalization
- The parser layer converts raw fetched HTML/text into structured offer data that the market layer can aggregate.
6. Market computation and reporting
- leasing_analyzer/services/market.py computes the market picture from parsed offers: ranges, medians, comparisons, and deviation from client price.
- leasing_analyzer/services/output.py shapes the final response payload returned by the API.
- leasing_analyzer/services/specs.py appears to hold item/specification extraction or comparison helpers used during report assembly.
7. AI and external model clients
- leasing_analyzer/clients/ isolates outbound model/API integrations:
- gigachat.py for GigaChat
- sonar.py for Perplexity Sonar
- ai_analyzer.py as a higher-level AI abstraction
- These clients are optional dependencies used for text interpretation, characteristics extraction, analog comparison, and deep analysis.
- They are separated from the pipeline so the rest of the system can run with partial AI capability.
8. Document analysis subsystem
- leasing_analyzer/document/extractors.py handles file-text extraction for .txt, .docx, and .pdf.
- leasing_analyzer/document/service.py converts uploaded documents into the same downstream analysis flow used by the text API.
- This creates an important boundary: document ingestion first, then normalization into item name, price, and characteristics, then market analysis.
9. Session memory and context
- leasing_analyzer/memory/ provides a persistent session-memory feature keyed by sessionId.
- models.py, repository.py, and service.py indicate a simple persistence abstraction, backed by SQLite according to the README.
- This memory layer is used to retain short conversational context across multiple requests without coupling it to the HTTP layer.
10. Cross-cutting core utilities
- leasing_analyzer/core/ contains shared infrastructure:
- config.py for environment-driven settings
- logging.py for log setup
- rate_limit.py for throttling external calls
- sessions.py for session handling
- models.py, utils.py, and audit.py for shared structures and helpers
- These modules are not domain logic; they support reliability, observability, and request-level controls.
11. End-to-end request flow
- For POST /api/describe, the flow is: HTTP request → pipeline → search → fetcher → parsers → market computation → optional AI/Sonar augmentation → response assembly.
- For POST /api/analyze-document, the flow is: file upload → extractor → GigaChat-based text interpretation → same market pipeline → response assembly.
- Memory can be consulted and updated at the start/end of these flows when sessionId is present.
12. Operational dependencies and boundaries
- The service depends on several external systems that materially shape the architecture:
- Serper/search API for discovery
- GigaChat for primary AI extraction
- Perplexity/Sonar for analog reasoning
- Selenium + Chrome for market-page retrieval
- SQLite for local memory persistence
- Because these are external and failure-prone, the architecture is intentionally layered so each integration is isolated behind a client/service boundary.
13. Tests and validation
- The test suite is small and focused, with examples around the GigaChat client and seller deduplication.
- verify_env.py is part of operational validation, checking that the required environment is present before running the service.
- This suggests the repo is oriented more toward integration-heavy runtime behavior than pure unit-test coverage.
14. Architecture summary for a diagram
- Draw it as: browser UI and API clients on the left, FastAPI entrypoint in the center, orchestration services beneath, and external integrations on the right/bottom.
- The two main domain flows are “text item analysis” and “document analysis,” both converging on the same market-analysis core.
- Memory, logging, config, and rate limiting are shared support services spanning both flows.
Main entry points: pipeline.py Analysis pipeline · main.py FastAPI app · service.py Doc service · ai_analyzer.py AI analyzer
Components 19
UI & API
HTTP entrypoint, browser UI, and request handlers
- FastAPI app · Main runtime entrypoint serving UI, health, and analysis endpoints
api/main.py - Browser UI · Lightweight browser client and static assets
api/templates/index.html
Domain Core
Reusable analysis, parsing, memory, and support services
- Analysis pipeline · Coordinates search, fetch, parsing, market analysis, and AI augmentation
leasing_analyzer/services/pipeline.py - Search service · Builds queries and collects candidate result URLs
leasing_analyzer/services/search.py - Page fetcher · Retrieves target pages through Selenium-driven browsing
leasing_analyzer/services/fetcher.py - Parsers · Site-specific and generic parsers that turn raw pages into structured offers
leasing_analyzer/parsing - Market engine · Computes ranges, medians, comparisons, and deviation from client price
leasing_analyzer/services/market.py - Response builder · Assembles the final API response payload
leasing_analyzer/services/output.py - Specs helper · Item/spec extraction and comparison helpers used during report assembly
leasing_analyzer/services/specs.py - Doc service · Converts uploaded documents into the downstream analysis flow
leasing_analyzer/document/service.py - File extractors · Extracts text from txt, docx, and pdf uploads
leasing_analyzer/document/extractors.py - AI analyzer · Higher-level AI layer for text interpretation and extraction
leasing_analyzer/clients/ai_analyzer.py - Session memory · Stores short conversational context keyed by sessionId
leasing_analyzer/memory/service.py - Core services · Config, logging, rate limiting, sessions, audit, and shared utilities
leasing_analyzer/core/config.py
External Systems
Third-party services and runtime dependencies
- GigaChat client · Primary outbound client for AI-assisted extraction
leasing_analyzer/clients/gigachat.py - Sonar client · Perplexity Sonar integration for analog reasoning
leasing_analyzer/clients/sonar.py - SQLite store · Local backing store for session memory
- Selenium/Chrome · Browser automation runtime used for page retrieval
- Search API · External search provider used to find candidate pages
Connections 21
- Browser UI → FastAPI app: calls · Browser client sends requests to the FastAPI backend
- FastAPI app → Analysis pipeline: routes text analyze · HTTP analysis endpoints delegate to the main pipeline
- FastAPI app → Doc service: routes document analyze · Uploaded documents are handed to the document flow
- FastAPI app → Session memory: session context · Optional session state is loaded and updated around requests
- Analysis pipeline → Search service: discover · Finds candidate listing URLs and queries
- Analysis pipeline → Page fetcher: fetch · Retrieves candidate pages for analysis
- Analysis pipeline → Parsers: normalize · Parses raw content into structured offers
- Analysis pipeline → Market engine: aggregate · Computes market statistics and comparisons
- Analysis pipeline → AI analyzer: augment · Optional AI-assisted interpretation and extraction
- Analysis pipeline → Response builder: assemble · Builds the final API payload
- Analysis pipeline → Specs helper: compare specs · Uses item specification helpers during report assembly
- Doc service → File extractors: extract text · Reads text from uploaded files before analysis
- Doc service → GigaChat client: interpret text · Uses GigaChat for document understanding and extraction
- Doc service → Analysis pipeline: handoff · Converges back into the shared analysis pipeline
- AI analyzer → GigaChat client: primary model · Wraps the GigaChat client for AI operations
- AI analyzer → Sonar client: fallback reasoning · Uses Sonar for additional model-based reasoning
- Page fetcher → Selenium/Chrome: drives · Requires Selenium/Chrome to retrieve pages
- Search service → Search API: queries · Search discovery relies on an external search provider
- Session memory → SQLite store: persists · Session state is stored locally in SQLite
- FastAPI app → Core services: uses · HTTP layer relies on shared config, logging, rate limits, and session helpers
- Analysis pipeline → Core services: uses · Core services support orchestration and external-call controls