🐛 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,29 +23,27 @@ 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( try:
url, headers=headers, data=document.page_content response_json = response.json()
) result = orjson_dumps(response_json, indent_2=False)
try: except Exception:
response_json = response.json() result = response.text
result = orjson_dumps(response_json, indent_2=False) self.repr_value = result
except Exception: return Document(
result = response.text page_content=result,
self.repr_value = result metadata={
return Document( "source": url,
page_content=result, "headers": headers,
metadata={ "status_code": response,
"source": url, },
"headers": headers, )
"status_code": response,
},
)
except Exception as exc: except Exception as exc:
return Document( return Document(
page_content=str(exc), page_content=str(exc),
@ -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]
self.repr_value = documents with requests.Session() as session:
documents = [
self.post_document(session, doc, url, headers) for doc in document
]
self.repr_value = documents
return documents return documents