maigp-openai 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
.venv/
|
|
8
|
+
.eggs/
|
|
9
|
+
|
|
10
|
+
# Node
|
|
11
|
+
node_modules/
|
|
12
|
+
dist/
|
|
13
|
+
|
|
14
|
+
# Go
|
|
15
|
+
bin/
|
|
16
|
+
|
|
17
|
+
# Rust
|
|
18
|
+
target/
|
|
19
|
+
|
|
20
|
+
# .NET
|
|
21
|
+
bin/
|
|
22
|
+
obj/
|
|
23
|
+
|
|
24
|
+
# Java
|
|
25
|
+
*.class
|
|
26
|
+
out/
|
|
27
|
+
|
|
28
|
+
# IDE
|
|
29
|
+
.idea/
|
|
30
|
+
.vscode/
|
|
31
|
+
.kiro/
|
|
32
|
+
*.swp
|
|
33
|
+
|
|
34
|
+
# OS
|
|
35
|
+
.DS_Store
|
|
36
|
+
Thumbs.db
|
|
37
|
+
|
|
38
|
+
# Secrets (never commit)
|
|
39
|
+
.env
|
|
40
|
+
*.pem
|
|
41
|
+
*.key
|
|
42
|
+
*.pdf
|
|
43
|
+
BACKLOG/
|
|
44
|
+
patent/
|
|
45
|
+
docs/.archive/
|
|
46
|
+
review/
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""AIGP-OpenAI — Governed wrapper for direct OpenAI API calls."""
|
|
2
|
+
|
|
3
|
+
__version__ = "1.0.0"
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import time
|
|
7
|
+
from aigp_agent_core import AgentGovernance
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class GovernedOpenAI:
|
|
11
|
+
"""Wrap OpenAI client with AIGP governance.
|
|
12
|
+
|
|
13
|
+
Usage:
|
|
14
|
+
from openai import OpenAI
|
|
15
|
+
from aigp_openai import GovernedOpenAI
|
|
16
|
+
|
|
17
|
+
governed = GovernedOpenAI(
|
|
18
|
+
client=OpenAI(),
|
|
19
|
+
gov_url="https://your-gov-app.com",
|
|
20
|
+
app_id="my-openai-app",
|
|
21
|
+
hmac_secret="secret",
|
|
22
|
+
)
|
|
23
|
+
response = governed.chat("gpt-4o", [{"role": "user", "content": "Hello"}])
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, client, gov_url: str, app_id: str, hmac_secret: str, **kwargs):
|
|
27
|
+
self._client = client
|
|
28
|
+
self._gov = AgentGovernance(gov_url, app_id, hmac_secret, **kwargs)
|
|
29
|
+
|
|
30
|
+
def chat(self, model: str, messages: list, use_case: str = "chat", user_id: str = "", **kwargs):
|
|
31
|
+
"""Governed chat completion — CHECK before, RECORD after."""
|
|
32
|
+
self._gov.pre_invoke(use_case, model, user_id)
|
|
33
|
+
try:
|
|
34
|
+
response = self._client.chat.completions.create(model=model, messages=messages, **kwargs)
|
|
35
|
+
usage = {}
|
|
36
|
+
if response.usage:
|
|
37
|
+
usage = {"input_tokens": response.usage.prompt_tokens, "output_tokens": response.usage.completion_tokens}
|
|
38
|
+
self._gov.on_model_call(usage=usage)
|
|
39
|
+
self._run_async(self._gov.post_invoke())
|
|
40
|
+
return response
|
|
41
|
+
except Exception as e:
|
|
42
|
+
self._run_async(self._gov.on_error(e))
|
|
43
|
+
raise
|
|
44
|
+
|
|
45
|
+
async def achat(self, model: str, messages: list, use_case: str = "chat", user_id: str = "", **kwargs):
|
|
46
|
+
"""Async governed chat completion."""
|
|
47
|
+
self._gov.pre_invoke(use_case, model, user_id)
|
|
48
|
+
try:
|
|
49
|
+
response = await self._client.chat.completions.create(model=model, messages=messages, **kwargs)
|
|
50
|
+
usage = {}
|
|
51
|
+
if response.usage:
|
|
52
|
+
usage = {"input_tokens": response.usage.prompt_tokens, "output_tokens": response.usage.completion_tokens}
|
|
53
|
+
self._gov.on_model_call(usage=usage)
|
|
54
|
+
await self._gov.post_invoke()
|
|
55
|
+
return response
|
|
56
|
+
except Exception as e:
|
|
57
|
+
await self._gov.on_error(e)
|
|
58
|
+
raise
|
|
59
|
+
|
|
60
|
+
def _run_async(self, coro):
|
|
61
|
+
try:
|
|
62
|
+
asyncio.get_event_loop().run_until_complete(coro)
|
|
63
|
+
except RuntimeError:
|
|
64
|
+
asyncio.run(coro)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "maigp-openai"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "AIGP governance adapter for openai"
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = ["maigp-agent-core>=1.0.0"]
|
|
11
|
+
|
|
12
|
+
[tool.hatch.build.targets.wheel]
|
|
13
|
+
packages = ["aigp_openai"]
|