Download app/modules/llm.py from rk147/secforge: direct link, hf CLI and curl.
- Browser
- Download file 3.97 kB
-
https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/llm.py
- Command line
-
hf download hf://spaces/rk147/secforge/app/modules/llm.py
-
curl -L -o llm.py https://huggingface.co/spaces/rk147/secforge/resolve/main/app/modules/llm.py
3.97 kB
| """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, | |
| ) | |
| 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() |