* Add documentation code validation system - Create runnable example scripts in examples/api/ (19 files) - Add CodeSnippet component for extracting marked sections - Add raw-loader dependency for importing source files - Create sample retain-new.mdx showing new approach - Add README documenting coverage and gaps * Fix wheel glob expansion in test-doc-examples CI job * Fix CI issue * Fix wheel path - uv build outputs to repo root dist/ * Fix: use explicit shell expansion for wheel install * Fix: run cd in subshell so install runs from repo root * Add documentation code validation CI job - Use uv sync + uv run pattern (matches existing CI) - Add requests to test dependencies for cleanup scripts * Fix async API client usage in documents.py example * Fix main-methods.py: RecallResult and ReflectFact don't have weight attribute * Fix opinions.py: use actual API attributes instead of non-existent ones * Fix example scripts: remove non-existent API attributes - recall.py: remove .weight, fix entities iteration (dict not list) - retain.mjs: remove result.async check
104 lines
3.3 KiB
Python
104 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Opinions API examples for Hindsight.
|
|
Run: python examples/api/opinions.py
|
|
"""
|
|
import os
|
|
import requests
|
|
|
|
HINDSIGHT_URL = os.getenv("HINDSIGHT_API_URL", "http://localhost:8888")
|
|
|
|
# =============================================================================
|
|
# Setup (not shown in docs)
|
|
# =============================================================================
|
|
from hindsight_client import Hindsight
|
|
|
|
client = Hindsight(base_url=HINDSIGHT_URL)
|
|
|
|
# Seed some data about programming languages
|
|
client.retain(bank_id="my-bank", content="Python is widely used for data science and machine learning")
|
|
client.retain(bank_id="my-bank", content="Functional programming emphasizes immutability and pure functions")
|
|
client.retain(bank_id="my-bank", content="Rust has better memory safety than C++")
|
|
client.retain(bank_id="my-bank", content="C++ has a larger ecosystem and more libraries")
|
|
|
|
# =============================================================================
|
|
# Doc Examples
|
|
# =============================================================================
|
|
|
|
# [docs:opinion-form]
|
|
# Ask a question - the system may form opinions based on stored facts
|
|
answer = client.reflect(
|
|
bank_id="my-bank",
|
|
query="What do you think about functional programming?"
|
|
)
|
|
|
|
print(answer.text)
|
|
# [/docs:opinion-form]
|
|
|
|
|
|
# [docs:opinion-search]
|
|
# Search for facts about a topic
|
|
results = client.recall(
|
|
bank_id="my-bank",
|
|
query="programming languages"
|
|
)
|
|
|
|
for result in results.results:
|
|
print(f"- {result.text}")
|
|
# [/docs:opinion-search]
|
|
|
|
|
|
# [docs:opinion-disposition]
|
|
# Create two memory banks with different dispositions
|
|
client.create_bank(
|
|
bank_id="open-minded",
|
|
disposition={"skepticism": 2, "literalism": 2, "empathy": 4}
|
|
)
|
|
|
|
client.create_bank(
|
|
bank_id="conservative",
|
|
disposition={"skepticism": 5, "literalism": 5, "empathy": 2}
|
|
)
|
|
|
|
# Store the same facts to both
|
|
facts = [
|
|
"Rust has better memory safety than C++",
|
|
"C++ has a larger ecosystem and more libraries",
|
|
"Rust compile times are longer than C++"
|
|
]
|
|
for fact in facts:
|
|
client.retain(bank_id="open-minded", content=fact)
|
|
client.retain(bank_id="conservative", content=fact)
|
|
|
|
# Ask both the same question - different dispositions lead to different responses
|
|
q = "Should we rewrite our C++ codebase in Rust?"
|
|
|
|
answer1 = client.reflect(bank_id="open-minded", query=q)
|
|
print("Open-minded response:", answer1.text[:100], "...")
|
|
|
|
answer2 = client.reflect(bank_id="conservative", query=q)
|
|
print("Conservative response:", answer2.text[:100], "...")
|
|
# [/docs:opinion-disposition]
|
|
|
|
|
|
# [docs:opinion-in-reflect]
|
|
answer = client.reflect(bank_id="my-bank", query="What language should I learn?")
|
|
|
|
print("Response:", answer.text)
|
|
|
|
# See which facts influenced the response
|
|
if answer.based_on:
|
|
print("\nBased on these facts:")
|
|
for fact in answer.based_on:
|
|
print(f" - {fact.text}")
|
|
# [/docs:opinion-in-reflect]
|
|
|
|
|
|
# =============================================================================
|
|
# Cleanup (not shown in docs)
|
|
# =============================================================================
|
|
requests.delete(f"{HINDSIGHT_URL}/v1/default/banks/my-bank")
|
|
requests.delete(f"{HINDSIGHT_URL}/v1/default/banks/open-minded")
|
|
requests.delete(f"{HINDSIGHT_URL}/v1/default/banks/conservative")
|
|
|
|
print("opinions.py: All examples passed")
|