docs
0
# Models
1
2
Model management and information retrieval for accessing available AI models across different providers. Provides unified interface to list, retrieve details, and manage models from various AI providers.
3
4
## Capabilities
5
6
### Model Listing
7
8
Retrieves list of available models from the configured provider(s), including model capabilities, permissions, and availability status.
9
10
```python { .api }
11
class Models:
12
def list(self, **kwargs) -> ModelList:
13
"""
14
List available models from the configured provider.
15
16
Args:
17
**kwargs: Additional provider-specific parameters
18
19
Returns:
20
ModelList: List of available models with metadata
21
"""
22
23
def retrieve(
24
self,
25
model: str,
26
*,
27
timeout: Union[float, NotGiven] = NOT_GIVEN,
28
**kwargs
29
) -> Model:
30
"""
31
Retrieve detailed information about a specific model.
32
33
Args:
34
model: Model identifier (e.g., "gpt-4", "claude-3-opus")
35
timeout: Request timeout in seconds
36
**kwargs: Additional provider-specific parameters
37
38
Returns:
39
Model: Detailed model information and capabilities
40
"""
41
42
def delete(
43
self,
44
model: str,
45
*,
46
timeout: Union[float, NotGiven] = NOT_GIVEN,
47
**kwargs
48
) -> ModelDeleted:
49
"""
50
Delete a custom model (if supported by provider).
51
52
Args:
53
model: Model identifier to delete
54
timeout: Request timeout in seconds
55
**kwargs: Additional provider-specific parameters
56
57
Returns:
58
ModelDeleted: Confirmation of model deletion
59
"""
60
61
class AsyncModels:
62
async def list(self, **kwargs) -> ModelList:
63
"""Async version of list method."""
64
65
async def retrieve(
66
self,
67
model: str,
68
*,
69
timeout: Union[float, NotGiven] = NOT_GIVEN,
70
**kwargs
71
) -> Model:
72
"""Async version of retrieve method."""
73
74
async def delete(
75
self,
76
model: str,
77
*,
78
timeout: Union[float, NotGiven] = NOT_GIVEN,
79
**kwargs
80
) -> ModelDeleted:
81
"""Async version of delete method."""
82
```
83
84
### Usage Examples
85
86
```python
87
from portkey_ai import Portkey
88
89
# Initialize client
90
portkey = Portkey(
91
api_key="PORTKEY_API_KEY",
92
virtual_key="VIRTUAL_KEY"
93
)
94
95
# List all available models
96
models = portkey.models.list()
97
print(f"Available models: {len(models.data)}")
98
99
for model in models.data:
100
print(f"- {model.id}: {model.owned_by}")
101
102
# Get details for a specific model
103
model_info = portkey.models.retrieve("gpt-4")
104
print(f"Model: {model_info.id}")
105
print(f"Created: {model_info.created}")
106
print(f"Owned by: {model_info.owned_by}")
107
print(f"Permissions: {model_info.permission}")
108
109
# Filter models by provider (provider-specific)
110
openai_models = portkey.models.list(provider="openai")
111
```
112
113
### Async Usage
114
115
```python
116
import asyncio
117
from portkey_ai import AsyncPortkey
118
119
async def list_models():
120
portkey = AsyncPortkey(
121
api_key="PORTKEY_API_KEY",
122
virtual_key="VIRTUAL_KEY"
123
)
124
125
# List models asynchronously
126
models = await portkey.models.list()
127
128
# Get model details
129
model_details = await portkey.models.retrieve("gpt-4")
130
131
return models, model_details
132
133
models, details = asyncio.run(list_models())
134
print(f"Found {len(models.data)} models")
135
print(f"GPT-4 details: {details.id}")
136
```
137
138
### Multi-Provider Model Discovery
139
140
```python
141
# List models across multiple providers using Portkey configuration
142
config = {
143
"mode": "fallback",
144
"options": [
145
{"provider": "openai", "api_key": "openai_key"},
146
{"provider": "anthropic", "api_key": "anthropic_key"},
147
{"provider": "cohere", "api_key": "cohere_key"}
148
]
149
}
150
151
portkey = Portkey(
152
api_key="PORTKEY_API_KEY",
153
config=config
154
)
155
156
# This will list models from the primary provider in the fallback chain
157
all_models = portkey.models.list()
158
159
# Access models by capability
160
chat_models = [m for m in all_models.data if "chat" in m.id.lower()]
161
completion_models = [m for m in all_models.data if "text" in m.id.lower()]
162
```
163
164
## Types
165
166
```python { .api }
167
class ModelList:
168
"""List of available models"""
169
object: str # "list"
170
data: List[Model]
171
_headers: Optional[dict] # Response headers
172
173
class Model:
174
"""Individual model information"""
175
id: str # Model identifier
176
object: str # "model"
177
created: int # Unix timestamp of creation
178
owned_by: str # Organization that owns the model
179
permission: List[ModelPermission] # Model permissions
180
root: Optional[str] # Root model identifier
181
parent: Optional[str] # Parent model identifier
182
_headers: Optional[dict] # Response headers
183
184
class ModelPermission:
185
"""Model permission details"""
186
id: str
187
object: str # "model_permission"
188
created: int
189
allow_create_engine: bool
190
allow_sampling: bool
191
allow_logprobs: bool
192
allow_search_indices: bool
193
allow_view: bool
194
allow_fine_tuning: bool
195
organization: str
196
group: Optional[str]
197
is_blocking: bool
198
199
class ModelDeleted:
200
"""Model deletion confirmation"""
201
id: str # Deleted model identifier
202
object: str # "model"
203
deleted: bool # True if successfully deleted
204
_headers: Optional[dict] # Response headers
205
```