Implementation:Run llama Llama index LLM Utils
Overview
This module provides utility functions for LLM resolution and JSON parsing in LlamaIndex. The primary function resolve_llm converts various LLM specifications (strings, LLM instances, LangChain models) into a unified LLM object. The module also provides parse_partial_json for parsing incomplete JSON strings into valid Python dictionaries.
Source file: llama-index-core/llama_index/core/llms/utils.py (198 lines)
Type Definitions
LLMType = Union[str, LLM, "BaseLanguageModel"]
The LLMType union type represents all accepted input formats for LLM specification:
str-- string identifiers like"default"or"local:model_path"LLM-- a LlamaIndex LLM instanceBaseLanguageModel-- a LangChain language model instance (v0.x or v1.x)
resolve_llm Function
def resolve_llm(
llm: Optional[LLMType] = None,
callback_manager: Optional[CallbackManager] = None
) -> LLM:
Resolves an LLM specification into a concrete LLM instance. The resolution logic follows this decision tree:
Case 1: llm == "default"
- If the
IS_TESTINGenvironment variable is set, returns aMockLLMinstance. - Otherwise, attempts to create an
OpenAIinstance fromllama_index.llms.openai. - Validates the OpenAI API key.
- Raises
ImportErrorif thellama-index-llms-openaipackage is not installed. - Raises
ValueErrorwith a descriptive message if the API key is invalid.
Case 2: llm is a string (not "default")
The string must follow the format "local[:model_path]":
- Splits the string on
":"to extract the prefix and optional model path. - Validates that the prefix is
"local"; raisesValueErrorotherwise. - Creates a
LlamaCPPinstance fromllama_index.llms.llama_cppwith:
- The specified model path
- Default
messages_to_promptandcompletion_to_promptfunctions n_gpu_layers=1in model kwargs
- Raises
ImportErrorif thellama-index-llms-llama-cpppackage is not installed.
Case 3: llm is a LangChain BaseLanguageModel
- Wraps the model in a
LangChainLLMadapter fromllama_index.llms.langchain. - Raises
ImportErrorif thellama-index-llms-langchainpackage is not installed.
Case 4: llm is None
- Prints a message that LLM is explicitly disabled.
- Returns a
MockLLMinstance.
Final Steps
After resolution, the function:
- Asserts that the result is an
LLMinstance. - Sets the
callback_managerusing the provided argument, the LLM's existing callback manager, or the globalSettings.callback_manager(in that priority order). - Returns the configured LLM.
parse_partial_json Function
def parse_partial_json(s: str) -> Dict:
Parses an incomplete JSON string into a valid Python dictionary. This is adapted from the Open Interpreter project and is useful for handling streaming LLM outputs that may produce partial JSON.
Algorithm
- Direct parse attempt: First tries
json.loads(s). If successful, returns the result. - Character-by-character processing: Iterates through each character, tracking:
- A stack of expected closing characters (
}and]) - Whether currently inside a string literal
- Whether the current character is escaped
- A stack of expected closing characters (
- String handling: Newlines inside strings are converted to
\nescape sequences. - Incomplete key detection: If still inside a string at the end and no colon follows the last quote, the incomplete key is removed.
- Trailing cleanup:
- If the string ends with
":", appendsnullas a default value. - If the string ends with
",", removes the trailing comma.
- If the string ends with
- Stack closure: Any remaining open structures are closed in reverse order.
- Final parse: Attempts
json.loadson the repaired string. - Error handling: Raises
ValueError("Malformed partial JSON encountered.")if both parse attempts fail, or if mismatched closing characters are encountered.
Dependencies
llama_index.core.llms.llm.LLM-- the core LLM classllama_index.core.llms.callbacks.CallbackManager-- callback managementllama_index.core.settings.Settings-- global settings (lazy import)llama_index.core.llms.mock.MockLLM-- mock LLM for testing and disabled states (lazy import)llama_index.llms.openai-- OpenAI LLM integration (optional, lazy import)llama_index.llms.llama_cpp-- LlamaCPP integration (optional, lazy import)llama_index.llms.langchain-- LangChain adapter (optional, lazy import)- LangChain's
BaseLanguageModel(v0.x or v1.x, optional, conditional import)