-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopenai_example.py
More file actions
115 lines (93 loc) · 3.53 KB
/
openai_example.py
File metadata and controls
115 lines (93 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
OpenAI + Agent Veil Protocol integration example.
Shows how to:
1. Use AVP tool definitions with OpenAI function calling
2. Handle AVP tool calls in the response loop
3. Check reputation before delegating tasks
4. Log interaction results as attestations
Prerequisites:
pip install agentveil openai
Usage:
python examples/openai_example.py
"""
import json
from agentveil import AVPAgent
from agentveil.tools.openai import (
avp_tool_definitions,
handle_avp_tool_call,
configure,
)
AVP_URL = "https://agentveil.dev"
def main():
# === Step 1: Configure AVP tools ===
configure(base_url=AVP_URL, agent_name="openai_researcher")
# === Step 2: Register AVP agents ===
print("=== Registering agents on AVP ===")
researcher = AVPAgent.create(AVP_URL, name="openai_researcher")
researcher.register(display_name="OpenAI Researcher")
researcher.publish_card(capabilities=["research", "analysis"], provider="openai")
writer = AVPAgent.create(AVP_URL, name="openai_writer")
writer.register(display_name="OpenAI Writer")
writer.publish_card(capabilities=["writing", "editing"], provider="openai")
print(f"Researcher: {researcher.did[:40]}...")
print(f"Writer: {writer.did[:40]}...")
# === Step 3: Get tool definitions ===
print("\n=== Tool definitions ===")
tools = avp_tool_definitions()
for t in tools:
print(f" - {t['function']['name']}: {t['function']['description'][:60]}...")
# === Step 4: Simulate tool calls ===
print("\n=== Simulating tool calls ===")
# check_avp_reputation
result = handle_avp_tool_call("check_avp_reputation", {"did": writer.did})
print(f"Reputation: {result}")
# should_delegate_to_agent
result = handle_avp_tool_call(
"should_delegate_to_agent", {"did": writer.did, "min_score": 0.3}
)
print(f"Delegation: {result}")
# log_avp_interaction
result = handle_avp_tool_call(
"log_avp_interaction",
{"did": writer.did, "outcome": "positive", "context": "research_task"},
)
print(f"Attestation: {result}")
# === Step 5: Check updated reputation ===
print("\n=== Updated reputation ===")
rep = researcher.get_reputation(writer.did)
print(f"Writer: score={rep['score']:.3f}, confidence={rep['confidence']:.3f}")
# === Step 6: OpenAI function calling loop (requires OPENAI_API_KEY) ===
print("\n=== OpenAI function calling (requires OPENAI_API_KEY) ===")
print("""
# To run with actual OpenAI:
#
# from openai import OpenAI
#
# client = OpenAI()
# messages = [{"role": "user", "content": "Check this agent's reputation: %s"}]
#
# response = client.chat.completions.create(
# model="gpt-4",
# messages=messages,
# tools=avp_tool_definitions(),
# )
#
# # Handle tool calls
# if response.choices[0].message.tool_calls:
# for tc in response.choices[0].message.tool_calls:
# args = json.loads(tc.function.arguments)
# result = handle_avp_tool_call(tc.function.name, args)
# messages.append(response.choices[0].message)
# messages.append({
# "role": "tool",
# "tool_call_id": tc.id,
# "content": result,
# })
# final = client.chat.completions.create(
# model="gpt-4", messages=messages, tools=avp_tool_definitions()
# )
# print(final.choices[0].message.content)
""" % writer.did)
print("=== Done ===")
if __name__ == "__main__":
main()