Spaces:
Sleeping
Sleeping
RabotiahovDmytro
commited on
Update chatbot/bot.py
Browse files- chatbot/bot.py +69 -69
chatbot/bot.py
CHANGED
@@ -1,69 +1,69 @@
|
|
1 |
-
from chatbot.retriever import HybridRetrieverReranker
|
2 |
-
from litellm import completion
|
3 |
-
import os
|
4 |
-
import ast
|
5 |
-
|
6 |
-
GROQ_API_KEY =
|
7 |
-
DENSE_RETRIEVER_MODEL_NAME = "all-MiniLM-L6-v2"
|
8 |
-
CROSS_ENCODER_MODEL_NAME = "cross-encoder/ms-marco-MiniLM-L-12-v2"
|
9 |
-
LLM_CORE_MODEL_NAME = "groq/llama3-8b-8192"
|
10 |
-
|
11 |
-
|
12 |
-
class QuestionAnsweringBot:
|
13 |
-
|
14 |
-
def __init__(self, docs, enable_bm25=True, enable_dense=True, enable_rerank=True, top_k_bm25=60, top_n_dense=30, top_n_rerank=2) -> None:
|
15 |
-
self.retriever = HybridRetrieverReranker(docs)
|
16 |
-
self.enable_bm25 = enable_bm25
|
17 |
-
self.enable_dense = enable_dense
|
18 |
-
self.enable_rerank = enable_rerank
|
19 |
-
self.top_k_bm25=top_k_bm25
|
20 |
-
self.top_n_dense=top_n_dense
|
21 |
-
self.top_n_rerank=top_n_rerank
|
22 |
-
|
23 |
-
def __get_answer__(self, question: str) -> str:
|
24 |
-
PROMPT = """\
|
25 |
-
You are an intelligent assistant designed to provide accurate and relevant answers based on the provided context.
|
26 |
-
|
27 |
-
Rules:
|
28 |
-
- Always analyze the provided context thoroughly before answering.
|
29 |
-
- Respond with factual and concise information.
|
30 |
-
- If context is ambiguous or insufficient or you can't find answer, say 'I don't know.'
|
31 |
-
- Do not speculate or fabricate information beyond the provided context.
|
32 |
-
- Follow user instructions on the response style(default style is detailed response if user didn't provide any specifications):
|
33 |
-
- If the user asks for a detailed response, provide comprehensive explanations.
|
34 |
-
- If the user requests brevity, give concise and to-the-point answers.
|
35 |
-
- When applicable, summarize and synthesize information from the context to answer effectively.
|
36 |
-
- Avoid using information outside the given context.
|
37 |
-
"""
|
38 |
-
context = self.retriever.hybrid_retrieve(question,
|
39 |
-
enable_bm25=self.enable_bm25,
|
40 |
-
enable_dense=self.enable_dense,
|
41 |
-
enable_rerank=self.enable_rerank,
|
42 |
-
top_k_bm25=self.top_k_bm25,
|
43 |
-
top_n_dense=self.top_n_dense,
|
44 |
-
top_n_rerank=self.top_n_rerank
|
45 |
-
)
|
46 |
-
|
47 |
-
context_text = [doc['raw_text'] for doc in context]
|
48 |
-
|
49 |
-
response = completion(
|
50 |
-
model=LLM_CORE_MODEL_NAME,
|
51 |
-
temperature=0.0,
|
52 |
-
messages=[
|
53 |
-
{"role": "system", "content": PROMPT},
|
54 |
-
{"role": "user", "content": f"Context: {context_text}\nQuestion: {question}"}
|
55 |
-
],
|
56 |
-
api_key=GROQ_API_KEY
|
57 |
-
)
|
58 |
-
return response, context
|
59 |
-
|
60 |
-
def form_response(self, question):
|
61 |
-
llm_response, context = self.__get_answer__(question)
|
62 |
-
|
63 |
-
metadata_raw = [doc['chapter_name'] for doc in context]
|
64 |
-
metadata_cleaned = [ast.literal_eval(item) for item in metadata_raw]
|
65 |
-
|
66 |
-
print('User:', question)
|
67 |
-
print('System:', llm_response.choices[0].message.content)
|
68 |
-
|
69 |
-
return f"**{llm_response.choices[0].message.content}**\n\nResources: {[chapter for doc in metadata_cleaned for chapter in doc]}"
|
|
|
1 |
+
from chatbot.retriever import HybridRetrieverReranker
|
2 |
+
from litellm import completion
|
3 |
+
import os
|
4 |
+
import ast
|
5 |
+
|
6 |
+
GROQ_API_KEY = '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'
|
7 |
+
DENSE_RETRIEVER_MODEL_NAME = "all-MiniLM-L6-v2"
|
8 |
+
CROSS_ENCODER_MODEL_NAME = "cross-encoder/ms-marco-MiniLM-L-12-v2"
|
9 |
+
LLM_CORE_MODEL_NAME = "groq/llama3-8b-8192"
|
10 |
+
|
11 |
+
|
12 |
+
class QuestionAnsweringBot:
|
13 |
+
|
14 |
+
def __init__(self, docs, enable_bm25=True, enable_dense=True, enable_rerank=True, top_k_bm25=60, top_n_dense=30, top_n_rerank=2) -> None:
|
15 |
+
self.retriever = HybridRetrieverReranker(docs)
|
16 |
+
self.enable_bm25 = enable_bm25
|
17 |
+
self.enable_dense = enable_dense
|
18 |
+
self.enable_rerank = enable_rerank
|
19 |
+
self.top_k_bm25=top_k_bm25
|
20 |
+
self.top_n_dense=top_n_dense
|
21 |
+
self.top_n_rerank=top_n_rerank
|
22 |
+
|
23 |
+
def __get_answer__(self, question: str) -> str:
|
24 |
+
PROMPT = """\
|
25 |
+
You are an intelligent assistant designed to provide accurate and relevant answers based on the provided context.
|
26 |
+
|
27 |
+
Rules:
|
28 |
+
- Always analyze the provided context thoroughly before answering.
|
29 |
+
- Respond with factual and concise information.
|
30 |
+
- If context is ambiguous or insufficient or you can't find answer, say 'I don't know.'
|
31 |
+
- Do not speculate or fabricate information beyond the provided context.
|
32 |
+
- Follow user instructions on the response style(default style is detailed response if user didn't provide any specifications):
|
33 |
+
- If the user asks for a detailed response, provide comprehensive explanations.
|
34 |
+
- If the user requests brevity, give concise and to-the-point answers.
|
35 |
+
- When applicable, summarize and synthesize information from the context to answer effectively.
|
36 |
+
- Avoid using information outside the given context.
|
37 |
+
"""
|
38 |
+
context = self.retriever.hybrid_retrieve(question,
|
39 |
+
enable_bm25=self.enable_bm25,
|
40 |
+
enable_dense=self.enable_dense,
|
41 |
+
enable_rerank=self.enable_rerank,
|
42 |
+
top_k_bm25=self.top_k_bm25,
|
43 |
+
top_n_dense=self.top_n_dense,
|
44 |
+
top_n_rerank=self.top_n_rerank
|
45 |
+
)
|
46 |
+
|
47 |
+
context_text = [doc['raw_text'] for doc in context]
|
48 |
+
|
49 |
+
response = completion(
|
50 |
+
model=LLM_CORE_MODEL_NAME,
|
51 |
+
temperature=0.0,
|
52 |
+
messages=[
|
53 |
+
{"role": "system", "content": PROMPT},
|
54 |
+
{"role": "user", "content": f"Context: {context_text}\nQuestion: {question}"}
|
55 |
+
],
|
56 |
+
api_key=GROQ_API_KEY
|
57 |
+
)
|
58 |
+
return response, context
|
59 |
+
|
60 |
+
def form_response(self, question):
|
61 |
+
llm_response, context = self.__get_answer__(question)
|
62 |
+
|
63 |
+
metadata_raw = [doc['chapter_name'] for doc in context]
|
64 |
+
metadata_cleaned = [ast.literal_eval(item) for item in metadata_raw]
|
65 |
+
|
66 |
+
print('User:', question)
|
67 |
+
print('System:', llm_response.choices[0].message.content)
|
68 |
+
|
69 |
+
return f"**{llm_response.choices[0].message.content}**\n\nResources: {[chapter for doc in metadata_cleaned for chapter in doc]}"
|