rag-core-api 3.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rag_core_api/api_endpoints/__init__.py +0 -0
- rag_core_api/api_endpoints/chat.py +28 -0
- rag_core_api/api_endpoints/information_piece_remover.py +27 -0
- rag_core_api/api_endpoints/information_piece_uploader.py +27 -0
- rag_core_api/apis/__init__.py +0 -0
- rag_core_api/apis/rag_api.py +195 -0
- rag_core_api/apis/rag_api_base.py +102 -0
- rag_core_api/dependency_container.py +274 -0
- rag_core_api/embeddings/__init__.py +0 -0
- rag_core_api/embeddings/embedder.py +5 -0
- rag_core_api/evaluator/__init__.py +0 -0
- rag_core_api/evaluator/evaluator.py +11 -0
- rag_core_api/graph/__init__.py +0 -0
- rag_core_api/graph/graph_base.py +70 -0
- rag_core_api/impl/__init__.py +0 -0
- rag_core_api/impl/answer_generation_chains/__init__.py +0 -0
- rag_core_api/impl/answer_generation_chains/answer_generation_chain.py +67 -0
- rag_core_api/impl/answer_generation_chains/language_detection_chain.py +137 -0
- rag_core_api/impl/answer_generation_chains/rephrasing_chain.py +64 -0
- rag_core_api/impl/api_endpoints/__init__.py +0 -0
- rag_core_api/impl/api_endpoints/default_chat.py +52 -0
- rag_core_api/impl/api_endpoints/default_information_pieces_remover.py +69 -0
- rag_core_api/impl/api_endpoints/default_information_pieces_uploader.py +53 -0
- rag_core_api/impl/embeddings/__init__.py +0 -0
- rag_core_api/impl/embeddings/embedder_type.py +5 -0
- rag_core_api/impl/embeddings/langchain_community_embedder.py +5 -0
- rag_core_api/impl/embeddings/stackit_embedder.py +5 -0
- rag_core_api/impl/evaluator/__init__.py +0 -0
- rag_core_api/impl/evaluator/langfuse_ragas_evaluator.py +259 -0
- rag_core_api/impl/graph/__init__.py +0 -0
- rag_core_api/impl/graph/chat_graph.py +305 -0
- rag_core_api/impl/graph/graph_state/__init__.py +0 -0
- rag_core_api/impl/graph/graph_state/graph_state.py +116 -0
- rag_core_api/impl/rag_api.py +141 -0
- rag_core_api/impl/reranking/__init__.py +0 -0
- rag_core_api/impl/reranking/flashrank_reranker.py +66 -0
- rag_core_api/impl/retriever/__init__.py +0 -0
- rag_core_api/impl/retriever/composite_retriever.py +106 -0
- rag_core_api/impl/retriever/no_or_empty_collection_error.py +5 -0
- rag_core_api/impl/retriever/retriever_quark.py +104 -0
- rag_core_api/impl/settings/__init__.py +0 -0
- rag_core_api/impl/settings/chat_history_settings.py +25 -0
- rag_core_api/impl/settings/embedder_class_type_settings.py +5 -0
- rag_core_api/impl/settings/error_messages.py +46 -0
- rag_core_api/impl/settings/ollama_embedder_settings.py +5 -0
- rag_core_api/impl/settings/ragas_settings.py +41 -0
- rag_core_api/impl/settings/reranker_settings.py +23 -0
- rag_core_api/impl/settings/retriever_settings.py +44 -0
- rag_core_api/impl/settings/sparse_embedder_settings.py +23 -0
- rag_core_api/impl/settings/stackit_embedder_settings.py +5 -0
- rag_core_api/impl/settings/vector_db_settings.py +32 -0
- rag_core_api/impl/vector_databases/__init__.py +0 -0
- rag_core_api/impl/vector_databases/qdrant_database.py +228 -0
- rag_core_api/main.py +62 -0
- rag_core_api/mapper/__init__.py +0 -0
- rag_core_api/mapper/information_piece_mapper.py +166 -0
- rag_core_api/models/__init__.py +0 -0
- rag_core_api/models/chat_history.py +99 -0
- rag_core_api/models/chat_history_message.py +85 -0
- rag_core_api/models/chat_request.py +93 -0
- rag_core_api/models/chat_response.py +103 -0
- rag_core_api/models/chat_role.py +40 -0
- rag_core_api/models/content_type.py +42 -0
- rag_core_api/models/delete_request.py +99 -0
- rag_core_api/models/extra_models.py +9 -0
- rag_core_api/models/information_piece.py +108 -0
- rag_core_api/models/key_value_pair.py +85 -0
- rag_core_api/prompt_templates/__init__.py +0 -0
- rag_core_api/prompt_templates/answer_generation_prompt.py +28 -0
- rag_core_api/prompt_templates/language_detection_prompt.py +23 -0
- rag_core_api/prompt_templates/question_rephrasing_prompt.py +23 -0
- rag_core_api/reranking/__init__.py +0 -0
- rag_core_api/reranking/reranker.py +58 -0
- rag_core_api/retriever/__init__.py +0 -0
- rag_core_api/retriever/retriever.py +59 -0
- rag_core_api/utils/__init__.py +0 -0
- rag_core_api/utils/utils.py +48 -0
- rag_core_api/vector_databases/__init__.py +0 -0
- rag_core_api/vector_databases/vector_database.py +130 -0
- rag_core_api-3.2.0.dist-info/METADATA +31 -0
- rag_core_api-3.2.0.dist-info/RECORD +82 -0
- rag_core_api-3.2.0.dist-info/WHEEL +4 -0
|
File without changes
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Module for base class of chat endpoint."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
from rag_core_api.models.chat_request import ChatRequest
|
|
6
|
+
from rag_core_api.models.chat_response import ChatResponse
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Chat(ABC):
|
|
10
|
+
"""Base class for chat endpoint."""
|
|
11
|
+
|
|
12
|
+
@abstractmethod
|
|
13
|
+
async def achat(self, session_id: str, chat_request: ChatRequest) -> ChatResponse:
|
|
14
|
+
"""
|
|
15
|
+
Abstract method to handle asynchronous chat requests.
|
|
16
|
+
|
|
17
|
+
Parameters
|
|
18
|
+
----------
|
|
19
|
+
session_id : str
|
|
20
|
+
The unique identifier for the chat session.
|
|
21
|
+
chat_request : ChatRequest
|
|
22
|
+
The request object containing the chat details.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
-------
|
|
26
|
+
ChatResponse
|
|
27
|
+
The response object containing the chat results.
|
|
28
|
+
"""
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Module for the InformationPieceRemover abstract base class."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
from rag_core_api.models.delete_request import DeleteRequest
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class InformationPieceRemover(ABC):
|
|
9
|
+
"""Abstract base class for removing information pieces.
|
|
10
|
+
|
|
11
|
+
This class defines the interface for removing information pieces based on a given delete request.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def remove_information_piece(self, delete_request: DeleteRequest) -> None:
|
|
16
|
+
"""
|
|
17
|
+
Remove information pieces based on the given delete request.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
delete_request : DeleteRequest
|
|
22
|
+
The request object containing the details of the information pieces to be deleted.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
-------
|
|
26
|
+
None
|
|
27
|
+
"""
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""Module for the InformationPiecesUploader abstract base class."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
from rag_core_api.models.information_piece import InformationPiece
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class InformationPiecesUploader(ABC):
|
|
9
|
+
"""Abstract base class for uploading information pieces.
|
|
10
|
+
|
|
11
|
+
This class defines the interface for uploading a list of information pieces.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def upload_information_piece(self, information_piece: list[InformationPiece]) -> None:
|
|
16
|
+
"""
|
|
17
|
+
Abstract method to upload a list of information pieces.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
information_piece : list[InformationPiece]
|
|
22
|
+
A list of InformationPiece objects to be uploaded.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
-------
|
|
26
|
+
None
|
|
27
|
+
"""
|
|
File without changes
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"""Module containing the RAG API endpoints."""
|
|
2
|
+
|
|
3
|
+
# coding: utf-8
|
|
4
|
+
# flake8: noqa: D105
|
|
5
|
+
|
|
6
|
+
import importlib
|
|
7
|
+
import logging
|
|
8
|
+
import pkgutil
|
|
9
|
+
from asyncio import FIRST_COMPLETED, CancelledError, create_task, sleep, wait
|
|
10
|
+
from contextlib import suppress
|
|
11
|
+
from typing import Any, Awaitable, List # noqa: F401
|
|
12
|
+
|
|
13
|
+
from fastapi import ( # noqa: F401
|
|
14
|
+
APIRouter,
|
|
15
|
+
BackgroundTasks,
|
|
16
|
+
Body,
|
|
17
|
+
Cookie,
|
|
18
|
+
Depends,
|
|
19
|
+
Form,
|
|
20
|
+
Header,
|
|
21
|
+
HTTPException,
|
|
22
|
+
Path,
|
|
23
|
+
Query,
|
|
24
|
+
Request,
|
|
25
|
+
Response,
|
|
26
|
+
Security,
|
|
27
|
+
status,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
import rag_core_api.impl
|
|
31
|
+
from rag_core_api.apis.rag_api_base import BaseRagApi
|
|
32
|
+
from rag_core_api.models.chat_request import ChatRequest
|
|
33
|
+
from rag_core_api.models.chat_response import ChatResponse
|
|
34
|
+
from rag_core_api.models.delete_request import DeleteRequest
|
|
35
|
+
from rag_core_api.models.information_piece import InformationPiece
|
|
36
|
+
|
|
37
|
+
logger = logging.getLogger(__name__)
|
|
38
|
+
|
|
39
|
+
router = APIRouter()
|
|
40
|
+
|
|
41
|
+
ns_pkg = rag_core_api.impl
|
|
42
|
+
for _, name, _ in pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + "."):
|
|
43
|
+
importlib.import_module(name)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def _disconnected(request: Request) -> None:
|
|
47
|
+
while True:
|
|
48
|
+
try:
|
|
49
|
+
if await request.is_disconnected():
|
|
50
|
+
break
|
|
51
|
+
await sleep(1.0)
|
|
52
|
+
except CancelledError:
|
|
53
|
+
break
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@router.post(
|
|
57
|
+
"/chat/{session_id}",
|
|
58
|
+
responses={
|
|
59
|
+
200: {"model": ChatResponse, "description": "OK."},
|
|
60
|
+
500: {"description": "Internal Server Error!"},
|
|
61
|
+
},
|
|
62
|
+
tags=["rag"],
|
|
63
|
+
response_model_by_alias=True,
|
|
64
|
+
)
|
|
65
|
+
async def chat(
|
|
66
|
+
request: Request,
|
|
67
|
+
session_id: str = Path(..., description=""),
|
|
68
|
+
chat_request: ChatRequest = Body(None, description="Chat with RAG."),
|
|
69
|
+
) -> ChatResponse | None:
|
|
70
|
+
"""
|
|
71
|
+
Asynchronously handles the chat endpoint for the RAG API.
|
|
72
|
+
|
|
73
|
+
Parameters
|
|
74
|
+
----------
|
|
75
|
+
request : Request
|
|
76
|
+
The request object.
|
|
77
|
+
session_id : str
|
|
78
|
+
The session ID for the chat.
|
|
79
|
+
chat_request : ChatRequest, optional
|
|
80
|
+
The chat request payload
|
|
81
|
+
|
|
82
|
+
Returns
|
|
83
|
+
-------
|
|
84
|
+
ChatResponse or None
|
|
85
|
+
The chat response if the chat task completes successfully, otherwise None.
|
|
86
|
+
|
|
87
|
+
Raises
|
|
88
|
+
------
|
|
89
|
+
CancelledError
|
|
90
|
+
If the task is cancelled.
|
|
91
|
+
|
|
92
|
+
Notes
|
|
93
|
+
-----
|
|
94
|
+
This function creates two asynchronous tasks: one for handling disconnection and one for processing the chat
|
|
95
|
+
request. It waits for either task to complete first and cancels the remaining tasks.
|
|
96
|
+
"""
|
|
97
|
+
disconnect_task = create_task(_disconnected(request))
|
|
98
|
+
chat_task = create_task(BaseRagApi.subclasses[0]().chat(session_id, chat_request))
|
|
99
|
+
done, pending = await wait(
|
|
100
|
+
[disconnect_task, chat_task],
|
|
101
|
+
return_when=FIRST_COMPLETED,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# cancel all remaining tasks
|
|
105
|
+
for task in pending:
|
|
106
|
+
task.cancel()
|
|
107
|
+
with suppress(CancelledError):
|
|
108
|
+
await task
|
|
109
|
+
if chat_task in done:
|
|
110
|
+
return chat_task.result()
|
|
111
|
+
logger.info("Request got cancelled!")
|
|
112
|
+
return None
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@router.post(
|
|
116
|
+
"/evaluate",
|
|
117
|
+
responses={
|
|
118
|
+
201: {"description": "Accepted."},
|
|
119
|
+
500: {"description": "Internal Server Error."},
|
|
120
|
+
},
|
|
121
|
+
tags=["rag"],
|
|
122
|
+
response_model_by_alias=True,
|
|
123
|
+
)
|
|
124
|
+
async def evaluate() -> None:
|
|
125
|
+
"""
|
|
126
|
+
Asynchronously evaluate the RAG.
|
|
127
|
+
|
|
128
|
+
Returns
|
|
129
|
+
-------
|
|
130
|
+
None
|
|
131
|
+
"""
|
|
132
|
+
return await BaseRagApi.subclasses[0]().evaluate()
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@router.post(
|
|
136
|
+
"/information_pieces/remove",
|
|
137
|
+
responses={
|
|
138
|
+
202: {"description": "Accepted."},
|
|
139
|
+
404: {"description": "Ressource not Found"},
|
|
140
|
+
422: {"description": "ID or metadata missing."},
|
|
141
|
+
500: {"description": "Internal Server Error."},
|
|
142
|
+
},
|
|
143
|
+
tags=["rag"],
|
|
144
|
+
summary="remove information piece",
|
|
145
|
+
response_model_by_alias=True,
|
|
146
|
+
)
|
|
147
|
+
async def remove_information_piece(
|
|
148
|
+
delete_request: DeleteRequest = Body(None, description=""),
|
|
149
|
+
) -> None:
|
|
150
|
+
"""
|
|
151
|
+
Asynchronously removes information pieces.
|
|
152
|
+
|
|
153
|
+
This endpoint removes information pieces based on the provided delete request.
|
|
154
|
+
|
|
155
|
+
Parameters
|
|
156
|
+
----------
|
|
157
|
+
delete_request : DeleteRequest
|
|
158
|
+
The request body containing the details for the information piece to be removed.
|
|
159
|
+
|
|
160
|
+
Returns
|
|
161
|
+
-------
|
|
162
|
+
None
|
|
163
|
+
"""
|
|
164
|
+
return await BaseRagApi.subclasses[0]().remove_information_piece(delete_request)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@router.post(
|
|
168
|
+
"/information_pieces/upload",
|
|
169
|
+
responses={
|
|
170
|
+
201: {"description": "The file was successful uploaded."},
|
|
171
|
+
422: {"model": str, "description": "Wrong json format."},
|
|
172
|
+
500: {"model": str, "description": "Internal Server Error."},
|
|
173
|
+
},
|
|
174
|
+
tags=["rag"],
|
|
175
|
+
summary="Upload information pieces for vectordatabase",
|
|
176
|
+
response_model_by_alias=True,
|
|
177
|
+
)
|
|
178
|
+
async def upload_information_piece(
|
|
179
|
+
information_piece: List[InformationPiece] = Body(None, description=""),
|
|
180
|
+
) -> None:
|
|
181
|
+
"""
|
|
182
|
+
Asynchronously uploads information pieces for vectordatabase.
|
|
183
|
+
|
|
184
|
+
This endpoint allows for the upload of information pieces to the vector database.
|
|
185
|
+
|
|
186
|
+
Parameters
|
|
187
|
+
----------
|
|
188
|
+
information_piece : List[InformationPiece]
|
|
189
|
+
A list of information pieces to be uploaded (default None).
|
|
190
|
+
|
|
191
|
+
Returns
|
|
192
|
+
-------
|
|
193
|
+
None
|
|
194
|
+
"""
|
|
195
|
+
return await BaseRagApi.subclasses[0]().upload_information_piece(information_piece)
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""Module containing the base RAG API class."""
|
|
2
|
+
|
|
3
|
+
# coding: utf-8
|
|
4
|
+
# flake8: noqa: D105
|
|
5
|
+
|
|
6
|
+
from typing import ClassVar, Dict, List, Tuple # noqa: F401
|
|
7
|
+
|
|
8
|
+
from rag_core_api.models.chat_request import ChatRequest
|
|
9
|
+
from rag_core_api.models.chat_response import ChatResponse
|
|
10
|
+
from rag_core_api.models.delete_request import DeleteRequest
|
|
11
|
+
from rag_core_api.models.information_piece import InformationPiece
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class BaseRagApi:
|
|
15
|
+
"""Base class for RAG API.
|
|
16
|
+
|
|
17
|
+
This class serves as the base for the RAG API implementations. It provides
|
|
18
|
+
asynchronous methods for handling chat, evaluating the RAG, removing information
|
|
19
|
+
pieces, and uploading information pieces to a vector database.
|
|
20
|
+
|
|
21
|
+
Attributes
|
|
22
|
+
subclasses : ClassVar[Tuple]
|
|
23
|
+
A tuple that holds all subclasses of BaseRagApi.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
subclasses: ClassVar[Tuple] = ()
|
|
27
|
+
|
|
28
|
+
def __init_subclass__(cls, **kwargs):
|
|
29
|
+
super().__init_subclass__(**kwargs)
|
|
30
|
+
BaseRagApi.subclasses = BaseRagApi.subclasses + (cls,)
|
|
31
|
+
|
|
32
|
+
async def chat(
|
|
33
|
+
self,
|
|
34
|
+
session_id: str,
|
|
35
|
+
chat_request: ChatRequest,
|
|
36
|
+
) -> ChatResponse:
|
|
37
|
+
"""
|
|
38
|
+
Asynchronously handles the chat endpoint for the RAG API.
|
|
39
|
+
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
|
+
request : Request
|
|
43
|
+
The request object.
|
|
44
|
+
session_id : str
|
|
45
|
+
The session ID for the chat.
|
|
46
|
+
chat_request : ChatRequest, optional
|
|
47
|
+
The chat request payload
|
|
48
|
+
|
|
49
|
+
Returns
|
|
50
|
+
-------
|
|
51
|
+
ChatResponse or None
|
|
52
|
+
The chat response if the chat task completes successfully, otherwise None.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
async def evaluate(
|
|
56
|
+
self,
|
|
57
|
+
) -> None:
|
|
58
|
+
"""
|
|
59
|
+
Asynchronously evaluate the RAG.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
None
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
async def remove_information_piece(
|
|
67
|
+
self,
|
|
68
|
+
delete_request: DeleteRequest,
|
|
69
|
+
) -> None:
|
|
70
|
+
"""
|
|
71
|
+
Asynchronously removes information pieces.
|
|
72
|
+
|
|
73
|
+
This endpoint removes information pieces based on the provided delete request.
|
|
74
|
+
|
|
75
|
+
Parameters
|
|
76
|
+
----------
|
|
77
|
+
delete_request : DeleteRequest
|
|
78
|
+
The request body containing the details for the information piece to be removed.
|
|
79
|
+
|
|
80
|
+
Returns
|
|
81
|
+
-------
|
|
82
|
+
None
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
async def upload_information_piece(
|
|
86
|
+
self,
|
|
87
|
+
information_piece: List[InformationPiece],
|
|
88
|
+
) -> None:
|
|
89
|
+
"""
|
|
90
|
+
Asynchronously uploads information pieces for vectordatabase.
|
|
91
|
+
|
|
92
|
+
This endpoint allows for the upload of information pieces to the vector database.
|
|
93
|
+
|
|
94
|
+
Parameters
|
|
95
|
+
----------
|
|
96
|
+
information_piece : List[InformationPiece]
|
|
97
|
+
A list of information pieces to be uploaded (default None).
|
|
98
|
+
|
|
99
|
+
Returns
|
|
100
|
+
-------
|
|
101
|
+
None
|
|
102
|
+
"""
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
"""Module containing the dependency injection container for managing application dependencies."""
|
|
2
|
+
|
|
3
|
+
import qdrant_client
|
|
4
|
+
from dependency_injector.containers import DeclarativeContainer
|
|
5
|
+
from dependency_injector.providers import ( # noqa: WOT001
|
|
6
|
+
Configuration,
|
|
7
|
+
List,
|
|
8
|
+
Selector,
|
|
9
|
+
Singleton,
|
|
10
|
+
)
|
|
11
|
+
from langchain_community.document_compressors.flashrank_rerank import FlashrankRerank
|
|
12
|
+
from langchain_community.embeddings.ollama import OllamaEmbeddings
|
|
13
|
+
from langchain_ollama import ChatOllama
|
|
14
|
+
from langchain_openai import ChatOpenAI
|
|
15
|
+
from langchain_qdrant import QdrantVectorStore, FastEmbedSparse
|
|
16
|
+
from langfuse import Langfuse
|
|
17
|
+
|
|
18
|
+
from rag_core_api.impl.answer_generation_chains.answer_generation_chain import (
|
|
19
|
+
AnswerGenerationChain,
|
|
20
|
+
)
|
|
21
|
+
from rag_core_api.impl.answer_generation_chains.rephrasing_chain import RephrasingChain
|
|
22
|
+
from rag_core_api.impl.answer_generation_chains.language_detection_chain import LanguageDetectionChain
|
|
23
|
+
from rag_core_api.impl.api_endpoints.default_chat import DefaultChat
|
|
24
|
+
from rag_core_api.impl.api_endpoints.default_information_pieces_remover import (
|
|
25
|
+
DefaultInformationPiecesRemover,
|
|
26
|
+
)
|
|
27
|
+
from rag_core_api.impl.api_endpoints.default_information_pieces_uploader import (
|
|
28
|
+
DefaultInformationPiecesUploader,
|
|
29
|
+
)
|
|
30
|
+
from rag_core_api.impl.embeddings.langchain_community_embedder import (
|
|
31
|
+
LangchainCommunityEmbedder,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
from rag_core_api.impl.embeddings.stackit_embedder import StackitEmbedder
|
|
36
|
+
from rag_core_api.impl.evaluator.langfuse_ragas_evaluator import LangfuseRagasEvaluator
|
|
37
|
+
from rag_core_api.impl.graph.chat_graph import DefaultChatGraph
|
|
38
|
+
from rag_core_api.impl.reranking.flashrank_reranker import FlashrankReranker
|
|
39
|
+
from rag_core_api.impl.retriever.composite_retriever import CompositeRetriever
|
|
40
|
+
from rag_core_api.impl.retriever.retriever_quark import RetrieverQuark
|
|
41
|
+
from rag_core_api.impl.settings.chat_history_settings import ChatHistorySettings
|
|
42
|
+
from rag_core_api.impl.settings.embedder_class_type_settings import (
|
|
43
|
+
EmbedderClassTypeSettings,
|
|
44
|
+
)
|
|
45
|
+
from rag_core_api.impl.settings.error_messages import ErrorMessages
|
|
46
|
+
from rag_core_api.impl.settings.ollama_embedder_settings import OllamaEmbedderSettings
|
|
47
|
+
from rag_core_api.impl.settings.ragas_settings import RagasSettings
|
|
48
|
+
from rag_core_api.impl.settings.reranker_settings import RerankerSettings
|
|
49
|
+
from rag_core_api.impl.settings.retriever_settings import RetrieverSettings
|
|
50
|
+
from rag_core_api.impl.settings.sparse_embedder_settings import SparseEmbedderSettings
|
|
51
|
+
from rag_core_api.impl.settings.stackit_embedder_settings import StackitEmbedderSettings
|
|
52
|
+
from rag_core_api.impl.settings.vector_db_settings import VectorDatabaseSettings
|
|
53
|
+
from rag_core_api.impl.vector_databases.qdrant_database import QdrantDatabase
|
|
54
|
+
from rag_core_api.mapper.information_piece_mapper import InformationPieceMapper
|
|
55
|
+
from rag_core_api.prompt_templates.answer_generation_prompt import (
|
|
56
|
+
ANSWER_GENERATION_PROMPT,
|
|
57
|
+
)
|
|
58
|
+
from rag_core_api.prompt_templates.question_rephrasing_prompt import (
|
|
59
|
+
QUESTION_REPHRASING_PROMPT,
|
|
60
|
+
)
|
|
61
|
+
from rag_core_api.prompt_templates.language_detection_prompt import LANGUAGE_DETECTION_PROMPT
|
|
62
|
+
from rag_core_lib.impl.data_types.content_type import ContentType
|
|
63
|
+
from rag_core_lib.impl.langfuse_manager.langfuse_manager import LangfuseManager
|
|
64
|
+
from rag_core_lib.impl.llms.llm_factory import chat_model_provider
|
|
65
|
+
from rag_core_lib.impl.settings.langfuse_settings import LangfuseSettings
|
|
66
|
+
from rag_core_lib.impl.settings.ollama_llm_settings import OllamaSettings
|
|
67
|
+
from rag_core_lib.impl.settings.rag_class_types_settings import RAGClassTypeSettings
|
|
68
|
+
from rag_core_lib.impl.settings.retry_decorator_settings import RetryDecoratorSettings
|
|
69
|
+
from rag_core_lib.impl.settings.stackit_vllm_settings import StackitVllmSettings
|
|
70
|
+
from rag_core_lib.impl.tracers.langfuse_traced_runnable import LangfuseTracedRunnable
|
|
71
|
+
from rag_core_lib.impl.utils.async_threadsafe_semaphore import AsyncThreadsafeSemaphore
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class DependencyContainer(DeclarativeContainer):
|
|
75
|
+
"""Dependency injection container for managing application dependencies."""
|
|
76
|
+
|
|
77
|
+
class_selector_config = Configuration()
|
|
78
|
+
chat_history_config = Configuration()
|
|
79
|
+
|
|
80
|
+
# Settings
|
|
81
|
+
vector_database_settings = VectorDatabaseSettings()
|
|
82
|
+
retriever_settings = RetrieverSettings()
|
|
83
|
+
ollama_settings = OllamaSettings()
|
|
84
|
+
ollama_embedder_settings = OllamaEmbedderSettings()
|
|
85
|
+
langfuse_settings = LangfuseSettings()
|
|
86
|
+
stackit_vllm_settings = StackitVllmSettings()
|
|
87
|
+
error_messages = ErrorMessages()
|
|
88
|
+
rag_class_type_settings = RAGClassTypeSettings()
|
|
89
|
+
ragas_settings = RagasSettings()
|
|
90
|
+
reranker_settings = RerankerSettings()
|
|
91
|
+
embedder_class_type_settings = EmbedderClassTypeSettings()
|
|
92
|
+
stackit_embedder_settings = StackitEmbedderSettings()
|
|
93
|
+
chat_history_settings = ChatHistorySettings()
|
|
94
|
+
sparse_embedder_settings = SparseEmbedderSettings()
|
|
95
|
+
retry_decorator_settings = RetryDecoratorSettings()
|
|
96
|
+
chat_history_config.from_dict(chat_history_settings.model_dump())
|
|
97
|
+
|
|
98
|
+
class_selector_config.from_dict(rag_class_type_settings.model_dump() | embedder_class_type_settings.model_dump())
|
|
99
|
+
|
|
100
|
+
embedder = Selector(
|
|
101
|
+
class_selector_config.embedder_type,
|
|
102
|
+
ollama=Singleton(
|
|
103
|
+
LangchainCommunityEmbedder, embedder=Singleton(OllamaEmbeddings, **ollama_embedder_settings.model_dump())
|
|
104
|
+
),
|
|
105
|
+
stackit=Singleton(StackitEmbedder, stackit_embedder_settings, retry_decorator_settings),
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
sparse_embedder = Singleton(FastEmbedSparse, **sparse_embedder_settings.model_dump())
|
|
109
|
+
|
|
110
|
+
vectordb_client = Singleton(
|
|
111
|
+
qdrant_client.QdrantClient,
|
|
112
|
+
location=vector_database_settings.location,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
vectorstore = Singleton(
|
|
116
|
+
QdrantVectorStore,
|
|
117
|
+
client=vectordb_client,
|
|
118
|
+
collection_name=vector_database_settings.collection_name,
|
|
119
|
+
embedding=embedder,
|
|
120
|
+
sparse_embedding=sparse_embedder,
|
|
121
|
+
validate_collection_config=False,
|
|
122
|
+
retrieval_mode=vector_database_settings.retrieval_mode,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
vector_database = Singleton(
|
|
126
|
+
QdrantDatabase,
|
|
127
|
+
settings=vector_database_settings,
|
|
128
|
+
embedder=embedder,
|
|
129
|
+
sparse_embedder=sparse_embedder,
|
|
130
|
+
vectorstore=vectorstore,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
flashrank_reranker = Singleton(FlashrankRerank, top_n=reranker_settings.k_documents)
|
|
134
|
+
reranker = Singleton(FlashrankReranker, flashrank_reranker)
|
|
135
|
+
|
|
136
|
+
information_pieces_uploader = Singleton(DefaultInformationPiecesUploader, vector_database)
|
|
137
|
+
|
|
138
|
+
information_pieces_remover = Singleton(DefaultInformationPiecesRemover, vector_database)
|
|
139
|
+
|
|
140
|
+
image_retriever = Singleton(
|
|
141
|
+
RetrieverQuark,
|
|
142
|
+
vector_database,
|
|
143
|
+
ContentType.IMAGE,
|
|
144
|
+
retriever_settings.image_k_documents,
|
|
145
|
+
retriever_settings.image_threshold,
|
|
146
|
+
)
|
|
147
|
+
table_retriever = Singleton(
|
|
148
|
+
RetrieverQuark,
|
|
149
|
+
vector_database,
|
|
150
|
+
ContentType.TABLE,
|
|
151
|
+
retriever_settings.table_k_documents,
|
|
152
|
+
retriever_settings.table_threshold,
|
|
153
|
+
)
|
|
154
|
+
text_retriever = Singleton(
|
|
155
|
+
RetrieverQuark,
|
|
156
|
+
vector_database,
|
|
157
|
+
ContentType.TEXT,
|
|
158
|
+
retriever_settings.k_documents,
|
|
159
|
+
retriever_settings.threshold,
|
|
160
|
+
)
|
|
161
|
+
summary_retriever = Singleton(
|
|
162
|
+
RetrieverQuark,
|
|
163
|
+
vector_database,
|
|
164
|
+
ContentType.SUMMARY,
|
|
165
|
+
retriever_settings.summary_k_documents,
|
|
166
|
+
retriever_settings.summary_threshold,
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
composed_retriever = Singleton(
|
|
170
|
+
CompositeRetriever,
|
|
171
|
+
List(image_retriever, table_retriever, text_retriever, summary_retriever),
|
|
172
|
+
reranker,
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
information_piece_mapper = Singleton(InformationPieceMapper)
|
|
176
|
+
|
|
177
|
+
large_language_model = Selector(
|
|
178
|
+
class_selector_config.llm_type,
|
|
179
|
+
ollama=Singleton(chat_model_provider, ollama_settings, "ollama"),
|
|
180
|
+
stackit=Singleton(chat_model_provider, stackit_vllm_settings, "openai"),
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
prompt = ANSWER_GENERATION_PROMPT
|
|
184
|
+
rephrasing_prompt = QUESTION_REPHRASING_PROMPT
|
|
185
|
+
language_detection_prompt = LANGUAGE_DETECTION_PROMPT
|
|
186
|
+
|
|
187
|
+
langfuse = Singleton(
|
|
188
|
+
Langfuse,
|
|
189
|
+
public_key=langfuse_settings.public_key,
|
|
190
|
+
secret_key=langfuse_settings.secret_key,
|
|
191
|
+
host=langfuse_settings.host,
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
langfuse_manager = Singleton(
|
|
195
|
+
LangfuseManager,
|
|
196
|
+
langfuse=langfuse,
|
|
197
|
+
managed_prompts={
|
|
198
|
+
AnswerGenerationChain.__name__: prompt,
|
|
199
|
+
RephrasingChain.__name__: rephrasing_prompt,
|
|
200
|
+
LanguageDetectionChain.__name__: language_detection_prompt,
|
|
201
|
+
},
|
|
202
|
+
llm=large_language_model,
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
answer_generation_chain = Singleton(
|
|
206
|
+
AnswerGenerationChain,
|
|
207
|
+
langfuse_manager=langfuse_manager,
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
rephrasing_chain = Singleton(
|
|
211
|
+
RephrasingChain,
|
|
212
|
+
langfuse_manager=langfuse_manager,
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
language_detection_chain = Singleton(
|
|
216
|
+
LanguageDetectionChain,
|
|
217
|
+
langfuse_manager=langfuse_manager,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
chat_graph = Singleton(
|
|
221
|
+
DefaultChatGraph,
|
|
222
|
+
composed_retriever=composed_retriever,
|
|
223
|
+
rephrasing_chain=rephrasing_chain,
|
|
224
|
+
language_detection_chain=language_detection_chain,
|
|
225
|
+
mapper=information_piece_mapper,
|
|
226
|
+
answer_generation_chain=answer_generation_chain,
|
|
227
|
+
error_messages=error_messages,
|
|
228
|
+
chat_history_settings=chat_history_settings,
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
# wrap graph in tracer
|
|
232
|
+
traced_chat_graph = Singleton(
|
|
233
|
+
LangfuseTracedRunnable,
|
|
234
|
+
inner_chain=chat_graph,
|
|
235
|
+
settings=langfuse_settings,
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
chat_endpoint = Singleton(DefaultChat, traced_chat_graph)
|
|
239
|
+
|
|
240
|
+
ragas_llm = (
|
|
241
|
+
Singleton(
|
|
242
|
+
ChatOpenAI,
|
|
243
|
+
model=ragas_settings.model,
|
|
244
|
+
timeout=ragas_settings.timeout,
|
|
245
|
+
api_key=ragas_settings.openai_api_key,
|
|
246
|
+
)
|
|
247
|
+
if ragas_settings.use_openai
|
|
248
|
+
else Selector(
|
|
249
|
+
class_selector_config.llm_type,
|
|
250
|
+
stackit=Singleton(
|
|
251
|
+
ChatOpenAI,
|
|
252
|
+
model=ragas_settings.model if ragas_settings.model else stackit_vllm_settings.model,
|
|
253
|
+
timeout=ragas_settings.timeout,
|
|
254
|
+
openai_api_base=stackit_vllm_settings.base_url,
|
|
255
|
+
openai_api_key=stackit_vllm_settings.api_key,
|
|
256
|
+
),
|
|
257
|
+
ollama=Singleton(
|
|
258
|
+
ChatOllama,
|
|
259
|
+
model=ragas_settings.model if ragas_settings.model else ollama_settings.model,
|
|
260
|
+
base_url=ollama_settings.base_url,
|
|
261
|
+
),
|
|
262
|
+
)
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
evaluator = Singleton(
|
|
266
|
+
LangfuseRagasEvaluator,
|
|
267
|
+
chat_endpoint=chat_endpoint,
|
|
268
|
+
settings=ragas_settings,
|
|
269
|
+
langfuse_manager=langfuse_manager,
|
|
270
|
+
embedder=embedder,
|
|
271
|
+
semaphore=Singleton(AsyncThreadsafeSemaphore, ragas_settings.max_concurrency),
|
|
272
|
+
chat_history_config=chat_history_config,
|
|
273
|
+
chat_llm=ragas_llm,
|
|
274
|
+
)
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Module for the Evaluator class."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Evaluator(ABC):
|
|
7
|
+
"""Evaluator is responsible for evaluating questions in a dataset using various metrics."""
|
|
8
|
+
|
|
9
|
+
@abstractmethod
|
|
10
|
+
async def aevaluate() -> None:
|
|
11
|
+
"""Asynchronously evaluates the questions in the evaluation dataset."""
|
|
File without changes
|