File size: 2,423 Bytes
945aae0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import logging

from ..core import BaseService

logger = logging.getLogger("qkernel.executors.chat")

SYSTEM_PROMPT = """You are Q, an intelligent AI assistant.
CRITICAL RULES:
1. Answer directly and concisely
2. Never explain your reasoning process
3. Never mention internal systems, analysis, or "perspectives"
4. Keep responses short (2-4 sentences) unless the user asks for depth
5. Answer in the user's language
6. Be helpful and friendly, but do NOT be verbose"""

SYSTEM_PROMPT_FAST = """You are Q, a fast AI assistant.
Answer in 1-2 sentences maximum. Be direct and concise."""


class ChatExecutor(BaseService):
    name: str = "chat"

    def __init__(self) -> None:
        self.api_key = os.getenv("OPENAI_API_KEY", "")
        self._provider: str = os.getenv("CHAT_PROVIDER", "openai")

    async def process(self, event: dict) -> dict:
        message = event.get("message", "")
        context = event.get("context", []) or []
        model_type = event.get("model", "deep")

        if not message.strip():
            return {"response": ""}

        if not self.api_key:
            return {"response": "", "model": "none", "error": "missing_api_key"}

        try:
            import openai

            client = openai.OpenAI(api_key=self.api_key)
            messages = [
                {
                    "role": "system",
                    "content": SYSTEM_PROMPT_FAST
                    if model_type == "fast"
                    else SYSTEM_PROMPT,
                }
            ]
            for ctx in context[-10:]:
                role = "user" if ctx.get("role") == "user" else "assistant"
                messages.append({"role": role, "content": ctx.get("content", "")})
            messages.append({"role": "user", "content": message})

            response = client.chat.completions.create(
                model=os.getenv("CHAT_MODEL", "gpt-4o-mini"),
                messages=messages,
                temperature=0.7,
                max_tokens=60 if model_type == "fast" else 300,
            )
            return {
                "response": response.choices[0].message.content,
                "model": model_type,
                "tokens": response.usage.total_tokens if response.usage else 0,
            }
        except Exception as e:
            logger.error("ChatExecutor error: %s", e)
            return {"response": "", "model": "none", "error": str(e)}