File size: 2,816 Bytes
8bec3c3
 
 
5ca0311
 
 
 
 
 
 
 
 
 
 
 
 
9da0c7c
 
a9121c0
5ca0311
 
 
 
 
 
 
 
 
 
abd92f7
5ca0311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9121c0
5ca0311
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import shutil
shutil.rmtree("/root/.cache", ignore_errors=True)

import time
import warnings
import gradio as gr
from dotenv import load_dotenv
from loader import load_embeddings
from langchain_groq import ChatGroq

load_dotenv()
vector_store = load_embeddings()

PROMPT_TEMPLATE = """
You are a friendly and knowledgeable assistant who is proficient in PyTorch. Answer any questions that are related to PyTorch and answer questions according to the user input. If you’re unsure of the answer, respond with "I'm not sure about that," without making up answers. Make the user understand the concept by using simple english after the definition. 


-- Craft a long response about the answer that covers everything about that topic. 
After your conclusion, briefly summarize the entire response in a neat conversational way, making the user understand in under 200 lines only if it is an answer to the question, not for the greeting.

User Query: {user_input}

Context:
{context}

Response:
"""


llm = ChatGroq(model='llama-3.1-8b-instant', temperature=1, max_retries=3)

# Chatbot response function
def chatbot_response(user_input, history):
    # Perform similarity search to find relevant context
    query_result = vector_store.similarity_search_with_score(query=user_input, k=1)
    if query_result:
        top_document = query_result[0][0].page_content

        # Format the conversation history and context
        conversation_context = "\n\n".join([f"User: {q}\nTorchie: {r}" for q, r in history])
        llm_query = PROMPT_TEMPLATE.format(user_input=user_input, context=f"{conversation_context}\n\n{top_document}")


        # Get the LLM's response
        start_time = time.time()
        llm_answer = llm.invoke(llm_query)
        elapsed_time = time.time() - start_time

        # Format the response
        response = f"{llm_answer.content}\n\nResponse time: {elapsed_time:.2f} seconds"
    else:
        response = "I'm sorry, I couldn't find any relevant information."

    # Return only the response
    return response, history 

# Gradio interface function
def interface(user_input, history):
    bot_response, history = chatbot_response(user_input, history) 
    return bot_response, history 

# Define Gradio UI components
iface = gr.Interface(
    fn=interface, 
    inputs=[
        gr.Textbox(label="Your Question:", placeholder="Type your PyTorch-related question here."), 
        gr.State(value=[]) # State input to keep conversation history 
    ],
    outputs=[
        gr.Textbox(label="Torchie Response", show_copy_button=True), 
        gr.State() # State output to hold and return the conversation history 
    ],
    title="Torchie - Your PyTorch Assistant",
    description="Hi! I'm Torchie, your friendly PyTorch assistant. Ask me anything about PyTorch. :)"
)

iface.launch()