Loading...
Loading...
Build hierarchical memory systems for AI agents using GAM (General Agentic Memory) with text, video, and long-horizon trajectory support
npx skill4agent add aradotso/ai-agent-skills general-agentic-memorySkill by ara.so — AI Agent Skills collection.
# Full installation with all features
pip install -e ".[all]"
# Or minimal installation
pip install -e .# GAM Agent (memory building)
export GAM_API_KEY="sk-your-api-key"
export GAM_MODEL="gpt-4o-mini"
export GAM_API_BASE="https://api.openai.com/v1"
# Chat Agent (Q&A) — falls back to GAM Agent config when not set
export GAM_CHAT_API_KEY="sk-your-chat-api-key"
export GAM_CHAT_MODEL="gpt-4o"
export GAM_CHAT_API_BASE="https://api.openai.com/v1"Workflowfrom gam import Workflow
# Initialize workflow for text processing
wf = Workflow(
task_type="text",
gam_dir="./my_text_gam",
model="gpt-4o-mini",
api_key=None # Uses GAM_API_KEY env var
)
# Add content to memory
wf.add(input_file="research_paper.pdf")
# Query the memory
result = wf.request("What is the main conclusion of this paper?")
print(result.answer)
print(result.sources) # Retrieved memory chunksfrom gam import Workflow
# Initialize video workflow
wf = Workflow(
task_type="video",
gam_dir="./my_video_gam",
model="gpt-4o-mini"
)
# Add video content
wf.add(input_file="lecture.mp4")
# Query video memory
result = wf.request("What topics are covered in this lecture?")
print(result.answer)from gam import Workflow
# Initialize trajectory workflow
wf = Workflow(
task_type="long-horizon",
gam_dir="./agent_trajectory_gam",
model="gpt-4o-mini"
)
# Add agent trajectory log
wf.add(input_file="agent_execution.jsonl")
# Query the trajectory
result = wf.request("What tools did the agent use to solve the task?")
print(result.answer)from gam import Workflow
wf = Workflow(task_type="text", gam_dir="./my_gam")
# Add initial content
wf.add(input_file="document1.pdf")
# Later, add more content incrementally
wf.add(input_file="document2.pdf")
wf.add(input_file="document3.txt")
# Query across all added content
result = wf.request("Compare the approaches in all three documents")from gam.text.chunker import TextChunker
from gam.text.memory_builder import MemoryBuilder
from gam.text.taxonomy_builder import TaxonomyBuilder
from gam.text.chat_agent import ChatAgent
# Step 1: Chunk text
chunker = TextChunker(model="gpt-4o-mini")
chunks = chunker.chunk(text="Long document text here...")
# Step 2: Build memories
memory_builder = MemoryBuilder(model="gpt-4o-mini")
memories = memory_builder.build(chunks)
# Step 3: Create taxonomy
taxonomy_builder = TaxonomyBuilder(model="gpt-4o-mini")
taxonomy = taxonomy_builder.build(memories)
# Step 4: Save to GAM directory
gam_dir = "./my_gam"
taxonomy.save(gam_dir)
# Step 5: Query
chat_agent = ChatAgent(
gam_dir=gam_dir,
model="gpt-4o",
task_type="text"
)
answer = chat_agent.request("Your question here")
print(answer)from gam import Workflow
# Use custom API endpoint (e.g., local vLLM server)
wf = Workflow(
task_type="text",
gam_dir="./my_gam",
model="meta-llama/Llama-3-8B",
api_base="http://localhost:8000/v1",
api_key="EMPTY" # Some local servers don't require keys
)
wf.add(input_file="document.pdf")
result = wf.request("Summarize this document")gam-add# Add text document
gam-add --type text \
--gam-dir ./my_gam \
--input research_paper.pdf \
--model gpt-4o-mini
# Add video
gam-add --type video \
--gam-dir ./video_gam \
--input lecture.mp4
# Add long-horizon trajectory
gam-add --type long-horizon \
--gam-dir ./trajectory_gam \
--input agent_log.jsonl
# Use environment variables for API config
export GAM_API_KEY="sk-xxx"
export GAM_MODEL="gpt-4o-mini"
gam-add --type text --gam-dir ./my_gam --input document.txtgam-request# Query text memory
gam-request --type text \
--gam-dir ./my_gam \
--question "What is the main conclusion?" \
--model gpt-4o
# Query video memory
gam-request --type video \
--gam-dir ./video_gam \
--question "What happens at 5 minutes?"
# Query with custom chat model
export GAM_CHAT_MODEL="gpt-4o"
export GAM_CHAT_API_KEY="sk-xxx"
gam-request --type text \
--gam-dir ./my_gam \
--question "Summarize the key findings"gam-addgam-request--typetextvideolong-horizon--gam-dir--model--api-keyGAM_API_KEY--api-baseGAM_API_BASE# examples/run_api.py
from gam.api import create_app
import uvicorn
app = create_app()
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5001)# Run the API server
python examples/run_api.py --port 5001
# Interactive API docs available at:
# http://localhost:5001/docsimport requests
API_BASE = "http://localhost:5001"
# Add content
add_response = requests.post(
f"{API_BASE}/add",
json={
"task_type": "text",
"gam_dir": "./my_gam",
"input_file": "document.pdf",
"model": "gpt-4o-mini",
"api_key": None # Uses server's env vars
}
)
print(add_response.json())
# Query memory
query_response = requests.post(
f"{API_BASE}/request",
json={
"task_type": "text",
"gam_dir": "./my_gam",
"question": "What are the key findings?",
"model": "gpt-4o"
}
)
result = query_response.json()
print(result["answer"])
print(result["sources"])POST /addPOST /requestGET /healthGET /docsGET /redoc# Start web interface
python examples/run_web.py \
--model gpt-4o-mini \
--port 5000
# Access at http://localhost:5000from gam import Workflow
# Create a knowledge base from multiple documents
wf = Workflow(task_type="text", gam_dir="./knowledge_base")
documents = [
"research/paper1.pdf",
"research/paper2.pdf",
"research/paper3.pdf",
"notes/summary.txt"
]
for doc in documents:
wf.add(input_file=doc)
# Cross-document queries
result = wf.request("Compare the methodologies across all papers")from gam import Workflow
# Compress long agent execution traces
wf = Workflow(task_type="long-horizon", gam_dir="./agent_memory")
# Add trajectory
wf.add(input_file="agent_trace.jsonl")
# Query specific actions
result = wf.request("What API calls did the agent make?")
# Query reasoning
result = wf.request("Why did the agent choose this approach?")from gam import Workflow
# Build video memory
wf = Workflow(task_type="video", gam_dir="./video_memory")
wf.add(input_file="tutorial.mp4")
# Time-based queries
result = wf.request("What is demonstrated in the first 10 minutes?")
# Content-based queries
result = wf.request("Find all mentions of error handling")from gam.text.taxonomy_builder import TaxonomyBuilder
from gam.text.memory_builder import MemoryBuilder
# Build memories with custom chunking
memory_builder = MemoryBuilder(model="gpt-4o-mini")
memories = memory_builder.build(your_chunks)
# Organize with custom taxonomy strategy
taxonomy_builder = TaxonomyBuilder(
model="gpt-4o-mini",
max_depth=4 # Control hierarchy depth
)
taxonomy = taxonomy_builder.build(memories)
# Save to specific location
taxonomy.save("./custom_gam")AuthenticationErrorexport GAM_API_KEY="sk-your-key"
export GAM_MODEL="gpt-4o-mini"
# Verify
echo $GAM_API_KEYwf = Workflow(
task_type="text",
gam_dir="./my_gam",
api_key="sk-your-key", # Explicit key
model="gpt-4o-mini"
)# For OpenAI
wf = Workflow(model="gpt-4o-mini") # Correct
# For local vLLM
wf = Workflow(
model="meta-llama/Llama-3-8B", # Full model path
api_base="http://localhost:8000/v1"
)import os
gam_dir = "./my_gam"
if not os.path.exists(gam_dir):
print("GAM directory doesn't exist - need to run add() first")
# Check for memory files
if not os.path.exists(f"{gam_dir}/taxonomy.json"):
print("No taxonomy found - GAM may be corrupted")pip install -e ".[all]" # Includes video dependencies
# Verify ffmpeg is available (required for video)
which ffmpeg# Use powerful model for memory building (one-time cost)
wf = Workflow(
task_type="text",
gam_dir="./my_gam",
model="gpt-4o" # Better chunking and summarization
)
wf.add(input_file="large_document.pdf")
# Use efficient model for queries (frequent operation)
from gam.text.chat_agent import ChatAgent
chat = ChatAgent(
gam_dir="./my_gam",
model="gpt-4o-mini", # Faster and cheaper
task_type="text"
)docker run -v $(pwd)/my_gam:/app/my_gam \
-e GAM_API_KEY="sk-xxx" \
-e GAM_MODEL="gpt-4o-mini" \
your-imagegam_dirgpt-4ogpt-4o-minigpt-4o-mini# Good: Add documents incrementally
wf = Workflow(task_type="text", gam_dir="./docs")
wf.add(input_file="doc1.pdf")
wf.add(input_file="doc2.pdf")
# Avoid: Rebuilding entire GAM for new documents
# (GAM handles incremental addition efficiently)from gam import Workflow
try:
wf = Workflow(task_type="text", gam_dir="./my_gam")
wf.add(input_file="document.pdf")
result = wf.request("What is this about?")
print(result.answer)
except Exception as e:
print(f"Error: {e}")
# Handle appropriately (retry, log, etc.)cd research
pip install -e .from gam_research import MemoryAgent, ResearchAgent
# Use research implementation
memory_agent = MemoryAgent(model="gpt-4o")
research_agent = ResearchAgent(model="gpt-4o")research/README.md