Skip to content

Commit

Permalink
docs: add graphrag to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sykp241095 committed May 27, 2024
1 parent e3bd91c commit dc3a2ea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ Please make sure you have created a TiDB Serverless cluster with vector support
- [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.
- [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.

## Real World Applications

### tidb.ai

[tidb.ai](https://tidb.ai) is an amazing out-of-the-box RAG(Retrieval Augmented Generation) template project based on the TiDB Vector store, it contains ui and server logic, fork it on [github](https://github.com/pingcap/tidb.ai) and build your own application.

![out-of-box-conversational-search](https://github.com/pingcap/tidb.ai/assets/1237528/0784e26e-8392-4bbe-bda1-6a680b12a805 "Image Title")
![out-of-box-conversational-search](https://github.com/pingcap/tidb.ai/assets/1237528/0784e26e-8392-4bbe-bda1-6a680b12a805 "Image Title")
26 changes: 26 additions & 0 deletions examples/graphrag-demo/graphrag-demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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)

0 comments on commit dc3a2ea

Please sign in to comment.