Corin1998 commited on
Commit
102291f
·
verified ·
1 Parent(s): 0cb50de

Create chat.py

Browse files
Files changed (1) hide show
  1. app/lib/chat.py +28 -0
app/lib/chat.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Dict, Any
2
+ from openai import OpenAI
3
+ from ..config import settings
4
+
5
+ def get_client() -> OpenAI:
6
+ if not settings.openai_api_key:
7
+ raise RuntimeError("OPENAI_API_KEY is not set in the Space (Settings → Variables).")
8
+ return OpenAI(api_key=settings.openai_api_key)
9
+
10
+ def chat_completion(messages: List[Dict[str, str]], model: str | None = None, temperature: float = 0.7) -> Dict[str, Any]:
11
+ client = get_client()
12
+ used_model = model or settings.openai_model
13
+
14
+ resp = client.chat.completions.create(
15
+ model=used_model,
16
+ messages=messages,
17
+ temperature=temperature,
18
+ )
19
+ choice = resp.choices[0]
20
+ return {
21
+ "model": used_model,
22
+ "finish_reason": choice.finish_reason,
23
+ "message": {
24
+ "role": choice.message.role,
25
+ "content": choice.message.content,
26
+ },
27
+ "usage": resp.usage.model_dump() if hasattr(resp, "usage") and resp.usage else None
28
+ }