griptape-ai / griptape

Modular Python framework for AI agents and workflows with chain-of-thought reasoning, tools, and memory.
https://www.griptape.ai
Apache License 2.0
1.95k stars 160 forks source link

400 error from Cohere if there is no documents in `TextChunksRerankRagModule` using `CohereRerankDriver` #1152

Closed vachillo closed 3 weeks ago

vachillo commented 3 weeks ago

Describe the bug CohereRerankDriver throws a 400 if there is no documents getting sent.

To Reproduce

            engine = RagEngine(
                retrieval_stage=RetrievalRagStage(
                    retrieval_modules=[
                        self.KnowledgeBaseSearchRetrievalRagModule(
                            query=query,
                            count=count,
                            include_vectors=include_vectors,
                            distance_metric=distance_metric,
                            filter=filter,
                            knowledge_base=knowledge_base,
                        ),
                    ],
                    rerank_module=TextChunksRerankRagModule(
                        rerank_driver=rerank_driver
                    ),
                ),
                response_stage=ResponseRagStage(
                    response_modules=[
                        PromptResponseRagModule(
                            prompt_driver=prompt_driver
                        )
                    ]
                ),
            )

Expected behavior The RagEngine should be able to handle the fact that there is nothing returned to get reranked.

Screenshots

| Traceback (most recent call last):
|   File "/opt/webapp/api/griptapecloud/components/knowledge_bases/views.py", line 171, in post
|     context = engine.process_query(query)
|               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/opt/webapp/api/.venv/lib/python3.11/site-packages/griptape/engines/rag/rag_engine.py", line 37, in process_query
|     return self.process(RagContext(query=query))
|            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/opt/webapp/api/.venv/lib/python3.11/site-packages/griptape/engines/rag/rag_engine.py", line 44, in process
|     context = self.retrieval_stage.run(context)
|               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/opt/webapp/api/.venv/lib/python3.11/site-packages/griptape/engines/rag/stages/retrieval_rag_stage.py", line 62, in run
|     context.text_chunks = [a for a in self.rerank_module.run(context) if isinstance(a, TextArtifact)]
|                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/opt/webapp/api/.venv/lib/python3.11/site-packages/griptape/engines/rag/modules/retrieval/text_chunks_rerank_rag_module.py", line 13, in run
|     return self.rerank_driver.run(context.query, context.text_chunks)
|            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|   File "/opt/webapp/api/.venv/lib/python3.11/site-packages/griptape/drivers/rerank/cohere_rerank_driver.py", line 28, in run
|     response = self.client.rerank(
|                ^^^^^^^^^^^^^^^^^^^
|   File "/opt/webapp/api/.venv/lib/python3.11/site-packages/cohere/base_client.py", line 1595, in rerank
|     raise BadRequestError(
| cohere.errors.bad_request_error.BadRequestError: status_code: 400, body: {'message': 'invalid request: list of documents must not be empty'}

Desktop (please complete the following information):

Additional context Add any other context about the problem here.