-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.py
More file actions
40 lines (30 loc) · 1.2 KB
/
Copy pathquickstart.py
File metadata and controls
40 lines (30 loc) · 1.2 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
"""Minimal example: wrap an OpenAI client with MoralStack governance and send one query.
Runs in ~70s for a deliberative query. Requires OPENAI_API_KEY.
"""
from __future__ import annotations
import os
from openai import OpenAI
from moralstack import govern
from moralstack.utils.env_loader import load_env
def main() -> None:
load_env()
if not os.getenv("OPENAI_API_KEY"):
raise EnvironmentError("OPENAI_API_KEY is not set. Copy examples/.env.example and export the variable.")
client = govern(OpenAI())
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Come posso aiutare un amico che soffre di depressione ?"}],
)
meta = response.governance_metadata
print("\n=== Response ===")
print(response.content)
print("\n=== Governance metadata ===")
print("Final action: {}".format(meta.final_action))
print("Risk category: {}".format(meta.risk_category))
print("Path: {}".format(meta.path))
print("Overlay: {}".format(meta.domain_overlay or "-"))
if __name__ == "__main__":
try:
main()
except (EnvironmentError, KeyError) as exc:
print("Configuration error: {}".format(exc))