r/SQLv2 • • 13d ago

SQLv2 Lesson 6: your first learning goal. The database tries two sources, checks each answer with SQL, keeps score, and switches when the policy changes (beginner)

Lesson 6 of the beginner series, run on SynapCores 2.3.2 this morning with the output pasted. (This sub is run by the SynapCores team.)

The problem

Your support bot answers "how long do refunds take?" from the FAQ or the policy page. Only one is approved by legal at a time, and which one changes. Hard-code "use the policy page" and the bot breaks the day legal rewrites the FAQ instead.

What you want: try a source, check the answer against the database, keep score, prefer whatever has been passing. That's a learning goal.

Step 1: the data

CREATE TABLE refund_answers (source TEXT PRIMARY KEY, approved INTEGER, answer TEXT);
INSERT INTO refund_answers (source, approved, answer) VALUES
  ('faq', 0, 'Refunds take 5 business days.'),
  ('policy_page', 1, 'Refunds reach your card within 10 business days after we receive the item.');

Step 2: the goal

A goal = what you want done + ways to try it + a success check, sent as JSON to POST /v1/agents/goals. The important parts:

"task_type": "refund-question",  "learn": true,  "explore": 0.1,
"guardrails": {"allowed_actions": ["sql"]},
"steps": [{
  "action":       SELECT approved, answer FROM refund_answers WHERE source = 'faq'
  "alternatives": SELECT approved, answer FROM refund_answers WHERE source = 'policy_page'
  "acceptance":   {"kind": "json_equals", "pointer": "/rows/0/0", "expected": 1}
}]

In plain words:

  • Two strategies: FAQ first, policy page second.
  • Success check: first column of the first row must be 1 (approved). The database decides, not a model.
  • learn = true: keep a score per strategy for this task_type. Off unless you ask.
  • explore = 0.1: now and then try the other one anyway, so a change gets noticed.
  • guardrails: SQL only.

(Full goal JSON and the 20-line runner are in the first comment.)

Step 3: six customers ask the question

question 1: done   tried: faq -> policy_page
question 2: done   tried: policy_page
question 3: done   tried: policy_page
question 4: done   tried: policy_page
question 5: done   tried: policy_page
question 6: done   tried: policy_page

Question 1 tried the FAQ, failed the check (not approved), and fell back. From question 2 on, it goes straight to the policy page.

Step 4: look at what it learned (plain SQL)

SHOW AGENT POLICY;

Output for this task type (trimmed):

{"action": "sql:faq",         "count": 1, "reward_sum": -1.0, "mean": -1.0}
{"action": "sql:policy_page", "count": 6, "reward_sum":  6.0, "mean":  1.0}

Each try scores +1 (passed) or -1 (failed). A tally sheet you can query, not a trained model.

Step 5: legal changes the policy

UPDATE refund_answers SET approved = 0 WHERE source = 'policy_page';
UPDATE refund_answers SET approved = 1,
  answer = 'Refunds reach your card within 7 business days after we receive the item (new policy).'
WHERE source = 'faq';

Eight more customers ask:

question 7:  done   tried: policy_page -> faq
question 8:  done   tried: policy_page -> faq
question 9:  done   tried: policy_page -> faq
question 10: done   tried: faq
question 11: done   tried: faq
...
question 14: done   tried: faq

{"action": "sql:faq",         "count": 9, "reward_sum": 7.0, "mean": 0.78}
{"action": "sql:policy_page", "count": 9, "reward_sum": 3.0, "mean": 0.33}

Three fallbacks, then the score flips and the bot prefers the FAQ, with no code change. Every customer still got the approved answer, because the check ran on every try. And "success" is a check on your own data, so it means what you say it means.

Homework

Legal un-approves both sources (UPDATE refund_answers SET approved = 0;). Question 15 comes in.

  1. Does the bot answer? With what?
  2. What happens to the two scores?
  3. Is that the behaviour you want from a support bot, and why?

Reply with your guesses and I'll post the real output.

Free Community Edition, if you want to run this yourself: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-lesson-6

Where does "which source is the approved one right now" live at your work: a flag in a table, a doc owner, or tribal knowledge?

1 Upvotes

1 comment sorted by

1

u/Alternative_Pin9598 13d ago

The full goal (one per customer question; name must be unique):

{"database": "main",
 "goal": {"name": "refund-question-1", "objective": "answer the refund question with an approved answer",
          "persona": "lesson6-desk", "task_type": "refund-question", "learn": true, "explore": 0.1,
          "priority": 0, "expected_value": 1.0,
          "budget": {"iterations": 8, "tokens": 0, "cost_microunits": 0, "wall_ms": 30000},
          "guardrails": {"allowed_actions": ["sql"]}},
 "steps": [{"id": "find-answer", "max_attempts": 2,
   "action": {"tool": "sql", "args": {"sql": "SELECT approved, answer FROM refund_answers WHERE source = 'faq'",
              "params": [], "strategy": "faq"}, "max_tokens": 0, "max_cost_microunits": 0, "timeout_ms": 10000},
   "alternatives": [{"tool": "sql", "args": {"sql": "SELECT approved, answer FROM refund_answers WHERE source = 'policy_page'",
              "params": [], "strategy": "policy_page"}, "max_tokens": 0, "max_cost_microunits": 0, "timeout_ms": 10000}],
   "acceptance": {"kind": "json_equals", "pointer": "/rows/0/0", "expected": 1}}]}

Allow the persona to run SQL first (goals are off by default; nothing runs until you allow it): GET /v1/agents/learning/config?database=main, add "lesson6-desk": ["sql"] under persona_actions, and PUT it back.

The runner (Python, standard library only):

def ask(n):
    body = make_goal(n)                        # the JSON above with name refund-question-n
    gid = post("/v1/agents/goals", body)["data"]["goal"]["id"]
    while True:
        g = get(f"/v1/agents/goals/{gid}?database=main")["data"]
        if g["status"] not in ("pending", "running"):
            return g["status"]
        post(f"/v1/agents/goals/{gid}/execute", {"database": "main"})

Which strategy each attempt used is in GET /v1/agents/goals/{id}/plan under steps[0].action_attempts (action_index 0 = faq, 1 = policy_page).

Clean up: DROP TABLE refund_answers; and remove the persona from the config.

Want to run this on the real engine? r/synapcores is the product home: install steps, the 5 recipes to try first, and a new recipe every Monday. Start here: https://www.reddit.com/r/synapcores/comments/1xfxqej/

Missed Lesson 5 (graph hops + meaning search in one database)? https://www.reddit.com/r/SQLv2/comments/1xq8g8l/