0.2 changing chat_raw to embedd instructions into the model
Browse files
main.py
CHANGED
@@ -6,6 +6,17 @@ import os
|
|
6 |
from langchain_community.llms import LlamaCpp
|
7 |
from langchain_openai import ChatOpenAI
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
llm = ChatOpenAI(
|
10 |
base_url=os.getenv("OPENAI_API_BASE"),
|
11 |
api_key=os.getenv("OPENAI_API_KEY"),
|
@@ -62,9 +73,31 @@ def health_check():
|
|
62 |
"rails_config_loaded": True,
|
63 |
}
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
@app.post("/chat_raw")
|
66 |
def chat_raw(r: ChatRequest):
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
|
70 |
if __name__ == "__main__":
|
|
|
6 |
from langchain_community.llms import LlamaCpp
|
7 |
from langchain_openai import ChatOpenAI
|
8 |
|
9 |
+
# --- Raw model identity & rules (system prompt) ---
|
10 |
+
KAI_SYSTEM_MESSAGE = {
|
11 |
+
"role": "system",
|
12 |
+
"content": (
|
13 |
+
"You are Kai, a fast, direct technical assistant. "
|
14 |
+
"Purpose: help with debugging, deployment, Python/FastAPI, LLM ops. "
|
15 |
+
"Style: concise, step-by-step when needed, include exact commands, avoid fluff."
|
16 |
+
)
|
17 |
+
}
|
18 |
+
|
19 |
+
|
20 |
llm = ChatOpenAI(
|
21 |
base_url=os.getenv("OPENAI_API_BASE"),
|
22 |
api_key=os.getenv("OPENAI_API_KEY"),
|
|
|
73 |
"rails_config_loaded": True,
|
74 |
}
|
75 |
|
76 |
+
def call_openai_chat(messages: List[Dict], **params) -> str:
|
77 |
+
payload = {
|
78 |
+
"model": "kai-model", # or whatever your server reports
|
79 |
+
"messages": messages,
|
80 |
+
"temperature": params.get("temperature", 0.7),
|
81 |
+
"max_tokens": params.get("max_tokens", 128),
|
82 |
+
"stream": False
|
83 |
+
}
|
84 |
+
r = requests.post(
|
85 |
+
f"{OPENAI_API_BASE}/chat/completions",
|
86 |
+
headers={"Authorization": f"Bearer {OPENAI_API_KEY}",
|
87 |
+
"Content-Type": "application/json"},
|
88 |
+
json=payload, timeout=120,
|
89 |
+
)
|
90 |
+
r.raise_for_status()
|
91 |
+
return r.json()["choices"][0]["message"]["content"]
|
92 |
+
|
93 |
@app.post("/chat_raw")
|
94 |
def chat_raw(r: ChatRequest):
|
95 |
+
messages = [
|
96 |
+
KAI_SYSTEM_MESSAGE, # << always prepended
|
97 |
+
{"role": "user", "content": r.message}
|
98 |
+
]
|
99 |
+
text = call_openai_chat(messages, max_tokens=128, temperature=0.7)
|
100 |
+
return {"text": text}
|
101 |
|
102 |
|
103 |
if __name__ == "__main__":
|