跳转至

Dynamic Prompts Implementation

Yes, absolutely. In fact, this is one of the most important "Best Practices" in AI engineering.

Hardcoding prompts inside your main application logic (e.g., inside app.py) is messy. It makes the code hard to read and makes it difficult to tweak the AI's personality without breaking the app.

Here is the professional way to architect this using a Prompt Manager.

1. The Strategy: Create a prompts.py File

Instead of cluttering your logic, you create a dedicated file just for your "AI Instructions."

File Structure:

/my_app
  ├── app.py              # The UI and Logic
  ├── database.py         # The FalkorDBLite connection
  └── prompts.py          # ALL textual instructions for the AI live here

2. The Code Implementation

Step A: Write prompts.py

In this file, you write simple Python functions that take variables (like the topic or the student_answer) and return the formatted prompt string.

# prompts.py

def get_lecture_prompt(topic, context_data):
    """
    Generates the system instruction for the teaching phase.
    """
    return f"""
    You are The Professor, a world-class physicist and educator.

    TASK:
    Teach the concept of '{topic}' to a university student.

    CONTEXT (Use this source material):
    {context_data}

    GUIDELINES:
    1. Use the Feynman Technique (simple language, analogies).
    2. Be concise. Keep it under 300 words.
    3. Do not just list facts; explain the 'Why' and 'How'.

    OUTPUT FORMAT:
    Return a JSON object with:
    - "script": (The spoken text)
    - "mermaid_code": (A valid Mermaid.js flowchart code representing the concept)
    """

def get_grader_prompt(student_explanation, correct_answer):
    """
    Generates the instruction for the grading phase.
    """
    return f"""
    You are a strict but fair Grader.

    SOURCE TRUTH:
    {correct_answer}

    STUDENT EXPLANATION:
    {student_explanation}

    TASK:
    Compare the student's explanation to the source truth.

    OUTPUT FORMAT:
    Return a JSON object with:
    - "score": (Integer 0-100)
    - "feedback": (One sentence on what they missed)
    - "status": ("PASS" or "FAIL")
    """

Step B: Use it in app.py

Now your main code looks incredibly clean. You import the functions and use them.

# app.py
import prompts # Import your new file
from ollama import Client

client = Client()

def run_class(topic):
    # 1. Get the data (Simulated)
    context = "Quantum entanglement is..." 

    # 2. Get the Prompt (CLEAN!)
    # We just call the function. We don't see the ugly text block here.
    system_instruction = prompts.get_lecture_prompt(topic, context)

    # 3. Call the LLM
    response = client.generate(model="qwen2.5:32b", prompt=system_instruction)

    return response

3. Why this is better for "The Professor"

1. A/B Testing is Easy If you want to try a "Socratic" teaching style instead of a "Feynman" style, you don't touch your app logic. You just go to prompts.py and change the text inside get_lecture_prompt.

2. Dynamic Personas You can add a parameter to change the vibe instantly:

def get_lecture_prompt(topic, style="stern"):
    if style == "stern":
        tone = "Be rigorous, academic, and tolerate no ambiguity."
    else:
        tone = "Be chill, use slang, and act like a supportive friend."

    return f"You are a tutor. {tone} Explain {topic}..."

3. Debugging is Faster If the AI is outputting bad JSON or weird diagrams, you know exactly where to look (prompts.py). You don't have to scroll through hundreds of lines of UI code to find the prompt string.

Recommendation

Tell Google Antigravity:

"Refactor the code. Move all LLM prompts into a separate file named prompts.py and import them as functions into the main script."