diff --git a/application/api/answer/routes.py b/application/api/answer/routes.py index a809b4ef..ea8f6741 100644 --- a/application/api/answer/routes.py +++ b/application/api/answer/routes.py @@ -23,6 +23,7 @@ conversations_collection = db["conversations"] vectors_collection = db["vectors"] prompts_collection = db["prompts"] api_key_collection = db["api_keys"] +user_logs_collection = db["user_logs"] answer = Blueprint("answer", __name__) gpt_model = "" @@ -202,6 +203,19 @@ def complete_stream( # send data.type = "end" to indicate that the stream has ended as json data = json.dumps({"type": "id", "id": str(conversation_id)}) yield f"data: {data}\n\n" + + retriever_params = retriever.get_params() + user_logs_collection.insert_one({ + "action": "stream_answer", + "level": "info", + "user": "local", + "api_key": user_api_key, + "date": datetime.datetime.utcnow(), + "question": question, + "response": response_full, + "sources": source_log_docs, + "retriever_params": retriever_params + }) data = json.dumps({"type": "end"}) yield f"data: {data}\n\n" @@ -405,6 +419,18 @@ def api_answer(): result["conversation_id"] = save_conversation( conversation_id, question, response_full, source_log_docs, llm ) + retriever_params = retriever.get_params() + user_logs_collection.insert_one({ + "action": "api_answer", + "level": "info", + "user": "local", + "api_key": user_api_key, + "date": datetime.datetime.utcnow(), + "question": question, + "response": response_full, + "sources": source_log_docs, + "retriever_params": retriever_params + }) return result except Exception as e: @@ -460,6 +486,18 @@ def api_search(): ) docs = retriever.search() + retriever_params = retriever.get_params() + user_logs_collection.insert_one({ + "action": "api_search", + "level": "info", + "user": "local", + "api_key": user_api_key, + "date": datetime.datetime.utcnow(), + "question": question, + "sources": docs, + "retriever_params": retriever_params + }) + if data.get("isNoneDoc"): for doc in docs: doc["source"] = "None" diff --git a/application/retriever/base.py b/application/retriever/base.py index 4a37e810..fd99dbdd 100644 --- a/application/retriever/base.py +++ b/application/retriever/base.py @@ -12,3 +12,7 @@ class BaseRetriever(ABC): @abstractmethod def search(self, *args, **kwargs): pass + + @abstractmethod + def get_params(self): + pass diff --git a/application/retriever/brave_search.py b/application/retriever/brave_search.py index 5d1e1566..29666a57 100644 --- a/application/retriever/brave_search.py +++ b/application/retriever/brave_search.py @@ -101,3 +101,15 @@ class BraveRetSearch(BaseRetriever): def search(self): return self._get_data() + + def get_params(self): + return { + "question": self.question, + "source": self.source, + "chat_history": self.chat_history, + "prompt": self.prompt, + "chunks": self.chunks, + "token_limit": self.token_limit, + "gpt_model": self.gpt_model, + "user_api_key": self.user_api_key + } diff --git a/application/retriever/classic_rag.py b/application/retriever/classic_rag.py index 32f51234..88827188 100644 --- a/application/retriever/classic_rag.py +++ b/application/retriever/classic_rag.py @@ -120,3 +120,15 @@ class ClassicRAG(BaseRetriever): def search(self): return self._get_data() + + def get_params(self): + return { + "question": self.question, + "source": self.vectorstore, + "chat_history": self.chat_history, + "prompt": self.prompt, + "chunks": self.chunks, + "token_limit": self.token_limit, + "gpt_model": self.gpt_model, + "user_api_key": self.user_api_key + } diff --git a/application/retriever/duckduck_search.py b/application/retriever/duckduck_search.py index 6d2965f5..d746ecaa 100644 --- a/application/retriever/duckduck_search.py +++ b/application/retriever/duckduck_search.py @@ -118,3 +118,15 @@ class DuckDuckSearch(BaseRetriever): def search(self): return self._get_data() + + def get_params(self): + return { + "question": self.question, + "source": self.source, + "chat_history": self.chat_history, + "prompt": self.prompt, + "chunks": self.chunks, + "token_limit": self.token_limit, + "gpt_model": self.gpt_model, + "user_api_key": self.user_api_key + }