Spaces:
Sleeping
Sleeping
Mehmet Emin Aydin
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -10,19 +10,20 @@ from docx import Document
|
|
10 |
from langchain.text_splitter import CharacterTextSplitter
|
11 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
12 |
from langchain_community.vectorstores import FAISS
|
13 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
14 |
import pickle
|
15 |
from datetime import datetime
|
16 |
import io
|
17 |
from dotenv import load_dotenv
|
|
|
18 |
|
19 |
log_data = []
|
20 |
|
|
|
21 |
|
22 |
class User:
|
23 |
def __init__(self, username):
|
24 |
self.username = username
|
25 |
-
self.llm = "
|
26 |
self.embedder = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
27 |
|
28 |
|
@@ -73,8 +74,6 @@ def _create_embeddings_and_save(user: User, chunks: any) -> int:
|
|
73 |
|
74 |
|
75 |
def ask_question(user: User, question: str, api_key: str, vector_store : FAISS) -> tuple[str, int]:
|
76 |
-
|
77 |
-
|
78 |
if api_key:
|
79 |
os.environ["GOOGLE_API_KEY"] = api_key
|
80 |
else:
|
@@ -82,26 +81,36 @@ def ask_question(user: User, question: str, api_key: str, vector_store : FAISS)
|
|
82 |
if not is_loaded:
|
83 |
return "API key not found.", 400
|
84 |
|
85 |
-
llm = ChatGoogleGenerativeAI(model=user.llm, temperature=0, max_output_tokens=256, top_k=40, top_p=0.8)
|
86 |
docs = vector_store.similarity_search(question)
|
87 |
retrieved_chunks = docs[0].page_content + docs[1].page_content + docs[2].page_content
|
88 |
-
|
89 |
-
|
90 |
try:
|
91 |
-
response =
|
92 |
except Exception:
|
93 |
return "Wrong API key.", 400
|
94 |
|
95 |
-
answer = response
|
96 |
-
_log(user, question,
|
97 |
return answer, 200
|
98 |
|
99 |
|
100 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
102 |
log_message = (
|
103 |
f"{timestamp}, Username: {user.username}, Question: {question}, "
|
104 |
-
f"LLM: {user.llm}, Embedder: {user.embedder},
|
105 |
f"Retrieved Texts: {retrieved_chunks}, Answer: {answer}\n"
|
106 |
)
|
107 |
log_data.append(log_message)
|
|
|
10 |
from langchain.text_splitter import CharacterTextSplitter
|
11 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
12 |
from langchain_community.vectorstores import FAISS
|
|
|
13 |
import pickle
|
14 |
from datetime import datetime
|
15 |
import io
|
16 |
from dotenv import load_dotenv
|
17 |
+
from groq import Groq
|
18 |
|
19 |
log_data = []
|
20 |
|
21 |
+
client = Groq(api_key="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")
|
22 |
|
23 |
class User:
|
24 |
def __init__(self, username):
|
25 |
self.username = username
|
26 |
+
self.llm = "llama3-8b-8192"
|
27 |
self.embedder = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
28 |
|
29 |
|
|
|
74 |
|
75 |
|
76 |
def ask_question(user: User, question: str, api_key: str, vector_store : FAISS) -> tuple[str, int]:
|
|
|
|
|
77 |
if api_key:
|
78 |
os.environ["GOOGLE_API_KEY"] = api_key
|
79 |
else:
|
|
|
81 |
if not is_loaded:
|
82 |
return "API key not found.", 400
|
83 |
|
|
|
84 |
docs = vector_store.similarity_search(question)
|
85 |
retrieved_chunks = docs[0].page_content + docs[1].page_content + docs[2].page_content
|
86 |
+
prompt ="Question: " + question + " Context: " + retrieved_chunks
|
87 |
+
|
88 |
try:
|
89 |
+
response = get_completion(prompt, model=user.llm)
|
90 |
except Exception:
|
91 |
return "Wrong API key.", 400
|
92 |
|
93 |
+
answer = response + "\n\n **<Most Related Chunk>** \n\n" + retrieved_chunks
|
94 |
+
_log(user, question, retrieved_chunks, response)
|
95 |
return answer, 200
|
96 |
|
97 |
|
98 |
+
def get_completion(prompt, model="llama3-8b-8192"):
|
99 |
+
messages = [{"role": "system", "content": "Answer the following question based on the information given in the content. Since you are an easy-to-understand assistant, all your output should be only the answer to the question and should be strictly in the same language as the question."},
|
100 |
+
{"role": "user", "content": prompt}]
|
101 |
+
response = client.chat.completions.create(
|
102 |
+
model=model,
|
103 |
+
messages=messages,
|
104 |
+
temperature=0,
|
105 |
+
)
|
106 |
+
return response.choices[0].message.content.strip()
|
107 |
+
|
108 |
+
|
109 |
+
def _log(user: User, question: str, retrieved_chunks: str, answer: str):
|
110 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
111 |
log_message = (
|
112 |
f"{timestamp}, Username: {user.username}, Question: {question}, "
|
113 |
+
f"LLM: {user.llm}, Embedder: {user.embedder}, "
|
114 |
f"Retrieved Texts: {retrieved_chunks}, Answer: {answer}\n"
|
115 |
)
|
116 |
log_data.append(log_message)
|