File size: 3,610 Bytes
75c474a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Importing necessary libraries
import gradio as gr
import os
from langchain_core.messages import AIMessage, HumanMessage
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain

# Function to get vector store from a given URL
def getvecstore(url):
    loader = WebBaseLoader(url)
    document = loader.load()

    # Split the document into chunks
    text_splitter = RecursiveCharacterTextSplitter()
    document_chunks = text_splitter.split_documents(document)

    # Create a vector store from the chunks
    vector_store = Chroma.from_documents(document_chunks, OpenAIEmbeddings())

    return vector_store

# Function to get a context-aware retriever chain
def getcontext(vector_store):
    llm = ChatOpenAI()

    retriever = vector_store.as_retriever()

    # Define the prompt for context-aware retrieval
    prompt = ChatPromptTemplate.from_messages([
        MessagesPlaceholder(variable_name="chat_history"),
        ("user", "{input}"),
        ("user", "Given the above conversation, generate a search query to look up in order to get information relevant to the conversation")
    ])

    retriever_chain = create_history_aware_retriever(llm, retriever, prompt)

    return retriever_chain

# Function to get a conversational RAG (Retrieval-Augmented Generation) chain
def getragchain(retriever_chain):
    llm = ChatOpenAI()

    # Define the prompt for conversational RAG
    prompt = ChatPromptTemplate.from_messages([
        ("system", "Answer the user's questions based on the below context:\n\n{context}"),
        MessagesPlaceholder(variable_name="chat_history"),
        ("user", "{input}"),
    ])

    # Create a chain for generating responses based on context
    stuff_documents_chain = create_stuff_documents_chain(llm, prompt)

    return create_retrieval_chain(retriever_chain, stuff_documents_chain)

# Global variables
chat_history = [AIMessage(content="I am an assistant made to answer your questions?")]
vector_store = None

# Function to get response from the chatbot
def getresp(user_input, website_url):
    global chat_history, vector_store
    if vector_store is None:
        vector_store = getvecstore(website_url)

    retriever_chain = getcontext(vector_store)
    conversation_rag_chain = getragchain(retriever_chain)

    # Invoke the chain to get the response
    response = conversation_rag_chain.invoke({
        "chat_history": chat_history,
        "input": user_input
    })

    # Update chat history
    chat_history.append(HumanMessage(content=user_input))
    chat_history.append(AIMessage(content=response))

    return response['answer']

# Function to interact with the chatbot through Gradio interface
def interact(Query, URL, OpenAI_Key):
    # Set the OpenAI key
    os.environ['OPENAI_API_KEY'] = OpenAI_Key
    # Get the response from the chatbot
    response = getresp(Query, URL)
    return response

# Create a Gradio interface
iface = gr.Interface(
    fn=interact,
    inputs=["text", "text", "text"],
    outputs="text",
    title="Chat with LLM",
    description="Interact with a chatbot based on your website URL. Also Provide your OpenAI key as well to get it working."
)

# Launch the Gradio interface
iface.launch()