topologicpy.LLM module
- class topologicpy.LLM.LLM
Bases:
objectA provider-neutral interface for calling large language models.
The class follows the TopologicPy static-method style. Create an LLM configuration object with LLM.ByParameters(…), then pass that object to the other static methods:
llm = LLM.ByParameters(provider=”ollama”, model=”llama3.1”) text = LLM.Prompt(llm, “Say hello.”) data = LLM.JSON(llm, “Return {‘ok’: true} as JSON.”)
Supported providers in this minimal implementation: - “openai” - “anthropic” - “google” - “deepseek” - “huggingface” - “ollama” - “lmstudio” - “openai-compatible”
Methods
ByParameters([provider, model, apiKey, ...])Creates and returns a lightweight LLM configuration object.
JSON(llm, prompt[, schema, systemPrompt, ...])Sends a prompt to the configured LLM and returns parsed JSON.
Models(llm[, silent])Lists available models where the configured provider supports model listing.
Prompt(llm, prompt[, systemPrompt, ...])Sends a text prompt to the configured LLM and returns text.
ProviderInfo([provider, silent])Returns metadata about supported providers.
Response(llm, prompt[, systemPrompt, ...])Sends a prompt to the configured LLM and returns a normalized response dictionary.
Test(llm[, prompt, silent])Tests whether the configured LLM is reachable and can generate a response.
- static ByParameters(provider: str = 'openai', model: str = None, apiKey: str = None, baseURL: str = None, temperature: float = 0.2, maxOutputTokens: int = None, timeout: int = 60, systemPrompt: str = None, silent: bool = False)
Creates and returns a lightweight LLM configuration object.
- Parameters
- providerstr , optional
The LLM provider. Supported values are “openai”, “anthropic”, “google”, “deepseek”, “huggingface”, “ollama”, “lmstudio”, and “openai-compatible”. Default is “openai”.
- modelstr , optional
The model name. If None, a provider-specific default is used when available.
- apiKeystr , optional
The provider API key. If None, provider-specific environment variables are checked where applicable.
- baseURLstr , optional
The provider base URL. Required for some OpenAI-compatible local or remote providers. Defaults are used for Ollama, LM Studio, HuggingFace, and DeepSeek.
- temperaturefloat , optional
The sampling temperature. Default is 0.2.
- maxOutputTokensint , optional
The maximum number of output tokens. Default is None.
- timeoutint , optional
The timeout in seconds. Default is 60.
- systemPromptstr , optional
Optional default system prompt.
- silentbool , optional
If set to True, error and warning messages are suppressed. Default is False.
- Returns
- _LLMConfig
The LLM configuration object.
- static JSON(llm, prompt: Union[str, List[Dict[str, Any]]], schema: dict = None, systemPrompt: str = None, temperature: float = None, maxOutputTokens: int = None, timeout: int = None, repair: bool = True, silent: bool = False)
Sends a prompt to the configured LLM and returns parsed JSON.
- Parameters
- llm_LLMConfig
The LLM object returned by LLM.ByParameters.
- promptstr or list
The input prompt. If a list is supplied, it is treated as a chat message list with dictionaries containing “role” and “content”.
- schemadict , optional
Optional JSON schema. In this minimal implementation, the schema is included in the prompt for all providers, and provider-native schema enforcement may be added later.
- systemPromptstr , optional
The system prompt. Overrides llm.systemPrompt if supplied.
- temperaturefloat , optional
Overrides llm.temperature if supplied.
- maxOutputTokensint , optional
Overrides llm.maxOutputTokens if supplied.
- timeoutint , optional
Overrides llm.timeout if supplied.
- repairbool , optional
If True, attempts to parse JSON from fenced or surrounding text. Default is True.
- silentbool , optional
If set to True, error and warning messages are suppressed. Default is False.
- Returns
- dict or list
Parsed JSON, or None on failure.
- static Models(llm, silent: bool = False) Optional[List[str]]
Lists available models where the configured provider supports model listing.
- Parameters
- llm_LLMConfig
The LLM object returned by LLM.ByParameters.
- silentbool , optional
If set to True, error and warning messages are suppressed. Default is False.
- Returns
- list
The model names, or None on failure/unsupported provider.
- static Prompt(llm, prompt: Union[str, List[Dict[str, Any]]], systemPrompt: str = None, temperature: float = None, maxOutputTokens: int = None, timeout: int = None, silent: bool = False) Optional[str]
Sends a text prompt to the configured LLM and returns text.
- Parameters
- llm_LLMConfig
The LLM object returned by LLM.ByParameters.
- promptstr or list
The input prompt. If a list is supplied, it is treated as a chat message list with dictionaries containing “role” and “content”.
- systemPromptstr , optional
The system prompt. Overrides llm.systemPrompt if supplied.
- temperaturefloat , optional
Overrides llm.temperature if supplied.
- maxOutputTokensint , optional
Overrides llm.maxOutputTokens if supplied.
- timeoutint , optional
Overrides llm.timeout if supplied.
- silentbool , optional
If set to True, error and warning messages are suppressed. Default is False.
- Returns
- str
The generated text, or None on failure.
- static ProviderInfo(provider: str = None, silent: bool = False)
Returns metadata about supported providers.
- Parameters
- providerstr , optional
If specified, only metadata for the requested provider is returned. If None, metadata for all providers is returned.
- silentbool , optional
If set to True, error and warning messages are suppressed. Default is False.
- Returns
- dict
Provider metadata.
- static Response(llm, prompt: Union[str, List[Dict[str, Any]]], systemPrompt: str = None, temperature: float = None, maxOutputTokens: int = None, timeout: int = None, silent: bool = False) Dict[str, Any]
Sends a prompt to the configured LLM and returns a normalized response dictionary.
- Parameters
- llm_LLMConfig
The LLM object returned by LLM.ByParameters.
- promptstr or list
The input prompt. If a list is supplied, it is treated as a chat message list with dictionaries containing “role” and “content”.
- systemPromptstr , optional
The system prompt. Overrides llm.systemPrompt if supplied.
- temperaturefloat , optional
Overrides llm.temperature if supplied.
- maxOutputTokensint , optional
Overrides llm.maxOutputTokens if supplied.
- timeoutint , optional
Overrides llm.timeout if supplied.
- silentbool , optional
If set to True, error and warning messages are suppressed. Default is False.
- Returns
- dict
A normalized response dictionary with keys: ok, provider, model, text, json, raw, usage, error_type, message.
- static Test(llm, prompt: str = 'Reply with OK only.', silent: bool = False) Dict[str, Any]
Tests whether the configured LLM is reachable and can generate a response.
- Parameters
- llm_LLMConfig
The LLM object returned by LLM.ByParameters.
- promptstr , optional
The test prompt. Default is “Reply with OK only.”
- silentbool , optional
If set to True, error and warning messages are suppressed. Default is False.
- Returns
- dict
A normalized test dictionary.