2025-03-20 22:54:18 -04:00
|
|
|
import os
|
2025-04-20 16:34:29 -07:00
|
|
|
|
2025-03-20 22:54:18 -04:00
|
|
|
from dotenv import load_dotenv
|
2025-04-09 18:46:00 -07:00
|
|
|
from morphik import Morphik
|
2025-03-20 22:54:18 -04:00
|
|
|
|
|
|
|
# Load environment variables
|
|
|
|
load_dotenv()
|
|
|
|
|
2025-04-09 18:46:00 -07:00
|
|
|
# Connect to Morphik
|
|
|
|
db = Morphik(os.getenv("MORPHIK_URI"), timeout=10000, is_local=True)
|
2025-03-20 22:54:18 -04:00
|
|
|
|
|
|
|
# Basic text ingestion
|
|
|
|
text_doc = db.ingest_text(
|
2025-04-09 18:46:00 -07:00
|
|
|
"Morphik is an open-source database designed for AI applications that simplifies working with unstructured data.",
|
2025-04-20 16:34:29 -07:00
|
|
|
metadata={"category": "tech", "author": "Morphik"},
|
2025-03-20 22:54:18 -04:00
|
|
|
)
|
|
|
|
print(f"Ingested text document with ID: {text_doc.external_id}")
|
|
|
|
|
|
|
|
# Basic file ingestion
|
|
|
|
file_doc = db.ingest_file(
|
2025-04-20 16:34:29 -07:00
|
|
|
"examples/assets/colpali_example.pdf", metadata={"category": "research", "topic": "technology"}
|
2025-03-20 22:54:18 -04:00
|
|
|
)
|
|
|
|
print(f"Ingested file with ID: {file_doc.external_id}")
|
|
|
|
|
|
|
|
# Basic retrieval
|
2025-04-20 16:34:29 -07:00
|
|
|
chunks = db.retrieve_chunks(query="What is Morphik?", k=3)
|
2025-03-20 22:54:18 -04:00
|
|
|
|
|
|
|
print("Retrieved chunks:")
|
|
|
|
for chunk in chunks:
|
|
|
|
print(f"Content: {chunk.content[:100]}...")
|
|
|
|
print(f"Score: {chunk.score}\n")
|
|
|
|
|
|
|
|
# Basic query with RAG
|
2025-04-09 18:46:00 -07:00
|
|
|
response = db.query("What is Morphik and what is it used for?")
|
2025-03-20 22:54:18 -04:00
|
|
|
print("Query response:")
|
2025-04-20 16:34:29 -07:00
|
|
|
print(response.completion)
|