Skip to content

Commit aac7cf2

Browse files
authored
升级项目依赖到最新稳定版本 (#5)
1 parent 83fc37e commit aac7cf2

File tree

11 files changed

+1713
-809
lines changed

11 files changed

+1713
-809
lines changed

.script/integration_test_compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
elasticsearch:
3-
image: docker.elastic.co/elasticsearch/elasticsearch:9.1.3
3+
image: docker.elastic.co/elasticsearch/elasticsearch:9.1.5
44
environment:
55
# 单节点模式配置
66
- discovery.type=single-node
@@ -17,7 +17,7 @@ services:
1717
# 检查插件是否已安装
1818
if ! elasticsearch-plugin list | grep -q analysis-ik; then
1919
echo '🔧 安装IK插件...'
20-
elasticsearch-plugin install --batch https://release.infinilabs.com/analysis-ik/stable/elasticsearch-analysis-ik-9.1.3.zip
20+
elasticsearch-plugin install --batch https://release.infinilabs.com/analysis-ik/stable/elasticsearch-analysis-ik-9.1.5.zip
2121
echo '✅ IK插件安装完成'
2222
fi
2323
# 启动ES

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 使用官方 Python 镜像
2-
FROM python:3.12-slim
2+
FROM python:3.13-slim
33

44
# 设置环境变量
55
ENV PYTHONUNBUFFERED=1 \

README.md

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
### 环境要求
1818

19-
- **Python**: 3.12
19+
- **Python**: 3.x
2020
- **uv**: 现代Python包管理器(强烈推荐)
2121
- **Elasticsearch**: 9.x(用于文档存储和搜索)
2222

@@ -50,7 +50,7 @@ make setup
5050
```
5151

5252
`make setup` 会自动:
53-
- ✅ 创建 Python 3.12 虚拟环境
53+
- ✅ 创建 Python 虚拟环境
5454
- ✅ 安装所有依赖(包括开发工具)
5555
- ✅ 配置 Git 预提交钩子
5656

@@ -293,4 +293,72 @@ Docker 打包的时候,忽略掉了很多文件,具体可以参考项目下
293293

294294
因此在使用 docker 来部署的时候,必须挂载:
295295
- .env 文件
296-
- config.yaml 文件
296+
- config.yaml 文件
297+
298+
## 升级项目依赖
299+
300+
### 升级Python版本
301+
302+
1. 手动更新 pyproject.toml 配置文件中所有与 Python 版本相关的字段:
303+
304+
- 更新项目元数据 [project]:
305+
306+
```diff
307+
- requires-python = ">=3.12"
308+
+ requires-python = ">=3.13"
309+
```
310+
311+
- 更新 Ruff 配置 [tool.ruff]:
312+
313+
```diff
314+
- target-version = "py312"
315+
+ target-version = "py313"
316+
```
317+
318+
- 更新 MyPy 配置 [tool.mypy]:
319+
320+
```diff
321+
- python_version = "3.12"
322+
+ python_version = "3.13"
323+
```
324+
325+
2. 使用 uv 重建虚拟环境
326+
327+
328+
```bash
329+
# 1. (推荐) 删除旧的虚拟环境,确保一个完全纯净的开始
330+
rm -rf .venv
331+
332+
# 2. 使用 uv 创建一个新的虚拟环境。如果找不到,它会下载一个!
333+
uv venv --python 3.13
334+
335+
# 3. 激活新创建的环境
336+
source .venv/bin/activate
337+
338+
# 4. 同步所有依赖到新环境
339+
# uv sync 会读取 uv.lock 文件,在新环境中精确安装所有包
340+
uv sync
341+
```
342+
343+
### 升级其他依赖版本
344+
345+
> 使用 uv add 命令可以一站式完成版本更新、pyproject.toml 文件修改、锁文件更新和环境安装。
346+
347+
1. 检查所有过时的依赖名称:
348+
349+
```bash
350+
uv pip list --outdated
351+
```
352+
353+
2. 逐个升级`pyproject.toml`中定义的**直接**依赖,不要更新**间接依赖**:
354+
355+
```bash
356+
uv add pydantic@latest
357+
uv add langchain@latest
358+
```
359+
360+
3. 升级后务必运行测试:
361+
362+
```bash
363+
pytest
364+
```

app/utils/embedders/sentence_transformer.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from sentence_transformers import SentenceTransformer
16+
1517

1618
class SentenceTransformerEmbedder:
1719
def __init__(self, model_name: str, similarity: str) -> None:
@@ -20,8 +22,6 @@ def __init__(self, model_name: str, similarity: str) -> None:
2022
:param model_name: 模型名称
2123
:param similarity: 相似性算法名称 cosine,dot_product
2224
"""
23-
from sentence_transformers import SentenceTransformer
24-
2525
self.model = SentenceTransformer(model_name)
2626
self._similarity = similarity
2727

@@ -30,7 +30,12 @@ def embed_documents(self, texts: list[str]) -> list[list[float]]:
3030

3131
@property
3232
def dimensions(self) -> int:
33-
return int(self.model.get_sentence_embedding_dimension())
33+
d = self.model.get_sentence_embedding_dimension()
34+
if d is None:
35+
raise RuntimeError(
36+
"SentenceTransformerEmbedder: dimension cannot be None"
37+
)
38+
return d
3439

3540
@property
3641
def similarity_metric(self) -> str:

config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ elasticsearch:
1212
request_timeout: 60
1313

1414
embedder:
15-
model_name: "shibing624/text2vec-base-chinese" # "BAAI/bge-base-zh-v1.5"
15+
model_name: "BAAI/bge-base-zh-v1.5 " # "shibing624/text2vec-base-chinese" # "BAAI/bge-base-zh-v1.5"
1616
dimensions: 768
1717
similarity_metric: "cosine"
1818
index_type: "int8_hnsw" # 可选: "int8_hnsw", "hnsw", "flat"

pyproject.toml

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
[project]
22
name = "kbase"
33
version = "0.1.0"
4-
requires-python = ">=3.12,<3.13"
4+
requires-python = ">=3.13"
55
dependencies = [
66
"cos-python-sdk-v5>=1.9.38",
77
"elasticsearch>=9.1.0",
8-
"fastapi>=0.116.1",
8+
"fastapi>=0.118.2",
99
"gunicorn>=23.0.0",
1010
"langchain>=0.3.27",
11-
"langchain-community>=0.3.29",
11+
"langchain-community>=0.3.31",
1212
"mistune>=3.1.4",
13-
"numpy>=1.24,<2.0",
13+
"numpy>=2.3.3",
1414
"pdfplumber>=0.11.7",
15+
"pydantic>=2.12.0",
1516
"pydantic-settings>=2.10.1",
1617
"pypdf>=6.0.0",
1718
"python-multipart>=0.0.20",
1819
"pyyaml>=6.0.2",
20+
"ruff>=0.14.0",
1921
"scipy>=1.16.1",
20-
"sentence-transformers>=2.3.0,<2.7.0",
21-
"torch>=2.1.0",
22-
"transformers>=4.35.0,<4.50.0",
22+
"sentence-transformers>=5.1.1",
23+
"torch>=2.8.0",
24+
"transformers>=4.57.0",
2325
"unstructured[docx,pdf,pptx,xlsx]>=0.18.14",
2426
"uvicorn>=0.35.0",
2527
]
@@ -41,7 +43,7 @@ dev = [
4143

4244
[tool.ruff]
4345
line-length = 80
44-
target-version = "py312"
46+
target-version = "py313"
4547
lint.extend-select = [
4648
"B", # flake8-bugbear
4749
"I", # isort
@@ -71,7 +73,7 @@ suppress-none-returning = false # 检查返回None的函数
7173
strict = true # 严格的类型导入检查
7274

7375
[tool.mypy]
74-
python_version = "3.12"
76+
python_version = "3.13"
7577
packages = ["app", "tests"]
7678
strict = true
7779
warn_unreachable = true # 警告无法到达的代码
@@ -103,7 +105,3 @@ python_classes = "Test*"
103105
python_functions = "test_*"
104106

105107
[tool.uv]
106-
required-environments = [
107-
"sys_platform == 'darwin' and platform_machine == 'arm64'",
108-
"sys_platform == 'darwin' and platform_machine == 'x86_64'"
109-
]

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ def _builder(file_names: str | list[str]) -> Path | list[Path]: # noqa: F811
8484

8585

8686
@pytest.fixture(scope="module")
87-
def client() -> Generator[TestClient, Any, None]:
87+
def client() -> Generator[TestClient, Any]:
8888
"""V2 API 测试客户端"""
8989
with TestClient(app) as test_client:
9090
yield test_client
9191

9292

9393
@pytest.fixture(scope="session")
94-
def es_client() -> Generator[Elasticsearch, Any, None]:
94+
def es_client() -> Generator[Elasticsearch, Any]:
9595
"""
9696
Elasticsearch客户端 - 用于测试ES相关操作
9797
"""

tests/web/document/save_endpoint_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class TestSaveEndpoint:
3030
@pytest.fixture(scope="class", autouse=True)
3131
def setup_test_index(
3232
self, es_client: Elasticsearch
33-
) -> Generator[None, Any, None]:
33+
) -> Generator[None, Any]:
3434
"""设置测试索引"""
3535
# 如果索引存在则删除(清理之前的测试)
3636
if es_client.indices.exists(index=self.TEST_INDEX):

tests/web/document/structured_search_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TestStructuredSearch:
3232
@pytest.fixture(scope="class", autouse=True)
3333
def setup_environment(
3434
self, client: TestClient, es_client: Elasticsearch
35-
) -> Generator[None, Any, None]:
35+
) -> Generator[None, Any]:
3636
"""准备测试环境(索引+数据)"""
3737

3838
# 1. 清理已存在的索引

tests/web/document/vector_hybrid_search_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def setup_environment(
4242
self,
4343
client: TestClient,
4444
es_client: Elasticsearch,
45-
) -> Generator[None, Any, None]:
45+
) -> Generator[None, Any]:
4646
"""准备测试环境(索引+数据)"""
4747

4848
# 1. 清理已存在的索引

0 commit comments

Comments
 (0)