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

Fix: Make OpenAIEmbedding work when token usage info is not set #369

Merged
merged 2 commits into from
Nov 27, 2024

Conversation

aniketmaurya
Copy link
Collaborator

@aniketmaurya aniketmaurya commented Nov 27, 2024

Before submitting
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

What does this PR do?

OpenAIEmbeddingSpec requires to set token usage in the context otherwise it fails with the following example. This PR, makes it work and set usage to 0 when not provided.

Error

  File "/Users/aniket/Projects/github/LitServe/src/litserve/api.py", line 90, in encode_response
    return self._spec.encode_response(output, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aniket/Projects/github/LitServe/src/litserve/specs/openai_embedding.py", line 128, in encode_response
    "prompt_tokens": context_kwargs.get("prompt_tokens", 0),
                     ^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get'

Code

import litserve as ls
from sentence_transformers import SentenceTransformer

class EmbeddingsAPI(ls.LitAPI):
    def setup(self, device):
        self.model = SentenceTransformer('all-MiniLM-L6-v2', device=device)

    def predict(self, inputs):
        embeddings = self.model.encode(inputs)
        return embeddings

if __name__ == "__main__":
    api = EmbeddingsAPI()
    server = ls.LitServer(api, spec=ls.OpenAIEmbeddingSpec())
    server.run(port=8000)

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in GitHub issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

Copy link

codecov bot commented Nov 27, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 94%. Comparing base (771a1c9) to head (d1f56ed).
Report is 1 commits behind head on main.

Additional details and impacted files
@@         Coverage Diff         @@
##           main   #369   +/-   ##
===================================
- Coverage    94%    94%   -0%     
===================================
  Files        25     25           
  Lines      1563   1564    +1     
===================================
  Hits       1465   1465           
- Misses       98     99    +1     

@aniketmaurya aniketmaurya enabled auto-merge (squash) November 27, 2024 00:59
@aniketmaurya aniketmaurya merged commit ab6828a into main Nov 27, 2024
20 of 21 checks passed
@aniketmaurya aniketmaurya deleted the aniket/fix-encode-response branch November 27, 2024 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants