chatlas 0.10.0__py3-none-any.whl → 0.11.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of chatlas might be problematic. Click here for more details.

chatlas/__init__.py CHANGED
@@ -1,7 +1,12 @@
1
1
  from . import types
2
2
  from ._auto import ChatAuto
3
3
  from ._chat import Chat
4
- from ._content import ContentToolRequest, ContentToolResult, ContentToolResultImage
4
+ from ._content import (
5
+ ContentToolRequest,
6
+ ContentToolResult,
7
+ ContentToolResultImage,
8
+ ContentToolResultResource,
9
+ )
5
10
  from ._content_image import content_image_file, content_image_plot, content_image_url
6
11
  from ._content_pdf import content_pdf_file, content_pdf_url
7
12
  from ._interpolate import interpolate, interpolate_file
@@ -59,6 +64,7 @@ __all__ = (
59
64
  "ContentToolRequest",
60
65
  "ContentToolResult",
61
66
  "ContentToolResultImage",
67
+ "ContentToolResultResource",
62
68
  "interpolate",
63
69
  "interpolate_file",
64
70
  "Provider",
@@ -68,3 +74,9 @@ __all__ = (
68
74
  "Turn",
69
75
  "types",
70
76
  )
77
+
78
+ # Rebuild content models to resolve forward references to ToolAnnotation
79
+ ContentToolRequest.model_rebuild()
80
+ ContentToolResult.model_rebuild()
81
+ ContentToolResultImage.model_rebuild()
82
+ ContentToolResultResource.model_rebuild()
chatlas/_chat.py CHANGED
@@ -9,6 +9,7 @@ import warnings
9
9
  from pathlib import Path
10
10
  from threading import Thread
11
11
  from typing import (
12
+ TYPE_CHECKING,
12
13
  Any,
13
14
  AsyncGenerator,
14
15
  AsyncIterator,
@@ -43,13 +44,16 @@ from ._display import (
43
44
  )
44
45
  from ._logging import log_tool_error
45
46
  from ._mcp_manager import MCPSessionManager
46
- from ._provider import Provider, StandardModelParams, SubmitInputArgsT
47
+ from ._provider import ModelInfo, Provider, StandardModelParams, SubmitInputArgsT
47
48
  from ._tokens import compute_cost, get_token_pricing
48
49
  from ._tools import Tool, ToolRejectError
49
50
  from ._turn import Turn, user_turn
50
51
  from ._typing_extensions import TypedDict, TypeGuard
51
52
  from ._utils import MISSING, MISSING_TYPE, html_escape, wrap_async
52
53
 
54
+ if TYPE_CHECKING:
55
+ from mcp.types import ToolAnnotations
56
+
53
57
 
54
58
  class TokensDict(TypedDict):
55
59
  """
@@ -128,6 +132,78 @@ class Chat(Generic[SubmitInputArgsT, CompletionT]):
128
132
  self._standard_model_params: StandardModelParams = {}
129
133
  self._submit_input_kwargs: Optional[SubmitInputArgsT] = None
130
134
 
135
+ def list_models(self) -> list[ModelInfo]:
136
+ """
137
+ List all models available for the provider.
138
+
139
+ This method returns detailed information about all models supported by the provider,
140
+ including model IDs, pricing information, creation dates, and other metadata. This is
141
+ useful for discovering available models and their characteristics without needing to
142
+ consult provider documentation.
143
+
144
+ Examples
145
+ --------
146
+ Get all available models:
147
+
148
+ ```python
149
+ from chatlas import ChatOpenAI
150
+
151
+ chat = ChatOpenAI()
152
+ models = chat.list_models()
153
+ print(f"Found {len(models)} models")
154
+ print(f"First model: {models[0]['id']}")
155
+ ```
156
+
157
+ View models in a table format:
158
+
159
+ ```python
160
+ import pandas as pd
161
+ from chatlas import ChatAnthropic
162
+
163
+ chat = ChatAnthropic()
164
+ df = pd.DataFrame(chat.list_models())
165
+ print(df[["id", "input", "output"]].head()) # Show pricing info
166
+ ```
167
+
168
+ Find models by criteria:
169
+
170
+ ```python
171
+ from chatlas import ChatGoogle
172
+
173
+ chat = ChatGoogle()
174
+ models = chat.list_models()
175
+
176
+ # Find cheapest input model
177
+ cheapest = min(models, key=lambda m: m.get("input", float("inf")))
178
+ print(f"Cheapest model: {cheapest['id']}")
179
+ ```
180
+
181
+ Returns
182
+ -------
183
+ list[ModelInfo]
184
+ A list of ModelInfo dictionaries containing model information. Each dictionary
185
+ contains:
186
+
187
+ - `id` (str): The model identifier to use with the Chat constructor
188
+ - `name` (str, optional): Human-readable model name
189
+ - `input` (float, optional): Cost per input token in USD per million tokens
190
+ - `output` (float, optional): Cost per output token in USD per million tokens
191
+ - `cached_input` (float, optional): Cost per cached input token in USD per million tokens
192
+ - `created_at` (date, optional): Date the model was created
193
+ - `owned_by` (str, optional): Organization that owns the model
194
+ - `provider` (str, optional): Model provider name
195
+ - `size` (int, optional): Model size in bytes
196
+ - `url` (str, optional): URL with more information about the model
197
+
198
+ The list is typically sorted by creation date (most recent first).
199
+
200
+ Note
201
+ ----
202
+ Not all providers support this method. Some providers may raise NotImplementedError
203
+ with information about where to find model listings online.
204
+ """
205
+ return self.provider.list_models()
206
+
131
207
  def get_turns(
132
208
  self,
133
209
  *,
@@ -1462,6 +1538,7 @@ class Chat(Generic[SubmitInputArgsT, CompletionT]):
1462
1538
  *,
1463
1539
  force: bool = False,
1464
1540
  model: Optional[type[BaseModel]] = None,
1541
+ annotations: "Optional[ToolAnnotations]" = None,
1465
1542
  ):
1466
1543
  """
1467
1544
  Register a tool (function) with the chat.
@@ -1539,13 +1616,16 @@ class Chat(Generic[SubmitInputArgsT, CompletionT]):
1539
1616
  The primary reason why you might want to provide a model in
1540
1617
  Note that the name and docstring of the model takes precedence over the
1541
1618
  name and docstring of the function.
1619
+ annotations
1620
+ Additional properties that describe the tool and its behavior.
1621
+ Should be a `from mcp.types import ToolAnnotations` instance.
1542
1622
 
1543
1623
  Raises
1544
1624
  ------
1545
1625
  ValueError
1546
1626
  If a tool with the same name already exists and `force` is `False`.
1547
1627
  """
1548
- tool = Tool.from_func(func, model=model)
1628
+ tool = Tool.from_func(func, model=model, annotations=annotations)
1549
1629
  if tool.name in self._tools and not force:
1550
1630
  raise ValueError(
1551
1631
  f"Tool with name '{tool.name}' is already registered. "
@@ -1853,6 +1933,7 @@ class Chat(Generic[SubmitInputArgsT, CompletionT]):
1853
1933
  all_results: list[ContentToolResult] = []
1854
1934
  for x in turn.contents:
1855
1935
  if isinstance(x, ContentToolRequest):
1936
+ x.tool = self._tools.get(x.name)
1856
1937
  if echo == "output":
1857
1938
  self._echo_content(f"\n\n{x}\n\n")
1858
1939
  if content == "all":
@@ -1913,6 +1994,7 @@ class Chat(Generic[SubmitInputArgsT, CompletionT]):
1913
1994
  all_results: list[ContentToolResult] = []
1914
1995
  for x in turn.contents:
1915
1996
  if isinstance(x, ContentToolRequest):
1997
+ x.tool = self._tools.get(x.name)
1916
1998
  if echo == "output":
1917
1999
  self._echo_content(f"\n\n{x}\n\n")
1918
2000
  if content == "all":
@@ -2070,8 +2152,8 @@ class Chat(Generic[SubmitInputArgsT, CompletionT]):
2070
2152
  self._turns.extend([user_turn, turn])
2071
2153
 
2072
2154
  def _invoke_tool(self, request: ContentToolRequest):
2073
- tool_def = self._tools.get(request.name, None)
2074
- func = tool_def.func if tool_def is not None else None
2155
+ tool = request.tool
2156
+ func = tool.func if tool is not None else None
2075
2157
 
2076
2158
  if func is None:
2077
2159
  yield self._handle_tool_error_result(
@@ -2118,21 +2200,20 @@ class Chat(Generic[SubmitInputArgsT, CompletionT]):
2118
2200
  yield self._handle_tool_error_result(request, e)
2119
2201
 
2120
2202
  async def _invoke_tool_async(self, request: ContentToolRequest):
2121
- tool_def = self._tools.get(request.name, None)
2122
- func = None
2123
- if tool_def:
2124
- if tool_def._is_async:
2125
- func = tool_def.func
2126
- else:
2127
- func = wrap_async(tool_def.func)
2203
+ tool = request.tool
2128
2204
 
2129
- if func is None:
2205
+ if tool is None:
2130
2206
  yield self._handle_tool_error_result(
2131
2207
  request,
2132
2208
  error=RuntimeError("Unknown tool."),
2133
2209
  )
2134
2210
  return
2135
2211
 
2212
+ if tool._is_async:
2213
+ func = tool.func
2214
+ else:
2215
+ func = wrap_async(tool.func)
2216
+
2136
2217
  # First, invoke the request callbacks. If a ToolRejectError is raised,
2137
2218
  # treat it like a tool failure (i.e., gracefully handle it).
2138
2219
  result: ContentToolResult | None = None
chatlas/_content.py CHANGED
@@ -1,11 +1,14 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from pprint import pformat
4
- from typing import Any, Literal, Optional, Union
4
+ from typing import TYPE_CHECKING, Any, Literal, Optional, Union
5
5
 
6
6
  import orjson
7
7
  from pydantic import BaseModel, ConfigDict
8
8
 
9
+ if TYPE_CHECKING:
10
+ from ._tools import Tool
11
+
9
12
  ImageContentTypes = Literal[
10
13
  "image/png",
11
14
  "image/jpeg",
@@ -171,11 +174,15 @@ class ContentToolRequest(Content):
171
174
  The name of the tool/function to call.
172
175
  arguments
173
176
  The arguments to pass to the tool/function.
177
+ tool
178
+ The tool/function to be called. This is set internally by chatlas's tool
179
+ calling loop.
174
180
  """
175
181
 
176
182
  id: str
177
183
  name: str
178
184
  arguments: object
185
+ tool: Optional["Tool"] = None
179
186
 
180
187
  content_type: ContentTypeEnum = "tool_request"
181
188
 
chatlas/_provider.py CHANGED
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from abc import ABC, abstractmethod
4
+ from datetime import date
4
5
  from typing import (
5
6
  AsyncIterable,
6
7
  Generic,
@@ -16,7 +17,7 @@ from pydantic import BaseModel
16
17
  from ._content import Content
17
18
  from ._tools import Tool
18
19
  from ._turn import Turn
19
- from ._typing_extensions import TypedDict
20
+ from ._typing_extensions import NotRequired, TypedDict
20
21
 
21
22
  ChatCompletionT = TypeVar("ChatCompletionT")
22
23
  ChatCompletionChunkT = TypeVar("ChatCompletionChunkT")
@@ -35,6 +36,40 @@ submitting input to a model provider.
35
36
  """
36
37
 
37
38
 
39
+ class ModelInfo(TypedDict):
40
+ "Information returned from the `.list_models()` method"
41
+
42
+ id: str
43
+ "The model ID (this gets passed to the `model` parameter of the `Chat` constructor)"
44
+
45
+ cached_input: NotRequired[float | None]
46
+ "The cost per user token in USD per million tokens for cached input"
47
+
48
+ input: NotRequired[float | None]
49
+ "The cost per user token in USD per million tokens"
50
+
51
+ output: NotRequired[float | None]
52
+ "The cost per assistant token in USD per million tokens"
53
+
54
+ created_at: NotRequired[date]
55
+ "The date the model was created"
56
+
57
+ name: NotRequired[str]
58
+ "The model name"
59
+
60
+ owned_by: NotRequired[str]
61
+ "The owner of the model"
62
+
63
+ size: NotRequired[int]
64
+ "The size of the model in bytes"
65
+
66
+ provider: NotRequired[str]
67
+ "The provider of the model"
68
+
69
+ url: NotRequired[str]
70
+ "A URL to learn more about the model"
71
+
72
+
38
73
  class StandardModelParams(TypedDict, total=False):
39
74
  """
40
75
  A TypedDict representing the standard model parameters that can be set
@@ -102,6 +137,13 @@ class Provider(
102
137
  """
103
138
  return self._model
104
139
 
140
+ @abstractmethod
141
+ def list_models(self) -> list[ModelInfo]:
142
+ """
143
+ List all available models for the provider.
144
+ """
145
+ pass
146
+
105
147
  @overload
106
148
  @abstractmethod
107
149
  def chat_perform(
@@ -21,8 +21,8 @@ from ._content import (
21
21
  ContentToolResultResource,
22
22
  )
23
23
  from ._logging import log_model_default
24
- from ._provider import Provider, StandardModelParamNames, StandardModelParams
25
- from ._tokens import tokens_log
24
+ from ._provider import ModelInfo, Provider, StandardModelParamNames, StandardModelParams
25
+ from ._tokens import get_token_pricing, tokens_log
26
26
  from ._tools import Tool, basemodel_to_param_schema
27
27
  from ._turn import Turn, user_turn
28
28
  from ._utils import split_http_client_kwargs
@@ -209,6 +209,30 @@ class AnthropicProvider(
209
209
  self._client = Anthropic(**sync_kwargs) # type: ignore
210
210
  self._async_client = AsyncAnthropic(**async_kwargs)
211
211
 
212
+ def list_models(self):
213
+ models = self._client.models.list()
214
+
215
+ res: list[ModelInfo] = []
216
+ for m in models:
217
+ pricing = get_token_pricing(self.name, m.id) or {}
218
+ info: ModelInfo = {
219
+ "id": m.id,
220
+ "name": m.display_name,
221
+ "created_at": m.created_at.date(),
222
+ "input": pricing.get("input"),
223
+ "output": pricing.get("output"),
224
+ "cached_input": pricing.get("cached_input"),
225
+ }
226
+ res.append(info)
227
+
228
+ # Sort list by created_by field (more recent first)
229
+ res.sort(
230
+ key=lambda x: x.get("created_at", 0),
231
+ reverse=True,
232
+ )
233
+
234
+ return res
235
+
212
236
  @overload
213
237
  def chat_perform(
214
238
  self,
@@ -797,3 +821,26 @@ class AnthropicBedrockProvider(AnthropicProvider):
797
821
 
798
822
  self._client = AnthropicBedrock(**kwargs_full) # type: ignore
799
823
  self._async_client = AsyncAnthropicBedrock(**kwargs_full) # type: ignore
824
+
825
+ def list_models(self):
826
+ # boto3 should come via anthropic's bedrock extras
827
+ import boto3
828
+
829
+ bedrock = boto3.client("bedrock")
830
+ resp = bedrock.list_foundation_models()
831
+ models = resp["modelSummaries"]
832
+
833
+ res: list[ModelInfo] = []
834
+ for m in models:
835
+ pricing = get_token_pricing(self.name, m["modelId"]) or {}
836
+ info: ModelInfo = {
837
+ "id": m["modelId"],
838
+ "name": m["modelName"],
839
+ "provider": m["providerName"],
840
+ "input": pricing.get("input"),
841
+ "output": pricing.get("output"),
842
+ "cached_input": pricing.get("cached_input"),
843
+ }
844
+ res.append(info)
845
+
846
+ return res
@@ -153,7 +153,7 @@ def ChatCloudflare(
153
153
  base_url = f"{cloudflare_api}/{account}/ai/v1/"
154
154
 
155
155
  return Chat(
156
- provider=OpenAIProvider(
156
+ provider=CloudflareProvider(
157
157
  api_key=api_key,
158
158
  model=model,
159
159
  base_url=base_url,
@@ -163,3 +163,11 @@ def ChatCloudflare(
163
163
  ),
164
164
  system_prompt=system_prompt,
165
165
  )
166
+
167
+
168
+ class CloudflareProvider(OpenAIProvider):
169
+ def list_models(self):
170
+ raise NotImplementedError(
171
+ ".list_models() is not yet implemented for Cloudflare. "
172
+ "To view model availability online, see https://developers.cloudflare.com/workers-ai/models/"
173
+ )
@@ -128,6 +128,13 @@ class DatabricksProvider(OpenAIProvider):
128
128
  http_client=httpx.AsyncClient(auth=client._client.auth),
129
129
  )
130
130
 
131
+ def list_models(self):
132
+ raise NotImplementedError(
133
+ ".list_models() is not yet implemented for Databricks. "
134
+ "To view model availability online, see "
135
+ "https://docs.databricks.com/aws/en/machine-learning/model-serving/score-foundation-models#-foundation-model-types"
136
+ )
137
+
131
138
  # Databricks doesn't support stream_options
132
139
  def _chat_perform_args(
133
140
  self, stream, turns, tools, data_model=None, kwargs=None
@@ -3,9 +3,11 @@ from __future__ import annotations
3
3
  import os
4
4
  from typing import TYPE_CHECKING, Optional
5
5
 
6
+ import requests
7
+
6
8
  from ._chat import Chat
7
9
  from ._logging import log_model_default
8
- from ._provider_openai import OpenAIProvider
10
+ from ._provider_openai import ModelInfo, OpenAIProvider
9
11
  from ._utils import MISSING, MISSING_TYPE, is_testing
10
12
 
11
13
  if TYPE_CHECKING:
@@ -18,7 +20,7 @@ def ChatGithub(
18
20
  system_prompt: Optional[str] = None,
19
21
  model: Optional[str] = None,
20
22
  api_key: Optional[str] = None,
21
- base_url: str = "https://models.inference.ai.azure.com/",
23
+ base_url: str = "https://models.github.ai/inference/",
22
24
  seed: Optional[int] | MISSING_TYPE = MISSING,
23
25
  kwargs: Optional["ChatClientArgs"] = None,
24
26
  ) -> Chat["SubmitInputArgs", ChatCompletion]:
@@ -125,7 +127,7 @@ def ChatGithub(
125
127
  seed = 1014 if is_testing() else None
126
128
 
127
129
  return Chat(
128
- provider=OpenAIProvider(
130
+ provider=GitHubProvider(
129
131
  api_key=api_key,
130
132
  model=model,
131
133
  base_url=base_url,
@@ -135,3 +137,61 @@ def ChatGithub(
135
137
  ),
136
138
  system_prompt=system_prompt,
137
139
  )
140
+
141
+
142
+ class GitHubProvider(OpenAIProvider):
143
+ def __init__(self, base_url: str, **kwargs):
144
+ super().__init__(**kwargs)
145
+ self._base_url = base_url
146
+
147
+ def list_models(self) -> list[ModelInfo]:
148
+ # For some reason the OpenAI SDK API fails here? So perform request manually
149
+ # models = self._client.models.list()
150
+
151
+ base_url = self._base_url
152
+ if not base_url.endswith("/"):
153
+ base_url += "/"
154
+
155
+ if "azure" in base_url:
156
+ # i.e., https://models.inference.ai.azure.com
157
+ return list_models_gh_azure(base_url)
158
+ else:
159
+ # i.e., https://models.github.ai/inference/
160
+ return list_models_gh(base_url)
161
+
162
+
163
+ def list_models_gh(base_url: str = "https://models.github.ai/inference/"):
164
+ # replace /inference endpoint with /catalog
165
+ base_url = base_url.replace("/inference", "/catalog")
166
+ response = requests.get(f"{base_url}models")
167
+ response.raise_for_status()
168
+ models = response.json()
169
+
170
+ res: list[ModelInfo] = []
171
+ for m in models:
172
+ _id = m["id"].split("/")[-1]
173
+ info: ModelInfo = {
174
+ "id": _id,
175
+ "name": m["name"],
176
+ "provider": m["publisher"],
177
+ "url": m["html_url"],
178
+ }
179
+ res.append(info)
180
+
181
+ return res
182
+
183
+
184
+ def list_models_gh_azure(base_url: str = "https://models.inference.ai.azure.com"):
185
+ response = requests.get(f"{base_url}models")
186
+ response.raise_for_status()
187
+ models = response.json()
188
+
189
+ res: list[ModelInfo] = []
190
+ for m in models:
191
+ info: ModelInfo = {
192
+ "id": m["name"],
193
+ "provider": m["publisher"]
194
+ }
195
+ res.append(info)
196
+
197
+ return res
@@ -21,8 +21,8 @@ from ._content import (
21
21
  )
22
22
  from ._logging import log_model_default
23
23
  from ._merge import merge_dicts
24
- from ._provider import Provider, StandardModelParamNames, StandardModelParams
25
- from ._tokens import tokens_log
24
+ from ._provider import ModelInfo, Provider, StandardModelParamNames, StandardModelParams
25
+ from ._tokens import get_token_pricing, tokens_log
26
26
  from ._tools import Tool
27
27
  from ._turn import Turn, user_turn
28
28
 
@@ -180,6 +180,30 @@ class GoogleProvider(
180
180
 
181
181
  self._client = genai.Client(**kwargs_full)
182
182
 
183
+ def list_models(self):
184
+ models = self._client.models.list()
185
+
186
+ res: list[ModelInfo] = []
187
+ for m in models:
188
+ name = m.name or "[unknown]"
189
+ pricing = get_token_pricing(self.name, name) or {}
190
+ info: ModelInfo = {
191
+ "id": name,
192
+ "name": m.display_name or "[unknown]",
193
+ "input": pricing.get("input"),
194
+ "output": pricing.get("output"),
195
+ "cached_input": pricing.get("cached_input"),
196
+ }
197
+ res.append(info)
198
+
199
+ # Sort list by created_by field (more recent first)
200
+ res.sort(
201
+ key=lambda x: x.get("created", 0),
202
+ reverse=True,
203
+ )
204
+
205
+ return res
206
+
183
207
  @overload
184
208
  def chat_perform(
185
209
  self,
@@ -7,7 +7,7 @@ from typing import TYPE_CHECKING, Optional
7
7
  import orjson
8
8
 
9
9
  from ._chat import Chat
10
- from ._provider_openai import OpenAIProvider
10
+ from ._provider_openai import ModelInfo, OpenAIProvider
11
11
  from ._utils import MISSING_TYPE, is_testing
12
12
 
13
13
  if TYPE_CHECKING:
@@ -90,18 +90,19 @@ def ChatOllama(
90
90
  raise RuntimeError("Can't find locally running ollama.")
91
91
 
92
92
  if model is None:
93
- models = ollama_models(base_url)
93
+ models = ollama_model_info(base_url)
94
+ model_ids = [m["id"] for m in models]
94
95
  raise ValueError(
95
- f"Must specify model. Locally installed models: {', '.join(models)}"
96
+ f"Must specify model. Locally installed models: {', '.join(model_ids)}"
96
97
  )
97
98
  if isinstance(seed, MISSING_TYPE):
98
99
  seed = 1014 if is_testing() else None
99
100
 
100
101
  return Chat(
101
- provider=OpenAIProvider(
102
+ provider=OllamaProvider(
102
103
  api_key="ollama", # ignored
103
104
  model=model,
104
- base_url=f"{base_url}/v1",
105
+ base_url=base_url,
105
106
  seed=seed,
106
107
  name="Ollama",
107
108
  kwargs=kwargs,
@@ -110,10 +111,40 @@ def ChatOllama(
110
111
  )
111
112
 
112
113
 
113
- def ollama_models(base_url: str) -> list[str]:
114
- res = urllib.request.urlopen(url=f"{base_url}/api/tags")
115
- data = orjson.loads(res.read())
116
- return [re.sub(":latest$", "", x["name"]) for x in data["models"]]
114
+ class OllamaProvider(OpenAIProvider):
115
+ def __init__(self, *, api_key, model, base_url, seed, name, kwargs):
116
+ super().__init__(
117
+ api_key=api_key,
118
+ model=model,
119
+ base_url=f"{base_url}/v1",
120
+ seed=seed,
121
+ name=name,
122
+ kwargs=kwargs,
123
+ )
124
+ self.base_url = base_url
125
+
126
+ def list_models(self):
127
+ return ollama_model_info(self.base_url)
128
+
129
+
130
+ def ollama_model_info(base_url: str) -> list[ModelInfo]:
131
+ response = urllib.request.urlopen(url=f"{base_url}/api/tags")
132
+ data = orjson.loads(response.read())
133
+ models = data.get("models", [])
134
+ if not models:
135
+ return []
136
+
137
+ res: list[ModelInfo] = []
138
+ for model in models:
139
+ # TODO: add capabilities
140
+ info: ModelInfo = {
141
+ "id": re.sub(":latest$", "", model["name"]),
142
+ "created_at": model["modified_at"],
143
+ "size": model["size"],
144
+ }
145
+ res.append(info)
146
+
147
+ return res
117
148
 
118
149
 
119
150
  def has_ollama(base_url):
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import base64
4
+ from datetime import datetime
4
5
  from typing import TYPE_CHECKING, Any, Literal, Optional, cast, overload
5
6
 
6
7
  import orjson
@@ -23,8 +24,8 @@ from ._content import (
23
24
  )
24
25
  from ._logging import log_model_default
25
26
  from ._merge import merge_dicts
26
- from ._provider import Provider, StandardModelParamNames, StandardModelParams
27
- from ._tokens import tokens_log
27
+ from ._provider import ModelInfo, Provider, StandardModelParamNames, StandardModelParams
28
+ from ._tokens import get_token_pricing, tokens_log
28
29
  from ._tools import Tool, basemodel_to_param_schema
29
30
  from ._turn import Turn, user_turn
30
31
  from ._utils import MISSING, MISSING_TYPE, is_testing, split_http_client_kwargs
@@ -200,6 +201,32 @@ class OpenAIProvider(
200
201
  self._client = OpenAI(**sync_kwargs) # type: ignore
201
202
  self._async_client = AsyncOpenAI(**async_kwargs)
202
203
 
204
+ def list_models(self):
205
+ models = self._client.models.list()
206
+
207
+ res: list[ModelInfo] = []
208
+ for m in models:
209
+ pricing = get_token_pricing(self.name, m.id) or {}
210
+ info: ModelInfo = {
211
+ "id": m.id,
212
+ "owned_by": m.owned_by,
213
+ "input": pricing.get("input"),
214
+ "output": pricing.get("output"),
215
+ "cached_input": pricing.get("cached_input"),
216
+ }
217
+ # DeepSeek compatibility
218
+ if m.created is not None:
219
+ info["created_at"] = datetime.fromtimestamp(m.created).date()
220
+ res.append(info)
221
+
222
+ # More recent models first
223
+ res.sort(
224
+ key=lambda x: x.get("created_at", 0),
225
+ reverse=True,
226
+ )
227
+
228
+ return res
229
+
203
230
  @overload
204
231
  def chat_perform(
205
232
  self,