File size: 2,253 Bytes
2bfde77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359335a
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
import gradio as gr
import os
os.environ['OPENAI_API_KEY'] = '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'
os.environ["GOOGLE_CSE_ID"] = "21e6861f3ce974e7c"
os.environ["GOOGLE_API_KEY"] = "AIzaSyAa3jWvap90TkXd4kwhAHndP_BOVOOjpHg"

from langchain import OpenAI, ConversationChain
from langchain.prompts import PromptTemplate
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores.faiss import FAISS
from langchain.docstore.document import Document
from langchain.agents import Tool
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.utilities import GoogleSearchAPIWrapper
from langchain.agents import initialize_agent

from langchain.chains.conversation.memory import ConversationEntityMemory
from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE

from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain import SerpAPIWrapper, LLMChain

# ツールの準備
search = GoogleSearchAPIWrapper()
tools = [
    Tool(
        name = "Current Search",
        func=search.run,
        description="Use this allways",
    ),
]

# メモリの準備
memory = ConversationBufferMemory(memory_key="chat_history")

# エージェントの準備
llm=OpenAI(model_name = "text-davinci-003",temperature=0)
agent_chain = initialize_agent(
    tools, 
    llm, 
    agent="zero-shot-react-description", 
    verbose=True, 
    memory=memory
)

def chat(message, site,history):
    history = history or []
    #siteの//以前を削除
    site = site.replace("https://","")
    response = agent_chain.run(input=message+" site:"+site)
    history.append((message, response))

    return history, history


with gr.Blocks() as demo:
    gr.Markdown("<h3><center>WebSiteChatBotAI</center></h3>")
    site = gr.Textbox(placeholder="paste URL",label="WebSite")
    chatbot = gr.Chatbot()
    with gr.Row():
        inp = gr.Textbox(placeholder="Question",label =None)
        btn = gr.Button("Run").style(full_width=False)
        state = gr.State()
        agent_state = gr.State()
        btn.click(chat, [inp,site,state],[chatbot, state])
if __name__ == '__main__':
    demo.launch()