diff --git a/README.md b/README.md index 02b1ed9..355f508 100644 --- a/README.md +++ b/README.md @@ -242,5 +242,8 @@ There are some examples to show how to use the tidb-vector-python to interact wi - [OpenAI Embedding](./examples/openai_embedding/README.md): use the OpenAI embedding model to generate vectors for text data, store them in TiDB Vector, and search for similar text. - [Image Search](./examples/image_search/README.md): use the OpenAI CLIP model to generate vectors for image and text, store them in TiDB Vector, and search for similar images. - [LlamaIndex RAG with UI](./examples/llamaindex-tidb-vector-with-ui/README.md): use the LlamaIndex to build an [RAG(Retrieval-Augmented Generation)](https://docs.llamaindex.ai/en/latest/getting_started/concepts/) application. +- [Chat with URL](./llamaindex-tidb-vector/README.md): use LlamaIndex to build an [RAG(Retrieval-Augmented Generation)](https://docs.llamaindex.ai/en/latest/getting_started/concepts/) application that can chat with a URL. +- [GraphRAG](./examples/graphrag-demo/README.md): 20 lines code of using TiDB Serverless to build a Knowledge Graph based RAG application. +- [GraphRAG Step by Step Tutorial](./examples/graphrag-step-by-step-tutorial/README.md): Step by step tutorial to build a Knowledge Graph based RAG application with Colab notebook. for more examples, see the [examples](./examples) directory. diff --git a/examples/README.md b/examples/README.md index 193b641..e77e38f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -29,6 +29,8 @@ Please make sure you have created a TiDB Serverless cluster with vector support - [Image Search](./image_search/README.md): use the OpenAI CLIP model to generate vectors for image and text. - [LlamaIndex RAG with UI](./llamaindex-tidb-vector-with-ui/README.md): use the LlamaIndex to build an [RAG(Retrieval-Augmented Generation)](https://docs.llamaindex.ai/en/latest/getting_started/concepts/) application. - [Chat with URL](./llamaindex-tidb-vector/README.md): use LlamaIndex to build an [RAG(Retrieval-Augmented Generation)](https://docs.llamaindex.ai/en/latest/getting_started/concepts/) application that can chat with a URL. +- [GraphRAG](./examples/graphrag-demo/README.md): 20 lines code of using TiDB Serverless to build a Knowledge Graph based RAG application. +- [GraphRAG Step by Step Tutorial](./examples/graphrag-step-by-step-tutorials/README.md): Step by step tutorial to build a Knowledge Graph based RAG application with Colab notebook. ## Real World Applications diff --git a/examples/graphrag-helloworld/README.md b/examples/graphrag-demo/README.md similarity index 92% rename from examples/graphrag-helloworld/README.md rename to examples/graphrag-demo/README.md index fa81d02..7fc3766 100644 --- a/examples/graphrag-helloworld/README.md +++ b/examples/graphrag-demo/README.md @@ -1,4 +1,4 @@ -# LlamaIndex RAG Example +# GraphRAG Demo This example demonstrates how to use the DSPy and TiDB Serverless to build a simple GraphRAG application. It crawled an example webpage and index the content to TiDB Serverless with Graph, then use the Graph and Vector to search the content and generate the answer with OpenAI. @@ -21,7 +21,7 @@ git clone https://github.com/pingcap/tidb-vector-python.git ### Create a virtual environment ```bash -cd tidb-vector-python/examples/graphrag-helloworld +cd tidb-vector-python/examples/graphrag-demo python3 -m venv .venv source .venv/bin/activate ``` @@ -48,7 +48,7 @@ Get the `OPENAI_API_KEY` from [OpenAI](https://platform.openai.com/docs/quicksta ```text -$ python3 simple-retrieve.py +$ python3 graphrag-demo.py Input your TIDB connection string: Input your OpenAI API Key: Enter your question: diff --git a/examples/graphrag-helloworld/init.sql b/examples/graphrag-demo/init.sql similarity index 100% rename from examples/graphrag-helloworld/init.sql rename to examples/graphrag-demo/init.sql diff --git a/examples/graphrag-helloworld/requirements.txt b/examples/graphrag-demo/requirements.txt similarity index 100% rename from examples/graphrag-helloworld/requirements.txt rename to examples/graphrag-demo/requirements.txt diff --git a/examples/graphrag-helloworld/simple-retrieve.py b/examples/graphrag-helloworld/simple-retrieve.py deleted file mode 100644 index 89b30a0..0000000 --- a/examples/graphrag-helloworld/simple-retrieve.py +++ /dev/null @@ -1,26 +0,0 @@ -from sqlalchemy import create_engine, text -import openai -import getpass - -# TiDB Connection String Pattern: -# mysql+pymysql://{TIDB_USER}:{TIDB_PASSWORD}@{TIDB_HOST}:{TIDB_PORT}/{TIDB_DB_NAME}?ssl_verify_cert=True&ssl_verify_identity=True - -db_engine = create_engine(getpass.getpass("Input your TIDB connection string:")) -oai_cli = openai.OpenAI(api_key=getpass.getpass("Input your OpenAI API Key:")) -question = input("Enter your question:") -embedding = str(oai_cli.embeddings.create(input=[question], model="text-embedding-3-small").data[0].embedding) - -with db_engine.connect() as conn: - result = conn.execute(text(""" - WITH initial_entity AS ( - SELECT id FROM `entities` - ORDER BY VEC_Cosine_Distance(description_vec, :embedding) LIMIT 1 - ), entities_ids AS ( - SELECT source_entity_id i FROM relationships r INNER JOIN initial_entity i ON r.target_entity_id = i.id - UNION SELECT target_entity_id i FROM relationships r INNER JOIN initial_entity i ON r.source_entity_id = i.id - UNION SELECT initial_entity.id i FROM initial_entity - ) SELECT description FROM `entities` WHERE id IN (SELECT i FROM entities_ids);"""), {"embedding": embedding}).fetchall() - - print(oai_cli.chat.completions.create(model="gpt-4o", messages=[ - {"role": "system", "content": f"Please carefully answer the question by {str(result)}"}, - {"role": "user", "content": question}]).choices[0].message.content)