-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependencies.py
55 lines (42 loc) · 1.67 KB
/
dependencies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# dependencies.py
import os
import asyncio
from fastapi import HTTPException, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastembed import TextEmbedding
from qdrant_client import AsyncQdrantClient
# Singleton class to manage a single instance of TextEmbedding
class SingletonTextEmbedding:
_instance = None
@classmethod
def get_instance(cls):
if cls._instance is None:
raise Exception("SingletonTextEmbedding has not been initialized")
return cls._instance
@classmethod
async def initialize(cls):
if cls._instance is None:
cls._instance = TextEmbedding(
model_name=os.getenv("LOCAL_MODEL"), cache_dir="/app/models", parallel="none", threads=3
)
# Function to initialize text embedding at app startup
async def initialize_text_embedding():
await SingletonTextEmbedding.initialize()
# Dependency to get embeddings model
def get_embeddings_model():
return SingletonTextEmbedding.get_instance()
# Function to create Qdrant client
async def create_qdrant_client():
return AsyncQdrantClient(
url=os.getenv("QDRANT_HOST", "http://qdrant:6333"),
api_key=os.getenv("QDRANT_API_KEY"),
)
# This function checks if the provided API key is valid or not
async def get_api_key(
credentials: HTTPAuthorizationCredentials = Depends(HTTPBearer(auto_error=False)),
):
if os.getenv("MEMORIES_API_KEY") and (
not credentials or credentials.credentials != os.getenv("MEMORIES_API_KEY")
):
raise HTTPException(status_code=403, detail="Invalid or missing API key")
return credentials.credentials if credentials else None