Update app.py
Browse files
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
|
9 |
from langchain.docstore.document import Document
|
10 |
-
from langchain.
|
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
|
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 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
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 =
|
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>
|
61 |
-
gr.Markdown("<p><center>
|
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)
|