presenton/servers/fastapi/utils/available_models.py

34 lines
1.1 KiB
Python

import aiohttp
from openai import AsyncOpenAI
from google import genai
async def list_available_openai_compatible_models(url: str, api_key: str) -> list[str]:
client = AsyncOpenAI(api_key=api_key, base_url=url)
models = (await client.models.list()).data
if models:
return list(map(lambda x: x.id, models))
return []
async def list_available_anthropic_models(api_key: str) -> list[str]:
async with aiohttp.ClientSession(
headers={
"x-api-key": api_key,
"anthropic-version": "2023-06-01",
}
) as session:
async with session.get(
"https://api.anthropic.com/v1/models",
params={"limit": 50},
) as response:
response.raise_for_status()
data = await response.json()
models = data.get("data", [])
return [model.get("id") for model in models if model.get("id")]
async def list_available_google_models(api_key: str) -> list[str]:
client = genai.Client(api_key=api_key)
return list(map(lambda x: x.name, client.models.list(config={"page_size": 50})))