Spaces:
Runtime error
Runtime error
Added and configured a component to trim the architectures to remove some common junk referencing the vector retrieved documents.
Browse files- config/architectures.json +2 -1
- src/architectures.py +31 -2
config/architectures.json
CHANGED
@@ -16,7 +16,8 @@
|
|
16 |
"steps": [
|
17 |
{"class": "InputRequestScreener"},
|
18 |
{"class": "RetrievalAugmentor", "params": {"vector_store": "01_all_products"}},
|
19 |
-
{"class": "HFLlamaHttpRequestor", "params": {"model": "meta-llama/Llama-2-7b-chat-hf", "system_prompt": "You are a helpful domestic appliance advisor. Please answer the following customer question, answering only from the facts provided.
|
|
|
20 |
{"class": "OutputResponseScreener"}
|
21 |
],
|
22 |
"img": "architecture_rag.jpg"
|
|
|
16 |
"steps": [
|
17 |
{"class": "InputRequestScreener"},
|
18 |
{"class": "RetrievalAugmentor", "params": {"vector_store": "01_all_products"}},
|
19 |
+
{"class": "HFLlamaHttpRequestor", "params": {"model": "meta-llama/Llama-2-7b-chat-hf", "system_prompt": "You are a helpful domestic appliance advisor. Please answer the following customer question, answering only from the facts provided. Answer based on the background provided, do not make things up, and say if you cannot answer.", "max_tokens": 2000}},
|
20 |
+
{"class": "ResponseTrimmer", "params": {"regexes": ["^.*information provided[0-9A-Za-z,]*? ", "^.*background[0-9A-Za-z,]*? "]}},
|
21 |
{"class": "OutputResponseScreener"}
|
22 |
],
|
23 |
"img": "architecture_rag.jpg"
|
src/architectures.py
CHANGED
@@ -6,6 +6,7 @@ architecture components.
|
|
6 |
import chromadb
|
7 |
import json
|
8 |
import os
|
|
|
9 |
import traceback
|
10 |
|
11 |
from abc import ABC, abstractmethod
|
@@ -306,8 +307,12 @@ class RetrievalAugmentor(ArchitectureComponent):
|
|
306 |
documents = results['documents'][0] # Index 0 as we are always asking one question
|
307 |
|
308 |
# Update the request to include the retrieved documents
|
309 |
-
new_query = f'QUESTION: {input_query}\n\n'
|
310 |
-
new_query += '\n'.join([f'FACT: {d}' for d in documents])
|
|
|
|
|
|
|
|
|
311 |
|
312 |
# Put the request back into the architecture request
|
313 |
request.request = new_query
|
@@ -356,3 +361,27 @@ class HFLlamaHttpRequestor(ArchitectureComponent):
|
|
356 |
raise ValueError(f'No model {self.model} configured in the environment')
|
357 |
response = llm(request.request, system_prompt=self.system_prompt, max_new_tokens=self.max_tokens, temperature=self.temperature)
|
358 |
request.response = response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import chromadb
|
7 |
import json
|
8 |
import os
|
9 |
+
import regex as re
|
10 |
import traceback
|
11 |
|
12 |
from abc import ABC, abstractmethod
|
|
|
307 |
documents = results['documents'][0] # Index 0 as we are always asking one question
|
308 |
|
309 |
# Update the request to include the retrieved documents
|
310 |
+
#new_query = f'QUESTION: {input_query}\n\n'
|
311 |
+
#new_query += '\n'.join([f'FACT: {d}' for d in documents])
|
312 |
+
new_query = '{"background": ['
|
313 |
+
new_query += ', '.join([f'"{d}"' for d in documents])
|
314 |
+
new_query += ']}\n\nQUESTION: '
|
315 |
+
new_query += input_query
|
316 |
|
317 |
# Put the request back into the architecture request
|
318 |
request.request = new_query
|
|
|
361 |
raise ValueError(f'No model {self.model} configured in the environment')
|
362 |
response = llm(request.request, system_prompt=self.system_prompt, max_new_tokens=self.max_tokens, temperature=self.temperature)
|
363 |
request.response = response
|
364 |
+
|
365 |
+
|
366 |
+
class ResponseTrimmer(ArchitectureComponent):
|
367 |
+
"""
|
368 |
+
A concrete pipeline component which trims the response based on a regex match,
|
369 |
+
then uppercases the first character of what is left.
|
370 |
+
"""
|
371 |
+
description = "Trims the response based on a regex"
|
372 |
+
|
373 |
+
def __init__(self, regexes: List[str]):
|
374 |
+
quoted_regexes = [f'"{r}"' for r in regexes]
|
375 |
+
self.regex_display = f"[{', '.join(quoted_regexes)}]"
|
376 |
+
self.regexes = [re.compile(r, re.IGNORECASE) for r in regexes]
|
377 |
+
|
378 |
+
def process_request(self, request: ArchitectureRequest):
|
379 |
+
new_response = request.response
|
380 |
+
for regex in self.regexes:
|
381 |
+
new_response = regex.sub('', new_response)
|
382 |
+
new_response = new_response[:1].upper() + new_response[1:]
|
383 |
+
request.response = new_response
|
384 |
+
|
385 |
+
def config_description(self) -> str:
|
386 |
+
return f"Regexes: {self.regex_display}"
|
387 |
+
|