File size: 2,200 Bytes
7ec5b19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import streamlit as st
from llama_index import VectorStoreIndex, download_loader
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain.chains.conversation.memory import ConversationBufferMemory
from streamlit_chat import message

def myApp():
    # Download SimpleWebPageReader
    SimpleWebPageReader = download_loader("SimpleWebPageReader")

    # Set OpenAI API key
    openai.api_key = "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"  # Replace with your actual API key

    st.header("Chat with Web")

    # Input for the website URL
    website_url = st.text_input("Website URL", key="url")

    if website_url:
        try:
            # Initialize SimpleWebPageReader with the provided website URL
            loader = SimpleWebPageReader()
            documents = loader.load_data(urls=[website_url])

            # Create VectorStoreIndex from documents
            index = VectorStoreIndex.from_documents(documents)

            # Initialize LangChain OpenAI
            llm = OpenAI(openai_api_key="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", temperature=0, streaming = true)

            # Initialize ConversationBufferMemory
            memory = ConversationBufferMemory(memory_key="chat_history")

            # Initialize agent chain
            tools = [
                Tool(
                    name="Website Index",
                    func=lambda q: index.as_query_engine(),
                    description="Useful when you want to answer questions about the text on websites.",
                ),
            ]
            query_engine = index.as_query_engine()

            # Get user input for the query
            user_query = st.text_input("Your Question")

            if st.button("Ask"):
                # Query the LangChain agent with user input
                message(user_query, is_user=True)
                response = query_engine.query(user_query)

                # Display the response
                st.text("Response:")
                message(str(response))

        except Exception as e:
            st.error(f"An error occurred: {e}")



if __name__ == "__main__":
    myApp()