🐛 fix(PostRequest.py): remove redundant session creation and move it to the calling function to improve efficiency and reusability

✨ feat(PostRequest.py): add support for processing multiple documents in a single request to improve performance and reduce network overhead
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-08-22 15:32:00 -03:00
commit 4efe06e440

View file

@ -23,15 +23,13 @@ class PostRequest(CustomComponent):
def post_document( def post_document(
self, self,
session: requests.Session,
document: Document, document: Document,
url: str, url: str,
headers: Optional[dict] = None, headers: Optional[dict] = None,
) -> Document: ) -> Document:
try: try:
with requests.Session() as session: response = session.post(url, headers=headers, data=document.page_content)
response = session.post(
url, headers=headers, data=document.page_content
)
try: try:
response_json = response.json() response_json = response.json()
result = orjson_dumps(response_json, indent_2=False) result = orjson_dumps(response_json, indent_2=False)
@ -67,6 +65,10 @@ class PostRequest(CustomComponent):
if not isinstance(document, list): if not isinstance(document, list):
document = [document] document = [document]
documents = [self.post_document(doc, url, headers) for doc in document]
with requests.Session() as session:
documents = [
self.post_document(session, doc, url, headers) for doc in document
]
self.repr_value = documents self.repr_value = documents
return documents return documents