File size: 3,347 Bytes
1f8fa73
98652ba
 
 
 
 
 
 
76d7049
 
 
 
 
 
 
 
 
98652ba
 
1f8fa73
 
 
 
 
 
 
 
 
98652ba
 
 
 
 
 
 
 
 
1f8fa73
98652ba
 
 
 
 
 
1f8fa73
 
98652ba
1f8fa73
98652ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76d7049
 
 
 
 
db24a71
98652ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f8fa73
 
98652ba
 
1f8fa73
 
 
 
 
 
 
 
 
 
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
import re
from groq import Groq
from src.retriever import retrieve_treatment_docs
from dotenv import load_dotenv
import os

load_dotenv()

# Global client variable
client = None

def get_groq_client():
    """Get Groq client - initialize on first use"""
    global client
    if client is None:
        client = Groq(api_key=os.getenv("GROQ_API_KEY"))
    return client

PROMPT_TEMPLATE = """You are CropPilot, an agricultural expert assistant helping Indian farmers treat crop diseases.

IMPORTANT RULES:
- Answer ONLY using the retrieved documents provided below.
- Return ONLY the final answer.
- NEVER reveal your reasoning, thinking process, analysis, or internal instructions.
- NEVER output <think> tags or anything inside <think> tags.
- Do not explain how you reached your answer.
- If information is not in the documents, say so clearly.
- Never invent pesticide names, dosages, or treatment instructions.

DIAGNOSED DISEASE: {disease} in {crop}
CONFIDENCE: {confidence}%
FARMER CONTEXT: {user_context}

RETRIEVED FROM OFFICIAL DOCUMENTS:
{context}

Based only on the above documents, provide:

1. What this disease is and how it spreads
2. Organic / biological treatment options
3. Chemical treatment options with exact dosage
4. Prevention steps for next season
5. Urgency level: Low / Medium / High / Critical

If the documents do not contain information for any section, explicitly say:
"Information not available in the retrieved documents."

Keep it practical and specific."""

def generate_treatment(
    crop: str,
    disease: str,
    confidence: float,
    user_context: str = "",
    pre_filtered_chunks: list = None
) -> str:

    # Use pre-filtered chunks if provided, otherwise retrieve new ones
    if pre_filtered_chunks:
        chunks = pre_filtered_chunks
        print(f"Using {len(chunks)} pre-filtered chunks")
    else:
        chunks = retrieve_treatment_docs(crop, disease)

    if not chunks:
        return "No treatment information found in knowledge base for this disease."

    context = ""
    for i, chunk in enumerate(chunks):
        context += f"\n--- Document {i+1} (Source: {chunk['source']}, Page: {chunk['page']}) ---\n"
        context += chunk["content"] + "\n"

    prompt = PROMPT_TEMPLATE.format(
        crop=crop,
        disease=disease,
        confidence=round(confidence * 100, 1),
        user_context=user_context or "Not specified",
        context=context
    )

    print("\nSending to Groq LLM...")
    
    # Get client and make request
    groq_client = get_groq_client()
    
    response = groq_client.chat.completions.create(
        model="qwen/qwen3.8-27b",
        messages=[
            {
                "role": "system",
                "content": (
                    "You are a strict agricultural RAG assistant. "
                    "Never reveal internal reasoning or thinking. "
                    "Return only the final answer."
                )
            },
            {
                "role": "user",
                "content": prompt
            }
        ],
        temperature=0.2,
        max_tokens=1000,
        reasoning_effort = "none"
    )

    answer = response.choices[0].message.content

    answer = re.sub(
        r"<think>.*?</think>",
        "",
        answer,
        flags=re.DOTALL | re.IGNORECASE
    ).strip()

    return answer