Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 2 |
from langchain.vectorstores import Chroma
|
| 3 |
-
from langchain.text_splitter import
|
| 4 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 5 |
from langchain.chat_models import ChatOpenAI
|
| 6 |
from langchain.chains import RetrievalQA
|
| 7 |
from langchain.document_loaders import BSHTMLLoader, DirectoryLoader
|
| 8 |
-
from langchain
|
| 9 |
-
from langchain.agents import AgentType
|
| 10 |
-
from langchain.tools import BaseTool
|
| 11 |
-
from langchain import LLMMathChain, SerpAPIWrapper
|
| 12 |
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
|
| 13 |
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
| 14 |
-
from langchain import
|
| 15 |
|
| 16 |
import os
|
| 17 |
from glob import glob
|
| 18 |
import shutil
|
| 19 |
|
| 20 |
-
files = glob("
|
| 21 |
|
| 22 |
-
os.mkdir('./data')
|
| 23 |
destination_folder = './data/'
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
for html_file in files:
|
| 26 |
-
|
| 27 |
|
| 28 |
bshtml_dir_loader = DirectoryLoader('./data/', loader_cls=BSHTMLLoader)
|
| 29 |
data = bshtml_dir_loader.load()
|
| 30 |
|
| 31 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 32 |
-
chunk_size
|
| 33 |
-
chunk_overlap
|
| 34 |
-
length_function
|
| 35 |
)
|
| 36 |
|
| 37 |
documents = text_splitter.split_documents(data)
|
|
@@ -53,22 +55,22 @@ search = SerpAPIWrapper()
|
|
| 53 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 54 |
readonlymemory = ReadOnlySharedMemory(memory=memory)
|
| 55 |
|
| 56 |
-
shakespeare_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=doc_retriever,
|
|
|
|
| 57 |
|
| 58 |
tools = [
|
| 59 |
Tool(
|
| 60 |
-
name
|
| 61 |
func=shakespeare_qa.run,
|
| 62 |
description="useful for when you need to answer questions about Shakespeare's works. Input should be a fully formed question."
|
| 63 |
),
|
| 64 |
Tool(
|
| 65 |
-
name
|
| 66 |
func=search.run,
|
| 67 |
description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question."
|
| 68 |
),
|
| 69 |
]
|
| 70 |
|
| 71 |
-
|
| 72 |
prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
|
| 73 |
suffix = """Begin!"
|
| 74 |
|
|
@@ -77,9 +79,9 @@ Question: {input}
|
|
| 77 |
{agent_scratchpad}"""
|
| 78 |
|
| 79 |
prompt = ZeroShotAgent.create_prompt(
|
| 80 |
-
tools,
|
| 81 |
-
prefix=prefix,
|
| 82 |
-
suffix=suffix,
|
| 83 |
input_variables=["input", "chat_history", "agent_scratchpad"]
|
| 84 |
)
|
| 85 |
|
|
@@ -90,9 +92,10 @@ agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbo
|
|
| 90 |
|
| 91 |
|
| 92 |
def make_inference(query):
|
| 93 |
-
response = agent_chain.run(input=
|
| 94 |
return (response)
|
| 95 |
|
|
|
|
| 96 |
if __name__ == "__main__":
|
| 97 |
# make a gradio interface
|
| 98 |
import gradio as gr
|
|
|
|
| 1 |
+
__import__('pysqlite3')
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
|
| 5 |
+
|
| 6 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 7 |
from langchain.vectorstores import Chroma
|
| 8 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
| 9 |
from langchain.chat_models import ChatOpenAI
|
| 10 |
from langchain.chains import RetrievalQA
|
| 11 |
from langchain.document_loaders import BSHTMLLoader, DirectoryLoader
|
| 12 |
+
from langchain import SerpAPIWrapper
|
|
|
|
|
|
|
|
|
|
| 13 |
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
|
| 14 |
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
| 15 |
+
from langchain import LLMChain
|
| 16 |
|
| 17 |
import os
|
| 18 |
from glob import glob
|
| 19 |
import shutil
|
| 20 |
|
| 21 |
+
files = glob("shakespeare/**/*.html")
|
| 22 |
|
|
|
|
| 23 |
destination_folder = './data/'
|
| 24 |
+
if not os.path.exists(destination_folder):
|
| 25 |
+
os.mkdir('./data')
|
| 26 |
|
| 27 |
+
for html_file in files:
|
| 28 |
+
shutil.copy(html_file, destination_folder + html_file.split("/")[-1])
|
| 29 |
|
| 30 |
bshtml_dir_loader = DirectoryLoader('./data/', loader_cls=BSHTMLLoader)
|
| 31 |
data = bshtml_dir_loader.load()
|
| 32 |
|
| 33 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 34 |
+
chunk_size=1000,
|
| 35 |
+
chunk_overlap=20,
|
| 36 |
+
length_function=len,
|
| 37 |
)
|
| 38 |
|
| 39 |
documents = text_splitter.split_documents(data)
|
|
|
|
| 55 |
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 56 |
readonlymemory = ReadOnlySharedMemory(memory=memory)
|
| 57 |
|
| 58 |
+
shakespeare_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=doc_retriever,
|
| 59 |
+
memory=readonlymemory)
|
| 60 |
|
| 61 |
tools = [
|
| 62 |
Tool(
|
| 63 |
+
name="Shakespeare QA System",
|
| 64 |
func=shakespeare_qa.run,
|
| 65 |
description="useful for when you need to answer questions about Shakespeare's works. Input should be a fully formed question."
|
| 66 |
),
|
| 67 |
Tool(
|
| 68 |
+
name="SERP API Search",
|
| 69 |
func=search.run,
|
| 70 |
description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question."
|
| 71 |
),
|
| 72 |
]
|
| 73 |
|
|
|
|
| 74 |
prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
|
| 75 |
suffix = """Begin!"
|
| 76 |
|
|
|
|
| 79 |
{agent_scratchpad}"""
|
| 80 |
|
| 81 |
prompt = ZeroShotAgent.create_prompt(
|
| 82 |
+
tools,
|
| 83 |
+
prefix=prefix,
|
| 84 |
+
suffix=suffix,
|
| 85 |
input_variables=["input", "chat_history", "agent_scratchpad"]
|
| 86 |
)
|
| 87 |
|
|
|
|
| 92 |
|
| 93 |
|
| 94 |
def make_inference(query):
|
| 95 |
+
response = agent_chain.run(input=query)
|
| 96 |
return (response)
|
| 97 |
|
| 98 |
+
|
| 99 |
if __name__ == "__main__":
|
| 100 |
# make a gradio interface
|
| 101 |
import gradio as gr
|