mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
30 KiB
30 KiB
In [ ]:
from semantica.core import Config
config = Config()
print(config.to_yaml())In [ ]:
!pip install -U semantica
In [ ]:
import os
from pathlib import Path
os.environ["SEMANTICA_API_KEY"] = "your_openai_key"
os.environ["SEMANTICA_EMBEDDING_PROVIDER"] = "openai"
os.environ["SEMANTICA_MODEL_NAME"] = "gpt-4"
config_text = """api_keys:
openai: your_key_here
anthropic: your_key_here
embedding:
provider: openai
model: text-embedding-3-large
dimensions: 3072
knowledge_graph:
backend: networkx
temporal: true
"""
Path("config.yaml").write_text(config_text, encoding="utf-8")
Path("config.yaml").read_text(encoding="utf-8")In [ ]:
from pathlib import Path
docs_dir = Path("welcome_docs")
docs_dir.mkdir(exist_ok=True)
text_path = docs_dir / "apple.txt"
text_content = (
"Apple Inc. was founded by Steve Jobs, Steve Wozniak and Ronald Wayne in"
" Cupertino, California."
)
text_path.write_text(text_content, encoding="utf-8")
print(f"Created sample document at {text_path}")In [ ]:
from semantica.ingest import FileIngestor
from semantica.parse import DocumentParser
from semantica.normalize import TextNormalizer
from semantica.semantic_extract import NERExtractor, RelationExtractor
from semantica.kg import GraphBuilder, GraphAnalyzer
from semantica.embeddings import EmbeddingGenerator
from semantica.vector_store import VectorStore, HybridSearch
ingestor = FileIngestor()
documents = ingestor.ingest(str(docs_dir))
parser = DocumentParser()
parsed_docs = parser.parse(documents)
normalizer = TextNormalizer()
normalized_docs = normalizer.normalize(parsed_docs)
ner = NERExtractor()
entities = ner.extract(normalized_docs)
rel_extractor = RelationExtractor()
relationships = rel_extractor.extract(normalized_docs, entities)
builder = GraphBuilder()
kg = builder.build(entities, relationships)
analyzer = GraphAnalyzer()
metrics = analyzer.analyze(kg)
emb_generator = EmbeddingGenerator()
embeddings = emb_generator.generate_embeddings(documents, data_type="text")
vec_store = VectorStore()
vec_store.store(embeddings, documents, metadata={})
hybrid = HybridSearch(vec_store)
search_results = hybrid.search("Apple founders", top_k=3)
len(search_results)In [ ]:
from semantica.visualization import KGVisualizer
# Create a visualizer instance
viz = KGVisualizer(layout="force", color_scheme="vibrant")
# Generate an interactive network visualization
# This returns a Plotly figure object that renders in the notebook
fig = viz.visualize_network(kg, output="interactive")
fig.show()In [ ]:
from semantica.ontology import OntologyGenerator
generator = OntologyGenerator(base_uri="https://example.org/ontology/")
# Generate ontology from the extracted data
ontology = generator.generate_ontology({
"entities": entities,
"relationships": relationships
})
# View inferred classes
[cls["name"] for cls in ontology.get("classes", [])[:5]]In [ ]:
from semantica.split import TextSplitter
splitter = TextSplitter(chunk_size=100, chunk_overlap=20)
chunks = splitter.split_documents(documents)
print(f"Original documents: {len(documents)}")
print(f"Generated chunks: {len(chunks)}")In [ ]:
from semantica.reasoning import Reasoner
# Simple rule: If X founded Y, then X works_for Y
rule = """
IF (?x founded ?y) THEN (?x works_for ?y)
"""
reasoner = Reasoner()
reasoner.add_rule(rule)
inferred_facts = reasoner.infer_facts(kg)
print(f"Inferred {len(inferred_facts)} new facts")In [ ]:
from semantica.export import GraphExporter
exporter = GraphExporter()
exporter.export(kg, format="json", output_path="knowledge_graph.json")
print("Graph exported to knowledge_graph.json")In [ ]:
from semantica.core import Semantica, ConfigManager
config_manager = ConfigManager()
config = config_manager.load_from_file("config.yaml")
framework = Semantica(config=config)
framework.initialize()
kb_result = framework.build_knowledge_base(
sources=[str(docs_dir)],
embeddings=True,
graph=True,
)
framework.shutdown()
sorted(kb_result.keys())