enoreyes commited on
Commit
a89ac38
1 Parent(s): b5f78f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -36
app.py CHANGED
@@ -3,63 +3,41 @@ import os
3
 
4
  from langchain import OpenAI, ConversationChain
5
  from langchain.prompts import PromptTemplate
6
- from langchain.embeddings.openai import OpenAIEmbeddings
7
  from langchain.text_splitter import CharacterTextSplitter
8
- from langchain.vectorstores.faiss import FAISS
9
  from langchain.docstore.document import Document
10
- from langchain.agents import Tool
11
  from langchain.chains.conversation.memory import ConversationBufferMemory
12
- from langchain.utilities import GoogleSearchAPIWrapper
13
- from langchain.agents import initialize_agent
14
 
15
  from langchain.chains.conversation.memory import ConversationEntityMemory
16
  from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
17
 
18
- from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
19
- from langchain import SerpAPIWrapper, LLMChain
20
 
21
- # ツールの準備
22
- search = GoogleSearchAPIWrapper()
23
- tools = [
24
- Tool(
25
- name = "Current Search",
26
- func=search.run,
27
- description="Use this allways",
28
- ),
29
- ]
30
-
31
- # メモリの準備
32
  memory = ConversationBufferMemory(memory_key="chat_history")
33
 
34
- # エージェントの準備
35
- llm=OpenAI(model_name = "text-davinci-003",temperature=0)
36
- agent_chain = initialize_agent(
37
- tools,
38
- llm,
39
- agent="zero-shot-react-description",
40
- verbose=True,
41
- memory=memory
42
- )
43
 
44
  def chat(message, site,history):
45
  history = history or []
46
- #siteの//以前を削除
47
- site = site.replace("https://","")
48
  response = ""
49
  try:
50
- response = agent_chain.run(input=message+" site:"+site)
51
- except KeyError:
52
- if not response:
53
- response = "not found in the site"
54
  history.append((message, response))
55
 
56
  return history, history
57
 
58
 
59
  with gr.Blocks() as demo:
60
- gr.Markdown("<h3><center>WebSiteChatBotAI</center></h3>")
61
- gr.Markdown("<p><center>paste web site URL and input question and push Run</center></p>")
62
- site = gr.Textbox(placeholder="paste URL",label="WebSite")
63
  chatbot = gr.Chatbot()
64
  with gr.Row():
65
  inp = gr.Textbox(placeholder="Question",label =None)
 
3
 
4
  from langchain import OpenAI, ConversationChain
5
  from langchain.prompts import PromptTemplate
 
6
  from langchain.text_splitter import CharacterTextSplitter
7
+ from langchain.vectorstores import Chroma
8
  from langchain.docstore.document import Document
9
+ from langchain.embeddings import HuggingFaceInstructEmbeddings
10
  from langchain.chains.conversation.memory import ConversationBufferMemory
 
 
11
 
12
  from langchain.chains.conversation.memory import ConversationEntityMemory
13
  from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
14
 
15
+ from langchain import LLMChain
 
16
 
 
 
 
 
 
 
 
 
 
 
 
17
  memory = ConversationBufferMemory(memory_key="chat_history")
18
 
19
+ persist_directory="db"
20
+ llm=OpenAI(model_name = "text-davinci-003", temperature=0)
21
+ vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding)
22
+ model_name = "hkunlp/instructor-large"
23
+ embed_instruction = "Represent the text from the BMW website for retrieval"
24
+ query_instruction = "Query the most relevant text from the BMW website"
25
+ embeddings = HuggingFaceInstructEmbeddings(model_name=model_name, embed_instruction=embed_instruction, query_instruction=query_instruction)
26
+ chain = RetrievalQAWithSourcesChain.from_chain_type(llm, chain_type="stuff", retriever=db.as_retriever(), memory=memory)
 
27
 
28
  def chat(message, site,history):
29
  history = history or []
 
 
30
  response = ""
31
  try:
32
+ response = chain.run(input=message)
 
 
 
33
  history.append((message, response))
34
 
35
  return history, history
36
 
37
 
38
  with gr.Blocks() as demo:
39
+ gr.Markdown("<h3><center>BMW Chat Bot</center></h3>")
40
+ gr.Markdown("<p><center>Ask questions about BMW</center></p>")
 
41
  chatbot = gr.Chatbot()
42
  with gr.Row():
43
  inp = gr.Textbox(placeholder="Question",label =None)