Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +124 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 2 |
+
from langchain.vectorstores import Chroma
|
| 3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 5 |
+
from langchain.llms import OpenAI
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import os
|
| 8 |
+
import subprocess
|
| 9 |
+
|
| 10 |
+
# Load environment variables from .env file
|
| 11 |
+
load_dotenv()
|
| 12 |
+
# Access environment variables
|
| 13 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 14 |
+
SERPAPI_API_KEY = os.getenv("SERPAPI_API_KEY")
|
| 15 |
+
|
| 16 |
+
# subprocess.run(["git", "clone", "https://github.com/TheMITTech/shakespeare"], check=True)
|
| 17 |
+
|
| 18 |
+
from glob import glob
|
| 19 |
+
files = glob("./shakespeare/**/*.html")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
import shutil
|
| 23 |
+
import os
|
| 24 |
+
# os.mkdir('./data')
|
| 25 |
+
# destination_folder = './data/'
|
| 26 |
+
# for html_file in files:
|
| 27 |
+
# shutil.move(html_file, destination_folder + html_file.split("/")[-1])
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
from langchain.document_loaders import BSHTMLLoader, DirectoryLoader
|
| 31 |
+
from bs4 import BeautifulSoup
|
| 32 |
+
bshtml_dir_loader = DirectoryLoader('./data/', loader_cls=BSHTMLLoader)
|
| 33 |
+
data = bshtml_dir_loader.load()
|
| 34 |
+
|
| 35 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 36 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 37 |
+
chunk_size = 1000,
|
| 38 |
+
chunk_overlap = 20,
|
| 39 |
+
length_function = len,
|
| 40 |
+
)
|
| 41 |
+
documents = text_splitter.split_documents(data)
|
| 42 |
+
|
| 43 |
+
embeddings = OpenAIEmbeddings()
|
| 44 |
+
|
| 45 |
+
from langchain.vectorstores import Chroma
|
| 46 |
+
persist_directory = "vector_db"
|
| 47 |
+
vectordb = Chroma.from_documents(documents=documents, embedding=embeddings, persist_directory=persist_directory)
|
| 48 |
+
|
| 49 |
+
vectordb.persist()
|
| 50 |
+
vectordb = None
|
| 51 |
+
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
|
| 52 |
+
|
| 53 |
+
from langchain.chat_models import ChatOpenAI
|
| 54 |
+
llm = ChatOpenAI(temperature=0, model="gpt-4")
|
| 55 |
+
doc_retriever = vectordb.as_retriever()
|
| 56 |
+
|
| 57 |
+
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
|
| 58 |
+
from langchain.chains import RetrievalQA
|
| 59 |
+
shakespeare_qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=doc_retriever)
|
| 60 |
+
|
| 61 |
+
from langchain.utilities import SerpAPIWrapper
|
| 62 |
+
search = SerpAPIWrapper()
|
| 63 |
+
|
| 64 |
+
from langchain.agents import initialize_agent, Tool
|
| 65 |
+
from langchain.agents import AgentType
|
| 66 |
+
from langchain.tools import BaseTool
|
| 67 |
+
from langchain.llms import OpenAI
|
| 68 |
+
from langchain import LLMMathChain, SerpAPIWrapper
|
| 69 |
+
|
| 70 |
+
tools = [
|
| 71 |
+
Tool(
|
| 72 |
+
name = "Shakespeare QA System",
|
| 73 |
+
func=shakespeare_qa.run,
|
| 74 |
+
description="useful for when you need to answer questions about Shakespeare's works. Input should be a fully formed question."
|
| 75 |
+
),
|
| 76 |
+
Tool(
|
| 77 |
+
name = "SERP API Search",
|
| 78 |
+
func=search.run,
|
| 79 |
+
description="useful for when you need to answer questions about ruff (a python linter). Input should be a fully formed question."
|
| 80 |
+
),
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
|
| 84 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 85 |
+
readonlymemory = ReadOnlySharedMemory(memory=memory)
|
| 86 |
+
|
| 87 |
+
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
| 88 |
+
|
| 89 |
+
prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
|
| 90 |
+
suffix = """Begin!"
|
| 91 |
+
|
| 92 |
+
{chat_history}
|
| 93 |
+
Question: {input}
|
| 94 |
+
{agent_scratchpad}"""
|
| 95 |
+
|
| 96 |
+
prompt = ZeroShotAgent.create_prompt(
|
| 97 |
+
tools,
|
| 98 |
+
prefix=prefix,
|
| 99 |
+
suffix=suffix,
|
| 100 |
+
input_variables=["input", "chat_history", "agent_scratchpad"]
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
from langchain import OpenAI, LLMChain, PromptTemplate
|
| 105 |
+
llm_chain = LLMChain(llm=llm, prompt=prompt)
|
| 106 |
+
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
|
| 107 |
+
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory)
|
| 108 |
+
|
| 109 |
+
def make_inference(query):
|
| 110 |
+
return(agent_chain.run(input=query))
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
# make a gradio interface
|
| 114 |
+
import gradio as gr
|
| 115 |
+
|
| 116 |
+
gr.Interface(
|
| 117 |
+
make_inference,
|
| 118 |
+
[
|
| 119 |
+
gr.inputs.Textbox(lines=2, label="Query"),
|
| 120 |
+
],
|
| 121 |
+
gr.outputs.Textbox(label="Response"),
|
| 122 |
+
title="🗣️QuestionMyDoc-OpenAI📄",
|
| 123 |
+
description="🗣️QuestionMyDoc-OpenAI📄 is a tool that allows you to ask questions about a document. In this case - Shakespears.",
|
| 124 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
openai
|
| 3 |
+
tiktoken
|
| 4 |
+
chromadb
|
| 5 |
+
beautifulsoup4
|
| 6 |
+
lxml
|
| 7 |
+
bs4
|
| 8 |
+
google-search-results
|