Skip to content

Commit 2fb499c

Browse files
authored
Merge pull request #10 from FlowLLM-AI/vector_store_update
refactor(core): rename async embedding methods and update vector store
2 parents 8392ba7 + 05a63a0 commit 2fb499c

23 files changed

Lines changed: 4435 additions & 3446 deletions

.github/workflows/pre-commit.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Pre-commit
2+
3+
on: [ push, pull_request ]
4+
5+
jobs:
6+
run:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: True
10+
matrix:
11+
os: [ ubuntu-latest ]
12+
env:
13+
OS: ${{ matrix.os }}
14+
PYTHON: '3.10'
15+
steps:
16+
- uses: actions/checkout@master
17+
- name: Setup Python
18+
uses: actions/setup-python@master
19+
with:
20+
python-version: '3.10'
21+
- name: Update setuptools
22+
run: |
23+
pip install -U setuptools wheel
24+
- name: Install
25+
run: |
26+
pip install -q -e .[dev]
27+
- name: Install pre-commit
28+
run: |
29+
pre-commit install
30+
- name: Pre-commit starts
31+
run: |
32+
pre-commit run --all-files > pre-commit.log 2>&1 || true
33+
cat pre-commit.log
34+
if grep -q Failed pre-commit.log; then
35+
echo -e "\e[41m [**FAIL**] Please install pre-commit and format your code first. \e[0m"
36+
exit 1
37+
fi
38+
echo -e "\e[46m ********************************Passed******************************** \e[0m"

docs/zh/guide/async_op_advance_guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RAGSearchOp(BaseAsyncOp):
3030
assert query, "query 不能为空"
3131

3232
# 2. 使用 Embedding 生成查询向量(一行代码示意 emb)
33-
query_vector = await self.embedding_model.get_embeddings_async(query)
33+
query_vector = await self.embedding_model.async_get_embeddings(query)
3434

3535
# 3. 使用 VectorStore 进行语义搜索(一行代码示意 vectorstore)
3636
nodes = await self.vector_store.async_search(query=query, workspace_id=workspace_id, top_k=top_k)

flowllm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
from . import gallery # noqa: E402, F401 # pylint: disable=wrong-import-position,unused-import
1212
from . import extensions # noqa: E402, F401 # pylint: disable=wrong-import-position,unused-import
1313

14-
__version__ = "0.2.0.8"
14+
__version__ = "0.2.0.9"

flowllm/core/embedding_model/base_embedding_model.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _get_embeddings(self, input_text: str | List[str]):
6262
"""
6363
raise NotImplementedError
6464

65-
async def _get_embeddings_async(self, input_text: str | List[str]):
65+
async def _async_get_embeddings(self, input_text: str | List[str]):
6666
"""
6767
Abstract async method to get embeddings from the model.
6868
@@ -104,7 +104,7 @@ def get_embeddings(self, input_text: str | List[str]):
104104
# Return None if all retries failed and raise_exception is False
105105
return None
106106

107-
async def get_embeddings_async(self, input_text: str | List[str]):
107+
async def async_get_embeddings(self, input_text: str | List[str]):
108108
"""
109109
Get embeddings asynchronously with retry logic and error handling.
110110
@@ -120,7 +120,7 @@ async def get_embeddings_async(self, input_text: str | List[str]):
120120
# Retry loop with exponential backoff potential
121121
for i in range(self.max_retries):
122122
try:
123-
return await self._get_embeddings_async(input_text)
123+
return await self._async_get_embeddings(input_text)
124124

125125
except Exception as e:
126126
logger.exception(f"embedding model name={self.model_name} encounter error with e={e.args}")
@@ -173,7 +173,7 @@ def get_node_embeddings(self, nodes: VectorNode | List[VectorNode]):
173173
else:
174174
raise TypeError(f"unsupported type={type(nodes)}")
175175

176-
async def get_node_embeddings_async(self, nodes: VectorNode | List[VectorNode]):
176+
async def async_get_node_embeddings(self, nodes: VectorNode | List[VectorNode]):
177177
"""
178178
Generate embeddings asynchronously for VectorNode objects and update their vector fields.
179179
@@ -191,7 +191,7 @@ async def get_node_embeddings_async(self, nodes: VectorNode | List[VectorNode]):
191191
"""
192192
# Handle single VectorNode
193193
if isinstance(nodes, VectorNode):
194-
nodes.vector = await self.get_embeddings_async(nodes.content)
194+
nodes.vector = await self.async_get_embeddings(nodes.content)
195195
return nodes
196196

197197
# Handle list of VectorNodes with batch processing
@@ -201,7 +201,7 @@ async def get_node_embeddings_async(self, nodes: VectorNode | List[VectorNode]):
201201
for i in range(0, len(nodes), self.max_batch_size):
202202
batch_nodes = nodes[i : i + self.max_batch_size]
203203
batch_content = [node.content for node in batch_nodes]
204-
batch_tasks.append(self.get_embeddings_async(batch_content))
204+
batch_tasks.append(self.async_get_embeddings(batch_content))
205205

206206
# Execute all batch tasks concurrently
207207
batch_results = await asyncio.gather(*batch_tasks)
@@ -220,3 +220,9 @@ async def get_node_embeddings_async(self, nodes: VectorNode | List[VectorNode]):
220220

221221
else:
222222
raise TypeError(f"unsupported type={type(nodes)}")
223+
224+
def close(self):
225+
"""Close the client connection or clean up resources."""
226+
227+
async def async_close(self):
228+
"""Asynchronously close the client connection or clean up resources."""

flowllm/core/embedding_model/openai_compatible_embedding_model.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _get_embeddings(self, input_text: str | List[str]):
105105
else:
106106
raise RuntimeError(f"unsupported type={type(input_text)}")
107107

108-
async def _get_embeddings_async(self, input_text: str | List[str]):
108+
async def _async_get_embeddings(self, input_text: str | List[str]):
109109
"""
110110
Get embeddings asynchronously from the OpenAI-compatible API.
111111
@@ -139,3 +139,11 @@ async def _get_embeddings_async(self, input_text: str | List[str]):
139139

140140
else:
141141
raise RuntimeError(f"unsupported type={type(input_text)}")
142+
143+
def close(self):
144+
"""Close the OpenAI clients and clean up resources."""
145+
self._client.close()
146+
147+
async def async_close(self):
148+
"""Asynchronously close the OpenAI clients and clean up resources."""
149+
await self._async_client.close()

flowllm/core/llm/base_llm.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,9 @@ async def achat(
260260
await asyncio.sleep(1 + i)
261261

262262
return default_value
263+
264+
def close(self):
265+
"""Close the client connection or clean up resources."""
266+
267+
async def async_close(self):
268+
"""Asynchronously close the client connection or clean up resources."""

flowllm/core/llm/openai_compatible_llm.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,9 @@ async def _achat(
429429
content=answer_content,
430430
tool_calls=tool_calls,
431431
)
432+
433+
def close(self):
434+
self._client.close()
435+
436+
async def async_close(self):
437+
await self._aclient.close()

0 commit comments

Comments
 (0)