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
a48ac6a2
Commit
a48ac6a2
authored
Jul 01, 2024
by
Jun Siang Cheah
Browse files
refac: lazily load sentence_transformers to reduce start up memory usage
parent
17c68436
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
4 deletions
+6
-4
backend/apps/rag/main.py
backend/apps/rag/main.py
+4
-2
backend/apps/rag/utils.py
backend/apps/rag/utils.py
+2
-2
No files found.
backend/apps/rag/main.py
View file @
a48ac6a2
...
@@ -48,8 +48,6 @@ import mimetypes
...
@@ -48,8 +48,6 @@ import mimetypes
import
uuid
import
uuid
import
json
import
json
import
sentence_transformers
from
apps.webui.models.documents
import
(
from
apps.webui.models.documents
import
(
Documents
,
Documents
,
DocumentForm
,
DocumentForm
,
...
@@ -190,6 +188,8 @@ def update_embedding_model(
...
@@ -190,6 +188,8 @@ def update_embedding_model(
update_model
:
bool
=
False
,
update_model
:
bool
=
False
,
):
):
if
embedding_model
and
app
.
state
.
config
.
RAG_EMBEDDING_ENGINE
==
""
:
if
embedding_model
and
app
.
state
.
config
.
RAG_EMBEDDING_ENGINE
==
""
:
import
sentence_transformers
app
.
state
.
sentence_transformer_ef
=
sentence_transformers
.
SentenceTransformer
(
app
.
state
.
sentence_transformer_ef
=
sentence_transformers
.
SentenceTransformer
(
get_model_path
(
embedding_model
,
update_model
),
get_model_path
(
embedding_model
,
update_model
),
device
=
DEVICE_TYPE
,
device
=
DEVICE_TYPE
,
...
@@ -204,6 +204,8 @@ def update_reranking_model(
...
@@ -204,6 +204,8 @@ def update_reranking_model(
update_model
:
bool
=
False
,
update_model
:
bool
=
False
,
):
):
if
reranking_model
:
if
reranking_model
:
import
sentence_transformers
app
.
state
.
sentence_transformer_rf
=
sentence_transformers
.
CrossEncoder
(
app
.
state
.
sentence_transformer_rf
=
sentence_transformers
.
CrossEncoder
(
get_model_path
(
reranking_model
,
update_model
),
get_model_path
(
reranking_model
,
update_model
),
device
=
DEVICE_TYPE
,
device
=
DEVICE_TYPE
,
...
...
backend/apps/rag/utils.py
View file @
a48ac6a2
...
@@ -442,8 +442,6 @@ from langchain_core.documents import BaseDocumentCompressor, Document
...
@@ -442,8 +442,6 @@ from langchain_core.documents import BaseDocumentCompressor, Document
from
langchain_core.callbacks
import
Callbacks
from
langchain_core.callbacks
import
Callbacks
from
langchain_core.pydantic_v1
import
Extra
from
langchain_core.pydantic_v1
import
Extra
from
sentence_transformers
import
util
class
RerankCompressor
(
BaseDocumentCompressor
):
class
RerankCompressor
(
BaseDocumentCompressor
):
embedding_function
:
Any
embedding_function
:
Any
...
@@ -468,6 +466,8 @@ class RerankCompressor(BaseDocumentCompressor):
...
@@ -468,6 +466,8 @@ class RerankCompressor(BaseDocumentCompressor):
[(
query
,
doc
.
page_content
)
for
doc
in
documents
]
[(
query
,
doc
.
page_content
)
for
doc
in
documents
]
)
)
else
:
else
:
from
sentence_transformers
import
util
query_embedding
=
self
.
embedding_function
(
query
)
query_embedding
=
self
.
embedding_function
(
query
)
document_embedding
=
self
.
embedding_function
(
document_embedding
=
self
.
embedding_function
(
[
doc
.
page_content
for
doc
in
documents
]
[
doc
.
page_content
for
doc
in
documents
]
...
...
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