Skip to content

Commit 710fe0a

Browse files
committed
feat(example): python-basic gold — C3.2
0 parents  commit 710fe0a

5 files changed

Lines changed: 140 additions & 0 deletions

File tree

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SYNAPSE_TOKEN=sk_connect_xxx
2+
SYNAPSE_BASE_URL=https://forge.synapselayer.org

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Synapse Layer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Synapse Layer — Python Basic Example
2+
3+
[![Python 3.9+](https://img.shields.io/badge/Python-3.9+-blue.svg)](https://python.org)
4+
[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](./LICENSE)
5+
6+
Store and recall AI agent memories with semantic search in under 30 seconds.
7+
8+
## What this does
9+
10+
This example connects to the Synapse Layer API, stores a memory with semantic
11+
content, and recalls it using a natural language query. It demonstrates the
12+
core store/recall loop that gives your AI agents persistent memory across
13+
sessions and tools.
14+
15+
## Prerequisites
16+
17+
- Python 3.9+
18+
- A Synapse Layer account and `sk_connect_` token ([get yours here](https://forge.synapselayer.org/dashboard/connect))
19+
20+
## Install
21+
22+
```bash
23+
git clone https://github.com/SynapseLayer/synapse-layer-python-basic
24+
cd synapse-layer-python-basic && pip install -r requirements.txt
25+
cp .env.example .env # edit with your token
26+
```
27+
28+
## Run
29+
30+
```bash
31+
python3 main.py
32+
```
33+
34+
## Expected output
35+
36+
```
37+
[store] Saving memory...
38+
[store] Done.
39+
40+
[recall] Searching for user preferences...
41+
[0] content: The user prefers dark mode and communicates in Portuguese.
42+
tq_score: 0.716
43+
44+
[recall] 1 memories found.
45+
```
46+
47+
## Get your token
48+
49+
1. Go to [forge.synapselayer.org](https://forge.synapselayer.org)
50+
2. Sign up or log in
51+
3. Navigate to **Dashboard → Connect**
52+
4. Generate a new `sk_connect_` token
53+
5. Paste it in your `.env` file

main.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""Synapse Layer — Python Basic Example
2+
Store a memory and recall it with semantic search.
3+
"""
4+
import os
5+
import requests
6+
from dotenv import load_dotenv
7+
8+
load_dotenv()
9+
10+
TOKEN = os.getenv("SYNAPSE_TOKEN", "")
11+
BASE_URL = os.getenv("SYNAPSE_BASE_URL", "https://forge.synapselayer.org")
12+
13+
if not TOKEN or TOKEN == "sk_connect_xxx":
14+
print("ERROR: Set SYNAPSE_TOKEN in .env")
15+
print("Get yours at: https://forge.synapselayer.org/dashboard/connect")
16+
exit(1)
17+
18+
HEADERS = {"Content-Type": "application/json", "Authorization": f"Bearer {TOKEN}"}
19+
AGENT = "python-example"
20+
21+
22+
def store_memory():
23+
"""Store a memory via the Forge API."""
24+
print("[store] Saving memory...")
25+
resp = requests.post(f"{BASE_URL}/api/forge", headers=HEADERS, json={
26+
"action": "store",
27+
"agent": AGENT,
28+
"content": "The user prefers dark mode and communicates in Portuguese.",
29+
"intent": "user_preference",
30+
})
31+
resp.raise_for_status()
32+
print("[store] Done.\n")
33+
34+
35+
def recall_memory():
36+
"""Recall memories with a semantic query (cross-agent)."""
37+
print("[recall] Searching for user preferences...")
38+
resp = requests.post(f"{BASE_URL}/api/forge", headers=HEADERS, json={
39+
"action": "recall",
40+
"query": "What are the user preferences?",
41+
"topK": 5,
42+
})
43+
resp.raise_for_status()
44+
data = resp.json()
45+
memories = data.get("memories", [])
46+
if memories:
47+
for i, mem in enumerate(memories):
48+
print(f" [{i}] content: {mem.get('content', 'N/A')}")
49+
print(f" tq_score: {mem.get('trustQuotient', 'N/A')}")
50+
print(f"\n[recall] {len(memories)} memories found.")
51+
else:
52+
print("[recall] No memories found yet.")
53+
54+
55+
if __name__ == "__main__":
56+
try:
57+
store_memory()
58+
recall_memory()
59+
except requests.exceptions.HTTPError as e:
60+
print(f"API Error: {e.response.status_code}{e.response.text[:200]}")
61+
except Exception as e:
62+
print(f"Error: {e}")

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests==2.33.1
2+
python-dotenv==1.0.1

0 commit comments

Comments
 (0)