Pydantic-AI models for LLMling-agent
Project description
LLMling-models
llmling-models
Collection of model wrappers and adapters for use with LLMling-Agent, but should work with the underlying pydantic-ai API without issues.
WARNING:
This is just a prototype for now and will likely change in the future. Also, pydantic-ais APIs dont seem stable yet, so things might not work across all pydantic-ai versions. I will try to keep this up to date as fast as possible.
Available Models
LLM Library Adapter
Adapter to use models from the LLM library with Pydantic-AI:
from pydantic_ai import Agent
from llmling_models.adapters import LLMAdapter
# Basic usage
adapter = LLMAdapter(model_name="gpt-4o-mini")
agent = Agent(model=adapter)
result = await agent.run("Write a short poem")
# Streaming support
async with agent.run_stream("Test prompt") as response:
async for chunk in response.stream():
print(chunk)
# Usage statistics
result = await agent.run("Test prompt")
usage = result.usage()
print(f"Request tokens: {usage.request_tokens}")
print(f"Response tokens: {usage.response_tokens}")
(Examples need to be wrapped in async function and run with asyncio.run)
AISuite Adapter
Adapter to use models from AISuite with Pydantic-AI:
from pydantic_ai import Agent
from llmling_models.adapters import AISuiteAdapter
# Basic usage
adapter = AISuiteAdapter(model="model_name")
agent = Agent(adapter)
result = await agent.run("Write a story")
LiteLLM Adapter
Adapter to use models from the LiteLLM library with Pydantic-AI:
from pydantic_ai import Agent
from llmling_models.adapters import LiteLLMAdapter
# Basic usage
adapter = LiteLLMAdapter(model="openai/gpt-4o-mini")
agent = Agent(model=adapter)
result = await agent.run("Write a short poem")
# Tool usage
@agent.tool_plain
def multiply(a: int, b: int) -> int:
"""Calculate a simple math operation."""
return a * b
result = await agent.run("What is 42 multiplied by 56?")
# Streaming support
async with agent.run_stream("Tell me a story") as stream:
async for chunk in stream.stream_text(delta=True):
print(chunk, end="", flush=True)
Multi-Models
Augmented Model
Enhances prompts through pre- and post-processing steps using auxiliary language models:
from llmling_models import AugmentedModel
model = AugmentedModel(
main_model="openai:gpt-4",
pre_prompt={
"text": "Expand this question: {input}",
"model": "openai:gpt-3.5-turbo"
},
post_prompt={
"text": "Summarize this response concisely: {output}",
"model": "openai:gpt-3.5-turbo"
}
)
agent = Agent(model)
# The question will be expanded before processing
# and the response will be summarized afterward
result = await agent.run("What is AI?")
Input Model
A model that delegates responses to human input, useful for testing, debugging, or creating hybrid human-AI workflows:
from pydantic_ai import Agent
from llmling_models import InputModel
# Basic usage with default console input
model = InputModel(
prompt_template="🤖 Question: {prompt}",
show_system=True,
input_prompt="Your answer: ",
)
# Create agent with system context
agent = Agent(
model=model,
system_prompt="You are helping test an input model. Be concise.",
)
# Run interactive conversation
result = await agent.run("What's your favorite color?")
print(f"You responded: {result.output}")
# Supports streaming input
async with agent.run_stream("Tell me a story...") as response:
async for chunk in response.stream():
print(chunk, end="", flush=True)
Features:
- Interactive console input for testing and debugging
- Support for streaming input (character by character, but not "true" async with default handler)
- Configurable message formatting
- Custom input handlers for different input sources
- System message display control
- Full conversation context support
This model is particularly useful for:
- Testing complex prompt chains
- Creating hybrid human-AI workflows
- Debugging agent behavior
- Collecting human feedback
- Educational scenarios where human input is needed
User Select Model
An interactive model that lets users manually choose which model to use for each prompt:
from pydantic_ai import Agent
from llmling_models import UserSelectModel
# Basic setup with model list
model = UserSelectModel(
models=["openai:gpt-4o-mini", "openai:gpt-3.5-turbo", "anthropic:claude-3"]
)
agent = Agent(model)
# The user will be shown the prompt and available models,
# and can choose which one to use for the response
result = await agent.run("What is the meaning of life?")
Model Delegation
Dynamically selects models based on given prompt. Uses a selector model to choose the most appropriate model for each task:
from pydantic_ai import Agent
from llmling_models import DelegationMultiModel
# Basic setup with model list
delegation_model = DelegationMultiModel(
selector_model="openai:gpt-4-turbo",
models=["openai:gpt-4", "openai:gpt-3.5-turbo"],
selection_prompt="Pick gpt-4 for complex tasks, gpt-3.5-turbo for simple queries."
)
# Advanced setup with model descriptions
delegation_model = DelegationMultiModel(
selector_model="openai:gpt-4-turbo",
models=["openai:gpt-4", "anthropic:claude-2", "openai:gpt-3.5-turbo"],
model_descriptions={
"openai:gpt-4": "Complex reasoning, math problems, and coding tasks",
"anthropic:claude-2": "Long-form analysis and research synthesis",
"openai:gpt-3.5-turbo": "Simple queries, chat, and basic information"
},
selection_prompt="Select the most appropriate model for the task."
)
agent = Agent(delegation_model)
# The selector model will analyze the prompt and choose the most suitable model
result = await agent.run("Solve this complex mathematical proof...")
Cost-Optimized Model
Selects models based on input cost limits, automatically choosing the most appropriate model within your budget constraints:
from pydantic_ai import Agent
from llmling_models import CostOptimizedMultiModel
# Use cheapest model that can handle the task
cost_model = CostOptimizedMultiModel(
models=[
"openai:gpt-4", # More expensive
"openai:gpt-3.5-turbo", # Less expensive
],
max_input_cost=0.1, # Maximum cost in USD per request
strategy="cheapest_possible" # Use cheapest model that fits
)
# Or use the best model within budget
cost_model = CostOptimizedMultiModel(
models=[
"openai:gpt-4-32k", # Most expensive
"openai:gpt-4", # Medium cost
"openai:gpt-3.5-turbo", # Cheapest
],
max_input_cost=0.5, # Higher budget
strategy="best_within_budget" # Use best model within budget
)
agent = Agent(cost_model)
result = await agent.run("Your prompt here")
Token-Optimized Model
Automatically selects models based on input token count and context window requirements:
from pydantic_ai import Agent
from llmling_models import TokenOptimizedMultiModel
# Create model that automatically handles different context lengths
token_model = TokenOptimizedMultiModel(
models=[
"openai:gpt-4-32k", # 32k context
"openai:gpt-4", # 8k context
"openai:gpt-3.5-turbo", # 4k context
],
strategy="efficient" # Use smallest sufficient context window
)
# Or maximize context window availability
token_model = TokenOptimizedMultiModel(
models=[
"openai:gpt-4-32k", # 32k context
"openai:gpt-4", # 8k context
"openai:gpt-3.5-turbo", # 4k context
],
strategy="maximum_context" # Use largest available context window
)
agent = Agent(token_model)
# Will automatically select appropriate model based on input length
result = await agent.run("Your long prompt here...")
# Long inputs automatically use models with larger context windows
result = await agent.run("Very long document..." * 1000)
The cost-optimized model ensures you stay within budget while getting the best possible model for your needs, while the token-optimized model automatically handles varying input lengths by selecting models with appropriate context windows.
Remote Input Model
A model that connects to a remote human operator, allowing distributed human-in-the-loop operations:
from pydantic_ai import Agent
from llmling_models import RemoteInputModel
# Basic setup with WebSocket (preferred for streaming)
model = RemoteInputModel(
url="ws://operator:8000/v1/chat/stream",
api_key="your-api-key"
)
# Or use REST API
model = RemoteInputModel(
url="http://operator:8000/v1/chat",
api_key="your-api-key"
)
agent = Agent(model)
# The request will be forwarded to the remote operator
result = await agent.run("What's the meaning of life?")
print(f"Remote operator responded: {result.output}")
# Streaming also works with WebSocket protocol
async with agent.run_stream("Tell me a story...") as response:
async for chunk in response.stream():
print(chunk, end="", flush=True)
Features:
- Distributed human-in-the-loop operations
- WebSocket support for real-time streaming
- REST API for simpler setups
- Full conversation context support
- Secure authentication via API keys
Setting up a Remote Model Server
Setting up a remote model server is straightforward. You just need a pydantic-ai model and can start serving it:
from llmling_models.remote_model.server import ModelServer
# Create and start server
server = ModelServer(
model="openai:gpt-4",
api_key="your-secret-key", # Optional authentication
)
server.run(port=8000)
That's it! The server now accepts both REST and WebSocket connections and handles all the message protocol details for you.
Features:
- Simple setup - just provide a model
- Optional API key authentication
- Automatic handling of both REST and WebSocket protocols
- Full pydantic-ai message protocol support
- Usage statistics forwarding
- Built-in error handling and logging
For development, you might want to run the server locally:
server = ModelServer(
model="openai:gpt-4",
api_key="dev-key"
)
server.run(host="localhost", port=8000)
For production, you'll typically want to run it on a public server with proper authentication:
server = ModelServer(
model="openai:gpt-4",
api_key="your-secure-key", # Make sure to use a strong key
title="Production GPT-4 Server",
description="Serves GPT-4 model for production use"
)
server.run(
host="0.0.0.0", # Accept connections from anywhere
port=8000,
workers=4 # Multiple workers for better performance
)
Both REST and WebSocket protocols are supported, with WebSocket being preferred for streaming capabilities. They also maintain the full pydantic-ai message protocol, ensuring compatibility with all features of the framework.
All multi models are generically typed to follow pydantic best practices. Usefulness for that is debatable though. :P
Providers
LLMling-models extends the capabilities of pydantic-ai with additional provider implementations that make it easy to connect to various LLM API services.
Available Providers
The package includes the following provider implementations:
OpenRouter Provider
Connect to OpenRouter's API service to access multiple models from different providers:
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from llmling_models.providers import infer_provider
# Method 1: Using infer_provider
provider = infer_provider("openrouter")
model = OpenAIModel("anthropic/claude-3-opus", provider=provider)
# Method 2: Direct instantiation
from llmling_models.providers.openrouter_provider import OpenRouterProvider
provider = OpenRouterProvider(api_key="your-api-key") # Or use OPENROUTER_API_KEY env var
model = OpenAIModel("openai/o3-mini", provider=provider)
agent = Agent(model=model)
result = await agent.run("Hello world!")
Grok (X.AI) Provider
Connect to X.AI's Grok models:
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from llmling_models.providers.grok_provider import GrokProvider
provider = GrokProvider(api_key="your-api-key") # Or use X_AI_API_KEY/GROK_API_KEY env var
model = OpenAIModel("grok-2-1212", provider=provider)
agent = Agent(model=model)
result = await agent.run("Hello Grok!")
Perplexity Provider
Connect to Perplexity's API for advanced web search and reasoning capabilities:
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from llmling_models.providers.perplexity_provider import PerplexityProvider
provider = PerplexityProvider(api_key="your-api-key") # Or use PERPLEXITY_API_KEY env var
model = OpenAIModel("sonar-medium-online", provider=provider)
agent = Agent(model=model)
result = await agent.run("What's the latest on quantum computing?")
GitHub Copilot Provider
Connect to GitHub Copilot's API for code-focused tasks (requires token management):
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from llmling_models.providers.copilot_provider import CopilotProvider
# Requires tokonomics.CopilotTokenManager to handle token management
provider = CopilotProvider() # Uses tokonomics for authentication
model = OpenAIModel("gpt-4o-mini", provider=provider)
agent = Agent(model=model)
result = await agent.run("Write a function to calculate Fibonacci numbers")
LM Studio Provider
Connect to local LM Studio inference server for open-source models:
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
from llmling_models.providers.lm_studio_provider import LMStudioProvider
provider = LMStudioProvider(base_url="http://localhost:11434/v1")
model = OpenAIModel("model_name", provider=provider) # Use model loaded in LM Studio
agent = Agent(model=model)
result = await agent.run("Tell me about yourself")
Provider Utility Functions
infer_provider
The infer_provider function extends pydantic-ai's provider inference to include all LLMling-models providers:
from llmling_models.providers import infer_provider
# Get provider by name
provider = infer_provider("openrouter") # Returns OpenRouterProvider instance
provider = infer_provider("grok") # Returns GrokProvider instance
provider = infer_provider("perplexity") # Returns PerplexityProvider instance
provider = infer_provider("copilot") # Returns CopilotProvider instance
provider = infer_provider("lm-studio") # Returns LMStudioProvider instance
# Still works with standard providers too
provider = infer_provider("openai") # Returns pydantic_ai's OpenAIProvider
Extended infer_model Function
LLMling-models provides an extended infer_model function that resolves various model notations to appropriate instances:
from llmling_models import infer_model
# Provider prefixes (requires appropriate API keys as env vars)
model = infer_model("openai:gpt-4o") # OpenAI models
model = infer_model("openrouter:anthropic/opus") # OpenRouter (requires OPENROUTER_API_KEY)
model = infer_model("grok:grok-2-1212") # Grok/X.AI (requires X_AI_API_KEY)
model = infer_model("perplexity:sonar-medium") # Perplexity (requires PERPLEXITY_API_KEY)
model = infer_model("deepseek:deepseek-chat") # DeepSeek (requires DEEPSEEK_API_KEY)
model = infer_model("copilot:gpt-4o-mini") # GitHub Copilot (requires token management)
model = infer_model("lm-studio:model-name") # LM Studio local models
# LLMling's special models
model = infer_model("llm:gpt-4") # LLM library adapter
model = infer_model("aisuite:anthropic:claude") # AISuite adapter
model = infer_model("simple-openai:gpt-4") # Simple HTTPX-based OpenAI client
model = infer_model("input") # Interactive human input model
model = infer_model("remote_model:ws://url") # Remote model proxy
model = infer_model("remote_input:ws://url") # Remote human input
model = infer_model("import:module.path:Class") # Import model from Python path
# Testing
model = infer_model("test:Custom response") # Test model with fixed output
The function provides a fallback to a simple HTTPX-based OpenAI client in environments where the full OpenAI library is not available (like Pyodide/WebAssembly contexts).
Environment Variable Configuration
For convenience, most providers support configuration via environment variables:
| Provider | Environment Variable | Purpose |
|---|---|---|
| OpenRouter | OPENROUTER_API_KEY |
API key for authentication |
| Grok (X.AI) | X_AI_API_KEY or GROK_API_KEY |
API key for authentication |
| DeepSeek | DEEPSEEK_API_KEY |
API key for authentication |
| Perplexity | PERPLEXITY_API_KEY |
API key for authentication |
| Copilot | Uses tokonomics token management | - |
| LM Studio | LM_STUDIO_BASE_URL |
Base URL for local server |
| OpenAI | OPENAI_API_KEY |
API key for authentication |
## Installation
```bash
pip install llmling-models
Requirements
- Python 3.12+
- pydantic-ai
- llm (optional, for LLM adapter)
- aisuite (optional, for aisuite adapter)
- Either tokenizers or transformers for improved token calculation
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llmling_models-0.10.2.tar.gz.
File metadata
- Download URL: llmling_models-0.10.2.tar.gz
- Upload date:
- Size: 62.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a72b96264f2849b3ee0bfba7d8cc11319efc210b52dc2bba0e4b3fe1265ff39
|
|
| MD5 |
a3d54162ca8585c586dac7d268e0541b
|
|
| BLAKE2b-256 |
1b7d8f5389d8d19e6228b2c244fabeb8ca93e1926230c024291c46e197c45152
|
Provenance
The following attestation bundles were made for llmling_models-0.10.2.tar.gz:
Publisher:
build.yml on phil65/LLMling-models
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llmling_models-0.10.2.tar.gz -
Subject digest:
2a72b96264f2849b3ee0bfba7d8cc11319efc210b52dc2bba0e4b3fe1265ff39 - Sigstore transparency entry: 199999336
- Sigstore integration time:
-
Permalink:
phil65/LLMling-models@9e4956b46c7f22424aa114c552969ba87ea74ca0 -
Branch / Tag:
refs/tags/v0.10.2 - Owner: https://github.com/phil65
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@9e4956b46c7f22424aa114c552969ba87ea74ca0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file llmling_models-0.10.2-py3-none-any.whl.
File metadata
- Download URL: llmling_models-0.10.2-py3-none-any.whl
- Upload date:
- Size: 78.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92d65a04f10632cf149465676467b16f63719133b09a68d5fe0176151f629351
|
|
| MD5 |
67a47cac65903f8eb2bf00c9b7162f9e
|
|
| BLAKE2b-256 |
5f4ac4a95c6a2324af95c7e01e164564ace064ecd412396d1fdf237fa961f100
|
Provenance
The following attestation bundles were made for llmling_models-0.10.2-py3-none-any.whl:
Publisher:
build.yml on phil65/LLMling-models
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
llmling_models-0.10.2-py3-none-any.whl -
Subject digest:
92d65a04f10632cf149465676467b16f63719133b09a68d5fe0176151f629351 - Sigstore transparency entry: 199999338
- Sigstore integration time:
-
Permalink:
phil65/LLMling-models@9e4956b46c7f22424aa114c552969ba87ea74ca0 -
Branch / Tag:
refs/tags/v0.10.2 - Owner: https://github.com/phil65
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@9e4956b46c7f22424aa114c552969ba87ea74ca0 -
Trigger Event:
push
-
Statement type: