Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Chat.prepare idempotent #482

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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