Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
9c190b56
"vscode:/vscode.git/clone" did not exist on "4a3213f101d0a972e469c56951906d1e7144ebcc"
Commit
9c190b56
authored
May 07, 2024
by
Timothy J. Baek
Browse files
feat: /v1/models endpoint added for ollama proxy
parent
72675ec1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
0 deletions
+70
-0
backend/apps/ollama/main.py
backend/apps/ollama/main.py
+70
-0
No files found.
backend/apps/ollama/main.py
View file @
9c190b56
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment