A powerful knowledge graph generation library using LangChain and LLM models
Project description
Knowledge Graph Generator with Langchain
A powerful Python library for generating knowledge graphs from unstructured text using LangChain and Large Language Models (LLMs). Extract entities, relationships, and create structured knowledge representations with support for clustering, chunking, and parallel processing.
Features
- ** LLM-Powered Extraction**: Leverage any LangChain-compatible language model for intelligent entity and relation extraction
- ** Knowledge Graph Generation**: Create structured graphs with entities, relations, and edges from raw text or conversations
- ** Semantic Clustering**: Automatically cluster similar entities and relations using LLM-based semantic understanding
- ** Parallel Processing**: Handle large texts efficiently with concurrent chunk processing
- ** Conversation Support**: Extract knowledge graphs from conversational data (chat logs, dialogues)
- ** Flexible Input**: Process both plain text and structured conversation formats
- ** Customizable**: Fine-tune extraction with context, chunk sizes, and clustering parameters
- ** Export Support**: Save generated graphs to JSON format for further analysis
Installation
pip install langchain-kggen
Quick Start
Basic Usage
from langchain_kggen import KGGen
from langchain_openai import ChatOpenAI # or any LangChain-compatible LLM
from langchain_ollama import ChatOllama
# Initialize your LLM
llm = ChatOpenAI(model="gpt-4o")
llm = Chatollama(model='deepseek-r1:32b') # if working with local models
# Your input text
text = """
Apple Inc. is a technology company founded by Steve Jobs in 1976.
The company is headquartered in Cupertino, California.
Tim Cook is the current CEO of Apple.
"""
# Create KGGen instance
kg_generator = KGGen(llm=llm, input_data=text)
# Generate knowledge graph
graph = kg_generator.generate()
print("Entities:", graph.entities)
print("Relations:", graph.relations)
print("Edges:", graph.edges)
Processing Conversations
# Conversation format
conversation = [
{"role": "user", "content": "Tell me about artificial intelligence"},
{"role": "assistant", "content": "AI is a field of computer science focused on creating intelligent machines"},
{"role": "user", "content": "What are the main types of AI?"},
{"role": "assistant", "content": "The main types include narrow AI, general AI, and superintelligence"}
]
# Generate knowledge graph from conversation
kg_generator = KGGen(llm=llm, input_data=conversation)
graph = kg_generator.generate()
Advanced Features
# Generate with clustering and chunking
graph = kg_generator.generate(
context="Technology and business domain", # Additional context
chunk_size=1000, # Split large texts
cluster=True, # Enable semantic clustering
max_workers=5, # Parallel processing
llm_delay=1.0, # Rate limiting
output_folder="./output", # Save to file
file_name="my_knowledge_graph.json" # Custom filename
)
# Access clustering information
if graph.entity_clusters:
print("Entity clusters:", graph.entity_clusters)
if graph.edge_clusters:
print("Edge clusters:", graph.edge_clusters)
Standalone Clustering
# Cluster an existing graph
clustered_graph = kg_generator.cluster(
graph=graph,
context="Business and technology context"
)
Aggregating Multiple Graphs
# Combine multiple knowledge graphs
graph1 = kg_generator.generate()
graph2 = kg_generator.generate()
aggregated_graph = kg_generator.aggregate([graph1, graph2])
<Architecture
The library consists of several key components:
Core Classes
KGGen: Main interface for knowledge graph generationGraph: Pydantic model representing a knowledge graph structure
Extraction Modules
get_entities: Extract entities (subjects/objects) from textget_relations: Extract subject-predicate-object relationsget_clusters: Perform semantic clustering of entities and relations
Utilities
chunk_text: Intelligent text chunking with sentence boundary respectstate: Graph data models and structures
=� Graph Structure
The generated Graph object contains:
class Graph(BaseModel):
entities: set[str] # All unique entities
edges: set[str] # All unique predicates/relations
relations: set[Tuple[str, str, str]] # (subject, predicate, object) triples
entity_clusters: Optional[dict[str, set[str]]] # Entity clustering mappings
edge_clusters: Optional[dict[str, set[str]]] # Edge clustering mappings
Configuration Options
Generation Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
llm |
BaseChatModel |
None | Override default LLM |
context |
str |
"" |
Additional context for extraction |
chunk_size |
int |
None | Split text into chunks |
cluster |
bool |
False |
Enable semantic clustering |
max_workers |
int |
10 |
Parallel processing threads |
llm_delay |
float |
2.0 |
Delay between LLM calls |
output_folder |
str |
None | Save location |
file_name |
str |
'knowledge_graph.json' |
Output filename |
Use Cases
- ** Document Analysis**: Extract key concepts and relationships from research papers, reports
- ** Conversation Mining**: Analyze chat logs, interviews, customer support tickets
- ** News Processing**: Build knowledge bases from news articles and press releases
- ** Information Extraction**: Transform unstructured data into structured knowledge
- ** AI Training Data**: Create structured datasets for machine learning applications
- ** Business Intelligence**: Extract insights from business documents and communications
Best Practices
- Context Matters: Provide relevant context to improve extraction accuracy
- Chunk Large Texts: Use chunking for texts > 2000 tokens to avoid LLM limits
- Enable Clustering: Use clustering to merge similar entities and reduce noise
- Rate Limiting: Adjust
llm_delaybased on your LLM provider's rate limits - Iterative Refinement: Review outputs and adjust context/parameters as needed
Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
Acknowledgments
- Built on top of the excellent LangChain framework
- Inspired by advances in Large Language Model capabilities for information extraction
- Thanks to the open-source community for feedback and contributions
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_kggen-0.1.1.tar.gz.
File metadata
- Download URL: langchain_kggen-0.1.1.tar.gz
- Upload date:
- Size: 120.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b1e8dcea46bdb345aa3c1a91fcbde8aae256a6dc6719529193bc0a7da54f49c
|
|
| MD5 |
22db0315ac96e8582ad2e4ea4dca627b
|
|
| BLAKE2b-256 |
effe9ee753de5ce37048e3d60658c6ffbc552cf31c617229c76234657d6c956e
|
File details
Details for the file langchain_kggen-0.1.1-py3-none-any.whl.
File metadata
- Download URL: langchain_kggen-0.1.1-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6a1e92476ebf9b39780e82b190df89e6bf88371b2000381971e675ec4708ea4
|
|
| MD5 |
c89bd90d6d1fa4581f18dba0d03e3236
|
|
| BLAKE2b-256 |
cf2065b711e54cc441a2577efc1631837db94fb7d7eeaba1300bd7c04526ce06
|