chore: format

This commit is contained in:
Timothy J. Baek 2024-10-20 18:38:06 -07:00
parent 768b7e139c
commit 9936583477
6 changed files with 85 additions and 72 deletions

View File

@ -110,8 +110,7 @@ class ChromaClient:
def insert(self, collection_name: str, items: list[VectorItem]): def insert(self, collection_name: str, items: list[VectorItem]):
# Insert the items into the collection, if the collection does not exist, it will be created. # Insert the items into the collection, if the collection does not exist, it will be created.
collection = self.client.get_or_create_collection( collection = self.client.get_or_create_collection(
name=collection_name, name=collection_name, metadata={"hnsw:space": "cosine"}
metadata={"hnsw:space": "cosine"}
) )
ids = [item["id"] for item in items] ids = [item["id"] for item in items]
@ -131,8 +130,7 @@ class ChromaClient:
def upsert(self, collection_name: str, items: list[VectorItem]): def upsert(self, collection_name: str, items: list[VectorItem]):
# Update the items in the collection, if the items are not present, insert them. If the collection does not exist, it will be created. # Update the items in the collection, if the items are not present, insert them. If the collection does not exist, it will be created.
collection = self.client.get_or_create_collection( collection = self.client.get_or_create_collection(
name=collection_name, name=collection_name, metadata={"hnsw:space": "cosine"}
metadata={"hnsw:space": "cosine"}
) )
ids = [item["id"] for item in items] ids = [item["id"] for item in items]

View File

@ -9,6 +9,7 @@ from open_webui.config import QDRANT_URI
NO_LIMIT = 999999999 NO_LIMIT = 999999999
class QdrantClient: class QdrantClient:
def __init__(self): def __init__(self):
self.collection_prefix = "open-webui" self.collection_prefix = "open-webui"
@ -38,15 +39,15 @@ class QdrantClient:
collection_name_with_prefix = f"{self.collection_prefix}_{collection_name}" collection_name_with_prefix = f"{self.collection_prefix}_{collection_name}"
self.client.create_collection( self.client.create_collection(
collection_name=collection_name_with_prefix, collection_name=collection_name_with_prefix,
vectors_config=models.VectorParams(size=dimension, distance=models.Distance.COSINE), vectors_config=models.VectorParams(
size=dimension, distance=models.Distance.COSINE
),
) )
print(f"collection {collection_name_with_prefix} successfully created!") print(f"collection {collection_name_with_prefix} successfully created!")
def _create_collection_if_not_exists(self, collection_name, dimension): def _create_collection_if_not_exists(self, collection_name, dimension):
if not self.has_collection( if not self.has_collection(collection_name=collection_name):
collection_name=collection_name
):
self._create_collection( self._create_collection(
collection_name=collection_name, dimension=dimension collection_name=collection_name, dimension=dimension
) )
@ -56,19 +57,20 @@ class QdrantClient:
PointStruct( PointStruct(
id=item["id"], id=item["id"],
vector=item["vector"], vector=item["vector"],
payload={ payload={"text": item["text"], "metadata": item["metadata"]},
"text": item["text"],
"metadata": item["metadata"]
},
) )
for item in items for item in items
] ]
def has_collection(self, collection_name: str) -> bool: def has_collection(self, collection_name: str) -> bool:
return self.client.collection_exists(f"{self.collection_prefix}_{collection_name}") return self.client.collection_exists(
f"{self.collection_prefix}_{collection_name}"
)
def delete_collection(self, collection_name: str): def delete_collection(self, collection_name: str):
return self.client.delete_collection(collection_name=f"{self.collection_prefix}_{collection_name}") return self.client.delete_collection(
collection_name=f"{self.collection_prefix}_{collection_name}"
)
def search( def search(
self, collection_name: str, vectors: list[list[float | int]], limit: int self, collection_name: str, vectors: list[list[float | int]], limit: int
@ -87,7 +89,7 @@ class QdrantClient:
ids=get_result.ids, ids=get_result.ids,
documents=get_result.documents, documents=get_result.documents,
metadatas=get_result.metadatas, metadatas=get_result.metadatas,
distances=[[point.score for point in query_response.points]] distances=[[point.score for point in query_response.points]],
) )
def query(self, collection_name: str, filter: dict, limit: Optional[int] = None): def query(self, collection_name: str, filter: dict, limit: Optional[int] = None):
@ -101,7 +103,10 @@ class QdrantClient:
field_conditions = [] field_conditions = []
for key, value in filter.items(): for key, value in filter.items():
field_conditions.append( field_conditions.append(
models.FieldCondition(key=f"metadata.{key}", match=models.MatchValue(value=value))) models.FieldCondition(
key=f"metadata.{key}", match=models.MatchValue(value=value)
)
)
points = self.client.query_points( points = self.client.query_points(
collection_name=f"{self.collection_prefix}_{collection_name}", collection_name=f"{self.collection_prefix}_{collection_name}",
@ -117,7 +122,7 @@ class QdrantClient:
# Get all the items in the collection. # Get all the items in the collection.
points = self.client.query_points( points = self.client.query_points(
collection_name=f"{self.collection_prefix}_{collection_name}", collection_name=f"{self.collection_prefix}_{collection_name}",
limit=NO_LIMIT # otherwise qdrant would set limit to 10! limit=NO_LIMIT, # otherwise qdrant would set limit to 10!
) )
return self._result_to_get_result(points.points) return self._result_to_get_result(points.points)
@ -162,9 +167,7 @@ class QdrantClient:
return self.client.delete( return self.client.delete(
collection_name=f"{self.collection_prefix}_{collection_name}", collection_name=f"{self.collection_prefix}_{collection_name}",
points_selector=models.FilterSelector( points_selector=models.FilterSelector(
filter=models.Filter( filter=models.Filter(must=field_conditions)
must=field_conditions
)
), ),
) )

View File

@ -409,7 +409,10 @@ OAUTH_ROLES_CLAIM = PersistentConfig(
OAUTH_ALLOWED_ROLES = PersistentConfig( OAUTH_ALLOWED_ROLES = PersistentConfig(
"OAUTH_ALLOWED_ROLES", "OAUTH_ALLOWED_ROLES",
"oauth.allowed_roles", "oauth.allowed_roles",
[role.strip() for role in os.environ.get("OAUTH_ALLOWED_ROLES", "user,admin").split(",")], [
role.strip()
for role in os.environ.get("OAUTH_ALLOWED_ROLES", "user,admin").split(",")
],
) )
OAUTH_ADMIN_ROLES = PersistentConfig( OAUTH_ADMIN_ROLES = PersistentConfig(
@ -418,6 +421,7 @@ OAUTH_ADMIN_ROLES = PersistentConfig(
[role.strip() for role in os.environ.get("OAUTH_ADMIN_ROLES", "admin").split(",")], [role.strip() for role in os.environ.get("OAUTH_ADMIN_ROLES", "admin").split(",")],
) )
def load_oauth_providers(): def load_oauth_providers():
OAUTH_PROVIDERS.clear() OAUTH_PROVIDERS.clear()
if GOOGLE_CLIENT_ID.value and GOOGLE_CLIENT_SECRET.value: if GOOGLE_CLIENT_ID.value and GOOGLE_CLIENT_SECRET.value:

View File

@ -208,8 +208,6 @@ app.state.config.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE = (
app.state.MODELS = {} app.state.MODELS = {}
################################## ##################################
# #
# ChatCompletion Middleware # ChatCompletion Middleware

View File

@ -25,7 +25,10 @@ from open_webui.config import (
OAUTH_PICTURE_CLAIM, OAUTH_PICTURE_CLAIM,
OAUTH_USERNAME_CLAIM, OAUTH_USERNAME_CLAIM,
OAUTH_ALLOWED_ROLES, OAUTH_ALLOWED_ROLES,
OAUTH_ADMIN_ROLES, WEBHOOK_URL, JWT_EXPIRES_IN, AppConfig, OAUTH_ADMIN_ROLES,
WEBHOOK_URL,
JWT_EXPIRES_IN,
AppConfig,
) )
from open_webui.constants import ERROR_MESSAGES from open_webui.constants import ERROR_MESSAGES
from open_webui.env import WEBUI_SESSION_COOKIE_SAME_SITE, WEBUI_SESSION_COOKIE_SECURE from open_webui.env import WEBUI_SESSION_COOKIE_SAME_SITE, WEBUI_SESSION_COOKIE_SECURE
@ -170,7 +173,9 @@ class OAuthManager:
# If the user does not exist, check if signups are enabled # If the user does not exist, check if signups are enabled
if auth_manager_config.ENABLE_OAUTH_SIGNUP.value: if auth_manager_config.ENABLE_OAUTH_SIGNUP.value:
# Check if an existing user with the same email already exists # Check if an existing user with the same email already exists
existing_user = Users.get_user_by_email(user_data.get("email", "").lower()) existing_user = Users.get_user_by_email(
user_data.get("email", "").lower()
)
if existing_user: if existing_user:
raise HTTPException(400, detail=ERROR_MESSAGES.EMAIL_TAKEN) raise HTTPException(400, detail=ERROR_MESSAGES.EMAIL_TAKEN)
@ -182,16 +187,18 @@ class OAuthManager:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(picture_url) as resp: async with session.get(picture_url) as resp:
picture = await resp.read() picture = await resp.read()
base64_encoded_picture = base64.b64encode(picture).decode( base64_encoded_picture = base64.b64encode(
"utf-8" picture
) ).decode("utf-8")
guessed_mime_type = mimetypes.guess_type(picture_url)[0] guessed_mime_type = mimetypes.guess_type(picture_url)[0]
if guessed_mime_type is None: if guessed_mime_type is None:
# assume JPG, browsers are tolerant enough of image formats # assume JPG, browsers are tolerant enough of image formats
guessed_mime_type = "image/jpeg" guessed_mime_type = "image/jpeg"
picture_url = f"data:{guessed_mime_type};base64,{base64_encoded_picture}" picture_url = f"data:{guessed_mime_type};base64,{base64_encoded_picture}"
except Exception as e: except Exception as e:
log.error(f"Error downloading profile image '{picture_url}': {e}") log.error(
f"Error downloading profile image '{picture_url}': {e}"
)
picture_url = "" picture_url = ""
if not picture_url: if not picture_url:
picture_url = "/user.png" picture_url = "/user.png"
@ -216,7 +223,9 @@ class OAuthManager:
auth_manager_config.WEBHOOK_MESSAGES.USER_SIGNUP(user.name), auth_manager_config.WEBHOOK_MESSAGES.USER_SIGNUP(user.name),
{ {
"action": "signup", "action": "signup",
"message": auth_manager_config.WEBHOOK_MESSAGES.USER_SIGNUP(user.name), "message": auth_manager_config.WEBHOOK_MESSAGES.USER_SIGNUP(
user.name
),
"user": user.model_dump_json(exclude_none=True), "user": user.model_dump_json(exclude_none=True),
}, },
) )
@ -243,4 +252,5 @@ class OAuthManager:
redirect_url = f"{request.base_url}auth#token={jwt_token}" redirect_url = f"{request.base_url}auth#token={jwt_token}"
return RedirectResponse(url=redirect_url) return RedirectResponse(url=redirect_url)
oauth_manager = OAuthManager() oauth_manager = OAuthManager()