Skip to content
Module 2 · Data LayerBeginner1 min read · 239 words

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

  1. openai_service.py - LLM interactions
  2. monitoring_service.py - Cost tracking
  3. multi_agent_email_service.py - Workflow orchestration
  4. document_service.py - RAG operations
  5. 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

👉 Lesson 09: API Routers →


Module 2 · Data Layer