File size: 2,137 Bytes
fc670b4
b91b29d
7107ac8
4949586
ee8c5b2
 
4949586
 
 
 
 
 
 
 
f257a89
4949586
f257a89
4949586
f257a89
fc670b4
 
4949586
fc670b4
 
ee8c5b2
 
fc670b4
ee8c5b2
fc670b4
 
ee8c5b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc670b4
 
 
 
ee8c5b2
 
fc670b4
ee8c5b2
fc670b4
 
ee8c5b2
 
b91b29d
 
fc670b4
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
import os
import gradio as gr
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from groq import Groq

# Path FAISS index
faiss_path = "faiss_index"

# Pastikan file FAISS index ada sebelum loading
if not os.path.exists(f"{faiss_path}/index.faiss"):
    raise FileNotFoundError(f"⚠️ File FAISS index tidak ditemukan di {faiss_path}. Pastikan Anda telah membuat dan mengunggahnya!")

# Load FAISS index
vector_store = FAISS.load_local(
    faiss_path,
    HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2"),
    allow_dangerous_deserialization=True  
)

# Load API Key dari variabel lingkungan
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
    raise ValueError("⚠️ API Key Groq tidak ditemukan! Setel variabel lingkungan 'GROQ_API_KEY'.")

# Inisialisasi API Groq
client = Groq(api_key=GROQ_API_KEY)

def retrieve_and_generate(query, history=[]):
    """Retrieve knowledge base & generate response."""
    # Retrieve top 3 documents
    docs = vector_store.similarity_search(query, k=3)
    context = "\n\n".join([doc.page_content for doc in docs])

    # Generate response with LLM
    response = client.chat.completions.create(
        model="mixtral-8x7b-32768",
        messages=[
            {"role": "system", "content": "Anda adalah asisten AI yang menjawab pertanyaan tentang RoboHome berdasarkan dokumen ini."},
            {"role": "user", "content": f"{context}\n\nPertanyaan: {query}"}
        ],
        temperature=0.7,
        max_tokens=200
    )

    # Return hasil dalam format chat
    bot_response = response.choices[0].message.content
    history.append((query, bot_response))  # Simpan ke history chat
    return history, history

# UI dengan Gradio
iface = gr.ChatInterface(
    fn=retrieve_and_generate,
    chatbot=gr.Chatbot(label="Jawaban RoboHome"),
    textbox=gr.Textbox(label="Ajukan pertanyaan tentang RoboHome"),
    title="RoboHome RAG Chatbot",
    description="Chatbot ini menjawab pertanyaan berdasarkan dokumentasi RoboHome.",
)

iface.launch(share=True)  # Share=True untuk membuat link publik