Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -777,35 +777,37 @@ def groq_with_memory(message: str, topic: str = "general", retries: int = 3) ->
|
|
| 777 |
messages = [{"role": "user", "content": message.strip()}]
|
| 778 |
|
| 779 |
# Deteksi gaya tokoh dari prompt
|
| 780 |
-
style_instruction = "
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
|
|
|
|
|
|
| 798 |
|
| 799 |
payload = {
|
| 800 |
"model": "meta-llama/llama-4-maverick-17b-128e-instruct",
|
| 801 |
"messages": [
|
| 802 |
{
|
| 803 |
"role": "system",
|
| 804 |
-
"content": f"Kamu asisten
|
| 805 |
}
|
| 806 |
] + messages,
|
| 807 |
-
"max_tokens":
|
| 808 |
-
"temperature":
|
| 809 |
"frequency_penalty": 0.7,
|
| 810 |
"presence_penalty": 0.8,
|
| 811 |
"top_p": 0.95
|
|
@@ -821,20 +823,20 @@ def groq_with_memory(message: str, topic: str = "general", retries: int = 3) ->
|
|
| 821 |
response_content = result["choices"][0]["message"]["content"]
|
| 822 |
storage.add_message("assistant", response_content, topic)
|
| 823 |
return response_content, ""
|
| 824 |
-
return f"
|
| 825 |
except HTTPError as e:
|
| 826 |
if response.status_code == 429 and attempt < retries - 1:
|
| 827 |
sleep(2 ** attempt)
|
| 828 |
continue
|
| 829 |
-
return f"
|
| 830 |
|
| 831 |
return "❌ Max retries exceeded", ""
|
| 832 |
|
| 833 |
except Exception as e:
|
| 834 |
-
return f"Error: {str(e)}", ""
|
| 835 |
-
|
| 836 |
|
| 837 |
|
|
|
|
| 838 |
def cleanup_old_messages(days: int = 30) -> str:
|
| 839 |
try:
|
| 840 |
cutoff = (datetime.now() - timedelta(days=days)).isoformat()
|
|
|
|
| 777 |
messages = [{"role": "user", "content": message.strip()}]
|
| 778 |
|
| 779 |
# Deteksi gaya tokoh dari prompt
|
| 780 |
+
style_instruction = "santai, cerdas, conversational, kayak ngobrol sama temen"
|
| 781 |
+
style_match = re.search(r"gaya\s+([\w\s]+)", message.lower(), re.IGNORECASE)
|
| 782 |
+
if style_match:
|
| 783 |
+
style_name = style_match.group(1).strip()
|
| 784 |
+
if "schreiter" in style_name:
|
| 785 |
+
style_instruction = "gaya Tom Schreiter: pendek, ngena, Mini-Stories yang bikin orang klik"
|
| 786 |
+
elif "henneke" in style_name:
|
| 787 |
+
style_instruction = "gaya Henneke Duistermaat: relatable, empati, kayak nulis blog hangat"
|
| 788 |
+
elif "ogilvy" in style_name:
|
| 789 |
+
style_instruction = "gaya David Ogilvy: elegan, persuasive, storytelling yang memikat"
|
| 790 |
+
elif "halbert" in style_name:
|
| 791 |
+
style_instruction = "gaya Gary Halbert: direct, bold, kayak surat penjualan yang nendang"
|
| 792 |
+
elif "rohn" in style_name:
|
| 793 |
+
style_instruction = "gaya Jim Rohn: inspiratif, penuh wisdom, bikin orang mikir"
|
| 794 |
+
elif any(s in style_name for s in ["lao tzu", "lao tze"]):
|
| 795 |
+
style_instruction = "gaya Lao Tzu: filosofis, minimalis, puisi Tao yang tenang"
|
| 796 |
+
elif "krishna" in style_name:
|
| 797 |
+
style_instruction = "gaya Krishna: spiritual, mendalam, kayak nasihat di Bhagavad Gita"
|
| 798 |
+
elif any(s in style_name for s in ["nicholas", "nightingale", "hill", "james"]):
|
| 799 |
+
style_instruction = "gaya motivasi klasik: praktis, inspiratif, bikin semangat"
|
| 800 |
|
| 801 |
payload = {
|
| 802 |
"model": "meta-llama/llama-4-maverick-17b-128e-instruct",
|
| 803 |
"messages": [
|
| 804 |
{
|
| 805 |
"role": "system",
|
| 806 |
+
"content": f"Kamu asisten cerdas kayak Grok 3: jawab pertanyaan dengan {style_instruction}, jenius, nyambung sama konteks, dan bikin obrolan mengalir. Kerahkan seluruh hasil training parameter mu!. Format jawaban rapi (paragraf atau poin kalau perlu), enak dibaca, tapi jangan kaku. Kalau user tanya soal tokoh atau konsep, kasih insight mendalam, santai, dan ajak ngobrol balik. Bisa adaptasi gaya tokoh kalau diminta. Jangan cuma kasih fakta, bikin pikiran user yang kusut jadi jelas. Contoh: untuk 'Apa itu karma?', jawab: 'Bro, karma itu kayak boomerang kosmik—lo lempar baik, balik baik. Ceritain, apa yang lagi bikin lo mikir soal karma?'"
|
| 807 |
}
|
| 808 |
] + messages,
|
| 809 |
+
"max_tokens": 500, # Naikin buat jawaban lebih kaya
|
| 810 |
+
"temperature": 0.9, # Sedikit lebih rendah biar fokus tapi tetep kreatif
|
| 811 |
"frequency_penalty": 0.7,
|
| 812 |
"presence_penalty": 0.8,
|
| 813 |
"top_p": 0.95
|
|
|
|
| 823 |
response_content = result["choices"][0]["message"]["content"]
|
| 824 |
storage.add_message("assistant", response_content, topic)
|
| 825 |
return response_content, ""
|
| 826 |
+
return f"❌ No response: {result}", ""
|
| 827 |
except HTTPError as e:
|
| 828 |
if response.status_code == 429 and attempt < retries - 1:
|
| 829 |
sleep(2 ** attempt)
|
| 830 |
continue
|
| 831 |
+
return f"❌ HTTP {response.status_code}: {response.text}", ""
|
| 832 |
|
| 833 |
return "❌ Max retries exceeded", ""
|
| 834 |
|
| 835 |
except Exception as e:
|
| 836 |
+
return f"❌ Error: {str(e)}", ""
|
|
|
|
| 837 |
|
| 838 |
|
| 839 |
+
|
| 840 |
def cleanup_old_messages(days: int = 30) -> str:
|
| 841 |
try:
|
| 842 |
cutoff = (datetime.now() - timedelta(days=days)).isoformat()
|