Update app/modules/llm.py
Browse files- app/modules/llm.py +72 -10
app/modules/llm.py
CHANGED
|
@@ -1,16 +1,21 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import json
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
-
from
|
|
|
|
| 5 |
from app.utils.prompts import (
|
| 6 |
EXPLAIN_CODE_WITH_LANG, CLASSIFY_ISSUE_WITH_LANG, PROPOSE_FIX_WITH_LANG,
|
| 7 |
-
build_chat_prompt
|
| 8 |
)
|
| 9 |
|
|
|
|
| 10 |
@st.cache_resource
|
| 11 |
-
def get_client():
|
| 12 |
return Groq(api_key=GROQ_API_KEY)
|
| 13 |
|
|
|
|
| 14 |
def _call(prompt: str, max_tokens: int = MAX_TOKENS) -> str:
|
| 15 |
client = get_client()
|
| 16 |
resp = client.chat.completions.create(
|
|
@@ -21,6 +26,7 @@ def _call(prompt: str, max_tokens: int = MAX_TOKENS) -> str:
|
|
| 21 |
)
|
| 22 |
return resp.choices[0].message.content.strip()
|
| 23 |
|
|
|
|
| 24 |
def _parse_json(text: str) -> dict:
|
| 25 |
try:
|
| 26 |
start = text.find("{")
|
|
@@ -29,36 +35,92 @@ def _parse_json(text: str) -> dict:
|
|
| 29 |
except Exception:
|
| 30 |
return {"error": "Parse failed", "raw": text[:200]}
|
| 31 |
|
|
|
|
| 32 |
def explain_code(code: str, language: str = "python") -> dict:
|
| 33 |
-
"""Explain code — always uses the language-aware prompt."""
|
| 34 |
prompt = EXPLAIN_CODE_WITH_LANG.format(language=language, code=code)
|
| 35 |
return _parse_json(_call(prompt))
|
| 36 |
|
|
|
|
| 37 |
def classify_issue(issue: dict, code: str, language: str = "python") -> dict:
|
| 38 |
-
"""Classify issue — always uses the language-aware prompt."""
|
| 39 |
prompt = CLASSIFY_ISSUE_WITH_LANG.format(
|
| 40 |
language=language,
|
| 41 |
issue=issue.get("message", ""),
|
| 42 |
-
code=code
|
| 43 |
)
|
| 44 |
return _parse_json(_call(prompt, max_tokens=200))
|
| 45 |
|
|
|
|
| 46 |
def propose_fix(issue: dict, code: str, language: str = "python") -> str:
|
| 47 |
-
"""Propose fix — always uses the language-aware prompt."""
|
| 48 |
prompt = PROPOSE_FIX_WITH_LANG.format(
|
| 49 |
language=language,
|
| 50 |
issue=issue.get("message", ""),
|
| 51 |
-
code=code
|
| 52 |
)
|
| 53 |
return _call(prompt, max_tokens=512)
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
client = get_client()
|
| 57 |
system, messages = build_chat_prompt(chat_history, user_query, context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
resp = client.chat.completions.create(
|
| 59 |
model=MODEL_NAME,
|
| 60 |
messages=[{"role": "system", "content": system}] + messages,
|
| 61 |
max_tokens=MAX_TOKENS,
|
| 62 |
temperature=0.3,
|
| 63 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return resp.choices[0].message.content.strip()
|
|
|
|
| 1 |
+
"""LLM interface — text, vision, and chat."""
|
| 2 |
+
|
| 3 |
import json
|
| 4 |
+
import base64
|
| 5 |
import streamlit as st
|
| 6 |
+
from groq import Groq
|
| 7 |
+
from app.config import GROQ_API_KEY, MODEL_NAME, VISION_MODEL_NAME, MAX_TOKENS, TEMPERATURE
|
| 8 |
from app.utils.prompts import (
|
| 9 |
EXPLAIN_CODE_WITH_LANG, CLASSIFY_ISSUE_WITH_LANG, PROPOSE_FIX_WITH_LANG,
|
| 10 |
+
build_chat_prompt,
|
| 11 |
)
|
| 12 |
|
| 13 |
+
|
| 14 |
@st.cache_resource
|
| 15 |
+
def get_client() -> Groq:
|
| 16 |
return Groq(api_key=GROQ_API_KEY)
|
| 17 |
|
| 18 |
+
|
| 19 |
def _call(prompt: str, max_tokens: int = MAX_TOKENS) -> str:
|
| 20 |
client = get_client()
|
| 21 |
resp = client.chat.completions.create(
|
|
|
|
| 26 |
)
|
| 27 |
return resp.choices[0].message.content.strip()
|
| 28 |
|
| 29 |
+
|
| 30 |
def _parse_json(text: str) -> dict:
|
| 31 |
try:
|
| 32 |
start = text.find("{")
|
|
|
|
| 35 |
except Exception:
|
| 36 |
return {"error": "Parse failed", "raw": text[:200]}
|
| 37 |
|
| 38 |
+
|
| 39 |
def explain_code(code: str, language: str = "python") -> dict:
|
|
|
|
| 40 |
prompt = EXPLAIN_CODE_WITH_LANG.format(language=language, code=code)
|
| 41 |
return _parse_json(_call(prompt))
|
| 42 |
|
| 43 |
+
|
| 44 |
def classify_issue(issue: dict, code: str, language: str = "python") -> dict:
|
|
|
|
| 45 |
prompt = CLASSIFY_ISSUE_WITH_LANG.format(
|
| 46 |
language=language,
|
| 47 |
issue=issue.get("message", ""),
|
| 48 |
+
code=code,
|
| 49 |
)
|
| 50 |
return _parse_json(_call(prompt, max_tokens=200))
|
| 51 |
|
| 52 |
+
|
| 53 |
def propose_fix(issue: dict, code: str, language: str = "python") -> str:
|
|
|
|
| 54 |
prompt = PROPOSE_FIX_WITH_LANG.format(
|
| 55 |
language=language,
|
| 56 |
issue=issue.get("message", ""),
|
| 57 |
+
code=code,
|
| 58 |
)
|
| 59 |
return _call(prompt, max_tokens=512)
|
| 60 |
|
| 61 |
+
|
| 62 |
+
def chat_response(
|
| 63 |
+
user_query: str,
|
| 64 |
+
chat_history: list,
|
| 65 |
+
context: str = "",
|
| 66 |
+
linked_files: list = None,
|
| 67 |
+
) -> str:
|
| 68 |
+
"""
|
| 69 |
+
Chat response with optional file context.
|
| 70 |
+
Injects a clear system note about available files so model
|
| 71 |
+
never claims it cannot see them.
|
| 72 |
+
"""
|
| 73 |
client = get_client()
|
| 74 |
system, messages = build_chat_prompt(chat_history, user_query, context)
|
| 75 |
+
|
| 76 |
+
if linked_files:
|
| 77 |
+
file_list = ", ".join(linked_files)
|
| 78 |
+
system += (
|
| 79 |
+
f"\n\n[System: The user has loaded the following files into this session: {file_list}. "
|
| 80 |
+
f"Relevant excerpts from these files are provided above in the context block. "
|
| 81 |
+
f"You have full access to their contents via that context. "
|
| 82 |
+
f"NEVER say you cannot access files or that you are text-based only.]"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
resp = client.chat.completions.create(
|
| 86 |
model=MODEL_NAME,
|
| 87 |
messages=[{"role": "system", "content": system}] + messages,
|
| 88 |
max_tokens=MAX_TOKENS,
|
| 89 |
temperature=0.3,
|
| 90 |
)
|
| 91 |
+
return resp.choices[0].message.content.strip()
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def vision_response(
|
| 95 |
+
user_query: str,
|
| 96 |
+
image_bytes: bytes,
|
| 97 |
+
media_type: str = "image/png",
|
| 98 |
+
context: str = "",
|
| 99 |
+
) -> str:
|
| 100 |
+
"""Send an image + query to the vision model."""
|
| 101 |
+
client = get_client()
|
| 102 |
+
b64 = base64.b64encode(image_bytes).decode("utf-8")
|
| 103 |
+
|
| 104 |
+
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."
|
| 105 |
+
if context:
|
| 106 |
+
system += f"\n\nAdditional context from linked files:\n```\n{context}\n```"
|
| 107 |
+
|
| 108 |
+
resp = client.chat.completions.create(
|
| 109 |
+
model=VISION_MODEL_NAME,
|
| 110 |
+
messages=[
|
| 111 |
+
{"role": "system", "content": system},
|
| 112 |
+
{
|
| 113 |
+
"role": "user",
|
| 114 |
+
"content": [
|
| 115 |
+
{
|
| 116 |
+
"type": "image_url",
|
| 117 |
+
"image_url": {"url": f"data:{media_type};base64,{b64}"},
|
| 118 |
+
},
|
| 119 |
+
{"type": "text", "text": user_query},
|
| 120 |
+
],
|
| 121 |
+
},
|
| 122 |
+
],
|
| 123 |
+
max_tokens=MAX_TOKENS,
|
| 124 |
+
temperature=0.3,
|
| 125 |
+
)
|
| 126 |
return resp.choices[0].message.content.strip()
|