Skip to content

Commit 551095c

Browse files
committed
refactor: lazy instantiation of all HybridSearchRetriever object variables
1 parent 4fd0e8c commit 551095c

File tree

1 file changed

+60
-18
lines changed

1 file changed

+60
-18
lines changed

models/hybrid_search_retreiver.py

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,69 @@ def create_documents(self, texts):
7878
class HybridSearchRetriever:
7979
"""Hybrid Search Retriever (OpenAI + Pinecone)"""
8080

81+
_chat: ChatOpenAI = None
82+
_openai_embeddings: OpenAIEmbeddings = None
83+
_pinecone_index: pinecone.Index = None
84+
_vector_store: Pinecone = None
85+
_text_splitter: TextSplitter = None
86+
_b25_encoder: BM25Encoder = None
87+
8188
# prompting wrapper
82-
chat = ChatOpenAI(
83-
api_key=Credentials.OPENAI_API_KEY,
84-
organization=Credentials.OPENAI_API_ORGANIZATION,
85-
cache=Config.OPENAI_CHAT_CACHE,
86-
max_retries=Config.OPENAI_CHAT_MAX_RETRIES,
87-
model=Config.OPENAI_CHAT_MODEL_NAME,
88-
temperature=Config.OPENAI_CHAT_TEMPERATURE,
89-
)
89+
@property
90+
def chat(self):
91+
"""ChatOpenAI read-only property."""
92+
if self._chat is None:
93+
self._chat = ChatOpenAI(
94+
api_key=Credentials.OPENAI_API_KEY,
95+
organization=Credentials.OPENAI_API_ORGANIZATION,
96+
cache=Config.OPENAI_CHAT_CACHE,
97+
max_retries=Config.OPENAI_CHAT_MAX_RETRIES,
98+
model=Config.OPENAI_CHAT_MODEL_NAME,
99+
temperature=Config.OPENAI_CHAT_TEMPERATURE,
100+
)
101+
return self._chat
90102

91103
# embeddings
92-
openai_embeddings = OpenAIEmbeddings(
93-
api_key=Credentials.OPENAI_API_KEY, organization=Credentials.OPENAI_API_ORGANIZATION
94-
)
95-
pinecone_index = pinecone.Index(index_name=Config.PINECONE_INDEX_NAME)
96-
vector_store = Pinecone(
97-
index=pinecone_index, embedding=openai_embeddings, text_key=Config.PINECONE_VECTORSTORE_TEXT_KEY
98-
)
99-
100-
text_splitter = TextSplitter()
101-
bm25_encoder = BM25Encoder().default()
104+
@property
105+
def openai_embeddings(self):
106+
"""OpenAIEmbeddings read-only property."""
107+
if self._openai_embeddings is None:
108+
self._openai_embeddings = OpenAIEmbeddings(
109+
api_key=Credentials.OPENAI_API_KEY, organization=Credentials.OPENAI_API_ORGANIZATION
110+
)
111+
return self._openai_embeddings
112+
113+
@property
114+
def pinecone_index(self):
115+
"""pinecone.Index read-only property."""
116+
if self._pinecone_index is None:
117+
self._pinecone_index = pinecone.Index(index_name=Config.PINECONE_INDEX_NAME)
118+
return self._pinecone_index
119+
120+
@property
121+
def vector_store(self):
122+
"""Pinecone read-only property."""
123+
if self._vector_store is None:
124+
self._vector_store = Pinecone(
125+
index=self.pinecone_index,
126+
embedding=self.openai_embeddings,
127+
text_key=Config.PINECONE_VECTORSTORE_TEXT_KEY,
128+
)
129+
return self._vector_store
130+
131+
@property
132+
def text_splitter(self):
133+
"""TextSplitter read-only property."""
134+
if self._text_splitter is None:
135+
self._text_splitter = TextSplitter()
136+
return self._text_splitter
137+
138+
@property
139+
def bm25_encoder(self):
140+
"""BM25Encoder read-only property."""
141+
if self._b25_encoder is None:
142+
self._b25_encoder = BM25Encoder().default()
143+
return self._b25_encoder
102144

103145
def cached_chat_request(
104146
self, system_message: Union[str, SystemMessage], human_message: Union[str, HumanMessage]

0 commit comments

Comments
 (0)