|
| 1 | +""" |
| 2 | +Synapse Layer — LlamaIndex Integration Example |
| 3 | +
|
| 4 | +Demonstrates how to use Synapse Layer as: |
| 5 | +1. A retriever for RAG pipelines (SynapseRetriever) |
| 6 | +2. A persistent chat store (SynapseChatStore) |
| 7 | +
|
| 8 | +Requirements: |
| 9 | + pip install synapse-layer[llamaindex] |
| 10 | +
|
| 11 | +Author : Security & Architecture Team @ Synapse Layer |
| 12 | +License: Apache 2.0 |
| 13 | +""" |
| 14 | + |
| 15 | +import asyncio |
| 16 | + |
| 17 | +from synapse_memory.integrations.llamaindex import ( |
| 18 | + SynapseRetriever, |
| 19 | + SynapseChatStore, |
| 20 | +) |
| 21 | +from llama_index.core.base.llms.types import ChatMessage, MessageRole |
| 22 | + |
| 23 | + |
| 24 | +async def retriever_demo(): |
| 25 | + """Demonstrate the SynapseRetriever.""" |
| 26 | + print("=" * 60) |
| 27 | + print("SynapseRetriever Demo") |
| 28 | + print("=" * 60) |
| 29 | + |
| 30 | + retriever = SynapseRetriever(agent_id="demo-retriever", top_k=3) |
| 31 | + print(f"Initialized: {retriever}") |
| 32 | + |
| 33 | + # Populate the memory store |
| 34 | + await retriever.astore("Our deployment strategy uses blue-green on Kubernetes.") |
| 35 | + await retriever.astore("The primary database is PostgreSQL 16 on AWS RDS.") |
| 36 | + await retriever.astore("API rate limit is 1000 requests per minute per client.") |
| 37 | + print("Stored 3 knowledge entries.\n") |
| 38 | + |
| 39 | + # Retrieve relevant nodes |
| 40 | + from llama_index.core.schema import QueryBundle |
| 41 | + query = QueryBundle(query_str="What is our deployment strategy?") |
| 42 | + nodes = await retriever._aretrieve(query) |
| 43 | + |
| 44 | + print(f"Query: '{query.query_str}'") |
| 45 | + for nws in nodes: |
| 46 | + tq = nws.score |
| 47 | + print(f" [{tq:.2f}] {nws.node.text}") |
| 48 | + |
| 49 | + # In a real LlamaIndex application: |
| 50 | + # |
| 51 | + # from llama_index.core import VectorStoreIndex |
| 52 | + # index = VectorStoreIndex.from_documents(documents) |
| 53 | + # query_engine = index.as_query_engine(retriever=retriever) |
| 54 | + # response = query_engine.query("What is our deployment strategy?") |
| 55 | + |
| 56 | + |
| 57 | +async def chat_store_demo(): |
| 58 | + """Demonstrate the SynapseChatStore.""" |
| 59 | + print("\n" + "=" * 60) |
| 60 | + print("SynapseChatStore Demo") |
| 61 | + print("=" * 60) |
| 62 | + |
| 63 | + store = SynapseChatStore(agent_id="demo-chat") |
| 64 | + print(f"Initialized: {store}") |
| 65 | + |
| 66 | + # Add messages to a conversation |
| 67 | + store.add_message("session-1", ChatMessage( |
| 68 | + role=MessageRole.USER, |
| 69 | + content="What databases do we support?", |
| 70 | + )) |
| 71 | + store.add_message("session-1", ChatMessage( |
| 72 | + role=MessageRole.ASSISTANT, |
| 73 | + content="We support PostgreSQL, MySQL, and SQLite.", |
| 74 | + )) |
| 75 | + store.add_message("session-1", ChatMessage( |
| 76 | + role=MessageRole.USER, |
| 77 | + content="Which one is recommended for production?", |
| 78 | + )) |
| 79 | + print("Added 3 messages to session-1.\n") |
| 80 | + |
| 81 | + # Retrieve conversation |
| 82 | + messages = store.get_messages("session-1") |
| 83 | + for msg in messages: |
| 84 | + print(f" [{msg.role.value}] {msg.content}") |
| 85 | + |
| 86 | + # List keys |
| 87 | + print(f"\nActive sessions: {store.get_keys()}") |
| 88 | + |
| 89 | + # In a real LlamaIndex application: |
| 90 | + # |
| 91 | + # from llama_index.core.memory import ChatMemoryBuffer |
| 92 | + # memory = ChatMemoryBuffer.from_defaults( |
| 93 | + # chat_store=store, |
| 94 | + # chat_store_key="session-1", |
| 95 | + # ) |
| 96 | + |
| 97 | + |
| 98 | +async def main(): |
| 99 | + await retriever_demo() |
| 100 | + await chat_store_demo() |
| 101 | + print("\nDone.") |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + asyncio.run(main()) |
0 commit comments