add codes from guides to a separate folder (#2003)
<!-- .github/pull_request_template.md -->
## Description
<!--
Please provide a clear, human-generated description of the changes in
this PR.
DO NOT use AI-generated descriptions. We want to understand your thought
process and reasoning.
-->
## Acceptance Criteria
## Type of Change
<!-- Please check the relevant option -->
- [ ] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] Documentation update
- [ ] Code refactoring
- [ ] Performance improvement
- [ ] Other (please specify):
## Screenshots/Videos (if applicable)
## Pre-submission Checklist
<!-- Please check all boxes that apply before submitting your PR -->
- [ ] **I have tested my changes thoroughly before submitting this PR**
- [x] **This PR contains minimal changes necessary to address the
issue/feature**
- [ ] My code follows the project's coding standards and style
guidelines
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added necessary documentation (if applicable)
- [ ] All new and existing tests pass
- [ ] I have searched existing PRs to ensure this change hasn't been
submitted already
- [ ] I have linked any relevant issues in the description
- [ ] My commits have clear and descriptive messages
## DCO Affirmation
I affirm that all code in every commit of this pull request conforms to
the terms of the Topoteretes Developer Certificate of Origin.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Documentation**
* Added a dozen new example guides demonstrating: custom data models and
relationships; custom prompts and graph queries; LLM-driven extraction
and pipelines; graph visualization; low-level LLM usage; memify
quickstart; ontology quickstart; S3 storage integration; temporal
cognify workflows; basic and advanced search scenarios.
* Added a sample RDF/OWL ontology file for ontology examples.
<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
commit
ac4cb0d0d0
12 changed files with 677 additions and 0 deletions
39
examples/guides/custom_data_models.py
Normal file
39
examples/guides/custom_data_models.py
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
import asyncio
|
||||||
|
from typing import Any
|
||||||
|
from pydantic import SkipValidation
|
||||||
|
|
||||||
|
import cognee
|
||||||
|
from cognee.infrastructure.engine import DataPoint
|
||||||
|
from cognee.infrastructure.engine.models.Edge import Edge
|
||||||
|
from cognee.tasks.storage import add_data_points
|
||||||
|
|
||||||
|
|
||||||
|
class Person(DataPoint):
|
||||||
|
name: str
|
||||||
|
# Keep it simple for forward refs / mixed values
|
||||||
|
knows: SkipValidation[Any] = None # single Person or list[Person]
|
||||||
|
# Recommended: specify which fields to index for search
|
||||||
|
metadata: dict = {"index_fields": ["name"]}
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
# Start clean (optional in your app)
|
||||||
|
await cognee.prune.prune_data()
|
||||||
|
await cognee.prune.prune_system(metadata=True)
|
||||||
|
|
||||||
|
alice = Person(name="Alice")
|
||||||
|
bob = Person(name="Bob")
|
||||||
|
charlie = Person(name="Charlie")
|
||||||
|
|
||||||
|
# Create relationships - field name becomes edge label
|
||||||
|
alice.knows = bob
|
||||||
|
# You can also do lists: alice.knows = [bob, charlie]
|
||||||
|
|
||||||
|
# Optional: add weights and custom relationship types
|
||||||
|
bob.knows = (Edge(weight=0.9, relationship_type="friend_of"), charlie)
|
||||||
|
|
||||||
|
await add_data_points([alice, bob, charlie])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
30
examples/guides/custom_prompts.py
Normal file
30
examples/guides/custom_prompts.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import asyncio
|
||||||
|
import cognee
|
||||||
|
from cognee.api.v1.search import SearchType
|
||||||
|
|
||||||
|
custom_prompt = """
|
||||||
|
Extract only people and cities as entities.
|
||||||
|
Connect people to cities with the relationship "lives_in".
|
||||||
|
Ignore all other entities.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
await cognee.add(
|
||||||
|
[
|
||||||
|
"Alice moved to Paris in 2010, while Bob has always lived in New York.",
|
||||||
|
"Andreas was born in Venice, but later settled in Lisbon.",
|
||||||
|
"Diana and Tom were born and raised in Helsinki. Diana currently resides in Berlin, while Tom never moved.",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
await cognee.cognify(custom_prompt=custom_prompt)
|
||||||
|
|
||||||
|
res = await cognee.search(
|
||||||
|
query_type=SearchType.GRAPH_COMPLETION,
|
||||||
|
query_text="Where does Alice live?",
|
||||||
|
)
|
||||||
|
print(res)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
53
examples/guides/custom_tasks_and_pipelines.py
Normal file
53
examples/guides/custom_tasks_and_pipelines.py
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
import asyncio
|
||||||
|
from typing import Any, Dict, List
|
||||||
|
from pydantic import BaseModel, SkipValidation
|
||||||
|
|
||||||
|
import cognee
|
||||||
|
from cognee.modules.engine.operations.setup import setup
|
||||||
|
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
||||||
|
from cognee.infrastructure.engine import DataPoint
|
||||||
|
from cognee.tasks.storage import add_data_points
|
||||||
|
from cognee.modules.pipelines import Task, run_pipeline
|
||||||
|
|
||||||
|
|
||||||
|
class Person(DataPoint):
|
||||||
|
name: str
|
||||||
|
# Optional relationships (we'll let the LLM populate this)
|
||||||
|
knows: List["Person"] = []
|
||||||
|
# Make names searchable in the vector store
|
||||||
|
metadata: Dict[str, Any] = {"index_fields": ["name"]}
|
||||||
|
|
||||||
|
|
||||||
|
class People(BaseModel):
|
||||||
|
persons: List[Person]
|
||||||
|
|
||||||
|
|
||||||
|
async def extract_people(text: str) -> List[Person]:
|
||||||
|
system_prompt = (
|
||||||
|
"Extract people mentioned in the text. "
|
||||||
|
"Return as `persons: Person[]` with each Person having `name` and optional `knows` relations. "
|
||||||
|
"If the text says someone knows someone set `knows` accordingly. "
|
||||||
|
"Only include facts explicitly stated."
|
||||||
|
)
|
||||||
|
people = await LLMGateway.acreate_structured_output(text, system_prompt, People)
|
||||||
|
return people.persons
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
await cognee.prune.prune_data()
|
||||||
|
await cognee.prune.prune_system(metadata=True)
|
||||||
|
await setup()
|
||||||
|
|
||||||
|
text = "Alice knows Bob."
|
||||||
|
|
||||||
|
tasks = [
|
||||||
|
Task(extract_people), # input: text -> output: list[Person]
|
||||||
|
Task(add_data_points), # input: list[Person] -> output: list[Person]
|
||||||
|
]
|
||||||
|
|
||||||
|
async for _ in run_pipeline(tasks=tasks, data=text, datasets=["people_demo"]):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
14
examples/guides/graph_visualization.py
Normal file
14
examples/guides/graph_visualization.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import asyncio
|
||||||
|
import cognee
|
||||||
|
from cognee.api.v1.visualize.visualize import visualize_graph
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
await cognee.add(["Alice knows Bob.", "NLP is a subfield of CS."])
|
||||||
|
await cognee.cognify()
|
||||||
|
|
||||||
|
await visualize_graph("./graph_after_cognify.html")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
31
examples/guides/low_level_llm.py
Normal file
31
examples/guides/low_level_llm.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from typing import List
|
||||||
|
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
||||||
|
|
||||||
|
|
||||||
|
class MiniEntity(BaseModel):
|
||||||
|
name: str
|
||||||
|
type: str
|
||||||
|
|
||||||
|
|
||||||
|
class MiniGraph(BaseModel):
|
||||||
|
nodes: List[MiniEntity]
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
system_prompt = (
|
||||||
|
"Extract entities as nodes with name and type. "
|
||||||
|
"Use concise, literal values present in the text."
|
||||||
|
)
|
||||||
|
|
||||||
|
text = "Apple develops iPhone; Audi produces the R8."
|
||||||
|
|
||||||
|
result = await LLMGateway.acreate_structured_output(text, system_prompt, MiniGraph)
|
||||||
|
print(result)
|
||||||
|
# MiniGraph(nodes=[MiniEntity(name='Apple', type='Organization'), ...])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
30
examples/guides/memify_quickstart.py
Normal file
30
examples/guides/memify_quickstart.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import asyncio
|
||||||
|
import cognee
|
||||||
|
from cognee.modules.search.types import SearchType
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
# 1) Add two short chats and build a graph
|
||||||
|
await cognee.add(
|
||||||
|
[
|
||||||
|
"We follow PEP8. Add type hints and docstrings.",
|
||||||
|
"Releases should not be on Friday. Susan must review PRs.",
|
||||||
|
],
|
||||||
|
dataset_name="rules_demo",
|
||||||
|
)
|
||||||
|
await cognee.cognify(datasets=["rules_demo"]) # builds graph
|
||||||
|
|
||||||
|
# 2) Enrich the graph (uses default memify tasks)
|
||||||
|
await cognee.memify(dataset="rules_demo")
|
||||||
|
|
||||||
|
# 3) Query the new coding rules
|
||||||
|
rules = await cognee.search(
|
||||||
|
query_type=SearchType.CODING_RULES,
|
||||||
|
query_text="List coding rules",
|
||||||
|
node_name=["coding_agent_rules"],
|
||||||
|
)
|
||||||
|
print("Rules:", rules)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
290
examples/guides/ontology_input_example/basic_ontology.owl
Normal file
290
examples/guides/ontology_input_example/basic_ontology.owl
Normal file
|
|
@ -0,0 +1,290 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<rdf:RDF
|
||||||
|
xmlns:ns1="http://example.org/ontology#"
|
||||||
|
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||||
|
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
|
||||||
|
>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Volkswagen">
|
||||||
|
<rdfs:comment>Created for making cars accessible to everyone.</rdfs:comment>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#VW_Golf"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#VW_ID4"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#VW_Touareg"/>
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#CarManufacturer"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Azure">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#CloudServiceProvider"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Porsche">
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Porsche_Cayenne"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Porsche_Taycan"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Porsche_911"/>
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#CarManufacturer"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
<rdfs:comment>Famous for high-performance sports cars.</rdfs:comment>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Meta">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#Instagram"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#Facebook"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#Oculus"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#WhatsApp"/>
|
||||||
|
<rdfs:comment>Pioneering social media and virtual reality technology.</rdfs:comment>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#TechnologyCompany">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Apple">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
<rdfs:comment>Known for its innovative consumer electronics and software.</rdfs:comment>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#iPad"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#iPhone"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#AppleWatch"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#MacBook"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Audi">
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Audi_eTron"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Audi_R8"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Audi_A8"/>
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#CarManufacturer"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
<rdfs:comment>Known for its modern designs and technology.</rdfs:comment>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#AmazonEcho">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Porsche_Taycan">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#ElectricCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#BMW">
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#BMW_7Series"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#BMW_M4"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#BMW_iX"/>
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#CarManufacturer"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
<rdfs:comment>Focused on performance and driving pleasure.</rdfs:comment>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#VW_Touareg">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SUV"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#SportsCar">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#Car"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#ElectricCar">
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#Car"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Google">
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#GooglePixel"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#GoogleCloud"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#Android"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#GoogleSearch"/>
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
<rdfs:comment>Started as a search engine and expanded into cloud computing and AI.</rdfs:comment>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#AmazonPrime">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SoftwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Car">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#WindowsOS">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SoftwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Android">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SoftwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Oculus">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#GoogleCloud">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#CloudServiceProvider"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Microsoft">
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#Surface"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#WindowsOS"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#Azure"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#Xbox"/>
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
<rdfs:comment>Dominant in software, cloud computing, and gaming.</rdfs:comment>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#GoogleSearch">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SoftwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Mercedes_SClass">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#LuxuryCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Audi_A8">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#LuxuryCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Sedan">
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#Car"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#VW_Golf">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#Sedan"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Facebook">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SoftwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#WhatsApp">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SoftwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#produces">
|
||||||
|
<rdfs:domain rdf:resource="http://example.org/ontology#CarManufacturer"/>
|
||||||
|
<rdfs:range rdf:resource="http://example.org/ontology#Car"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#BMW_7Series">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#LuxuryCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#BMW_M4">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SportsCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Audi_eTron">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#ElectricCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Kindle">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#BMW_iX">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#ElectricCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#SoftwareCompany">
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Audi_R8">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SportsCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Xbox">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Technology">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Mercedes_EQS">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#ElectricCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Porsche_911">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SportsCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#HardwareCompany">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#MercedesBenz">
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Mercedes_SClass"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Mercedes_EQS"/>
|
||||||
|
<ns1:produces rdf:resource="http://example.org/ontology#Mercedes_AMG_GT"/>
|
||||||
|
<rdfs:comment>Synonymous with luxury and quality.</rdfs:comment>
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#CarManufacturer"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Amazon">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#Kindle"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#AmazonEcho"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#AWS"/>
|
||||||
|
<ns1:develops rdf:resource="http://example.org/ontology#AmazonPrime"/>
|
||||||
|
<rdfs:comment>From e-commerce to cloud computing giant with AWS.</rdfs:comment>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Instagram">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SoftwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#AWS">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#CloudServiceProvider"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#SUV">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#Car"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#VW_ID4">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#ElectricCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#CloudServiceProvider">
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Surface">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#iPad">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#iPhone">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Mercedes_AMG_GT">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SportsCar"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#MacBook">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#develops">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
|
||||||
|
<rdfs:range rdf:resource="http://example.org/ontology#Technology"/>
|
||||||
|
<rdfs:domain rdf:resource="http://example.org/ontology#TechnologyCompany"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#LuxuryCar">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#Car"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#AppleWatch">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Porsche_Cayenne">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#SUV"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#GooglePixel">
|
||||||
|
<rdf:type rdf:resource="http://example.org/ontology#HardwareCompany"/>
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#Company">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
</rdf:Description>
|
||||||
|
<rdf:Description rdf:about="http://example.org/ontology#CarManufacturer">
|
||||||
|
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
|
||||||
|
<rdfs:subClassOf rdf:resource="http://example.org/ontology#Company"/>
|
||||||
|
</rdf:Description>
|
||||||
|
</rdf:RDF>
|
||||||
29
examples/guides/ontology_quickstart.py
Normal file
29
examples/guides/ontology_quickstart.py
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
import asyncio
|
||||||
|
import cognee
|
||||||
|
import os
|
||||||
|
from cognee.modules.ontology.ontology_config import Config
|
||||||
|
from cognee.modules.ontology.rdf_xml.RDFLibOntologyResolver import RDFLibOntologyResolver
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
texts = ["Audi produces the R8 and e-tron.", "Apple develops iPhone and MacBook."]
|
||||||
|
|
||||||
|
await cognee.add(texts)
|
||||||
|
# or: await cognee.add("/path/to/folder/of/files")
|
||||||
|
|
||||||
|
ontology_path = os.path.join(
|
||||||
|
os.path.dirname(os.path.abspath(__file__)), "ontology_input_example/basic_ontology.owl"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create full config structure manually
|
||||||
|
config: Config = {
|
||||||
|
"ontology_config": {
|
||||||
|
"ontology_resolver": RDFLibOntologyResolver(ontology_file=ontology_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await cognee.cognify(config=config)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
25
examples/guides/s3_storage.py
Normal file
25
examples/guides/s3_storage.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import asyncio
|
||||||
|
import cognee
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
# Single file
|
||||||
|
await cognee.add("s3://cognee-s3-small-test/Natural_language_processing.txt")
|
||||||
|
|
||||||
|
# Folder/prefix (recursively expands)
|
||||||
|
await cognee.add("s3://cognee-s3-small-test")
|
||||||
|
|
||||||
|
# Mixed list
|
||||||
|
await cognee.add(
|
||||||
|
[
|
||||||
|
"s3://cognee-s3-small-test/Natural_language_processing.txt",
|
||||||
|
"Some inline text to ingest",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process the data
|
||||||
|
await cognee.cognify()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
52
examples/guides/search_basics_additional.py
Normal file
52
examples/guides/search_basics_additional.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
import asyncio
|
||||||
|
import cognee
|
||||||
|
|
||||||
|
from cognee.api.v1.search import SearchType
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
await cognee.prune.prune_data()
|
||||||
|
await cognee.prune.prune_system(metadata=True)
|
||||||
|
|
||||||
|
text = """
|
||||||
|
Natural language processing (NLP) is an interdisciplinary
|
||||||
|
subfield of computer science and information retrieval.
|
||||||
|
First rule of coding: Do not talk about coding.
|
||||||
|
"""
|
||||||
|
|
||||||
|
text2 = """
|
||||||
|
Sandwiches are best served toasted with cheese, ham, mayo,
|
||||||
|
lettuce, mustard, and salt & pepper.
|
||||||
|
"""
|
||||||
|
|
||||||
|
await cognee.add(text, dataset_name="NLP_coding")
|
||||||
|
await cognee.add(text2, dataset_name="Sandwiches")
|
||||||
|
await cognee.add(text2)
|
||||||
|
|
||||||
|
await cognee.cognify()
|
||||||
|
|
||||||
|
# Make sure you've already run cognee.cognify(...) so the graph has content
|
||||||
|
answers = await cognee.search(query_text="What are the main themes in my data?")
|
||||||
|
assert len(answers) > 0
|
||||||
|
|
||||||
|
answers = await cognee.search(
|
||||||
|
query_text="List coding guidelines",
|
||||||
|
query_type=SearchType.CODING_RULES,
|
||||||
|
)
|
||||||
|
assert len(answers) > 0
|
||||||
|
|
||||||
|
answers = await cognee.search(
|
||||||
|
query_text="Give me a confident answer: What is NLP?",
|
||||||
|
system_prompt="Answer succinctly and state confidence at the end.",
|
||||||
|
)
|
||||||
|
assert len(answers) > 0
|
||||||
|
|
||||||
|
answers = await cognee.search(
|
||||||
|
query_text="Tell me about NLP",
|
||||||
|
only_context=True,
|
||||||
|
)
|
||||||
|
assert len(answers) > 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
27
examples/guides/search_basics_core.py
Normal file
27
examples/guides/search_basics_core.py
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
import asyncio
|
||||||
|
import cognee
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
# Start clean (optional in your app)
|
||||||
|
await cognee.prune.prune_data()
|
||||||
|
await cognee.prune.prune_system(metadata=True)
|
||||||
|
# Prepare knowledge base
|
||||||
|
await cognee.add(
|
||||||
|
[
|
||||||
|
"Alice moved to Paris in 2010. She works as a software engineer.",
|
||||||
|
"Bob lives in New York. He is a data scientist.",
|
||||||
|
"Alice and Bob met at a conference in 2015.",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
await cognee.cognify()
|
||||||
|
|
||||||
|
# Make sure you've already run cognee.cognify(...) so the graph has content
|
||||||
|
answers = await cognee.search(query_text="What are the main themes in my data?")
|
||||||
|
for answer in answers:
|
||||||
|
print(answer)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
57
examples/guides/temporal_cognify.py
Normal file
57
examples/guides/temporal_cognify.py
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
import asyncio
|
||||||
|
import cognee
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
text = """
|
||||||
|
In 1998 the project launched. In 2001 version 1.0 shipped. In 2004 the team merged
|
||||||
|
with another group. In 2010 support for v1 ended.
|
||||||
|
"""
|
||||||
|
|
||||||
|
await cognee.add(text, dataset_name="timeline_demo")
|
||||||
|
|
||||||
|
await cognee.cognify(datasets=["timeline_demo"], temporal_cognify=True)
|
||||||
|
|
||||||
|
from cognee.api.v1.search import SearchType
|
||||||
|
|
||||||
|
# Before / after queries
|
||||||
|
result = await cognee.search(
|
||||||
|
query_type=SearchType.TEMPORAL, query_text="What happened before 2000?", top_k=10
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result != []
|
||||||
|
|
||||||
|
result = await cognee.search(
|
||||||
|
query_type=SearchType.TEMPORAL, query_text="What happened after 2010?", top_k=10
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result != []
|
||||||
|
|
||||||
|
# Between queries
|
||||||
|
result = await cognee.search(
|
||||||
|
query_type=SearchType.TEMPORAL, query_text="Events between 2001 and 2004", top_k=10
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result != []
|
||||||
|
|
||||||
|
# Scoped descriptions
|
||||||
|
result = await cognee.search(
|
||||||
|
query_type=SearchType.TEMPORAL,
|
||||||
|
query_text="Key project milestones between 1998 and 2010",
|
||||||
|
top_k=10,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result != []
|
||||||
|
|
||||||
|
result = await cognee.search(
|
||||||
|
query_type=SearchType.TEMPORAL,
|
||||||
|
query_text="What happened after 2004?",
|
||||||
|
datasets=["timeline_demo"],
|
||||||
|
top_k=10,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result != []
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
Loading…
Add table
Reference in a new issue