-
Notifications
You must be signed in to change notification settings - Fork 72
udpate embed request with embedding_types #359
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
09fb178
udpate embed request with embedding_types
alekhya-n b7cf7b5
embedding types response update
alekhya-n 2390ebf
pyproject update
alekhya-n 5ab52f1
add tests for response type
alekhya-n 4e144d3
lint
alekhya-n 23dd648
fix test
alekhya-n 200af30
use constant in tests
alekhya-n 545e0a8
merge changes
alekhya-n 2a87117
Merge branch 'main' into alekhya/embedding_types
alekhya-n 92d5587
rebase fixed
alekhya-n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,65 @@ | ||
from typing import Any, Dict, Iterator, List, Optional | ||
from typing import Any, Dict, Iterator, List, Optional, Union | ||
|
||
from cohere.responses.base import CohereObject | ||
|
||
EMBEDDINGS_FLOATS_RESPONSE_TYPE = "embeddings_floats" | ||
EMBEDDINGS_BY_TYPE_RESPONSE_TYPE = "embeddings_by_type" | ||
|
||
|
||
class EmbeddingsByType(CohereObject): | ||
def __init__( | ||
self, | ||
float: Optional[List[List[float]]] = None, | ||
int8: Optional[List[List[int]]] = None, | ||
uint8: Optional[List[List[int]]] = None, | ||
binary: Optional[List[List[int]]] = None, | ||
ubinary: Optional[List[List[int]]] = None, | ||
) -> None: | ||
self.float = float | ||
self.int8 = int8 | ||
self.uint8 = uint8 | ||
self.binary = binary | ||
self.ubinary = ubinary | ||
|
||
|
||
class Embeddings(CohereObject): | ||
def __init__( | ||
self, | ||
embeddings: List[List[float]], | ||
compressed_embeddings: List[List[int]] = None, | ||
embeddings: Union[List[List[float]], EmbeddingsByType], | ||
response_type: str, | ||
meta: Optional[Dict[str, Any]] = None, | ||
) -> None: | ||
self.response_type = response_type | ||
self.embeddings = embeddings | ||
self.compressed_embeddings = compressed_embeddings | ||
self.meta = meta | ||
|
||
def __iter__(self) -> Iterator: | ||
return iter(self.embeddings) | ||
|
||
def __len__(self) -> int: | ||
return len(self.embeddings) | ||
|
||
|
||
class EmbeddingResponses: | ||
def __init__( | ||
self, | ||
) -> None: | ||
self.response_type = None | ||
self.embeddings_floats = [] | ||
self.embeddings_by_type = {} | ||
|
||
def add_response(self, response): | ||
self.response_type = response["response_type"] | ||
if self.response_type == EMBEDDINGS_FLOATS_RESPONSE_TYPE: | ||
self.embeddings_floats.extend(response["embeddings"]) | ||
elif self.response_type == EMBEDDINGS_BY_TYPE_RESPONSE_TYPE: | ||
for k, v in response["embeddings"].items(): | ||
if k not in self.embeddings_by_type: | ||
self.embeddings_by_type[k] = [] | ||
self.embeddings_by_type[k].extend(v) | ||
|
||
def get_embeddings(self) -> Union[List[List[float]], EmbeddingsByType]: | ||
if self.response_type == EMBEDDINGS_FLOATS_RESPONSE_TYPE: | ||
return self.embeddings_floats | ||
elif self.response_type == EMBEDDINGS_BY_TYPE_RESPONSE_TYPE: | ||
return EmbeddingsByType(**self.embeddings_by_type) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import pytest | ||
|
||
from cohere.responses import Embeddings | ||
from cohere.responses import EMBEDDINGS_FLOATS_RESPONSE_TYPE, Embeddings | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_async_embed_basic(async_client): | ||
embs = await async_client.embed(model="small", texts=["co:here", "cohere"]) | ||
assert len(embs) == 2 | ||
assert isinstance(embs, Embeddings) | ||
assert embs.response_type == EMBEDDINGS_FLOATS_RESPONSE_TYPE | ||
assert embs.meta | ||
assert embs.meta["api_version"] | ||
assert embs.meta["api_version"]["version"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.