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

Cache files retrieval call in annotations.set_citations #49

Open
dahifi opened this issue Jun 11, 2024 · 0 comments · May be fixed by #50
Open

Cache files retrieval call in annotations.set_citations #49

dahifi opened this issue Jun 11, 2024 · 0 comments · May be fixed by #50
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@dahifi
Copy link
Member

dahifi commented Jun 11, 2024

    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
                )

To cache the client retrieve call, you can use a dictionary to store the results of previous calls. Before making a call, you can check if the result is already in the cache. If it is, you can return the cached result instead of making a new call. Here's how you can modify the set_citations method to include caching:

class OpenAIAdapter:
    def __init__(self, message: ThreadMessage) -> None:
        # ...
        self.file_cache = {}

    async def set_citations(self) -> None:
        for annotation in self.annotations:
            citation = annotation.file_citation
            try:
                if citation.file_id in self.file_cache:
                    retrieved_file = self.file_cache[citation.file_id]
                else:
                    retrieved_file: FileObject = await self.client.files.retrieve(
                        citation.file_id
                    )
                    self.file_cache[citation.file_id] = retrieved_file

                # rest of your code...
            except Exception as e:
                # handle exception...

In this code, self.file_cache is a dictionary that stores the results of previous calls to self.client.files.retrieve. Before making a call, we check if the result is already in self.file_cache. If it is, we use the cached result instead of making a new call. If it's not, we make the call and store the result in self.file_cache for future use.

@dahifi dahifi added the enhancement New feature or request label Jun 11, 2024
@dahifi dahifi added this to the v1 milestone Jun 11, 2024
@Lauren1066 Lauren1066 linked a pull request Jun 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants