File size: 3,966 Bytes
e5e4bf3
 
98975cb
e5e4bf3
98975cb
e5e4bf3
 
e903db0
 
e5e4bf3
e903db0
98975cb
e5e4bf3
98975cb
e5e4bf3
98975cb
 
e5e4bf3
98975cb
 
 
 
 
 
 
 
 
 
e5e4bf3
98975cb
 
 
 
 
 
 
 
e5e4bf3
e903db0
c152917
e903db0
98975cb
e5e4bf3
e903db0
c152917
 
 
e5e4bf3
c152917
e903db0
98975cb
e5e4bf3
e903db0
c152917
 
 
e5e4bf3
c152917
e903db0
98975cb
e5e4bf3
 
 
 
 
 
 
 
 
 
 
 
98975cb
 
e5e4bf3
 
 
 
 
 
 
 
 
 
98975cb
 
 
 
 
 
e5e4bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98975cb
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""LLM interface — text, vision, and chat."""

import json
import base64
import streamlit as st
from groq import Groq
from app.config import GROQ_API_KEY, MODEL_NAME, VISION_MODEL_NAME, MAX_TOKENS, TEMPERATURE
from app.utils.prompts import (
    EXPLAIN_CODE_WITH_LANG, CLASSIFY_ISSUE_WITH_LANG, PROPOSE_FIX_WITH_LANG,
    build_chat_prompt,
)


@st.cache_resource
def get_client() -> Groq:
    return Groq(api_key=GROQ_API_KEY)


def _call(prompt: str, max_tokens: int = MAX_TOKENS) -> str:
    client = get_client()
    resp = client.chat.completions.create(
        model=MODEL_NAME,
        messages=[{"role": "user", "content": prompt}],
        max_tokens=max_tokens,
        temperature=TEMPERATURE,
    )
    return resp.choices[0].message.content.strip()


def _parse_json(text: str) -> dict:
    try:
        start = text.find("{")
        end = text.rfind("}") + 1
        return json.loads(text[start:end])
    except Exception:
        return {"error": "Parse failed", "raw": text[:200]}


def explain_code(code: str, language: str = "python") -> dict:
    prompt = EXPLAIN_CODE_WITH_LANG.format(language=language, code=code)
    return _parse_json(_call(prompt))


def classify_issue(issue: dict, code: str, language: str = "python") -> dict:
    prompt = CLASSIFY_ISSUE_WITH_LANG.format(
        language=language,
        issue=issue.get("message", ""),
        code=code,
    )
    return _parse_json(_call(prompt, max_tokens=200))


def propose_fix(issue: dict, code: str, language: str = "python") -> str:
    prompt = PROPOSE_FIX_WITH_LANG.format(
        language=language,
        issue=issue.get("message", ""),
        code=code,
    )
    return _call(prompt, max_tokens=512)


def chat_response(
    user_query: str,
    chat_history: list,
    context: str = "",
    linked_files: list = None,
) -> str:
    """
    Chat response with optional file context.
    Injects a clear system note about available files so model
    never claims it cannot see them.
    """
    client = get_client()
    system, messages = build_chat_prompt(chat_history, user_query, context)

    if linked_files:
        file_list = ", ".join(linked_files)
        system += (
            f"\n\n[System: The user has loaded the following files into this session: {file_list}. "
            f"Relevant excerpts from these files are provided above in the context block. "
            f"You have full access to their contents via that context. "
            f"NEVER say you cannot access files or that you are text-based only.]"
        )

    resp = client.chat.completions.create(
        model=MODEL_NAME,
        messages=[{"role": "system", "content": system}] + messages,
        max_tokens=MAX_TOKENS,
        temperature=0.3,
    )
    return resp.choices[0].message.content.strip()


def vision_response(
    user_query: str,
    image_bytes: bytes,
    media_type: str = "image/png",
    context: str = "",
) -> str:
    """Send an image + query to the vision model."""
    client = get_client()
    b64 = base64.b64encode(image_bytes).decode("utf-8")

    system = "You are an expert software engineer and code analyst. Answer questions about code, architecture diagrams, error screenshots, and related technical images clearly and concisely."
    if context:
        system += f"\n\nAdditional context from linked files:\n```\n{context}\n```"

    resp = client.chat.completions.create(
        model=VISION_MODEL_NAME,
        messages=[
            {"role": "system", "content": system},
            {
                "role": "user",
                "content": [
                    {
                        "type": "image_url",
                        "image_url": {"url": f"data:{media_type};base64,{b64}"},
                    },
                    {"type": "text", "text": user_query},
                ],
            },
        ],
        max_tokens=MAX_TOKENS,
        temperature=0.3,
    )
    return resp.choices[0].message.content.strip()