Spaces:
Runtime error
Runtime error
Your Name
commited on
Commit
·
2bfde77
1
Parent(s):
0951f0b
te
Browse files- app.py +68 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
os.environ['OPENAI_API_KEY'] = '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'
|
4 |
+
os.environ["GOOGLE_CSE_ID"] = "21e6861f3ce974e7c"
|
5 |
+
os.environ["GOOGLE_API_KEY"] = "AIzaSyAa3jWvap90TkXd4kwhAHndP_BOVOOjpHg"
|
6 |
+
|
7 |
+
from langchain import OpenAI, ConversationChain
|
8 |
+
from langchain.prompts import PromptTemplate
|
9 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
10 |
+
from langchain.text_splitter import CharacterTextSplitter
|
11 |
+
from langchain.vectorstores.faiss import FAISS
|
12 |
+
from langchain.docstore.document import Document
|
13 |
+
from langchain.agents import Tool
|
14 |
+
from langchain.chains.conversation.memory import ConversationBufferMemory
|
15 |
+
from langchain.utilities import GoogleSearchAPIWrapper
|
16 |
+
from langchain.agents import initialize_agent
|
17 |
+
|
18 |
+
from langchain.chains.conversation.memory import ConversationEntityMemory
|
19 |
+
from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
|
20 |
+
|
21 |
+
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
22 |
+
from langchain import SerpAPIWrapper, LLMChain
|
23 |
+
|
24 |
+
# ツールの準備
|
25 |
+
search = GoogleSearchAPIWrapper()
|
26 |
+
tools = [
|
27 |
+
Tool(
|
28 |
+
name = "Current Search",
|
29 |
+
func=search.run,
|
30 |
+
description="Use this allways",
|
31 |
+
),
|
32 |
+
]
|
33 |
+
|
34 |
+
# メモリの準備
|
35 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
36 |
+
|
37 |
+
# エージェントの準備
|
38 |
+
llm=OpenAI(model_name = "text-davinci-003",temperature=0)
|
39 |
+
agent_chain = initialize_agent(
|
40 |
+
tools,
|
41 |
+
llm,
|
42 |
+
agent="zero-shot-react-description",
|
43 |
+
verbose=True,
|
44 |
+
memory=memory
|
45 |
+
)
|
46 |
+
|
47 |
+
def chat(message, site,history):
|
48 |
+
history = history or []
|
49 |
+
#siteの//以前を削除
|
50 |
+
site = site.replace("https://","")
|
51 |
+
response = agent_chain.run(input=message+" site:"+site)
|
52 |
+
history.append((message, response))
|
53 |
+
|
54 |
+
return history, history
|
55 |
+
|
56 |
+
|
57 |
+
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("<h3><center>WebSiteChatBotAI</center></h3>")
|
59 |
+
site = gr.Textbox(placeholder="paste URL",label="WebSite")
|
60 |
+
chatbot = gr.Chatbot()
|
61 |
+
with gr.Row():
|
62 |
+
inp = gr.Textbox(placeholder="Question",label =None)
|
63 |
+
btn = gr.Button("Run").style(full_width=False)
|
64 |
+
state = gr.State()
|
65 |
+
agent_state = gr.State()
|
66 |
+
btn.click(chat, [inp,site,state],[chatbot, state])
|
67 |
+
if __name__ == '__main__':
|
68 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
openai
|
3 |
+
gradio
|
4 |
+
requests
|
5 |
+
google-api-python-client
|