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

refactor: Migrate Cohere to V2 #1321

Merged
merged 8 commits into from
Jan 28, 2025
Merged

refactor: Migrate Cohere to V2 #1321

merged 8 commits into from
Jan 28, 2025

Conversation

vblagoje
Copy link
Member

@vblagoje vblagoje commented Jan 24, 2025

Why:

  • Migrates our Cohere integration to V2 (embedder(s) and ranker).
  • Embedders can now use embedding_types to choose the size of the embeddings, providing more control over memory usage and performance.
  • (Chat)Generator migration is done in a separate PR.

What:

  • Introduced EmbeddingTypes enum to define and manage supported embedding types:
    • Allowed values:
      • float: Default float embeddings (valid for all models).
      • int8: Signed int8 embeddings (valid for v3 models only).
      • uint8: Unsigned int8 embeddings (valid for v3 models only).
      • binary: Signed binary embeddings (valid for v3 models only).
      • ubinary: Unsigned binary embeddings (valid for v3 models only).
  • Updated CohereDocumentEmbedder and CohereTextEmbedder to handle multiple embedding types.
  • Updated CohereRanker to use Cohere V2 API, added max_tokens_per_doc for token-level control, and deprecated the use of max_chunks_per_doc (now ignored).

How can it be used:

Users can specify the desired embedding type and initialize the embedders or ranker:

from haystack_integrations.components.embedders.cohere import CohereDocumentEmbedder
from haystack_integrations.components.embedders.cohere.embedding_types import EmbeddingTypes
from haystack_integrations.components.rankers.cohere import CohereRanker

embedder = CohereDocumentEmbedder(
    api_key="your_api_key",
    model="embed-english-v3.0",
    embedding_type=EmbeddingTypes.INT8,
)

ranker = CohereRanker(
    api_key="your_api_key",
    model="rerank-english-v2.0",
    max_tokens_per_doc=500,
)

How did you test it:

  • Updated and added unit tests for CohereDocumentEmbedder and CohereTextEmbedder to validate functionality with multiple embedding types.
  • Verified correct handling of embedding type combinations (e.g., int8, binary).
  • Updated CohereRanker tests

Notes for the reviewer:

  • Check the handling of embedding_types in the embedders to ensure that only valid types are accepted for v3 models.
  • Verify that max_tokens_per_doc in CohereRanker works as intended and aligns with the Cohere V2 API.
  • Ensure the integration supports seamless switching between embedding types and document configurations.
  • To be merged in tandem with PR and then a new major version of cohere-haystack integration should be released.

@github-actions github-actions bot added integration:cohere type:documentation Improvements or additions to documentation labels Jan 24, 2025
@vblagoje vblagoje marked this pull request as ready for review January 24, 2025 13:35
@vblagoje vblagoje requested a review from a team as a code owner January 24, 2025 13:35
@vblagoje vblagoje requested review from anakin87 and davidsbatista and removed request for a team January 24, 2025 13:35
@vblagoje
Copy link
Member Author

@davidsbatista please review ranker changes. This is for the next week @anakin87 @davidsbatista

Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

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

I left some comments, but in general I agree with the direction of this PR.

Just one question: should we use the opportunity of this refactoring to move the client definition to __init__?

Copy link
Contributor

@davidsbatista davidsbatista left a comment

Choose a reason for hiding this comment

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

LGTM, I would just suggest to replace the None by 4096 as this is the default value from Chohere API V2 (https://docs.cohere.com/v2/reference/rerank)

@vblagoje vblagoje marked this pull request as draft January 28, 2025 09:24
@vblagoje vblagoje marked this pull request as ready for review January 28, 2025 09:24
Copy link
Contributor

@davidsbatista davidsbatista left a comment

Choose a reason for hiding this comment

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

LGTM from my side

@anakin87
Copy link
Member

Looks good, but please answer this question:

should we use the opportunity of this refactoring to move the client definition to __init__?

@davidsbatista
Copy link
Contributor

@anakin87 so that we can then simply do, for instance:

from haystack_integrations.amazon_bedrock import AmazonBedrockDocumentEmbedder

or any other component?

I would vote for this simplification in importing

@anakin87
Copy link
Member

@davidsbatista no no, sorry if I did not explain myself better (we have discussed this with Vladimir in the past).

Currently, the Cohere client is recreated every time the run method is called.
In other components, we usually create the client at initialization and reuse it, so I propose to consider this refactoring as an opportunity to introduce this improvement.

@vblagoje
Copy link
Member Author

Ah right, @anakin87 apologies - too many context switches. Yes, let's fix that one in a separate PR to ease the cognitive load - as IIRC we agreed privately?

@anakin87 anakin87 self-requested a review January 28, 2025 10:31
Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

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

Yes, let's fix that one in a separate PR to ease the cognitive load - as IIRC we agreed privately?

OK

@vblagoje vblagoje merged commit b666b5a into main Jan 28, 2025
10 checks passed
@vblagoje vblagoje deleted the migrate_cohere_v1_v2 branch January 28, 2025 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration:cohere type:documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants