rowboat/apps/copilot/copilot.py

105 lines
3.1 KiB
Python
Raw Normal View History

2025-01-13 17:57:37 +05:30
from openai import OpenAI
from flask import Flask, request, jsonify
from pydantic import BaseModel, ValidationError
from typing import List, Dict, Any, Literal, Optional
2025-01-13 17:57:37 +05:30
import json
from lib import AgentContext, PromptContext, ToolContext, ChatContext
from client import PROVIDER_COPILOT_MODEL
from client import completions_client
2025-01-13 17:57:37 +05:30
class UserMessage(BaseModel):
role: Literal["user"]
content: str
class AssistantMessage(BaseModel):
role: Literal["assistant"]
content: str
class DataSource(BaseModel):
2025-05-12 20:40:02 +05:30
_id: str
name: str
description: Optional[str] = None
active: bool = True
status: str # 'pending' | 'ready' | 'error' | 'deleted'
error: Optional[str] = None
data: dict # The discriminated union based on type
2025-04-15 00:56:15 +05:30
with open('copilot_edit_agent.md', 'r', encoding='utf-8') as file:
copilot_instructions_edit_agent = file.read()
2025-01-13 17:57:37 +05:30
def get_response(
messages: List[UserMessage | AssistantMessage],
workflow_schema: str,
current_workflow_config: str,
2025-02-20 15:48:17 +05:30
context: AgentContext | PromptContext | ToolContext | ChatContext | None = None,
dataSources: Optional[List[DataSource]] = None,
2025-04-15 00:56:15 +05:30
copilot_instructions: str = copilot_instructions_edit_agent
2025-01-13 17:57:37 +05:30
) -> str:
# if context is provided, create a prompt for the context
if context:
2025-01-22 14:21:22 +05:30
match context:
case AgentContext():
context_prompt = f"""
2025-01-13 17:57:37 +05:30
**NOTE**: The user is currently working on the following agent:
{context.agentName}
"""
2025-01-22 14:21:22 +05:30
case PromptContext():
context_prompt = f"""
2025-01-13 17:57:37 +05:30
**NOTE**: The user is currently working on the following prompt:
{context.promptName}
"""
2025-01-22 14:21:22 +05:30
case ToolContext():
context_prompt = f"""
2025-01-13 17:57:37 +05:30
**NOTE**: The user is currently working on the following tool:
{context.toolName}
"""
2025-01-22 14:21:22 +05:30
case ChatContext():
context_prompt = f"""
2025-01-13 17:57:37 +05:30
**NOTE**: The user has just tested the following chat using the workflow above and has provided feedback / question below this json dump:
```json
{json.dumps(context.messages)}
```
"""
else:
context_prompt = ""
# Add dataSources to the context if provided
data_sources_prompt = ""
if dataSources:
data_sources_prompt = f"""
**NOTE**: The following data sources are available:
```json
{json.dumps([ds.model_dump() for ds in dataSources])}
```
"""
2025-01-13 17:57:37 +05:30
# add the workflow schema to the system prompt
sys_prompt = copilot_instructions.replace("{workflow_schema}", workflow_schema)
# add the current workflow config to the last user message
last_message = messages[-1]
last_message.content = f"""
Context:
The current workflow config is:
```
{current_workflow_config}
```
{context_prompt}
{data_sources_prompt}
2025-01-13 17:57:37 +05:30
User: {last_message.content}
"""
updated_msgs = [{"role": "system", "content": sys_prompt}] + [
message.model_dump() for message in messages
]
response = completions_client.chat.completions.create(
model=PROVIDER_COPILOT_MODEL,
2025-01-13 17:57:37 +05:30
messages=updated_msgs,
response_format={"type": "json_object"}
)
return response.choices[0].message.content