File size: 2,045 Bytes
87a558e
 
 
 
 
 
8daa164
87a558e
 
 
8daa164
87a558e
 
 
 
 
 
 
 
 
87b19d6
87a558e
 
87b19d6
 
 
 
87a558e
87b19d6
 
 
87a558e
87b19d6
 
87a558e
87b19d6
 
 
 
 
 
 
 
 
 
87a558e
8daa164
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
import plotly.express as px
from client import client

# Fungsi untuk menghasilkan plot acak
def random_plot():
    df = px.data.iris()
    fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", size='petal_length', hover_data=['petal_width'])
    return fig

# Fungsi untuk mencetak data suka/tidak suka
def print_like_dislike(x):
    print(x.index, x.value, x.liked)

# Fungsi untuk menambahkan pesan ke riwayat
def add_message(history, message):
    if message["files"]:
        for x in message["files"]:
            history.append(((x,), None))
    if message["text"] is not None:
        history.append((message["text"], None))
    return history, gr.ChatInterface(value="", interactive=True)

# Fungsi untuk menghubungkan dengan model Llama
def chat_mem(message=None, chat_history=None, max_tokens=2048):
    chat_history_role = [
        {"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."}
    ]
    
    # Tambahkan pesan pembuka jika chat_history kosong
    if chat_history is None or len(chat_history) == 0:
        chat_history = [("Selamat datang di chatbot Nopiyar.my.id! Ada yang bisa saya bantu?", None)]
    
    if message:
        chat_history_role.append({"role": "user", "content": message})
    
        if "siapa pembuat" in message.lower() or "siapa pencipta" in message.lower():
            assistant_response = "Chatbot ini dibuat oleh Riswan Nopiyar. Jika Anda memiliki pertanyaan lain atau membutuhkan bantuan lebih lanjut, jangan ragu untuk bertanya."
        else:
            chat_completion = client.chat_completion(
                messages=chat_history_role,
                max_tokens=max_tokens,
            )
            assistant_response = chat_completion.choices[0].message.content
        
        chat_history.append((message, assistant_response))
    
    return "", chat_history