How dnyaneshs1/ucode works
Purpose and Entry Points
Opti-Prompt is a local Python multi-turn LLM gateway that reduces token use through classification, prompt optimization, caching, and provider failover. It exposes two user-facing entry points:
• CLI: app/main.py
• Streamlit UI: app/streamlit_app.py
Both orchestrate the same core conversation and generation services; API credentials and operational limits are environment-driven via .env.example.
Request Orchestration and Provider Boundary
The central LLM execution layer is core/gemini_wrapper_v2.py. It receives a prompt plus conversation context, selects a provider/model, enforces generation limits, and handles failures/quota exhaustion by continuing through the fallback chain.
• core/provider_registry.py defines available provider configuration/order.
• core/gemini_wrapper_v2.py implements provider-specific invocation and context-aware continuation.
• .env.example supplies keys for Gemini, Groq, HuggingFace, GitHub Models, and OpenRouter.
External inference services are Gemini (primary), Groq, HuggingFace, GitHub Models, and OpenRouter. This is an API-client application, not a self-hosted LLM runtime.
Conversation State and Context Window
Session lifecycle and multi-turn state are owned by core/session_manager.py. Conversation history is persisted locally and constrained to a sliding context window before provider calls. core/context_manager.py supports preservation/transfer of context, especially when a provider returns only a partial response and execution falls back.
• core/session_manager.py
• core/context_manager.py
• app/streamlit_app.py
The UI maintains its Streamlit session state while durable conversation records are managed by the core session layer.
Optimization Pipeline
Before live generation, the system determines likely response complexity and uses it to choose output-token limits. core/complexity_classifier_module.py loads and applies the bundled SetFit classifier; its limits are data-driven.
• core/complexity_classifier_module.py
• classifier/complexity_token_limits.json
• classifier/complexity_classifier/model.safetensors
The classifier artifact and tokenizer/config files under classifier/complexity_classifier/ are shipped with the repository, making complexity decisions local rather than dependent on an external classification API.
Cache Layers
Caching is a first-class pre-provider boundary in core/semantic_cache.py. The documented flow checks an exact SHA256 prompt cache first, then searches semantic similarity before calling a remote model. Semantic matching relies on the bundled sentence-transformer embedder.
• core/semantic_cache.py
• embedder/model.safetensors
• embedder/config.json
Cache hits avoid or reduce new provider-token consumption; live responses can be stored for later exact or semantically similar requests.
Artifacts, Evaluation, and Observability
Generated code/content can be extracted and made available as local artifacts by core/artifact_manager.py. Runtime usage, cache state, complexity class, token use, latency, and provider outcomes are recorded through core/runtime_logger.py; core/evaluation_module.py provides an evaluation boundary around response/system behavior.
• core/artifact_manager.py
• core/runtime_logger.py
• core/evaluation_module.py
Operational data is local filesystem state (sessions, caches, artifacts, CSV usage reporting), rather than a database or deployed observability service.
End-to-End Control Flow
A user submits a turn through app/main.py or app/streamlit_app.py; prior session history is loaded; the prompt is classified and capped; exact/semantic caches are checked; and only a miss reaches core/gemini_wrapper_v2.py. The wrapper calls the registered primary provider, captures partial context if needed, and traverses fallback providers on quota/error. The final response updates session history, caches, artifacts where applicable, and runtime/usage logs.
Main entry points: gemini_wrapper_v2.py LLM wrapper · session_manager.py Session manager · semantic_cache.py Semantic cache · complexity_classifier_module.py Complexity classifier
Components 17
User Entry Points
- CLI
app/main.py - Streamlit UI · Maintains transient Streamlit session state.
app/streamlit_app.py
Core Request Pipeline
- Session manager · Persists history and applies the sliding context window.
core/session_manager.py - Context manager · Preserves context across partial responses and failover.
core/context_manager.py - Complexity classifier · Classifies requests before generation.
core/complexity_classifier_module.py - Semantic cache · Checks exact SHA256 prompts, then semantic similarity.
core/semantic_cache.py - LLM wrapper · Invokes providers, enforces limits, and continues through failover.
core/gemini_wrapper_v2.py - Provider registry · Defines provider configuration and fallback order.
core/provider_registry.py - Artifact manager · Extracts generated code and content as local artifacts.
core/artifact_manager.py - Runtime logger · Records usage, cache, complexity, latency, and provider outcomes.
core/runtime_logger.py - Evaluation module
core/evaluation_module.py
Local Models & State
- Complexity token limits
classifier/complexity_token_limits.json - Classifier model
classifier/complexity_classifier/model.safetensors - Sentence embedder
embedder/model.safetensors - Local filesystem state · Stores sessions, caches, artifacts, and CSV usage reports.
- Environment settings · Supplies provider API keys and operational limits.
.env.example
Inference Providers
- Remote LLM APIs · Gemini primary; Groq, HuggingFace, GitHub Models, and OpenRouter fallback.
Connections 23
- CLI → Session manager: submits turn
- Streamlit UI → Session manager: submits turn
- Session manager → Complexity classifier: prompt with history
- Session manager → Context manager: provides context
- Session manager → Local filesystem state: loads and saves history
- Complexity classifier → Complexity token limits: selects cap
- Complexity classifier → Classifier model: runs locally
- Complexity classifier → Semantic cache: classified request
- Semantic cache → Sentence embedder: semantic lookup
- Semantic cache → LLM wrapper: cache miss
- Semantic cache → Local filesystem state: reads and stores entries
- LLM wrapper → Provider registry: selects provider chain
- LLM wrapper → Remote LLM APIs: primary and fallback calls
- Remote LLM APIs → LLM wrapper: response or quota/error
- LLM wrapper → Context manager: partial-response transfer
- Environment settings → LLM wrapper: API keys and limits
- LLM wrapper → Session manager: final response
- LLM wrapper → Semantic cache: stores live response
- LLM wrapper → Artifact manager: generated content
- LLM wrapper → Runtime logger: execution outcome
- LLM wrapper → Evaluation module: response behavior
- Artifact manager → Local filesystem state: writes artifacts
- Runtime logger → Local filesystem state: writes CSV usage