Skip to content

Commit 017334e

Browse files
docs: add PydanticAI and Gemini integration examples
Closes #4, closes #3
1 parent 98d7557 commit 017334e

File tree

2 files changed

+269
-0
lines changed

2 files changed

+269
-0
lines changed

examples/gemini_example.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
"""
2+
Google Gemini + Agent Veil Protocol integration example.
3+
4+
Shows how to:
5+
1. Register agents on AVP
6+
2. Define AVP trust tools as Gemini function declarations
7+
3. Gemini calls trust tools via function calling
8+
4. Log interaction results as attestations
9+
10+
Prerequisites:
11+
pip install agentveil google-generativeai
12+
13+
Usage:
14+
python examples/gemini_example.py
15+
"""
16+
17+
import json
18+
19+
from agentveil import AVPAgent
20+
21+
AVP_URL = "https://agentveil.dev"
22+
23+
24+
# === AVP tool handlers ===
25+
26+
_avp_agent = None
27+
28+
29+
def _get_agent() -> AVPAgent:
30+
global _avp_agent
31+
if _avp_agent is None:
32+
_avp_agent = AVPAgent.load(AVP_URL, "gemini_researcher")
33+
return _avp_agent
34+
35+
36+
def check_reputation(did: str) -> dict:
37+
"""Check an agent's reputation score on AVP."""
38+
return _get_agent().get_reputation(did)
39+
40+
41+
def trust_check(did: str, min_tier: str = "basic") -> dict:
42+
"""Advisory trust decision: should I delegate to this agent?"""
43+
return _get_agent().can_trust(did, min_tier=min_tier)
44+
45+
46+
def log_attestation(did: str, outcome: str, context: str = "") -> dict:
47+
"""Log an interaction result as a signed attestation."""
48+
return _get_agent().attest(to_did=did, outcome=outcome, context=context)
49+
50+
51+
TOOL_HANDLERS = {
52+
"check_reputation": check_reputation,
53+
"trust_check": trust_check,
54+
"log_attestation": log_attestation,
55+
}
56+
57+
58+
def main():
59+
# === Step 1: Register agents ===
60+
print("=== Registering agents on AVP ===")
61+
62+
researcher = AVPAgent.create(AVP_URL, name="gemini_researcher")
63+
researcher.register(
64+
display_name="Gemini Researcher",
65+
capabilities=["research", "analysis"],
66+
provider="google",
67+
)
68+
69+
assistant = AVPAgent.create(AVP_URL, name="gemini_assistant")
70+
assistant.register(
71+
display_name="Gemini Assistant",
72+
capabilities=["writing", "summarization"],
73+
provider="google",
74+
)
75+
76+
print(f"Researcher: {researcher.did[:40]}...")
77+
print(f"Assistant: {assistant.did[:40]}...")
78+
79+
# === Step 2: Simulate tool calls ===
80+
print("\n=== Simulating tool calls ===")
81+
82+
rep = check_reputation(assistant.did)
83+
print(f"Reputation: score={rep['score']:.3f}, confidence={rep['confidence']:.3f}")
84+
85+
decision = trust_check(assistant.did, min_tier="basic")
86+
print(f"Trust: allowed={decision['allowed']}, tier={decision['tier']}")
87+
88+
att = log_attestation(assistant.did, "positive", "research_task")
89+
print(f"Attestation: {att}")
90+
91+
# === Step 3: Updated reputation ===
92+
print("\n=== Updated reputation ===")
93+
rep = researcher.get_reputation(assistant.did)
94+
print(f"Assistant: score={rep['score']:.3f}, confidence={rep['confidence']:.3f}")
95+
96+
# === Step 4: Gemini function calling (requires GOOGLE_API_KEY) ===
97+
print("\n=== Gemini function calling (requires GOOGLE_API_KEY) ===")
98+
print("""
99+
# To run with Gemini:
100+
#
101+
# import google.generativeai as genai
102+
#
103+
# genai.configure(api_key="YOUR_API_KEY")
104+
#
105+
# tools = genai.protos.Tool(function_declarations=[
106+
# genai.protos.FunctionDeclaration(
107+
# name="check_reputation",
108+
# description="Check an agent's reputation score on AVP",
109+
# parameters=genai.protos.Schema(
110+
# type=genai.protos.Type.OBJECT,
111+
# properties={"did": genai.protos.Schema(type=genai.protos.Type.STRING)},
112+
# required=["did"],
113+
# ),
114+
# ),
115+
# genai.protos.FunctionDeclaration(
116+
# name="trust_check",
117+
# description="Advisory trust decision: should I delegate to this agent?",
118+
# parameters=genai.protos.Schema(
119+
# type=genai.protos.Type.OBJECT,
120+
# properties={
121+
# "did": genai.protos.Schema(type=genai.protos.Type.STRING),
122+
# "min_tier": genai.protos.Schema(type=genai.protos.Type.STRING),
123+
# },
124+
# required=["did"],
125+
# ),
126+
# ),
127+
# genai.protos.FunctionDeclaration(
128+
# name="log_attestation",
129+
# description="Log a signed attestation after interaction",
130+
# parameters=genai.protos.Schema(
131+
# type=genai.protos.Type.OBJECT,
132+
# properties={
133+
# "did": genai.protos.Schema(type=genai.protos.Type.STRING),
134+
# "outcome": genai.protos.Schema(type=genai.protos.Type.STRING),
135+
# "context": genai.protos.Schema(type=genai.protos.Type.STRING),
136+
# },
137+
# required=["did", "outcome"],
138+
# ),
139+
# ),
140+
# ])
141+
#
142+
# model = genai.GenerativeModel("gemini-1.5-pro", tools=[tools])
143+
# chat = model.start_chat()
144+
#
145+
# response = chat.send_message("Check if %s is trustworthy")
146+
#
147+
# # Handle function calls
148+
# for part in response.parts:
149+
# if fn := part.function_call:
150+
# args = dict(fn.args)
151+
# result = TOOL_HANDLERS[fn.name](**args)
152+
# response = chat.send_message(
153+
# genai.protos.Content(parts=[
154+
# genai.protos.Part(function_response=genai.protos.FunctionResponse(
155+
# name=fn.name, response={"result": result}
156+
# ))
157+
# ])
158+
# )
159+
# print(response.text)
160+
""" % assistant.did)
161+
162+
print("=== Done ===")
163+
164+
165+
if __name__ == "__main__":
166+
main()

examples/pydantic_ai_example.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
PydanticAI + Agent Veil Protocol integration example.
3+
4+
Shows how to:
5+
1. Register an agent on AVP
6+
2. Define AVP trust tools as PydanticAI tools
7+
3. Agent checks reputation before acting
8+
4. Log interaction results as attestations
9+
10+
Prerequisites:
11+
pip install agentveil pydantic-ai
12+
13+
Usage:
14+
python examples/pydantic_ai_example.py
15+
"""
16+
17+
from agentveil import AVPAgent
18+
19+
AVP_URL = "https://agentveil.dev"
20+
21+
22+
def main():
23+
# === Step 1: Register agents on AVP ===
24+
print("=== Registering agents on AVP ===")
25+
26+
analyst = AVPAgent.create(AVP_URL, name="pydantic_analyst")
27+
analyst.register(
28+
display_name="PydanticAI Analyst",
29+
capabilities=["analysis", "research"],
30+
provider="openai",
31+
)
32+
33+
data_agent = AVPAgent.create(AVP_URL, name="pydantic_data")
34+
data_agent.register(
35+
display_name="PydanticAI Data Agent",
36+
capabilities=["data_processing"],
37+
provider="openai",
38+
)
39+
40+
print(f"Analyst: {analyst.did[:40]}...")
41+
print(f"Data Agent: {data_agent.did[:40]}...")
42+
43+
# === Step 2: Trust check before delegation ===
44+
print("\n=== Trust check ===")
45+
decision = analyst.can_trust(data_agent.did, min_tier="basic")
46+
print(f"Can trust: {decision['allowed']} (tier: {decision['tier']}, score: {decision['score']:.3f})")
47+
print(f"Reason: {decision['reason']}")
48+
49+
# === Step 3: Check reputation ===
50+
print("\n=== Reputation ===")
51+
rep = analyst.get_reputation(data_agent.did)
52+
print(f"Score: {rep['score']:.3f}, Confidence: {rep['confidence']:.3f}")
53+
54+
# === Step 4: Log attestation ===
55+
print("\n=== Attestation ===")
56+
result = analyst.attest(
57+
to_did=data_agent.did,
58+
outcome="positive",
59+
weight=0.8,
60+
context="analysis_task",
61+
)
62+
print(f"Attestation logged: {result}")
63+
64+
# === Step 5: PydanticAI agent with AVP tools (requires OPENAI_API_KEY) ===
65+
print("\n=== PydanticAI agent with AVP tools ===")
66+
print("""
67+
# To run with PydanticAI:
68+
#
69+
# from pydantic_ai import Agent, RunContext, Tool
70+
#
71+
# avp = AVPAgent.load("https://agentveil.dev", "pydantic_analyst")
72+
#
73+
# async def check_reputation(ctx: RunContext, did: str) -> dict:
74+
# \"\"\"Check an agent's reputation score on AVP.\"\"\"
75+
# return avp.get_reputation(did)
76+
#
77+
# async def trust_check(ctx: RunContext, did: str, min_tier: str = "basic") -> dict:
78+
# \"\"\"Advisory trust decision: should I delegate to this agent?\"\"\"
79+
# return avp.can_trust(did, min_tier=min_tier)
80+
#
81+
# async def log_attestation(ctx: RunContext, did: str, outcome: str, context: str = "") -> dict:
82+
# \"\"\"Log an interaction result as a signed attestation.\"\"\"
83+
# return avp.attest(to_did=did, outcome=outcome, context=context)
84+
#
85+
# agent = Agent(
86+
# "openai:gpt-4",
87+
# tools=[
88+
# Tool(check_reputation, description="Check agent reputation on AVP"),
89+
# Tool(trust_check, description="Check if agent meets trust threshold"),
90+
# Tool(log_attestation, description="Log signed attestation after interaction"),
91+
# ],
92+
# system_prompt="You are an analyst. Check trust before delegating. Log results.",
93+
# )
94+
#
95+
# result = await agent.run("Check if %s is trustworthy for data processing")
96+
# print(result.data)
97+
""" % data_agent.did)
98+
99+
print("=== Done ===")
100+
101+
102+
if __name__ == "__main__":
103+
main()

0 commit comments

Comments
 (0)