Skip to content

Commit 1481669

Browse files
authored
doc: update the python repo's readme (#67)
1 parent 6f74e51 commit 1481669

File tree

1 file changed

+159
-13
lines changed

1 file changed

+159
-13
lines changed

python/README.md

Lines changed: 159 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,152 @@
1-
# Lance Graph Python Package
1+
# Lance Graph
22

3-
This package exposes the Cypher graph query interface that wraps the
4-
`lance-graph` Rust crate. Development uses [uv](https://docs.astral.sh/uv/)
5-
to manage dependencies inside a project-local `.venv`.
3+
**A high-performance Cypher-capable graph query engine with Python bindings for building scalable, serverless knowledge graphs.**
64

7-
## Quick start
5+
Lance Graph combines a Rust-powered Cypher query engine with Python APIs for:
6+
- Fast graph queries using Cypher query language
7+
- AI-powered knowledge extraction from text (via LLM)
8+
- Lance-backed storage for efficient graph data management
9+
- Natural language Q&A over your knowledge graphs
10+
- FastAPI web service for graph queries
11+
12+
## Installation
13+
14+
```bash
15+
pip install lance-graph
16+
```
17+
18+
## Quick Start
19+
20+
### 1. Simple Cypher Query
21+
22+
```python
23+
import pyarrow as pa
24+
from lance_graph import CypherQuery, GraphConfig
25+
26+
# Create sample data
27+
people = pa.table({
28+
"person_id": [1, 2, 3, 4],
29+
"name": ["Alice", "Bob", "Carol", "David"],
30+
"age": [28, 34, 29, 42],
31+
})
32+
33+
# Configure graph schema
34+
config = (
35+
GraphConfig.builder()
36+
.with_node_label("Person", "person_id")
37+
.build()
38+
)
39+
40+
# Execute Cypher query
41+
query = CypherQuery("MATCH (p:Person) WHERE p.age > 30 RETURN p.name, p.age")
42+
result = query.with_config(config).execute({"Person": people})
43+
44+
print(result.to_pydict())
45+
# Output: {'name': ['Bob', 'David'], 'age': [34, 42]}
46+
```
47+
48+
### 2. Build a Knowledge Graph from Text
49+
50+
```python
51+
from pathlib import Path
52+
from knowledge_graph import (
53+
KnowledgeGraphConfig,
54+
LanceKnowledgeGraph,
55+
LanceGraphStore,
56+
get_extractor,
57+
)
58+
from knowledge_graph.cli.ingest import extract_and_add
59+
60+
# Initialize knowledge graph
61+
config = KnowledgeGraphConfig.from_root(Path("./my_graph"))
62+
config.ensure_directories()
63+
64+
# Create schema
65+
schema_path = config.resolved_schema_path()
66+
if not schema_path.exists():
67+
schema_content = """
68+
nodes:
69+
Entity:
70+
id_field: entity_id
71+
72+
relationships:
73+
RELATIONSHIP:
74+
source: source_entity_id
75+
target: target_entity_id
76+
"""
77+
schema_path.write_text(schema_content, encoding="utf-8")
78+
79+
store = LanceGraphStore(config)
80+
store.ensure_layout()
81+
82+
graph_config = config.load_graph_config()
83+
kg = LanceKnowledgeGraph(graph_config, storage=store)
84+
kg.ensure_initialized()
85+
86+
# Extract and add entities/relationships from text
87+
# Using heuristic extractor for testing without API key
88+
extractor = get_extractor("heuristic")
89+
# or using LLM extractor (requires API key)
90+
# extractor = get_extractor("llm", llm_model="gpt-4o-mini")
91+
text = """
92+
Albert Einstein developed the theory of relativity at Princeton.
93+
Marie Curie discovered radioactivity in Paris.
94+
"""
95+
96+
extract_and_add(text, kg, extractor, embedding_generator=None)
97+
98+
# Query the graph
99+
result = kg.query("""
100+
MATCH (e:Entity)
101+
RETURN e.name, e.entity_type
102+
LIMIT 10
103+
""")
104+
print(result.to_pylist())
105+
```
106+
107+
### 3. Natural Language Q&A
108+
109+
```python
110+
from knowledge_graph.llm.qa import ask_question
111+
112+
# Ask questions in natural language
113+
answer = ask_question(
114+
"Who discovered radioactivity?",
115+
kg,
116+
llm_model="gpt-4o-mini"
117+
)
118+
print(answer)
119+
# Output: Marie Curie discovered radioactivity.
120+
```
121+
122+
## Command-Line Interface
123+
124+
Lance Graph includes a CLI for building and querying knowledge graphs:
125+
126+
```bash
127+
# Initialize and extract
128+
knowledge_graph --root ./my_graph --init
129+
knowledge_graph --root ./my_graph --extract-and-add notes.txt
130+
131+
# Query with Cypher
132+
knowledge_graph --root ./my_graph "MATCH (e:Entity) RETURN e.name LIMIT 10"
133+
134+
# Natural language Q&A
135+
knowledge_graph --root ./my_graph --ask "Who discovered DNA?"
136+
```
137+
138+
For complete CLI documentation and examples, see the [main README](../README.md#cli-usage).
139+
140+
## Requirements
141+
142+
- Python 3.11+
143+
- Optional: OpenAI API key for LLM extraction
144+
145+
## Contributing
146+
147+
Lance Graph is open source! Contributions are welcome.
148+
149+
### Quick start
8150

9151
```bash
10152
cd python
@@ -16,7 +158,7 @@ maturin develop
16158
pytest python/tests/ -v
17159
```
18160

19-
## Development workflow
161+
### Development workflow
20162

21163
For linting and type checks:
22164

@@ -46,11 +188,15 @@ maturin develop
46188
- `python/python/knowledge_graph/` – CLI, FastAPI, and extractor utilities built on Lance
47189
- `python/python/tests/` – graph-centric functional tests
48190

49-
Refer to the repository root `README.md` for information about the Rust crate.
191+
For more information on development setup, building from source, running tests, and code quality guidelines, see [DEVELOPMENT.md](./DEVELOPMENT.md).
192+
193+
## License
194+
195+
Apache 2.0
196+
197+
## Links
50198

51-
> Run CLI commands through `uv run knowledge_graph ...`. The default uses an
52-
> LLM-backed extractor; install the LLM extra with `uv sync --extra llm` (or
53-
> `uv pip install -e '.[llm]'`) and configure `OPENAI_API_KEY`. Install
54-
> `uv sync --extra lance-storage` to enable Lance dataset persistence. Supply
55-
> extra options (e.g., `base_url`, HTTP headers) via `--llm-config`. Use
56-
> `--extractor heuristic` to avoid LLM calls during testing or offline work.
199+
- [GitHub](https://github.com/lancedb/lance-graph)
200+
- [Documentation](https://deepwiki.com/lancedb/lance-graph)
201+
- [PyPI](https://pypi.org/project/lance-graph/)
202+
- [LanceDB](https://lancedb.com/)

0 commit comments

Comments
 (0)