forked from PrefectHQ/marvin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaimlapi.py
More file actions
66 lines (51 loc) · 2.24 KB
/
Copy pathaimlapi.py
File metadata and controls
66 lines (51 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations
import os
from typing import overload
from httpx import AsyncClient as AsyncHTTPClient
from openai import AsyncOpenAI
from pydantic_ai.exceptions import UserError
from pydantic_ai.models import cached_async_http_client
from pydantic_ai.profiles import ModelProfile
from pydantic_ai.profiles.openai import openai_model_profile
from pydantic_ai.providers import Provider
class AIMLAPIProvider(Provider[AsyncOpenAI]):
"""Provider for the AI/ML API."""
@property
def name(self) -> str: # pragma: no cover - simple property
return "aimlapi"
@property
def base_url(self) -> str: # pragma: no cover - simple property
return "https://api.aimlapi.com/v1"
@property
def client(self) -> AsyncOpenAI:
return self._client
def model_profile(self, model_name: str) -> ModelProfile | None: # pragma: no cover - thin wrapper
return openai_model_profile(model_name)
@overload
def __init__(self) -> None: ...
@overload
def __init__(self, *, api_key: str) -> None: ...
@overload
def __init__(self, *, api_key: str, http_client: AsyncHTTPClient) -> None: ...
@overload
def __init__(self, *, openai_client: AsyncOpenAI | None = None) -> None: ...
def __init__(
self,
*,
api_key: str | None = None,
openai_client: AsyncOpenAI | None = None,
http_client: AsyncHTTPClient | None = None,
) -> None:
api_key = api_key or os.getenv("AIML_API_KEY")
if not api_key and openai_client is None:
raise UserError(
"You must provide either an API key (set the `AIML_API_KEY` environment variable or pass it via `AIMLAPIProvider(api_key=...)`) "
"or an OpenAI client (pass it via `AIMLAPIProvider(openai_client=...)`) to use the AI/ML API provider."
)
if openai_client is not None:
self._client = openai_client
elif http_client is not None:
self._client = AsyncOpenAI(base_url=self.base_url, api_key=api_key, http_client=http_client)
else:
http_client = cached_async_http_client(provider="aimlapi")
self._client = AsyncOpenAI(base_url=self.base_url, api_key=api_key, http_client=http_client)