LLM API¶
factory
¶
LLM Provider Factory - intelligently selects the best provider.
Priority order: 1. MCP Delegation (if in Claude Code/Cursor context) 2. Explicit CLI flags (--llm, --model) 3. Environment variables (NERVAPACK_LLM_PROVIDER, *_API_KEY) 4. Config file (.nervapack/config.yaml) 5. Ollama (default fallback)
detect_mcp_context()
¶
Detect if NervaPack is running in an MCP context.
Returns:
| Type | Description |
|---|---|
bool
|
True if running as MCP server (e.g., through Claude Code) |
Source code in src/nervapack/llm/factory.py
get_llm_provider(provider=None, model=None, api_key=None, prefer_mcp=True)
¶
Get the appropriate LLM provider based on context and configuration.
Priority order: 1. MCP delegation (if in MCP context and prefer_mcp=True) 2. Explicit parameters (provider, model, api_key) 3. Environment variables 4. Config file 5. Ollama (default)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
Optional[str]
|
Explicit provider name ("ollama", "claude", "openai", "mcp") |
None
|
model
|
Optional[str]
|
Model name (provider-specific) |
None
|
api_key
|
Optional[str]
|
API key for cloud providers |
None
|
prefer_mcp
|
bool
|
If True, auto-detect and prefer MCP delegation |
True
|
Returns:
| Type | Description |
|---|---|
LLMProvider
|
Configured LLM provider |
Raises:
| Type | Description |
|---|---|
ValueError
|
If provider is invalid or required config is missing |
Source code in src/nervapack/llm/factory.py
load_config_file(project_root='.')
¶
Load configuration from .nervapack/config.yaml.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_root
|
str
|
Project root directory |
'.'
|
Returns:
| Type | Description |
|---|---|
dict
|
Config dict, or empty dict if file doesn't exist |
Source code in src/nervapack/llm/factory.py
base
¶
Base LLM Provider abstraction for NervaPack.
Supports multiple LLM backends: - MCP Delegation (Claude Code/Cursor - uses existing auth) - Ollama (local, privacy-first) - Claude API (cloud, direct) - OpenAI API (cloud, direct)
LLMProvider
¶
Bases: ABC
Abstract base class for LLM providers.
Source code in src/nervapack/llm/base.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
chat(messages, system_prompt='', temperature=0.0, max_tokens=1024)
abstractmethod
¶
Send a chat completion request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
List[Dict[str, str]]
|
List of {role: str, content: str} |
required |
system_prompt
|
str
|
Optional system message |
''
|
temperature
|
float
|
Sampling temperature (0.0 = deterministic) |
0.0
|
max_tokens
|
int
|
Maximum tokens to generate |
1024
|
Returns:
| Type | Description |
|---|---|
str
|
Response text content |
Raises:
| Type | Description |
|---|---|
LLMProviderError
|
If the request fails |
Source code in src/nervapack/llm/base.py
validate_config()
abstractmethod
¶
Check if provider is properly configured.
Returns:
| Type | Description |
|---|---|
bool
|
True if provider can make requests, False otherwise |
get_provider_name()
abstractmethod
¶
Get the provider name for logging/display.
Returns:
| Type | Description |
|---|---|
str
|
Provider name (e.g., "ollama", "claude-api", "mcp-delegation") |
estimate_cost(num_calls)
abstractmethod
¶
Estimate cost for N API calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_calls
|
int
|
Number of binding calls to estimate |
required |
Returns:
| Type | Description |
|---|---|
Optional[float]
|
Estimated cost in USD, or None if free/unknown |
Source code in src/nervapack/llm/base.py
summarize_entity(entity_type, entity_name, content)
¶
Generate a summary for a code entity.
This is shared logic across all providers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_type
|
str
|
Type of entity (function, class, etc.) |
required |
entity_name
|
str
|
Name of the entity |
required |
content
|
str
|
Code content to summarize |
required |
Returns:
| Type | Description |
|---|---|
str
|
1-3 sentence summary |
Source code in src/nervapack/llm/base.py
bind_docs_to_ast(doc_chunk, ast_nodes)
¶
Identify which AST nodes a documentation chunk explains.
This is the critical LLM call during ingestion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc_chunk
|
str
|
Markdown documentation text |
required |
ast_nodes
|
List[Dict[str, str]]
|
List of {node_id, summary, ...} dicts |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List of node_ids that the doc chunk explains |