
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
A powerful tool for transforming documents into graph-based structures using Large Language Models (LLMs).
LLMGraphTransformer is a Python library designed to extract structured knowledge graphs from unstructured text using LLMs. It allows users to define schemas for nodes and relationships, ensuring that the extracted graph follows a strict format. 🔗📊
Install LLMGraphTransformer from PyPI:
pip install LLMGraphTransformer
from LLMGraphTransformer import LLMGraphTransformer
from LLMGraphTransformer.schema import NodeSchema, RelationshipSchema
from langchain_openai import ChatOpenAI
from langchain_core.documents import Document
from dotenv import load_dotenv
import os
load_dotenv(".env")
Node schemas define the types of entities that can be extracted from the text. Each node has:
📌 Example:
node_schemas = [
NodeSchema("Person", ["name", "birth_year", "death_year", "nationalitie", "profession"], "Represents an individual"),
NodeSchema("Organization", ["name", "founding_year", "industrie"], "Represents a group, company, or institution"),
NodeSchema("Location", ["name"], "Represents a geographical area such as a city, country, or region"),
NodeSchema("Award", ["name", "field"], "Represents an honor, prize, or recognition")
]
Relationship schemas define the allowed connections between entities. Each relationship has:
📌 Example:
relationship_schemas = [
RelationshipSchema("Person", "SPOUSE_OF", "Person"),
RelationshipSchema("Person", "MEMBER_OF", "Organization", ["start_year", "end_year", "year"]),
RelationshipSchema("Person", "AWARDED", "Award", ["year"]),
RelationshipSchema("Person", "LOCATED_IN", "Location"),
RelationshipSchema("Organization", "LOCATED_IN", "Location")
]
You can specify additional rules for extraction:
additional_instructions="""- all names must be extracted as uppercase"""
Provide the text from which the knowledge graph should be extracted:
text="""Marie Curie, born in 1867, was a Polish and naturalised-French physicist and chemist who conducted pioneering research on radioactivity.
She was the first woman to win a Nobel Prize, the first person to win a Nobel Prize twice, and the only person to win a Nobel Prize in two scientific fields.
Her husband, Pierre Curie, was a co-winner of her first Nobel Prize, making them the first-ever married couple to win the Nobel Prize and launching the Curie family legacy of five Nobel Prizes.
She was, in 1906, the first woman to become a professor at the University of Paris."""
Use OpenAI's API (or a compatible model) to process the text:
api_key = os.getenv("API_KEY")
base_url = os.getenv("BASE_URL")
model_name = os.getenv("MODEL_NAME")
llm = ChatOpenAI(
api_key=api_key,
base_url=base_url,
model=model_name,
temperature=0,
)
Create an instance of LLMGraphTransformer
:
llm_transformer = LLMGraphTransformer(
llm=llm,
allowed_nodes=node_schemas,
allowed_relationships=relationship_schemas,
additional_instructions=additional_instructions
)
Process the text into a structured knowledge graph:
document = Document(page_content=text)
graph_document = llm_transformer.convert_to_graph_document(document)
print(f"Nodes: {graph_document.nodes}")
print(f"Relationships: {graph_document.relationships}")
The extracted knowledge graph will be represented in JSON format with nodes
and relationships
:
{
"nodes": [
{
"id": "Marie Curie",
"type": "Person",
"properties": {
"name": "Marie Curie",
"birth_year": "1867",
"nationalitie": ["Polish", "French"],
"profession": ["physicist", "chemist"]
}
},
...
],
"relationships": [
{
"source": "Marie Curie",
"target": "Pierre Curie",
"type": "SPOUSE_OF"
},
...
]
}
This project is licensed under the MIT License.
Pull requests and feature suggestions are welcome! Open an issue for bug reports or improvements. 🚀
FAQs
A powerful tool for transforming documents into graph-based structures using Large Language Models (LLMs).
We found that LLMGraphTransformer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.