-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.py
More file actions
106 lines (93 loc) · 3.99 KB
/
Copy pathresponse.py
File metadata and controls
106 lines (93 loc) · 3.99 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# ============================== #
# CodeBot Response #
# ============================== #
# --- Imports ---
import os
from dotenv import load_dotenv
import cohere
from db import get_user_history
import asyncio
# --- Load Environment Variables ---
load_dotenv()
instr_query : str = os.getenv("SYSTEM_INSTR_MSG") # Instruction for regular queries
instr_dbg : str = os.getenv("SYSTEM_INSTR_DBT") # Instruction for debugging
instr_strict: str = os.getenv("SYSTEM_INSTR_STRICT") # Extra strict enforcement prompt
# --- Strict Response ---
# Parse boolean from env
strict: bool = os.getenv("STRICT", "true").lower() in ("true", "1", "yes", "y")
# Track original (unmodified) instructions to avoid appending repeatedly
_base_query = instr_query
_base_dbg = instr_dbg
# Update strictness flag and system instructions
async def set_strict(value: str) -> str:
global strict, instr_query, instr_dbg
strict = value.lower() not in ("n", "no", "nah", "nahh")
instr_query = _base_query + (instr_strict if strict else "")
instr_dbg = _base_dbg + (instr_strict if strict else "")
return "Strictness has been enabled" if strict else "Strictness has been disabled"
# --- Initialize Cohere Client (Singleton) ---
client = cohere.ClientV2(api_key=os.getenv("AI_API_KEY"))
# --- Helper: Build Prompt with User History ---
async def build_prompt(prompt: str, user_id: int) -> str:
history = "".join(await get_user_history(user_id)) # Retrieve past interactions
full_prompt = f"{history}\nQ: {prompt}\nA:" if history else f"Q: {prompt}\nA:"
return full_prompt
# --- Query Handler (General Code Help) ---
async def query(prompt: str, user_id: int) -> str:
full_prompt = await build_prompt(prompt, user_id)
response = client.chat(
model="command-a-03-2025",
messages=[
{"role": "system", "content": instr_query + instr_strict },
{"role": "user", "content": full_prompt},
],
)
return response.message.content[0].text
# --- Debug Handler (Bug Fixing / Review) ---
async def debug(prompt: str, user_id: int) -> str:
full_prompt = await build_prompt(prompt, user_id)
response = client.chat(
model="command-a-03-2025",
messages=[
{"role": "system", "content": instr_dbg + instr_strict },
{"role": "user", "content": full_prompt},
],
)
return response.message.content[0].text
# --- Resource Finder (Learning Links) ---
async def resources(topic: str, n: int) -> str:
response = client.chat(
model="command-a-03-2025",
messages=[
{"role": "system", "content": instr_query },
{"role": "user", "content": f"Give me links to at least {n} resources related to {topic}."}
],
)
return response.message.content[0].text
# --- Tip Generator (Coding Advice) ---
async def tips(topic: str, n: int) -> str:
response = client.chat(
model="command-a-03-2025",
messages=[
{"role": "system", "content": instr_query },
{"role": "user", "content": f"Give me {n} random tips related to {topic}."}
],
)
return response.message.content[0].text
# --- Help Menu Text ---
def chelp():
return (
"**📌 Available Commands:**\n"
"• `📩 /cph <query>` — *DMs you personalized help*\n"
"• `🌐 /coh <query>` — *Public help in this channel*\n"
"• `🐞 /dbg <code>` — *Analyze and improve your code*\n"
"• `🔍 /debug <code>` — *Find potential bugs in code*\n"
"• `📚 /resources <topic>` — *Get curated learning material*\n"
"• `📝 /tips <topic>` — *Receive a random tip related to topic*\n"
"• `🆘 /chelp` — *Display this help message menu*\n\n"
"• `🆘 /strict` — *Toggles the strictness of the bot {Default : True}*\n"
"**Type a command to get started. Happy coding!**"
)
# --- Error Fallback Text ---
def error():
return "⚠️ An error occurred while processing your request."