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

File caching #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions openai_assistant/test/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ def test_has_annotations(self):

result = OpenAIAdapter(message_with_citation).has_annotations()
self.assertTrue(result)

async def test_cache(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests that aren't mocked should be named test_cache_integration. otherwise mock the API client call. See test_on_chat_resume_logic for a way to do this.

adapter = OpenAIAdapter(message_with_citation)
await adapter.main()
self.assertIn("file-A8iZLTqbU5cxrNOWfe4gd404", adapter.files_cache)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work, unless you have the same key. Use TEST_FILE_ID in the env.

Update line 11: load_dotenv('../.env', override=True).

12 changes: 9 additions & 3 deletions openai_assistant/utils/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
We've had a lot of bugs with OpenAI's annotations, so we have to validate the indexes to make sure we don't do substitutions on the wrong indexes, or have missing annotations.

"""

from chainlit import Message
from chainlit.element import Text
from chainlit.logger import logger
Expand Down Expand Up @@ -36,6 +37,7 @@ def __init__(self, message: ThreadMessage) -> None:
)
self.citations: list[TextAnnotationFileCitationFileCitation] = []
self.elements: list[(str, str)] = []
self.files_cache: dict = {}

def has_annotations(self) -> bool:
return bool(self.annotations)
Expand All @@ -56,9 +58,13 @@ async def set_citations(self) -> None:
for annotation in self.annotations:
citation = annotation.file_citation
try:
retrieved_file: FileObject = await self.client.files.retrieve(
annotation.file_citation.file_id
)
if citation.file_id in self.files_cache:
retrieved_file: FileObject = self.files_cache[citation.file_id]
else:
retrieved_file: FileObject = await self.client.files.retrieve(
citation.file_id
)
self.files_cache[citation.file_id] = retrieved_file
filename = retrieved_file.filename
if ".pdf" in filename:
filename = filename[:-4]
Expand Down