LLM Integration
Relevant source files
The following files were used as context for generating this wiki page:
The LLM (Large Language Model) integration is a core component of the Resume Matcher backend, responsible for all AI-powered features. It provides a unified, robust, and configurable interface for interacting with various local and cloud-based LLM providers. The system is designed to handle tasks such as resume parsing, content tailoring, and generating supplementary documents like cover letters.
The central module for this integration is apps/backend/app/llm.py, which leverages the litellm library to abstract away the differences between providers. This allows the application to support a wide range of models from providers like OpenAI, Anthropic, Google Gemini, and local models via Ollama, all through a consistent set of functions.
Architecture Overview
The backend's LLM architecture is designed for flexibility and robustness. API routers and services make calls to a dedicated llm.py module, which acts as a wrapper around litellm. This wrapper handles provider-specific settings, API key management, JSON mode enforcement, and retry logic. Configuration is managed through environment variables and a JSON configuration file, which can be updated via the frontend settings page.
This diagram shows the high-level flow of an LLM-powered request.
LLM Configuration
LLM provider settings can be configured either through the frontend Settings page or by setting environment variables in the backend's .env file. The frontend provides a user-friendly interface for selecting a provider and entering credentials.
Sources: apps/frontend/app/(default)/settings/page.tsx:290-410, SETUP.md, INSTALL.md
Supported Providers
The system uses LiteLLM, enabling support for numerous providers. The primary ones configured in the UI are:
| Provider | LLM_PROVIDER Value | Default Model | API Key Required |
|---|---|---|---|
| OpenAI | openai | gpt-4o-mini | Yes |
| Anthropic | anthropic | claude-3-5-sonnet-20241022 | Yes |
| OpenRouter | openrouter | anthropic/claude-3.5-sonnet | Yes |
| Google Gemini | gemini | gemini-1.5-flash | Yes |
| DeepSeek | deepseek | deepseek-chat | Yes |
| Ollama | ollama | llama3.2 | No (uses LLM_API_BASE) |
Sources: apps/frontend/app/(default)/settings/page.tsx:50-56, SETUP.md, AGENTS.md, INSTALL.md
Configuration Parameters
The LLM is configured via pydantic-settings in app/config.py and can be updated via the /api/v1/config endpoints.
| Parameter | Environment Variable | UI Control | Description |
|---|---|---|---|
| Provider | LLM_PROVIDER | Provider Selection | The name of the LLM provider (e.g., openai, ollama). |
| Model | LLM_MODEL | Model Input | The specific model to use (e.g., gpt-4o-mini). |
| API Key | LLM_API_KEY | API Key Input | The API key for the selected cloud provider. |
| API Base URL | LLM_API_BASE | Ollama Server URL | The base URL for the API, primarily used for Ollama. |
| Content Language | CONTENT_LANGUAGE | Content Language | The language for generated content (e.g., en, es). |
Sources: apps/backend/app/config.py, apps/frontend/app/(default)/settings/page.tsx:348-410, apps/backend/app/routers/resumes.py:68-73
Core Functionality (llm.py)
The apps/backend/app/llm.py module is the central hub for all LLM interactions. It provides specialized functions for different types of completions and includes robust error handling and data extraction logic.
Sources: apps/backend/app/llm.py, AGENTS.md
Key Functions
| Function | Description |
|---|---|
complete_json() | Asynchronously calls an LLM and ensures the output is a valid JSON object. It automatically enables JSON mode for supported providers and includes retry logic. |
complete() | Asynchronously calls an LLM for plain text generation (e.g., cover letters). Includes basic error handling. |
_extract_json() | A helper function that robustly extracts a JSON object from a string, even if it's embedded in markdown code blocks or surrounded by other text. It includes protection against infinite recursion. |
health_check() | A function to test the connection to the configured LLM provider. |
Sources: apps/backend/app/llm.py, REPORT.md:28-31
JSON Completion and Retry Logic
The complete_json() function is critical for features that require structured data, like resume parsing. It enhances reliability with several features:
- JSON Mode: It automatically sets
response_format={"type": "json_object"}for providers that support it (OpenAI, Anthropic, Gemini, etc.). - Retry Mechanism: If the initial LLM call fails or returns invalid JSON, it automatically retries up to 2 times.
- Temperature Adjustment: On each retry, the
temperatureis lowered (from 0.1 to 0.0) to encourage more deterministic and correct output. - Robust Extraction: It uses
_extract_json()to find and parse the JSON object from the LLM's raw response.
This diagram illustrates the retry flow within complete_json.
Sources: apps/backend/app/llm.py:176-218, AGENTS.md
Asynchronous Safety
The integration is designed to work safely in an asynchronous FastAPI environment.
- API Key Handling: To prevent race conditions, API keys are passed directly to
litellm.acompletion(api_key=...)on each call, rather than modifying the globalos.environ. - Error Handling: LLM functions use a
try...exceptblock to catch exceptions. Detailed errors are logged on the server, but a genericValueErrororHTTPExceptionis raised to the client to avoid exposing internal implementation details.
Sources: AGENTS.md, REPORT.md:73-76, apps/backend/app/llm.py:88-91, 131-135
Prompt Management
LLM prompts are centralized in the apps/backend/app/prompts/ directory, separating them from business logic. This makes them easier to manage, version, and refine.
Sources: apps/backend/app/prompts/templates.py, apps/backend/app/prompts/enrichment.py
Prompt Templates
Prompts are defined as f-strings in Python files, using {variable} placeholders for dynamic content injection. They often include an example of the desired output schema to guide the LLM.
| Prompt Constant | File | Purpose |
|---|---|---|
PARSE_RESUME_PROMPT | templates.py | Parses raw resume text into a structured JSON object based on RESUME_SCHEMA_EXAMPLE. |
IMPROVE_RESUME_PROMPT | templates.py | Tailors a resume for a specific job description, rephrasing content to match keywords. |
EXTRACT_KEYWORDS_PROMPT | templates.py | Extracts key requirements, skills, and experience level from a job description. |
COVER_LETTER_PROMPT | templates.py | Generates a brief, professional cover letter based on a resume and job description. |
OUTREACH_MESSAGE_PROMPT | templates.py | Generates a concise networking message for LinkedIn or email. |
ANALYZE_RESUME_PROMPT | enrichment.py | Analyzes a resume to find weak descriptions and generates questions to elicit more detail. |
ENHANCE_DESCRIPTION_PROMPT | enrichment.py | Generates new, improved bullet points for a resume item based on user answers. |
Sources: apps/backend/app/prompts/templates.py, apps/backend/app/prompts/enrichment.py
Internationalization (i18n) in Prompts
The system supports generating content in multiple languages. This is achieved by including an {output_language} placeholder in relevant prompts. The backend retrieves the user's configured content language and passes it to the prompt.
# From apps/backend/app/prompts/templates.py
IMPROVE_RESUME_PROMPT = """Tailor this resume for the job. Output ONLY the JSON object, no other text.
IMPORTANT: Generate ALL text content (summary, descriptions, skills) in {output_language}.
...
"""
The language code (e.g., es) is mapped to its full name (e.g., Spanish) using the LANGUAGE_NAMES dictionary before being inserted into the prompt.
Sources: apps/backend/app/prompts/templates.py:4-13, 131-133, apps/backend/app/routers/resumes.py:270-271
LLM-Powered Services
The LLM integration powers several key features in the application, primarily exposed through the /resumes API router.
Resume Parsing
When a user uploads a resume, the upload_resume endpoint triggers a parsing workflow. The document is first converted to Markdown, and then parse_resume_to_json is called to structure the data.
This sequence diagram shows the resume parsing flow.
Resume Tailoring and Improvement
The /resumes/improve endpoint uses the LLM to tailor a master resume to a specific job description.
The flow is as follows:
- The job description is analyzed to extract keywords (
extract_job_keywords). - The original resume, job description, and keywords are passed to
improve_resume. improve_resumecallscomplete_jsonwith theIMPROVE_RESUME_PROMPT.- If enabled, a cover letter and outreach message are generated in parallel.
- A new, tailored resume record is created in the database.
Sources: apps/backend/app/routers/resumes.py:246-340, apps/backend/app/services/improver.py
Conclusion
The LLM integration in Resume Matcher is a well-architected system that provides the core AI functionality for the application. Its use of litellm ensures provider flexibility, while custom wrappers in llm.py add crucial robustness, including JSON validation, automatic retries, and safe asynchronous operations. By centralizing prompts and clearly defining service functions, the integration is both powerful and maintainable, enabling a rich set of features from resume parsing to multi-lingual content generation.