tech-envision commited on
Commit
10ac258
·
1 Parent(s): 4b17985

Insert system prompt at runtime

Browse files
Files changed (2) hide show
  1. README.md +3 -2
  2. src/chat.py +11 -11
README.md CHANGED
@@ -11,8 +11,9 @@ conversations can be resumed with context. One example tool is included:
11
  returned. The VM is created when a chat session starts and reused for all
12
  subsequent tool calls.
13
 
14
- The application now injects a system prompt that instructs the model to chain
15
- multiple tools when required. This prompt ensures the assistant can orchestrate
 
16
  tool calls in sequence to satisfy the user's request.
17
 
18
  ## Usage
 
11
  returned. The VM is created when a chat session starts and reused for all
12
  subsequent tool calls.
13
 
14
+ The application injects a system prompt on each request that instructs the
15
+ model to chain multiple tools when required. This prompt is **not** stored in
16
+ the chat history but is provided at runtime so the assistant can orchestrate
17
  tool calls in sequence to satisfy the user's request.
18
 
19
  ## Usage
src/chat.py CHANGED
@@ -50,7 +50,6 @@ class ChatSession:
50
  )
51
  self._vm = None
52
  self._messages: List[Msg] = self._load_history()
53
- self._ensure_system_prompt()
54
 
55
  async def __aenter__(self) -> "ChatSession":
56
  self._vm = VMRegistry.acquire(self._user.username)
@@ -82,18 +81,12 @@ class ChatSession:
82
  add_document(self._user.username, str(target), src.name)
83
  return f"/data/{src.name}"
84
 
85
- def _ensure_system_prompt(self) -> None:
86
- if any(m.get("role") == "system" for m in self._messages):
87
- return
88
-
89
- DBMessage.create(
90
- conversation=self._conversation, role="system", content=SYSTEM_PROMPT
91
- )
92
- self._messages.insert(0, {"role": "system", "content": SYSTEM_PROMPT})
93
-
94
  def _load_history(self) -> List[Msg]:
95
  messages: List[Msg] = []
96
  for msg in self._conversation.messages.order_by(DBMessage.created_at):
 
 
 
97
  if msg.role == "assistant":
98
  try:
99
  calls = json.loads(msg.content)
@@ -124,9 +117,16 @@ class ChatSession:
124
  DBMessage.create(conversation=conversation, role="assistant", content=content)
125
 
126
  async def ask(self, messages: List[Msg], *, think: bool = True) -> ChatResponse:
 
 
 
 
 
 
 
127
  return await self._client.chat(
128
  self._model,
129
- messages=messages,
130
  think=think,
131
  tools=[execute_terminal],
132
  options={"num_ctx": NUM_CTX},
 
50
  )
51
  self._vm = None
52
  self._messages: List[Msg] = self._load_history()
 
53
 
54
  async def __aenter__(self) -> "ChatSession":
55
  self._vm = VMRegistry.acquire(self._user.username)
 
81
  add_document(self._user.username, str(target), src.name)
82
  return f"/data/{src.name}"
83
 
 
 
 
 
 
 
 
 
 
84
  def _load_history(self) -> List[Msg]:
85
  messages: List[Msg] = []
86
  for msg in self._conversation.messages.order_by(DBMessage.created_at):
87
+ if msg.role == "system":
88
+ # Skip persisted system prompts from older versions
89
+ continue
90
  if msg.role == "assistant":
91
  try:
92
  calls = json.loads(msg.content)
 
117
  DBMessage.create(conversation=conversation, role="assistant", content=content)
118
 
119
  async def ask(self, messages: List[Msg], *, think: bool = True) -> ChatResponse:
120
+ """Send a chat request, automatically prepending the system prompt."""
121
+
122
+ if not messages or messages[0].get("role") != "system":
123
+ payload = [{"role": "system", "content": SYSTEM_PROMPT}, *messages]
124
+ else:
125
+ payload = messages
126
+
127
  return await self._client.chat(
128
  self._model,
129
+ messages=payload,
130
  think=think,
131
  tools=[execute_terminal],
132
  options={"num_ctx": NUM_CTX},