Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -115,7 +115,7 @@ async def startup():
|
|
115 |
domain_url = 'https://www.bofrost.de/faq/'
|
116 |
urls = get_all_links_from_domain(domain_url)
|
117 |
|
118 |
-
|
119 |
|
120 |
# Define API endpoint to receive queries and provide responses
|
121 |
@app.post("/generate/")
|
@@ -123,10 +123,24 @@ def generate(user_input):
|
|
123 |
|
124 |
return get_response(user_input, [])
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
|
|
127 |
def get_response(message, history=[]):
|
128 |
-
|
129 |
-
|
130 |
|
131 |
# Define the prompt as a ChatPromptValue object
|
132 |
#user_input = ChatPromptValue(user_input)
|
@@ -135,11 +149,19 @@ def get_response(message, history=[]):
|
|
135 |
#input_ids = user_input.tensor
|
136 |
|
137 |
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
|
141 |
history =[]
|
142 |
-
retriever_chain = get_context_retriever_chain(
|
143 |
conversation_rag_chain = get_conversational_rag_chain(retriever_chain)
|
144 |
|
145 |
response = conversation_rag_chain.invoke({
|
|
|
115 |
domain_url = 'https://www.bofrost.de/faq/'
|
116 |
urls = get_all_links_from_domain(domain_url)
|
117 |
|
118 |
+
print(urls)
|
119 |
|
120 |
# Define API endpoint to receive queries and provide responses
|
121 |
@app.post("/generate/")
|
|
|
123 |
|
124 |
return get_response(user_input, [])
|
125 |
|
126 |
+
def get_conversational_rag_chain(retriever_chain):
|
127 |
+
|
128 |
+
llm = load_model(model_name)
|
129 |
+
|
130 |
+
prompt = ChatPromptTemplate.from_messages([
|
131 |
+
("system", "Du bist eine freundlicher Mitarbeiterin Namens Susie und arbeitest in einenm Call Center. Du beantwortest basierend auf dem Context. Benutze nur den Inhalt des Context. Füge wenn möglich die Quelle hinzu. Antworte mit: Ich bin mir nicht sicher. Wenn die Antwort nicht aus dem Context hervorgeht. Antworte auf Deutsch, bitte? CONTEXT:\n\n{context}"),
|
132 |
+
MessagesPlaceholder(variable_name="chat_history"),
|
133 |
+
("user", "{input}"),
|
134 |
+
])
|
135 |
+
|
136 |
+
stuff_documents_chain = create_stuff_documents_chain(llm,prompt)
|
137 |
+
|
138 |
+
return create_retrieval_chain(retriever_chain, stuff_documents_chain)
|
139 |
|
140 |
+
|
141 |
def get_response(message, history=[]):
|
142 |
+
# dialog = history_to_dialog_format(history)
|
143 |
+
# dialog.append({"role": "user", "content": message})
|
144 |
|
145 |
# Define the prompt as a ChatPromptValue object
|
146 |
#user_input = ChatPromptValue(user_input)
|
|
|
149 |
#input_ids = user_input.tensor
|
150 |
|
151 |
|
152 |
+
model = "BAAI/bge-base-en-v1.5"
|
153 |
+
encode_kwargs = {
|
154 |
+
"normalize_embeddings": True
|
155 |
+
} # set True to compute cosine similarity
|
156 |
+
embeddings = HuggingFaceBgeEmbeddings(
|
157 |
+
model_name=model, encode_kwargs=encode_kwargs, model_kwargs={"device": "cpu"}
|
158 |
+
)
|
159 |
+
# load from disk
|
160 |
+
vector_store = Chroma(persist_directory="/home/user/.cache/chroma_db", embedding_function=embeddings)
|
161 |
+
|
162 |
|
163 |
history =[]
|
164 |
+
retriever_chain = get_context_retriever_chain(vector_store)
|
165 |
conversation_rag_chain = get_conversational_rag_chain(retriever_chain)
|
166 |
|
167 |
response = conversation_rag_chain.invoke({
|