Lesson 08: Service Layer
"Business logic belongs in services, not routers."
Introduction
The service layer contains all business logic - OpenAI calls, workflows, document processing, and monitoring.
Services Overview
- openai_service.py - LLM interactions
- monitoring_service.py - Cost tracking
- multi_agent_email_service.py - Workflow orchestration
- document_service.py - RAG operations
- rag_graph_service.py - LangGraph workflows
openai_service.py Functions
generate_agent_response()
Purpose: Chat completion with OpenAI Parameters: system_prompt, user_message, agent_name, language, tone Returns: str (assistant response)
analyze_email()
Purpose: Extract structured data from email Returns: dict with summary, intent, priority, deadline, action_items, suggested_reply
generate_rag_answer()
Purpose: Generate answer from retrieved context Parameters: question, contexts, agent_name, language, tone
evaluate_retrieved_context()
Purpose: Check if context contains answer Returns: dict with has_answer, confidence, reason
monitoring_service.py
estimate_tokens()
Approximates token count (1 token ≈ 4 characters)
estimate_openai_cost()
Calculates cost based on model pricing
multi_agent_email_service.py
run_analysis_agent()
Extract structured data from email
run_reply_agent()
Generate professional reply
run_reviewer_agent()
Check quality and approve/reject
document_service.py
split_text_into_chunks()
Breaks documents into segments (800 chars with 100 overlap)
create_embedding()
Generates OpenAI embedding vector
add_chunk_to_vector_store()
Saves to ChromaDB
search_agent_documents()
Semantic search for relevant chunks
rag_graph_service.py
RAGGraphState
TypedDict defining workflow state
Nodes
- retrieve_node: Fetch chunks
- evaluate_node: Check quality
- answer_node: Generate response
- refusal_node: Return "don't know"
build_corrective_rag_graph()
Constructs LangGraph state machine
Key Takeaways
🎯 Services contain business logic separated from HTTP 🎯 Each service wraps specific functionality 🎯 Services are testable without HTTP clients 🎯 LangGraph enables complex workflows
Next Steps
Module 2 · Data Layer