Spaces:
Sleeping
Sleeping
Create src/macg/llm_openai.py
Browse files- src/macg/llm_openai.py +40 -0
src/macg/llm_openai.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
from macg.llm_base import LLMClient
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class OpenAIResponsesLLM(LLMClient):
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
model: str = "gpt-5",
|
| 13 |
+
api_key: str | None = None,
|
| 14 |
+
max_output_tokens: int = 900,
|
| 15 |
+
temperature: float = 0.2,
|
| 16 |
+
base_url: str | None = None,
|
| 17 |
+
) -> None:
|
| 18 |
+
self.model = model
|
| 19 |
+
self.api_key = api_key or os.getenv("OPENAI_API_KEY")
|
| 20 |
+
if not self.api_key:
|
| 21 |
+
raise ValueError("OPENAI_API_KEY is not set.")
|
| 22 |
+
|
| 23 |
+
self.max_output_tokens = max_output_tokens
|
| 24 |
+
self.temperature = temperature
|
| 25 |
+
|
| 26 |
+
if base_url:
|
| 27 |
+
self.client = OpenAI(api_key=self.api_key, base_url=base_url)
|
| 28 |
+
else:
|
| 29 |
+
self.client = OpenAI(api_key=self.api_key)
|
| 30 |
+
|
| 31 |
+
def complete(self, system: str, prompt: str) -> str:
|
| 32 |
+
# Uses the Responses API
|
| 33 |
+
resp = self.client.responses.create(
|
| 34 |
+
model=self.model,
|
| 35 |
+
instructions=system,
|
| 36 |
+
input=prompt,
|
| 37 |
+
max_output_tokens=self.max_output_tokens,
|
| 38 |
+
temperature=self.temperature,
|
| 39 |
+
)
|
| 40 |
+
return resp.output_text
|