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
# Sample document for demonstration
long_document = """
# Artificial Intelligence and Machine Learning: A Comprehensive Overview
## Introduction
Artificial Intelligence ( AI ) represents the simulation of human intelligence in machines programmed to think and learn like humans . Machine Learning ( ML ) is a subset of AI focused on building systems that learn from data .
## Historical Development
The concept of AI dates back to ancient history with myths and stories about artificial beings . However , the formal field began in 1956 at the Dartmouth Conference where the term " Artificial Intelligence " was coined .
## Types of Artificial Intelligence
1. * * Narrow AI * * : Designed for specific tasks ( e . g . , voice assistants )
2. * * General AI * * : Hypothetical AI with human - level intelligence across various domains
3. * * Superintelligent AI * * : AI surpassing human intelligence , mostly theoretical
## Machine Learning Approaches
1. * * Supervised Learning * * : Models trained on labeled data
2. * * Unsupervised Learning * * : Finding patterns in unlabeled data
3. * * Reinforcement Learning * * : Learning through trial and error with rewards
"""
# Ingest the document
print ( " Ingesting document... " )
2025-04-20 16:34:29 -07:00
doc = db . ingest_text ( long_document , metadata = { " category " : " technology " , " topic " : " AI " } )
2025-03-20 22:54:18 -04:00
print ( f " Ingested document with ID: { doc . external_id } " )
# Create a cache
print ( " \n Creating cache... " )
cache_result = db . create_cache (
name = " ai_overview_cache " ,
model = " llama2 " ,
gguf_file = " llama-2-7b-chat.Q4_K_M.gguf " ,
2025-04-20 16:34:29 -07:00
docs = [ doc . external_id ] , # Include our document in the cache
2025-03-20 22:54:18 -04:00
)
print ( " Cache created successfully " )
# Get a reference to the cache
cache = db . get_cache ( " ai_overview_cache " )
# Update the cache to process documents
print ( " Updating cache... " )
cache . update ( )
print ( " Cache updated " )
# Query the cache directly
print ( " \n Querying the cache... " )
2025-04-20 16:34:29 -07:00
response = cache . query ( " What are the different types of artificial intelligence? " , max_tokens = 200 , temperature = 0.7 )
2025-03-20 22:54:18 -04:00
print ( " Response from cache: " )
print ( response . completion )
# Add more documents to the cache (if needed)
additional_doc = db . ingest_text (
" Reinforcement learning is a type of machine learning where an agent learns to make decisions by taking actions in an environment to maximize rewards. " ,
2025-04-20 16:34:29 -07:00
metadata = { " category " : " technology " , " topic " : " AI " , " subtopic " : " reinforcement learning " } ,
2025-03-20 22:54:18 -04:00
)
cache . add_docs ( [ additional_doc . external_id ] )
print ( " \n Added additional document to cache " )
# Update cache again to process the new document
cache . update ( )
2025-04-20 16:34:29 -07:00
print ( " Cache updated with new document " )