Skip to content

Commit c35d3b1

Browse files
feat: Let the user add their own system prompts (#643)
* feat: Let the user add their own system prompts Related: #454 This PR is not ready yet. For the moment it adds the system prompts to DB and associates it to a workspace. It's missing to use the system prompt and actually send it to the LLM * Finished functionality to add wrkspace system prompt * separated into it's own command system-prompt * Raise WorkspaceDoesNotExistError on update system-prompt * Added show system-prompt command * comment changes * unit test fixes * added some docstrings and mentioned dashboard
1 parent 28af062 commit c35d3b1

File tree

9 files changed

+342
-47
lines changed

9 files changed

+342
-47
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""add_workspace_system_prompt
2+
3+
Revision ID: a692c8b52308
4+
Revises: 5c2f3eee5f90
5+
Create Date: 2025-01-17 16:33:58.464223
6+
7+
"""
8+
9+
from typing import Sequence, Union
10+
11+
from alembic import op
12+
13+
# revision identifiers, used by Alembic.
14+
revision: str = "a692c8b52308"
15+
down_revision: Union[str, None] = "5c2f3eee5f90"
16+
branch_labels: Union[str, Sequence[str], None] = None
17+
depends_on: Union[str, Sequence[str], None] = None
18+
19+
20+
def upgrade() -> None:
21+
# Add column to workspaces table
22+
op.execute("ALTER TABLE workspaces ADD COLUMN system_prompt TEXT DEFAULT NULL;")
23+
24+
25+
def downgrade() -> None:
26+
op.execute("ALTER TABLE workspaces DROP COLUMN system_prompt;")

src/codegate/db/connection.py

+20-6
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,15 @@ async def record_context(self, context: Optional[PipelineContext]) -> None:
248248
except Exception as e:
249249
logger.error(f"Failed to record context: {context}.", error=str(e))
250250

251-
async def add_workspace(self, workspace_name: str) -> Optional[Workspace]:
251+
async def add_workspace(self, workspace_name: str) -> Workspace:
252252
"""Add a new workspace to the DB.
253253
254254
This handles validation and insertion of a new workspace.
255255
256256
It may raise a ValidationError if the workspace name is invalid.
257257
or a AlreadyExistsError if the workspace already exists.
258258
"""
259-
workspace = Workspace(id=str(uuid.uuid4()), name=workspace_name)
260-
259+
workspace = Workspace(id=str(uuid.uuid4()), name=workspace_name, system_prompt=None)
261260
sql = text(
262261
"""
263262
INSERT INTO workspaces (id, name)
@@ -275,6 +274,21 @@ async def add_workspace(self, workspace_name: str) -> Optional[Workspace]:
275274
raise AlreadyExistsError(f"Workspace {workspace_name} already exists.")
276275
return added_workspace
277276

277+
async def update_workspace(self, workspace: Workspace) -> Workspace:
278+
sql = text(
279+
"""
280+
UPDATE workspaces SET
281+
name = :name,
282+
system_prompt = :system_prompt
283+
WHERE id = :id
284+
RETURNING *
285+
"""
286+
)
287+
updated_workspace = await self._execute_update_pydantic_model(
288+
workspace, sql, should_raise=True
289+
)
290+
return updated_workspace
291+
278292
async def update_session(self, session: Session) -> Optional[Session]:
279293
sql = text(
280294
"""
@@ -392,11 +406,11 @@ async def get_workspaces(self) -> List[WorkspaceActive]:
392406
workspaces = await self._execute_select_pydantic_model(WorkspaceActive, sql)
393407
return workspaces
394408

395-
async def get_workspace_by_name(self, name: str) -> List[Workspace]:
409+
async def get_workspace_by_name(self, name: str) -> Optional[Workspace]:
396410
sql = text(
397411
"""
398412
SELECT
399-
id, name
413+
id, name, system_prompt
400414
FROM workspaces
401415
WHERE name = :name
402416
"""
@@ -422,7 +436,7 @@ async def get_active_workspace(self) -> Optional[ActiveWorkspace]:
422436
sql = text(
423437
"""
424438
SELECT
425-
w.id, w.name, s.id as session_id, s.last_update
439+
w.id, w.name, w.system_prompt, s.id as session_id, s.last_update
426440
FROM sessions s
427441
INNER JOIN workspaces w ON w.id = s.active_workspace_id
428442
"""

src/codegate/db/models.py

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Setting(BaseModel):
4343
class Workspace(BaseModel):
4444
id: str
4545
name: str
46+
system_prompt: Optional[str]
4647

4748
@field_validator("name", mode="plain")
4849
@classmethod
@@ -98,5 +99,6 @@ class WorkspaceActive(BaseModel):
9899
class ActiveWorkspace(BaseModel):
99100
id: str
100101
name: str
102+
system_prompt: Optional[str]
101103
session_id: str
102104
last_update: datetime.datetime

src/codegate/pipeline/cli/cli.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
PipelineResult,
99
PipelineStep,
1010
)
11-
from codegate.pipeline.cli.commands import Version, Workspace
11+
from codegate.pipeline.cli.commands import SystemPrompt, Version, Workspace
1212

1313
HELP_TEXT = """
1414
## CodeGate CLI\n
@@ -32,6 +32,7 @@ async def codegate_cli(command):
3232
available_commands = {
3333
"version": Version().exec,
3434
"workspace": Workspace().exec,
35+
"system-prompt": SystemPrompt().exec,
3536
}
3637
out_func = available_commands.get(command[0])
3738
if out_func is None:

0 commit comments

Comments
 (0)