-
Notifications
You must be signed in to change notification settings - Fork 0
feat(llm):improve some RAG function UT(tests) #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ba85fbc
aabac09
a012cb2
da5b6c0
ae1511c
fc67aa9
5db19ec
d1421a7
50d4852
cba0502
4919b4b
f756bec
2381c3b
8819689
0e28c89
a7e9b9b
bfffa16
2a0b616
be12bb3
20e360b
5fdf1b7
402b9ba
2a86265
d0ac13e
04b2f76
51bae93
fa67eff
843d8e8
9254a0a
6897b3e
9e40542
4b8f247
1a5a784
8f4358f
db02f9d
63f36f1
87744a2
fe8cecb
46f6ba5
93e95e5
09d09b5
5bc64c1
dbcad5f
2c3702b
232d8d0
c0c037c
d30ad5a
9117b1b
ff25472
073a46c
159bfd2
76e6192
f5f9318
6d6ceb6
533e179
b490f8b
10cff6a
119336d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,114 @@ | ||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||
| # Licensed to the Apache Software Foundation (ASF) under one | ||||||||||||||||||||||||||||||||||
| # or more contributor license agreements. See the NOTICE file | ||||||||||||||||||||||||||||||||||
| # distributed with this work for additional information | ||||||||||||||||||||||||||||||||||
| # regarding copyright ownership. The ASF licenses this file | ||||||||||||||||||||||||||||||||||
| # to you under the Apache License, Version 2.0 (the | ||||||||||||||||||||||||||||||||||
| # "License"); you may not use this file except in compliance | ||||||||||||||||||||||||||||||||||
| # with the License. You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||
| # Unless required by applicable law or agreed to in writing, | ||||||||||||||||||||||||||||||||||
| # software distributed under the License is distributed on an | ||||||||||||||||||||||||||||||||||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||||||||||||||||||||||||||||||||||
| # KIND, either express or implied. See the License for the | ||||||||||||||||||||||||||||||||||
| # specific language governing permissions and limitations | ||||||||||||||||||||||||||||||||||
| # under the License. | ||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| name: HugeGraph-LLM CI | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| on: | ||||||||||||||||||||||||||||||||||
| push: | ||||||||||||||||||||||||||||||||||
| branches: | ||||||||||||||||||||||||||||||||||
| - 'release-*' | ||||||||||||||||||||||||||||||||||
| pull_request: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| jobs: | ||||||||||||||||||||||||||||||||||
| build: | ||||||||||||||||||||||||||||||||||
| runs-on: ubuntu-latest | ||||||||||||||||||||||||||||||||||
| strategy: | ||||||||||||||||||||||||||||||||||
| fail-fast: false | ||||||||||||||||||||||||||||||||||
| matrix: | ||||||||||||||||||||||||||||||||||
| python-version: ["3.10", "3.11"] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| steps: | ||||||||||||||||||||||||||||||||||
| - name: Prepare HugeGraph Server Environment | ||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||
| docker run -d --name=graph -p 8080:8080 -e PASSWORD=admin hugegraph/hugegraph:1.3.0 | ||||||||||||||||||||||||||||||||||
| sleep 10 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 改用健康检查等待 HugeGraph 服务就绪,减少 10s 固定等待的不稳定性 固定 sleep 10 可能在慢机/冷镜像下仍未就绪,建议以 HTTP 探测重试方式等待服务可用。 run: |
docker run -d --name=graph -p 8080:8080 -e PASSWORD=admin hugegraph/hugegraph:1.3.0
- sleep 10
+ # 等待服务就绪(最多 ~60s)
+ for i in {1..30}; do
+ if curl -fsS http://localhost:8080/version >/dev/null 2>&1; then
+ echo "HugeGraph is ready"
+ break
+ fi
+ echo "Waiting HugeGraph to be ready... ($i)"
+ sleep 2
+ done备注:若官方提供健康检查端点不同,请替换为对应探测 URL。 📝 Committable suggestion
Suggested change
🧰 Tools🪛 YAMLlint (1.37.1)[warning] 37-37: wrong indentation: expected 6 but found 4 (indentation) |
||||||||||||||||||||||||||||||||||
| - uses: actions/checkout@v4 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Comment on lines
+36
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 【严重】steps 列表项缩进错误,工作流将无法被 GitHub Actions 解析 根据 yamllint 与 GHA 语法要求,steps: 下的每个列表项需要比 steps: 多 2 个空格(共 6 个空格)。当前使用了 4 个空格,解析会失败。请整体右移两个空格,贯穿整个 steps 段落直至文件末尾。 建议变更(节选,需对所有步骤统一右移两个空格): - steps:
- - name: Prepare HugeGraph Server Environment
+ steps:
+ - name: Prepare HugeGraph Server Environment
run: |
docker run -d --name=graph -p 8080:8080 -e PASSWORD=admin hugegraph/hugegraph:1.3.0
sleep 10
- - uses: actions/checkout@v4
+ - uses: actions/checkout@v4
- - name: Set up Python ${{ matrix.python-version }}
+ - name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- - name: Install uv
+ - name: Install uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
...
- - name: Run integration tests
+ - name: Run integration tests
run: |
source .venv/bin/activate
export SKIP_EXTERNAL_SERVICES=true
cd hugegraph-llm
export PYTHONPATH="$(pwd)/src:$PYTHONPATH"
python -m pytest src/tests/integration/test_graph_rag_pipeline.py src/tests/integration/test_kg_construction.py src/tests/integration/test_rag_pipeline.py -v --tb=shortAlso applies to: 44-48, 49-53, 54-66, 67-85, 86-91, 92-104, 105-111 🧰 Tools🪛 YAMLlint (1.37.1)[warning] 37-37: wrong indentation: expected 6 but found 4 (indentation) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| - name: Set up Python ${{ matrix.python-version }} | ||||||||||||||||||||||||||||||||||
| uses: actions/setup-python@v5 | ||||||||||||||||||||||||||||||||||
| with: | ||||||||||||||||||||||||||||||||||
| python-version: ${{ matrix.python-version }} | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - name: Install uv | ||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||
| curl -LsSf https://astral.sh/uv/install.sh | sh | ||||||||||||||||||||||||||||||||||
| echo "$HOME/.cargo/bin" >> $GITHUB_PATH | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Comment on lines
+49
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 替换 curl | sh 安装 uv 的方式,降低供应链风险 直接 curl 管道执行脚本存在供应链与完整性风险。建议改用官方 Action astral-sh/setup-uv 固定版本,更安全、可缓存。 - - name: Install uv
- run: |
- curl -LsSf https://astral.sh/uv/install.sh | sh
- echo "$HOME/.cargo/bin" >> $GITHUB_PATH
+ - name: Setup uv
+ uses: astral-sh/setup-uv@v4
+ with:
+ # 可选:固定版本,或留空使用最新稳定版
+ # version: "0.4.x"🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| - name: Cache dependencies | ||||||||||||||||||||||||||||||||||
| id: cache-deps | ||||||||||||||||||||||||||||||||||
| uses: actions/cache@v4 | ||||||||||||||||||||||||||||||||||
| with: | ||||||||||||||||||||||||||||||||||
| path: | | ||||||||||||||||||||||||||||||||||
| .venv | ||||||||||||||||||||||||||||||||||
| ~/.cache/uv | ||||||||||||||||||||||||||||||||||
| ~/.cache/pip | ||||||||||||||||||||||||||||||||||
| key: ${{ runner.os }}-venv-${{ matrix.python-version }}-${{ hashFiles('hugegraph-llm/requirements.txt', 'hugegraph-llm/pyproject.toml') }} | ||||||||||||||||||||||||||||||||||
| restore-keys: | | ||||||||||||||||||||||||||||||||||
| ${{ runner.os }}-venv-${{ matrix.python-version }}- | ||||||||||||||||||||||||||||||||||
| ${{ runner.os }}-venv- | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
Comment on lines
+62
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chain缓存 Key 未包含 hugegraph-python-client 依赖文件,可能导致缓存未失效 当前仅哈希 hugegraph-llm 的 requirements/pyproject。由于后续还会安装 hugegraph-python-client(editable),其依赖变化不会触发缓存失效,可能导致环境“脏缓存”。 - key: ${{ runner.os }}-venv-${{ matrix.python-version }}-${{ hashFiles('hugegraph-llm/requirements.txt', 'hugegraph-llm/pyproject.toml') }}
+ key: ${{ runner.os }}-venv-${{ matrix.python-version }}-${{ hashFiles(
+ 'hugegraph-llm/requirements.txt',
+ 'hugegraph-llm/pyproject.toml',
+ 'hugegraph-python-client/requirements.txt',
+ 'hugegraph-python-client/pyproject.toml'
+ ) }}
restore-keys: |
${{ runner.os }}-venv-${{ matrix.python-version }}-
${{ runner.os }}-venv-如未使用其中某些文件,保留在 hashFiles 中也无害(不存在的文件会被忽略)。 请帮忙确认一下仓库中是否存在 🏁 Script executed: #!/bin/bash
set -e
echo "1. 查找目录 'hugegraph-python-client':"
fd -HI "hugegraph-python-client" || echo "未找到相关目录"
echo ""
echo "2. 列出该目录内容(如果存在):"
if [ -d "hugegraph-python-client" ]; then
ls -R hugegraph-python-client
else
echo "目录不存在,跳过列出内容"
fi
echo ""
echo "3. 在工作流文件中搜索 'hugegraph-python-client' 的使用:"
rg -n "hugegraph-python-client" .github/workflows/hugegraph-llm.yml || echo "工作流中未引用该关键字"Length of output: 1878 在缓存 Key 中加入 hugegraph-python-client 的依赖文件 已确认
- key: ${{ runner.os }}-venv-${{ matrix.python-version }}-${{ hashFiles('hugegraph-llm/requirements.txt', 'hugegraph-llm/pyproject.toml') }}
+ key: ${{ runner.os }}-venv-${{ matrix.python-version }}-${{ hashFiles(
+ 'hugegraph-llm/requirements.txt',
+ 'hugegraph-llm/pyproject.toml',
+ 'hugegraph-python-client/pyproject.toml'
+ ) }}(如需,也可以额外保留对 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| - name: Install dependencies | ||||||||||||||||||||||||||||||||||
| if: steps.cache-deps.outputs.cache-hit != 'true' | ||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||
| uv venv | ||||||||||||||||||||||||||||||||||
| source .venv/bin/activate | ||||||||||||||||||||||||||||||||||
| uv pip install pytest pytest-cov | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 清理尾随空格,避免 yamllint 错误 这些行仅包含空格字符,yamllint 报错为 trailing-spaces。建议去除尾随空格或删除空白行。 -
+
@@
-
+
@@
-
+Also applies to: 85-85, 98-98 🧰 Tools🪛 YAMLlint (1.37.1)[error] 73-73: trailing spaces (trailing-spaces) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| if [ -f "hugegraph-llm/pyproject.toml" ]; then | ||||||||||||||||||||||||||||||||||
| cd hugegraph-llm | ||||||||||||||||||||||||||||||||||
| uv pip install -e . | ||||||||||||||||||||||||||||||||||
| uv pip install 'qianfan~=0.3.18' 'retry~=0.9.2' | ||||||||||||||||||||||||||||||||||
| cd .. | ||||||||||||||||||||||||||||||||||
| elif [ -f "hugegraph-llm/requirements.txt" ]; then | ||||||||||||||||||||||||||||||||||
| uv pip install -r hugegraph-llm/requirements.txt | ||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||
| echo "No dependency files found!" | ||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Download NLTK data | ||||||||||||||||||||||||||||||||||
| python -c "import nltk; nltk.download('stopwords'); nltk.download('punkt')" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - name: Install packages | ||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||
| source .venv/bin/activate | ||||||||||||||||||||||||||||||||||
| uv pip install -e ./hugegraph-python-client/ | ||||||||||||||||||||||||||||||||||
| uv pip install -e ./hugegraph-llm/ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - name: Run unit tests | ||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||
| source .venv/bin/activate | ||||||||||||||||||||||||||||||||||
| export SKIP_EXTERNAL_SERVICES=true | ||||||||||||||||||||||||||||||||||
| cd hugegraph-llm | ||||||||||||||||||||||||||||||||||
| export PYTHONPATH="$(pwd)/src:$PYTHONPATH" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if python -c "from hugegraph_llm.models.llms.qianfan import QianfanClient" 2>/dev/null; then | ||||||||||||||||||||||||||||||||||
| python -m pytest src/tests/config/ src/tests/document/ src/tests/middleware/ src/tests/operators/ src/tests/models/ src/tests/indices/ src/tests/test_utils.py -v --tb=short | ||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||
| python -m pytest src/tests/config/ src/tests/document/ src/tests/middleware/ src/tests/operators/ src/tests/models/ src/tests/indices/ src/tests/test_utils.py -v --tb=short --ignore=src/tests/models/llms/test_qianfan_client.py | ||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| - name: Run integration tests | ||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||
| source .venv/bin/activate | ||||||||||||||||||||||||||||||||||
| export SKIP_EXTERNAL_SERVICES=true | ||||||||||||||||||||||||||||||||||
| cd hugegraph-llm | ||||||||||||||||||||||||||||||||||
| export PYTHONPATH="$(pwd)/src:$PYTHONPATH" | ||||||||||||||||||||||||||||||||||
| python -m pytest src/tests/integration/test_graph_rag_pipeline.py src/tests/integration/test_kg_construction.py src/tests/integration/test_rag_pipeline.py -v --tb=short | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # CI 测试修复总结 | ||
|
|
||
| ## 问题分析 | ||
|
|
||
| 从最新的 CI 测试结果看,仍然有 10 个测试失败: | ||
|
|
||
| ### 主要问题类别 | ||
|
|
||
| 1. **BuildGremlinExampleIndex 相关问题 (3个失败)** | ||
| - 路径构造问题:CI 环境可能没有应用最新的代码更改 | ||
| - 空列表处理问题:IndexError 仍然发生 | ||
|
|
||
| 2. **BuildSemanticIndex 相关问题 (4个失败)** | ||
| - 缺少 `_get_embeddings_parallel` 方法 | ||
| - Mock 路径构造问题 | ||
|
|
||
| 3. **BuildVectorIndex 相关问题 (2个失败)** | ||
| - 类似的路径和方法调用问题 | ||
|
|
||
| 4. **OpenAIEmbedding 问题 (1个失败)** | ||
| - 缺少 `embedding_model_name` 属性 | ||
|
|
||
| ## 建议的解决方案 | ||
|
|
||
| ### 方案 1: 简化 CI 配置,跳过有问题的测试 | ||
|
|
||
| 在 CI 中暂时跳过这些有问题的测试,直到代码同步问题解决: | ||
|
|
||
| ```yaml | ||
| - name: Run unit tests | ||
| run: | | ||
| source .venv/bin/activate | ||
| export SKIP_EXTERNAL_SERVICES=true | ||
| cd hugegraph-llm | ||
| export PYTHONPATH="$(pwd)/src:$PYTHONPATH" | ||
|
|
||
| # 跳过有问题的测试 | ||
| python -m pytest src/tests/ -v --tb=short \ | ||
| --ignore=src/tests/integration/ \ | ||
| -k "not (TestBuildGremlinExampleIndex or TestBuildSemanticIndex or TestBuildVectorIndex or (TestOpenAIEmbedding and test_init))" | ||
| ``` | ||
|
|
||
| ### 方案 2: 更新 CI 配置,确保使用最新代码 | ||
|
|
||
| ```yaml | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 # 获取完整历史 | ||
|
|
||
| - name: Sync latest changes | ||
| run: | | ||
| git pull origin main # 确保获取最新更改 | ||
| ``` | ||
|
|
||
| ### 方案 3: 创建环境特定的测试配置 | ||
|
|
||
| 为 CI 环境创建特殊的测试配置,处理环境差异。 | ||
|
|
||
| ## 当前状态 | ||
|
|
||
| - ✅ 本地测试:BuildGremlinExampleIndex 测试通过 | ||
| - ❌ CI 测试:仍然失败,可能是代码同步问题 | ||
| - ✅ 大部分测试:208/223 通过 (93.3%) | ||
|
|
||
| ## 建议采取的行动 | ||
|
|
||
| 1. **短期解决方案**:更新 CI 配置,跳过有问题的测试 | ||
| 2. **中期解决方案**:确保 CI 环境代码同步 | ||
| 3. **长期解决方案**:改进测试的环境兼容性 |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,3 +14,61 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # KIND, either express or implied. See the License for the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # specific language governing permissions and limitations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Document module providing Document and Metadata classes for document handling. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This module implements classes for representing documents and their associated metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| in the HugeGraph LLM system. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from typing import Dict, Any, Optional, Union | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Metadata: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """A class representing metadata for a document. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This class stores metadata information like source, author, page, etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, **kwargs): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Initialize metadata with arbitrary key-value pairs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| **kwargs: Arbitrary keyword arguments to be stored as metadata. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for key, value in kwargs.items(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setattr(self, key, value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def as_dict(self) -> Dict[str, Any]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert metadata to a dictionary. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dict[str, Any]: A dictionary representation of metadata. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return dict(self.__dict__) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class Document: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """A class representing a document with content and metadata. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This class stores document content along with its associated metadata. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, content: str, metadata: Optional[Union[Dict[str, Any], Metadata]] = None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Document class should validate that content is not None and handle edge cases. Also consider adding type hints for better IDE support and runtime validation.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Initialize a document with content and metadata. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content: The text content of the document. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| metadata: Metadata associated with the document. Can be a dictionary or Metadata object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Raises: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ValueError: If content is None or empty string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not content: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("Document content cannot be None or empty") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.content = content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if metadata is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.metadata = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif isinstance(metadata, Metadata): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.metadata = metadata.as_dict() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.metadata = metadata | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,18 @@ | |
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """ | ||
| LLM models package for HugeGraph-LLM. | ||
|
|
||
| This package contains various LLM client implementations including: | ||
| - OpenAI clients | ||
| - Qianfan clients | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove Qianfan now |
||
| - Ollama clients | ||
| - LiteLLM clients | ||
| """ | ||
|
|
||
| # Import base class to make it available at package level | ||
| from .base import BaseLLM | ||
|
|
||
| __all__ = ["BaseLLM"] | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,7 +35,9 @@ def __init__( | |||||||||||||||||||
| ): | ||||||||||||||||||||
| self._llm = llm | ||||||||||||||||||||
| self._query = text | ||||||||||||||||||||
| self._language = llm_settings.language.lower() | ||||||||||||||||||||
| # 未传入值或者其他值,默认使用英文 | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 注释不够准确 注释说 "未传入值或者其他值",但实际上语言值是从 建议更新注释以准确反映逻辑: - # 未传入值或者其他值,默认使用英文
+ # 根据全局语言设置映射到支持的语言,非中文则默认使用英文🤖 Prompt for AI Agents |
||||||||||||||||||||
| lang_raw = llm_settings.language.lower() | ||||||||||||||||||||
| self._language = "chinese" if lang_raw == "cn" else "english" | ||||||||||||||||||||
|
Comment on lines
+39
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可能的 AttributeError 风险与语言映射逻辑过于简单 当前实现存在以下问题:
建议应用以下改进: - # 未传入值或者其他值,默认使用英文
- lang_raw = llm_settings.language.lower()
- self._language = "chinese" if lang_raw == "cn" else "english"
+ # 规范化语言设置,默认使用英文
+ lang_raw = (llm_settings.language or "en").lower()
+ # 支持常见的中文语言代码变体
+ if lang_raw in ("cn", "zh", "zh-cn", "zh-hans", "chinese"):
+ self._language = "chinese"
+ else:
+ self._language = "english"📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
|
|
||||||||||||||||||||
| def run(self, context: Dict[str, Any]) -> Dict[str, Any]: | ||||||||||||||||||||
| if self._query is None: | ||||||||||||||||||||
|
|
@@ -48,9 +50,6 @@ def run(self, context: Dict[str, Any]) -> Dict[str, Any]: | |||||||||||||||||||
| self._llm = LLMs().get_extract_llm() | ||||||||||||||||||||
| assert isinstance(self._llm, BaseLLM), "Invalid LLM Object." | ||||||||||||||||||||
|
|
||||||||||||||||||||
| # 未传入值或者其他值,默认使用英文 | ||||||||||||||||||||
| self._language = "chinese" if self._language == "cn" else "english" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| keywords = jieba.lcut(self._query) | ||||||||||||||||||||
| keywords = self._filter_keywords(keywords, lowercase=False) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.