Skip to content
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

feat(llm):improve some RAG function UT(tests) #192

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions .github/workflows/hugegraph-llm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: HugeGraph-LLM CI

on:
push:
branches:
- 'release-*'
pull_request:

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]

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

- 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

- 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') }}
restore-keys: |
${{ runner.os }}-venv-${{ matrix.python-version }}-
${{ runner.os }}-venv-

- name: Install dependencies
if: steps.cache-deps.outputs.cache-hit != 'true'
run: |
uv venv
source .venv/bin/activate
uv pip install pytest pytest-cov
uv pip install -r ./hugegraph-llm/requirements.txt

- name: Run unit tests
run: |
source .venv/bin/activate
export PYTHONPATH=$(pwd)/hugegraph-llm/src
export SKIP_EXTERNAL_SERVICES=true
cd hugegraph-llm
python -m pytest src/tests/operators/hugegraph_op/ src/tests/config/ src/tests/document/ src/tests/middleware/ -v

- name: Run integration tests
run: |
source .venv/bin/activate
export PYTHONPATH=$(pwd)/hugegraph-llm/src
export SKIP_EXTERNAL_SERVICES=true
cd hugegraph-llm
python -m pytest src/tests/integration/test_graph_rag_pipeline.py -v
Copy link
Member

@imbajin imbajin Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note each file should have a EOF line (U could config it in your IDE's settings)

image

So as others files

Copy link
Member

@imbajin imbajin Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/apache/incubator-hugegraph-ai/actions/runs/13693587346/job/38291894859?pr=192

And could check the CI status here (U could submit a PR in your own repo, select the upstream branch like
yanchaomei:main to test it separately)

image

Also better not use main/master as your default branch, keep it clean & it could sync the code with upstream
easily(one-click), if u want to modify some code u could checkout a new branch from main like dev-xx (This can avoid many potential conflicts and inconsistencies in the future, and also maintain clarity in using Git)

47 changes: 47 additions & 0 deletions hugegraph-llm/src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.

import os
import sys
import pytest
import nltk

# 获取项目根目录
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
# 添加到 Python 路径
sys.path.insert(0, project_root)

# 添加 src 目录到 Python 路径
src_path = os.path.join(project_root, "src")
sys.path.insert(0, src_path)

# 下载 NLTK 资源
def download_nltk_resources():
try:
nltk.data.find("corpora/stopwords")
except LookupError:
print("下载 NLTK stopwords 资源...")
nltk.download('stopwords', quiet=True)

# 在测试开始前下载 NLTK 资源
download_nltk_resources()

# 设置环境变量,跳过外部服务测试
os.environ['SKIP_EXTERNAL_SERVICES'] = 'true'

# 打印当前 Python 路径,用于调试
print("Python path:", sys.path)
6 changes: 6 additions & 0 deletions hugegraph-llm/src/tests/data/documents/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Alice is 25 years old and works as a software engineer at TechCorp.
Bob is 30 years old and is a data scientist at DataInc.
Alice and Bob are colleagues and they collaborate on AI projects.
They are working on a knowledge graph project that uses natural language processing.
The project aims to extract structured information from unstructured text.
TechCorp and DataInc are partner companies in the technology sector.
42 changes: 42 additions & 0 deletions hugegraph-llm/src/tests/data/kg/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"vertices": [
{
"vertex_label": "person",
"properties": ["name", "age", "occupation"]
},
{
"vertex_label": "company",
"properties": ["name", "industry"]
},
{
"vertex_label": "project",
"properties": ["name", "technology"]
}
],
"edges": [
{
"edge_label": "works_at",
"source_vertex_label": "person",
"target_vertex_label": "company",
"properties": []
},
{
"edge_label": "colleague",
"source_vertex_label": "person",
"target_vertex_label": "person",
"properties": []
},
{
"edge_label": "works_on",
"source_vertex_label": "person",
"target_vertex_label": "project",
"properties": []
},
{
"edge_label": "partner",
"source_vertex_label": "company",
"target_vertex_label": "company",
"properties": []
}
]
}
53 changes: 53 additions & 0 deletions hugegraph-llm/src/tests/data/prompts/test_prompts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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.

rag_prompt:
system: |
You are a helpful assistant that answers questions based on the provided context.
Use only the information from the context to answer the question.
If you don't know the answer, say "I don't know" or "I don't have enough information".
user: |
Context:
{context}

Question:
{query}

Answer:

kg_extraction_prompt:
system: |
You are a knowledge graph extraction assistant. Your task is to extract entities and relationships from the given text according to the provided schema.
Output the extracted information in a structured format that can be used to build a knowledge graph.
user: |
Text:
{text}

Schema:
{schema}

Extract entities and relationships from the text according to the schema:

summarization_prompt:
system: |
You are a summarization assistant. Your task is to create a concise summary of the provided text.
The summary should capture the main points and key information.
user: |
Text:
{text}

Please provide a concise summary:
54 changes: 54 additions & 0 deletions hugegraph-llm/src/tests/document/test_document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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.

import unittest
import importlib


class TestDocumentModule(unittest.TestCase):
def test_import_document_module(self):
"""Test that the document module can be imported."""
try:
import hugegraph_llm.document
self.assertTrue(True)
except ImportError:
self.fail("Failed to import hugegraph_llm.document module")

def test_import_chunk_split(self):
"""Test that the chunk_split module can be imported."""
try:
from hugegraph_llm.document import chunk_split
self.assertTrue(True)
except ImportError:
self.fail("Failed to import chunk_split module")

def test_chunk_splitter_class_exists(self):
"""Test that the ChunkSplitter class exists in the chunk_split module."""
try:
from hugegraph_llm.document.chunk_split import ChunkSplitter
self.assertTrue(True)
except ImportError:
self.fail("ChunkSplitter class not found in chunk_split module")

def test_module_reload(self):
"""Test that the document module can be reloaded."""
try:
import hugegraph_llm.document
importlib.reload(hugegraph_llm.document)
self.assertTrue(True)
except Exception as e:
self.fail(f"Failed to reload document module: {e}")
Loading
Loading