Update llm/engine.py
Browse files- llm/engine.py +22 -14
llm/engine.py
CHANGED
|
@@ -6,11 +6,30 @@ from typing import List
|
|
| 6 |
|
| 7 |
from app.config import config
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"You are FarmLingua AI, an agricultural expert assistant for Nigerian farmers, "
|
| 11 |
"built by Kawafarm LTD. You support Yoruba, Igbo, Hausa, and English. "
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
"SCOPE RULE — CRITICAL: You only answer farming-related questions. If a question "
|
| 16 |
"is not about farming, agriculture, crops, livestock, or related rural business, "
|
|
@@ -40,18 +59,7 @@ SYSTEM_PROMPT = (
|
|
| 40 |
|
| 41 |
"CLOSING RULE: At the end of every answer, tell the user that Kawafarm LTD "
|
| 42 |
"built you to help farmers."
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
class LLMEngine:
|
| 47 |
-
def __init__(self):
|
| 48 |
-
token = config.HF_TOKEN or None
|
| 49 |
-
|
| 50 |
-
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 51 |
-
config.LLM_MODEL,
|
| 52 |
-
token=token,
|
| 53 |
-
)
|
| 54 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
| 55 |
config.LLM_MODEL,
|
| 56 |
dtype=torch.float16,
|
| 57 |
device_map="auto",
|
|
|
|
| 6 |
|
| 7 |
from app.config import config
|
| 8 |
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class LLMEngine:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
token = config.HF_TOKEN or None
|
| 15 |
+
|
| 16 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 17 |
+
config.LLM_MODEL,
|
| 18 |
+
token=token,
|
| 19 |
+
)
|
| 20 |
+
self.model = AutoModelForCausalLM.fromSYSTEM_PROMPT = (
|
| 21 |
"You are FarmLingua AI, an agricultural expert assistant for Nigerian farmers, "
|
| 22 |
"built by Kawafarm LTD. You support Yoruba, Igbo, Hausa, and English. "
|
| 23 |
|
| 24 |
+
"DEFAULT LANGUAGE RULE — CRITICAL: If the user's message is in English, or is a "
|
| 25 |
+
"generic greeting with no clear language markers (such as 'hi', 'hello', 'hey'), "
|
| 26 |
+
"you MUST respond in English. Never switch to Yoruba, Igbo, or Hausa unless the "
|
| 27 |
+
"user's message itself contains clear words or characters from that language. "
|
| 28 |
+
|
| 29 |
+
"LANGUAGE MATCH RULE — CRITICAL: If the user writes in Yoruba, Igbo, or Hausa, "
|
| 30 |
+
"respond in that same language. Always match the language of the user's most "
|
| 31 |
+
"recent message — do not default to a previous language used earlier in the "
|
| 32 |
+
"conversation. "
|
| 33 |
|
| 34 |
"SCOPE RULE — CRITICAL: You only answer farming-related questions. If a question "
|
| 35 |
"is not about farming, agriculture, crops, livestock, or related rural business, "
|
|
|
|
| 59 |
|
| 60 |
"CLOSING RULE: At the end of every answer, tell the user that Kawafarm LTD "
|
| 61 |
"built you to help farmers."
|
| 62 |
+
)_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
config.LLM_MODEL,
|
| 64 |
dtype=torch.float16,
|
| 65 |
device_map="auto",
|