function get_llm_instance
Factory function that creates and returns an appropriate LLM (Large Language Model) instance based on the specified model name, automatically detecting the provider (OpenAI, Azure OpenAI, or Anthropic) and configuring it with the given parameters.
/tf/active/vicechatdev/docchat/llm_factory.py
171 - 208
moderate
Purpose
This function serves as a unified interface for instantiating different LLM providers without requiring the caller to know the specific implementation details. It abstracts away the complexity of choosing between OpenAI's Chat Completions API, Azure OpenAI deployments, and Anthropic's Claude models. The function uses model name prefixes to determine the appropriate provider and returns a configured LLM instance ready for use. This is particularly useful in applications that need to support multiple LLM providers or switch between them dynamically.
Source Code
def get_llm_instance(model_name: str = None, temperature: float = 0, max_tokens: int = 4096):
"""
Create LLM instance with automatic provider and parameter detection.
- GPT models (GPT-4, GPT-5): Uses OpenAI Chat Completions (OpenAIChatLLM)
- Azure OpenAI: Uses AzureOpenAIChatLLM
- Claude/Gemini: Uses LangChain provider wrappers (local import)
"""
model_name = model_name or config.LLM_MODEL
lower = model_name.lower()
# Azure OpenAI models
if lower.startswith('azure-'):
logger.info(f"LLM factory: Using Azure OpenAI for {model_name}")
# Get deployment name from config
model_config = config.AVAILABLE_MODELS.get(model_name, {})
deployment_name = model_config.get('deployment_name', 'OneCo-gpt')
return AzureOpenAIChatLLM(
deployment_name=deployment_name,
temperature=temperature,
max_tokens=max_tokens
)
# OpenAI models - all use Chat Completions API
if lower.startswith('gpt'):
logger.info(f"LLM factory: Using Chat Completions for {model_name}")
return OpenAIChatLLM(model=model_name, api_key=config.OPENAI_API_KEY, temperature=temperature, max_tokens=max_tokens)
# Anthropic Claude
if lower.startswith('claude'):
from langchain_anthropic import ChatAnthropic # type: ignore
return ChatAnthropic(
model=model_name,
temperature=temperature,
max_tokens=max_tokens,
api_key=config.ANTHROPIC_API_KEY,
)
raise ValueError(f"Unknown model provider for: {model_name}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
model_name |
str | None | positional_or_keyword |
temperature |
float | 0 | positional_or_keyword |
max_tokens |
int | 4096 | positional_or_keyword |
Parameter Details
model_name: The name of the LLM model to instantiate. If None, defaults to config.LLM_MODEL. Expected values include model names prefixed with 'gpt' (e.g., 'gpt-4', 'gpt-3.5-turbo'), 'azure-' (e.g., 'azure-gpt-4'), or 'claude' (e.g., 'claude-3-opus'). The function uses case-insensitive matching on the prefix to determine the provider.
temperature: Controls randomness in the model's output. Float value typically between 0 and 1, where 0 means deterministic output (most focused and consistent) and higher values increase randomness and creativity. Default is 0 for maximum consistency.
max_tokens: Maximum number of tokens the model can generate in its response. Integer value that limits the length of the output. Default is 4096. Note that this includes both input and output tokens for some providers, so actual output may be shorter.
Return Value
Returns an instantiated LLM object specific to the detected provider. For GPT models, returns an OpenAIChatLLM instance. For Azure models, returns an AzureOpenAIChatLLM instance. For Claude models, returns a ChatAnthropic instance from LangChain. All returned objects implement a common interface for text generation. Raises ValueError if the model_name doesn't match any known provider prefix.
Dependencies
openailangchain_anthropiclogging
Required Imports
import config
import logging
from openai import OpenAI
from openai import AzureOpenAI
Conditional/Optional Imports
These imports are only needed under specific conditions:
from langchain_anthropic import ChatAnthropic
Condition: only when instantiating Claude models (model_name starts with 'claude')
OptionalUsage Example
# Basic usage with GPT-4
llm = get_llm_instance(model_name='gpt-4', temperature=0.7, max_tokens=2000)
# Using default model from config
llm = get_llm_instance()
# Azure OpenAI with custom deployment
llm = get_llm_instance(model_name='azure-gpt-4', temperature=0, max_tokens=4096)
# Claude model
llm = get_llm_instance(model_name='claude-3-opus-20240229', temperature=0.5, max_tokens=1024)
# Generate text with the instance
response = llm.generate('What is the capital of France?')
Best Practices
- Always ensure the required API keys (OPENAI_API_KEY, ANTHROPIC_API_KEY) are properly configured in the config module before calling this function
- For Azure models, ensure the AVAILABLE_MODELS dictionary in config contains the correct deployment_name mapping
- Use temperature=0 for deterministic, consistent outputs (e.g., for testing or production systems requiring reproducibility)
- Set appropriate max_tokens based on your use case to avoid truncated responses or excessive costs
- Handle the ValueError exception that may be raised for unsupported model names
- The function performs case-insensitive model name matching, so 'GPT-4' and 'gpt-4' are equivalent
- Consider caching LLM instances if you're making multiple calls with the same configuration to avoid repeated initialization overhead
- Be aware that the langchain_anthropic import is lazy-loaded only when needed, which may cause import errors at runtime if the package is not installed when using Claude models
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class LLMClient 76.6% similar
-
class LLMClient_v1 70.8% similar
-
class LLMClient_v2 68.1% similar
-
class OpenAIChatLLM 65.7% similar
-
class AzureOpenAIChatLLM 64.2% similar