"vscode:/vscode.git/clone" did not exist on "4a3213f101d0a972e469c56951906d1e7144ebcc"
Commit 9c190b56 authored by Timothy J. Baek's avatar Timothy J. Baek
Browse files

feat: /v1/models endpoint added for ollama proxy

parent 72675ec1
...@@ -25,6 +25,7 @@ import uuid ...@@ -25,6 +25,7 @@ import uuid
import aiohttp import aiohttp
import asyncio import asyncio
import logging import logging
import time
from urllib.parse import urlparse from urllib.parse import urlparse
from typing import Optional, List, Union from typing import Optional, List, Union
...@@ -1031,6 +1032,75 @@ async def generate_openai_chat_completion( ...@@ -1031,6 +1032,75 @@ async def generate_openai_chat_completion(
) )
@app.get("/v1/models")
@app.get("/v1/models/{url_idx}")
async def get_openai_models(
url_idx: Optional[int] = None,
user=Depends(get_verified_user),
):
if url_idx == None:
models = await get_all_models()
if app.state.ENABLE_MODEL_FILTER:
if user.role == "user":
models["models"] = list(
filter(
lambda model: model["name"] in app.state.MODEL_FILTER_LIST,
models["models"],
)
)
return {
"data": [
{
"id": model["model"],
"object": "model",
"created": int(time.time()),
"owned_by": "openai",
}
for model in models["models"]
],
"object": "list",
}
else:
url = app.state.OLLAMA_BASE_URLS[url_idx]
try:
r = requests.request(method="GET", url=f"{url}/api/tags")
r.raise_for_status()
models = r.json()
return {
"data": [
{
"id": model["model"],
"object": "model",
"created": int(time.time()),
"owned_by": "openai",
}
for model in models["models"]
],
"object": "list",
}
except Exception as e:
log.exception(e)
error_detail = "Open WebUI: Server Connection Error"
if r is not None:
try:
res = r.json()
if "error" in res:
error_detail = f"Ollama: {res['error']}"
except:
error_detail = f"Ollama: {e}"
raise HTTPException(
status_code=r.status_code if r else 500,
detail=error_detail,
)
class UrlForm(BaseModel): class UrlForm(BaseModel):
url: str url: str
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment