File size: 2,433 Bytes
8817c45
 
244de83
8817c45
 
 
 
 
 
 
 
 
 
322a467
8817c45
 
 
 
322a467
8817c45
 
 
 
 
 
 
 
 
 
 
322a467
 
8817c45
322a467
 
 
 
8817c45
ecec6ae
8817c45
 
 
 
 
 
 
 
 
 
 
 
 
ecec6ae
8817c45
 
 
 
 
ecec6ae
8817c45
 
 
 
 
 
 
 
 
 
 
 
ecec6ae
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
import gradio as gr
from langchain.chains import ConversationChain
from .memory import EnhancedInMemoryHistory, get_by_session_id
from .models import route_llm, prompt

# Function to process input and generate a response
def process_input(user_input, session_id='1'):
    """
    Processes the user input and generates a response using the conversation chain.
    Parameters:
        user_input (str): The user's input message.
        session_id (str): The session ID for the chat (default is "1").
    Returns:
        Generator: A generator that streams the chatbot's response tokens.
    """
    memory = get_by_session_id(session_id)
    
    if user_input.lower() == 'exit':
        yield "Exiting the chat session."

    llm = route_llm(user_input)

    conversation_chain = ConversationChain(
        llm=llm,
        prompt=prompt,
        memory=memory,
        input_key='input',
        verbose=True
    )

    # Stream response tokens
    response_generator = conversation_chain.stream({"input": user_input})
    
    for token in response_generator:
        yield token  # Stream each token

    memory.save_context({'input': user_input}, ''.join(response_generator))

# Gradio interface function to handle input
def chatbot_interface(user_input, chat_history=None, session_id="1"):
    """
    Interface function for Gradio to handle input and output between the user and the chatbot.
    Parameters:
        user_input (str): The user's input message.
        session_id (str): The session ID for the chat (default is "1").
        chat_history (list): List of previous chat messages in the format [[user, bot], ...]
    Returns:
        list: Updated chat history including the new user and bot messages.
    """
    if chat_history is None:
        chat_history = []
    
    # Greeting at the start of the chat
    if user_input == "":
        bot_response = "Hi there! How can I help you today?"
    else:
        bot_response = process_input(user_input, session_id)
    
    # Add user input and bot response to chat history
    chat_history.append([user_input, bot_response])
    
    return chat_history

# Gradio launch
def launch_gradio_interface():
    gr.Interface(
        fn=chatbot_interface,
        inputs=[gr.Textbox(lines=7, label="Your input", placeholder="Type your message here...")],
        outputs=gr.Chatbot(label="Chat History"),
        title="AI Chatbot",
        live=False
    ).launch()