Spaces:
Sleeping
Sleeping
Create openai_client.py
Browse files- app/openai_client.py +77 -0
app/openai_client.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
import httpx
|
| 5 |
+
|
| 6 |
+
OPENAI_BASE = os.environ.get("OPENAI_BASE", "https://api.openai.com/v1")
|
| 7 |
+
OPENAI_MODEL = os.environ.get("OPENAI_MODEL", "gpt-4o-mini")
|
| 8 |
+
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 9 |
+
|
| 10 |
+
HEADERS = {
|
| 11 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}" if OPENAI_API_KEY else "",
|
| 12 |
+
"Content-Type": "application/json",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
async def openai_chat(messages, temperature: float = 0.7, max_tokens: int = 300) -> str:
|
| 17 |
+
"""自由回答(プレーンテキスト)"""
|
| 18 |
+
if not OPENAI_API_KEY:
|
| 19 |
+
raise RuntimeError("OPENAI_API_KEY is not set in environment variables.")
|
| 20 |
+
|
| 21 |
+
url = f"{OPENAI_BASE}/chat/completions"
|
| 22 |
+
payload = {
|
| 23 |
+
"model": OPENAI_MODEL,
|
| 24 |
+
"messages": messages,
|
| 25 |
+
"temperature": float(temperature),
|
| 26 |
+
"max_tokens": int(max_tokens),
|
| 27 |
+
}
|
| 28 |
+
async with httpx.AsyncClient(timeout=60) as client:
|
| 29 |
+
r = await client.post(url, headers=HEADERS, json=payload)
|
| 30 |
+
r.raise_for_status()
|
| 31 |
+
data = r.json()
|
| 32 |
+
return data["choices"][0]["message"]["content"].strip()
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
async def openai_chat_json(messages, temperature: float = 0.2, max_tokens: int = 1200) -> dict:
|
| 36 |
+
"""JSONモード(出力は厳密なJSON文字列)"""
|
| 37 |
+
if not OPENAI_API_KEY:
|
| 38 |
+
raise RuntimeError("OPENAI_API_KEY is not set in environment variables.")
|
| 39 |
+
|
| 40 |
+
url = f"{OPENAI_BASE}/chat/completions"
|
| 41 |
+
payload = {
|
| 42 |
+
"model": OPENAI_MODEL,
|
| 43 |
+
"messages": messages,
|
| 44 |
+
"temperature": float(temperature),
|
| 45 |
+
"max_tokens": int(max_tokens),
|
| 46 |
+
"response_format": {"type": "json_object"},
|
| 47 |
+
}
|
| 48 |
+
async with httpx.AsyncClient(timeout=60) as client:
|
| 49 |
+
r = await client.post(url, headers=HEADERS, json=payload)
|
| 50 |
+
r.raise_for_status()
|
| 51 |
+
data = r.json()
|
| 52 |
+
content = data["choices"][0]["message"]["content"].strip()
|
| 53 |
+
return json.loads(content)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def openai_judge(system: str, user: str) -> dict:
|
| 57 |
+
"""審査用(JSONモード)。同期呼び出し"""
|
| 58 |
+
if not OPENAI_API_KEY:
|
| 59 |
+
raise RuntimeError("OPENAI_API_KEY is not set in environment variables.")
|
| 60 |
+
|
| 61 |
+
url = f"{OPENAI_BASE}/chat/completions"
|
| 62 |
+
payload = {
|
| 63 |
+
"model": OPENAI_MODEL,
|
| 64 |
+
"messages": [
|
| 65 |
+
{"role": "system", "content": system},
|
| 66 |
+
{"role": "user", "content": user},
|
| 67 |
+
],
|
| 68 |
+
"response_format": {"type": "json_object"},
|
| 69 |
+
"temperature": 0.0,
|
| 70 |
+
"max_tokens": 400,
|
| 71 |
+
}
|
| 72 |
+
with httpx.Client(timeout=60) as client:
|
| 73 |
+
r = client.post(url, headers=HEADERS, json=payload)
|
| 74 |
+
r.raise_for_status()
|
| 75 |
+
data = r.json()
|
| 76 |
+
content = data["choices"][0]["message"]["content"].strip()
|
| 77 |
+
return json.loads(content)
|