File size: 2,345 Bytes
6f14d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import StreamingResponse, PlainTextResponse
from pydantic import BaseModel, Field
from typing import List, Literal
from app.helpers.plan_chat import ask_plan_question
from app.helpers.token_auth import get_token
from app.helpers.get_current_uesr import get_user_from_token
from app.models.project import Project
from app.helpers.vectorization import search_similar

router = APIRouter()

class HistoryItem(BaseModel):
    message: str
    from_: Literal["user", "ai"]

class PlanChatPayload(BaseModel):
    query: str
    history: List[HistoryItem]
    project_id: str

@router.post("/plan-chat")
async def plan_chat(data: PlanChatPayload, token: str = Depends(get_token)):
    """
    Handle chat messages for plan generation with context from scraped content
    """
    try:
        # Validate user
        user = await get_user_from_token(token=token)
        if not user:
            raise HTTPException(status_code=401, detail="Invalid token")

        # Get project context
        project = await Project.get_or_none(id=data.project_id)
        if not project:
            raise HTTPException(status_code=404, detail="Project not found")

        # Get relevant context from vectorstore
        context = await search_similar(data.query)

        # Prepare system prompt
        system_prompt = (
            "You are a solution architect assistant specialized in cloud architecture. "
            "Use the following context to help answer questions about the project plan. "
            "Focus on providing specific, actionable advice based on the project requirements "
            "and scraped documentation.\n\n"
            f"Project Context: {context}\n"
            f"Project Requirements: {project.requirements}\n"
            f"Project Features: {project.features}\n"
            f"Solution Stack: {project.solution_stack}\n"
        )

        async def response_stream():
            async for chunk in ask_plan_question(
                question=data.query,
                history=data.history,
                project_context=system_prompt
            ):
                yield chunk

        return StreamingResponse(response_stream(), media_type="text/plain")

    except Exception as e:
        return PlainTextResponse(str(e), status_code=500)