|
| 1 | +# Copyright (c) 2025 Agentspan |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""120 — OCG-backed long-term memory with human good/bad feedback links. |
| 5 | +
|
| 6 | +Enable memory on an agent and the runtime does two things automatically: |
| 7 | +
|
| 8 | + - BEFORE a run: relevant past memories (scoped to this agent/user) are |
| 9 | + retrieved from OCG and injected into the prompt — no tool call needed. |
| 10 | + - AFTER a run: the conversation is summarized (Claude-style: durable facts, |
| 11 | + not the raw transcript) by a small internal summarizer agent and saved back |
| 12 | + to OCG as a memory. |
| 13 | +
|
| 14 | +Feedback is HUMAN-only. Agents never vote. Instead, the runtime hands a |
| 15 | +``FeedbackEvent`` — including signed *capability URLs* (good/bad) — to the |
| 16 | +agent's ``feedback_sink``. A human (e.g. a support engineer) clicks a link to |
| 17 | +mark the memory good or bad; the link skips auth (its signature is the |
| 18 | +authorization), so the clicker needs no OCG account. Here the sink just prints |
| 19 | +the URLs as they'd appear in a Zendesk ticket comment. |
| 20 | +
|
| 21 | +Requires the OCG instance to be started with a feedback-link secret |
| 22 | +(``OCG_FEEDBACK_LINK_SECRET``) for the capability URLs to be minted. |
| 23 | +
|
| 24 | +Run (from the repo root):: |
| 25 | +
|
| 26 | + OCG_INSTANCE_URL=https://test.contextgraph.io \ |
| 27 | + OCG_TOKEN=<bearer-token> \ |
| 28 | + uv run python examples/agents/120_ocg_memory.py |
| 29 | +
|
| 30 | + # against an embedded server, also set AGENTSPAN_SERVER_URL. |
| 31 | +""" |
| 32 | + |
| 33 | +import os |
| 34 | + |
| 35 | +from conductor.ai.agents import Agent, AgentRuntime, OCGMemoryStore, SemanticMemory |
| 36 | +from conductor.ai.agents.ocg_memory import FeedbackEvent |
| 37 | + |
| 38 | +MODEL = os.environ.get("AGENTSPAN_LLM_MODEL", "openai/gpt-4o-mini") |
| 39 | + |
| 40 | +OCG_INSTANCE_URL = os.environ.get("OCG_INSTANCE_URL") or "" |
| 41 | +# Unlike the ocg.py retrieval tools (which resolve a credential server-side), |
| 42 | +# the memory store calls OCG directly from Python, so it holds the bearer token. |
| 43 | +OCG_TOKEN = os.environ.get("OCG_TOKEN") |
| 44 | +if not OCG_INSTANCE_URL: |
| 45 | + raise SystemExit("Set OCG_INSTANCE_URL to your OCG instance, e.g. https://test.contextgraph.io") |
| 46 | + |
| 47 | + |
| 48 | +def zendesk_sink(event: FeedbackEvent) -> None: |
| 49 | + """Deliver the good/bad links to a human. In production this would POST a |
| 50 | + comment to the Zendesk ticket; here we just print what would be sent.""" |
| 51 | + print("\n--- would post to Zendesk ticket ---") |
| 52 | + print(f"Saved memory: {event.memory_key}") |
| 53 | + print(f"Summary: {event.summary}") |
| 54 | + if event.good_url: |
| 55 | + print(f" 👍 Was this helpful? {event.good_url}") |
| 56 | + print(f" 👎 Not helpful: {event.bad_url}") |
| 57 | + print("------------------------------------\n") |
| 58 | + |
| 59 | + |
| 60 | +def main() -> None: |
| 61 | + store = OCGMemoryStore( |
| 62 | + url=OCG_INSTANCE_URL, |
| 63 | + agent="agent:support", |
| 64 | + user="user:alice", |
| 65 | + token=OCG_TOKEN, |
| 66 | + ) |
| 67 | + |
| 68 | + agent = Agent( |
| 69 | + name="support", |
| 70 | + model=MODEL, |
| 71 | + instructions=( |
| 72 | + "You are a customer support agent. Use any relevant context from " |
| 73 | + "memory to personalize your answer. A memory labeled [bad] was " |
| 74 | + "flagged by a human — treat it with suspicion." |
| 75 | + ), |
| 76 | + semantic_memory=SemanticMemory(store=store, max_results=5), |
| 77 | + feedback_sink=zendesk_sink, |
| 78 | + ) |
| 79 | + |
| 80 | + with AgentRuntime() as runtime: |
| 81 | + print("--- Turn 1 ---") |
| 82 | + runtime.run( |
| 83 | + agent, "Hi, I'm Alice. I'm on the Enterprise plan and prefer email." |
| 84 | + ).print_result() |
| 85 | + |
| 86 | + print("\n--- Turn 2 (should recall Alice's plan from memory) ---") |
| 87 | + runtime.run(agent, "What plan am I on again?").print_result() |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments