Yermek68 commited on
Commit
2efe391
·
verified ·
1 Parent(s): 6433069

Add EngineModel abstraction

Browse files
Files changed (1) hide show
  1. engine/model.py +42 -0
engine/model.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import httpx
2
+ from engine.config import EngineConfig
3
+
4
+
5
+ class EngineModel:
6
+
7
+ @staticmethod
8
+ async def generate(prompt: str) -> str:
9
+
10
+ if not EngineConfig.OPENAI_API_KEY:
11
+ return "❌ OPENAI_API_KEY not found"
12
+
13
+ headers = {
14
+ "Authorization": f"Bearer {EngineConfig.OPENAI_API_KEY}",
15
+ "Content-Type": "application/json"
16
+ }
17
+
18
+ payload = {
19
+ "model": EngineConfig.MODEL_NAME,
20
+ "messages": [
21
+ {"role": "system", "content": "You are Eroha, a strategic AI operating core."},
22
+ {"role": "user", "content": prompt}
23
+ ],
24
+ "temperature": EngineConfig.TEMPERATURE
25
+ }
26
+
27
+ try:
28
+ async with httpx.AsyncClient(timeout=30) as client:
29
+ response = await client.post(
30
+ "https://api.openai.com/v1/chat/completions",
31
+ headers=headers,
32
+ json=payload
33
+ )
34
+
35
+ if response.status_code != 200:
36
+ return f"❌ API Error: {response.text}"
37
+
38
+ data = response.json()
39
+ return data["choices"][0]["message"]["content"]
40
+
41
+ except Exception as e:
42
+ return f"❌ Internal error: {str(e)}"