|
| 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() |
0 commit comments