Skip to content

Commit

Permalink
make Chat.prepare idempotent (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeier authored Aug 7, 2024
1 parent 3dc586f commit c98b262
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
22 changes: 7 additions & 15 deletions ragna/core/_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,18 @@ async def prepare(self) -> Message:
Returns:
Welcome message.
Raises:
ragna.core.RagnaException: If chat is already
[`prepare`][ragna.core.Chat.prepare]d.
"""
if self._prepared or self.documents is None:
raise RagnaException(
"Chat is already prepared",
chat=self,
http_status_code=400,
http_detail=RagnaException.EVENT,
)

await self._run(self.source_storage.store, self.documents)
self._prepared = True

welcome = Message(
content="How can I help you with the documents?",
role=MessageRole.SYSTEM,
)

if self._prepared:
return welcome

await self._run(self.source_storage.store, self.documents)
self._prepared = True

self._messages.append(welcome)
return welcome

Expand Down
10 changes: 3 additions & 7 deletions tests/core/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
@pytest.mark.parametrize("input_type", ["corpus", "metadata_filter", "documents"])
def test_e2e(tmp_local_root, input_type):
async def main(*, input, source_storage, assistant):
chat = Rag().chat(
async with Rag().chat(
input=input,
source_storage=source_storage,
assistant=assistant,
)

if not (input is None or isinstance(input, MetadataFilter)):
await chat.prepare()

return await chat.answer("?")
) as chat:
return await chat.answer("?")

document_root = tmp_local_root / "documents"
document_root.mkdir()
Expand Down

0 comments on commit c98b262

Please sign in to comment.