mirror of
https://github.com/open-webui/open-webui
synced 2025-03-19 19:48:14 +00:00
fix: ollama version request when ollama api is disabled
This commit is contained in:
parent
1d6bbdf917
commit
d077b3dcdb
@ -274,54 +274,57 @@ async def get_ollama_tags(
|
|||||||
@app.get("/api/version")
|
@app.get("/api/version")
|
||||||
@app.get("/api/version/{url_idx}")
|
@app.get("/api/version/{url_idx}")
|
||||||
async def get_ollama_versions(url_idx: Optional[int] = None):
|
async def get_ollama_versions(url_idx: Optional[int] = None):
|
||||||
|
if app.state.config.ENABLE_OLLAMA_API:
|
||||||
|
if url_idx == None:
|
||||||
|
|
||||||
if url_idx == None:
|
# returns lowest version
|
||||||
|
tasks = [
|
||||||
|
fetch_url(f"{url}/api/version")
|
||||||
|
for url in app.state.config.OLLAMA_BASE_URLS
|
||||||
|
]
|
||||||
|
responses = await asyncio.gather(*tasks)
|
||||||
|
responses = list(filter(lambda x: x is not None, responses))
|
||||||
|
|
||||||
# returns lowest version
|
if len(responses) > 0:
|
||||||
tasks = [
|
lowest_version = min(
|
||||||
fetch_url(f"{url}/api/version") for url in app.state.config.OLLAMA_BASE_URLS
|
responses,
|
||||||
]
|
key=lambda x: tuple(
|
||||||
responses = await asyncio.gather(*tasks)
|
map(int, re.sub(r"^v|-.*", "", x["version"]).split("."))
|
||||||
responses = list(filter(lambda x: x is not None, responses))
|
),
|
||||||
|
)
|
||||||
|
|
||||||
if len(responses) > 0:
|
return {"version": lowest_version["version"]}
|
||||||
lowest_version = min(
|
else:
|
||||||
responses,
|
raise HTTPException(
|
||||||
key=lambda x: tuple(
|
status_code=500,
|
||||||
map(int, re.sub(r"^v|-.*", "", x["version"]).split("."))
|
detail=ERROR_MESSAGES.OLLAMA_NOT_FOUND,
|
||||||
),
|
)
|
||||||
)
|
|
||||||
|
|
||||||
return {"version": lowest_version["version"]}
|
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
url = app.state.config.OLLAMA_BASE_URLS[url_idx]
|
||||||
status_code=500,
|
|
||||||
detail=ERROR_MESSAGES.OLLAMA_NOT_FOUND,
|
r = None
|
||||||
)
|
try:
|
||||||
|
r = requests.request(method="GET", url=f"{url}/api/version")
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
return r.json()
|
||||||
|
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,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
url = app.state.config.OLLAMA_BASE_URLS[url_idx]
|
return {"version": False}
|
||||||
|
|
||||||
r = None
|
|
||||||
try:
|
|
||||||
r = requests.request(method="GET", url=f"{url}/api/version")
|
|
||||||
r.raise_for_status()
|
|
||||||
|
|
||||||
return r.json()
|
|
||||||
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 ModelNameForm(BaseModel):
|
class ModelNameForm(BaseModel):
|
||||||
|
@ -84,3 +84,7 @@ class ERROR_MESSAGES(str, Enum):
|
|||||||
WEB_SEARCH_ERROR = (
|
WEB_SEARCH_ERROR = (
|
||||||
lambda err="": f"{err if err else 'Oops! Something went wrong while searching the web.'}"
|
lambda err="": f"{err if err else 'Oops! Something went wrong while searching the web.'}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
OLLAMA_API_DISABLED = (
|
||||||
|
"The Ollama API is disabled. Please enable it to use this feature."
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user