Menambahkan chat awal
Browse files- functions.py +20 -21
functions.py
CHANGED
@@ -18,31 +18,30 @@ def add_message(history, message):
|
|
18 |
history.append(((x,), None))
|
19 |
if message["text"] is not None:
|
20 |
history.append((message["text"], None))
|
21 |
-
return history, gr.
|
22 |
|
23 |
# Fungsi untuk menghubungkan dengan model Llama
|
24 |
-
def chat_mem(message, chat_history, max_tokens=
|
25 |
-
chat_history_role = [
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
chat_history_role.append({"role": "assistant", "content": assistant_msg})
|
31 |
|
32 |
-
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
|
43 |
-
chat_history_role.append({"role": "assistant", "content": assistant_response})
|
44 |
-
|
45 |
-
modified = [msg["content"] for msg in chat_history_role]
|
46 |
-
chat_history = [(modified[i * 2 + 1], modified[i * 2 + 2]) for i in range(len(modified) // 2)]
|
47 |
-
|
48 |
return "", chat_history
|
|
|
18 |
history.append(((x,), None))
|
19 |
if message["text"] is not None:
|
20 |
history.append((message["text"], None))
|
21 |
+
return history, gr.ChatInterface(value="", interactive=True)
|
22 |
|
23 |
# Fungsi untuk menghubungkan dengan model Llama
|
24 |
+
def chat_mem(message=None, chat_history=None, max_tokens=2048):
|
25 |
+
chat_history_role = [
|
26 |
+
{"role": "system", "content": "Anda adalah Chat Bot milik Riswan Nopiyar, yang dirancang untuk membantu menjawab pertanyaan dan memberikan informasi dengan sopan dan akurat dalam bahasa Indonesia. Seluruh percakapan akan dilakukan dalam bahasa Indonesia."}
|
27 |
+
]
|
28 |
|
29 |
+
# Tambahkan pesan pembuka jika chat_history kosong
|
30 |
+
if chat_history is None or len(chat_history) == 0:
|
31 |
+
chat_history = [("Selamat datang di chatbot Nopiyar.my.id! Ada yang bisa saya bantu?", None)]
|
|
|
32 |
|
33 |
+
if message:
|
34 |
+
chat_history_role.append({"role": "user", "content": message})
|
35 |
|
36 |
+
if "siapa pembuat" in message.lower() or "siapa pencipta" in message.lower():
|
37 |
+
assistant_response = "Chatbot ini dibuat oleh Riswan Nopiyar. Jika Anda memiliki pertanyaan lain atau membutuhkan bantuan lebih lanjut, jangan ragu untuk bertanya."
|
38 |
+
else:
|
39 |
+
chat_completion = client.chat_completion(
|
40 |
+
messages=chat_history_role,
|
41 |
+
max_tokens=max_tokens,
|
42 |
+
)
|
43 |
+
assistant_response = chat_completion.choices[0].message.content
|
44 |
+
|
45 |
+
chat_history.append((message, assistant_response))
|
46 |
|
|
|
|
|
|
|
|
|
|
|
47 |
return "", chat_history
|