ai_se/SystemPrompter.py

76 lines
2.2 KiB
Python

import streamlit as st
from SearchManager import perform_search, parse_results
def generate_prompt(user_message: str) -> str:
if "debug" in st.session_state:
system_content = """\
You are a code-review assistant. You ALWAYS respond with valid JSON and
nothing else — no markdown code fences, no introductory text, no trailing
commentary. Your entire response must be parseable by json.loads().
"""
task = f"""\
<task> {user_message} Follow the schema</task>
<schema>
{{
"summary": "<one-sentence overview of the code quality>",
"bugs": [
{{
"id": 1,
"severity": "Critical|Medium|Style",
"line": <integer line number or null if not applicable>,
"function": "<name of the affected function>",
"description": "<what is wrong and why it matters>",
"fix": "<fix with code snippet>"
}}
],
"overall_quality": "Poor|Fair|Good|Excellent"
}}
</schema>
"""
context = (f" <code> {st.session_state['file_content']} </code>")
if "code_error" in st.session_state:
context += (f" <error> {st.session_state['code_error']} </error>")
if "bugs" in st.session_state:
context += (f" <bugs> {st.session_state['bugs']} </bugs>")
prompt = system_content + task + context
#st.session_state["last_prompt"] = (f"{prompt}")
return prompt
else:
system_content = """\
<persona>
You are an experienced Python coding assistant.
</persona>
<style>
Write python in PEP8 style. Keep explanaitions short.
</style>
<constraints>
Keep each answer under 200 words.
</constraints>
"""
task = (f"<task> {user_message} </task>")
context = ""
if "internet" in st.session_state:
context += (f"<internet_info> {parse_results(perform_search(user_message))} </internet_info>")
if "file_content" in st.session_state:
context += (f" <code> {st.session_state['file_content']} </code>")
if "code_error" in st.session_state:
context += (f" <error> {st.session_state['code_error']} </error>")
prompt = system_content + task + context
#st.session_state["last_prompt"] = (f"{prompt}")
return prompt