-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat(rag): add VoyageAI voyage-context-4 contextualized embedding support #6368
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
Open
fzowl
wants to merge
1
commit into
crewAIInc:main
Choose a base branch
from
fzowl:feat/embedding-model-voyage-context-4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−1
Open
Changes from all commits
Commits
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
104 changes: 104 additions & 0 deletions
104
lib/crewai/tests/rag/embeddings/test_voyageai_embedding_callable.py
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 |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """Tests for the VoyageAI embedding function.""" | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import numpy as np | ||
|
|
||
| from crewai.rag.embeddings.providers.voyageai.embedding_callable import ( | ||
| CONTEXTUALIZED_CHUNK_SIZE, | ||
| VoyageAIEmbeddingFunction, | ||
| ) | ||
|
|
||
|
|
||
| class TestVoyageAIEmbeddingFunction: | ||
| """Test the VoyageAI embedding function call routing.""" | ||
|
|
||
| def test_standard_model_uses_embed(self): | ||
| """Standard models should call the regular embed endpoint.""" | ||
| with patch("voyageai.Client") as mock_client_class: | ||
| mock_client = MagicMock() | ||
| mock_client_class.return_value = mock_client | ||
| mock_client.embed.return_value = MagicMock(embeddings=[[0.1, 0.2]]) | ||
|
|
||
| fn = VoyageAIEmbeddingFunction(api_key="voyage-key", model="voyage-2") | ||
| result = fn(["aa", "bb"]) | ||
|
|
||
| mock_client.embed.assert_called_once() | ||
| mock_client.contextualized_embed.assert_not_called() | ||
| assert np.allclose(result, [[0.1, 0.2]]) | ||
|
|
||
| def test_contextualized_model_uses_contextualized_embed(self): | ||
| """voyage-context-4 should call the contextualized embeddings endpoint.""" | ||
| with patch("voyageai.Client") as mock_client_class: | ||
| mock_client = MagicMock() | ||
| mock_client_class.return_value = mock_client | ||
| mock_client.contextualized_embed.return_value = MagicMock( | ||
| results=[ | ||
| MagicMock(embeddings=[[0.1, 0.2]]), | ||
| MagicMock(embeddings=[[0.3, 0.4]]), | ||
| ] | ||
| ) | ||
|
|
||
| fn = VoyageAIEmbeddingFunction( | ||
| api_key="voyage-key", model="voyage-context-4" | ||
| ) | ||
| result = fn(["aa", "bb"]) | ||
|
|
||
| mock_client.embed.assert_not_called() | ||
| mock_client.contextualized_embed.assert_called_once() | ||
| assert np.allclose(result, [[0.1, 0.2], [0.3, 0.4]]) | ||
|
|
||
| def test_contextualized_call_sets_chunk_size_to_max(self): | ||
| """chunk_size must be set to 32000 on every contextualized call.""" | ||
| with patch("voyageai.Client") as mock_client_class: | ||
| mock_client = MagicMock() | ||
| mock_client_class.return_value = mock_client | ||
| mock_client.contextualized_embed.return_value = MagicMock( | ||
| results=[MagicMock(embeddings=[[0.1, 0.2]])] | ||
| ) | ||
|
|
||
| fn = VoyageAIEmbeddingFunction( | ||
| api_key="voyage-key", model="voyage-context-4" | ||
| ) | ||
| fn(["aa"]) | ||
|
|
||
| _, kwargs = mock_client.contextualized_embed.call_args | ||
| assert kwargs["chunk_size"] == CONTEXTUALIZED_CHUNK_SIZE | ||
| assert CONTEXTUALIZED_CHUNK_SIZE == 32000 | ||
|
|
||
| def test_contextualized_input_is_flat_list(self): | ||
| """Input must be passed as a flat List[str], not wrapped in an extra list.""" | ||
| with patch("voyageai.Client") as mock_client_class: | ||
| mock_client = MagicMock() | ||
| mock_client_class.return_value = mock_client | ||
| mock_client.contextualized_embed.return_value = MagicMock( | ||
| results=[ | ||
| MagicMock(embeddings=[[0.1, 0.2]]), | ||
| MagicMock(embeddings=[[0.3, 0.4]]), | ||
| ] | ||
| ) | ||
|
|
||
| fn = VoyageAIEmbeddingFunction( | ||
| api_key="voyage-key", model="voyage-context-4" | ||
| ) | ||
| fn(["aa", "bb"]) | ||
|
|
||
| _, kwargs = mock_client.contextualized_embed.call_args | ||
| assert kwargs["inputs"] == ["aa", "bb"] | ||
|
|
||
| def test_contextualized_string_input_normalized_to_flat_list(self): | ||
| """A single string input is normalized to a flat list of one string.""" | ||
| with patch("voyageai.Client") as mock_client_class: | ||
| mock_client = MagicMock() | ||
| mock_client_class.return_value = mock_client | ||
| mock_client.contextualized_embed.return_value = MagicMock( | ||
| results=[MagicMock(embeddings=[[0.1, 0.2]])] | ||
| ) | ||
|
|
||
| fn = VoyageAIEmbeddingFunction( | ||
| api_key="voyage-key", model="voyage-context-4" | ||
| ) | ||
| fn("aa") | ||
|
|
||
| _, kwargs = mock_client.contextualized_embed.call_args | ||
| assert kwargs["inputs"] == ["aa"] |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🌐 Web query:
For voyageai Python client version 0.3.5, what is the signature ofClient.contextualized_embed, and whichinput_typevalues does it support? In particular, isquerysupported or is the endpoint document-only?💡 Result:
The contextualized_embed method in the Voyage AI Python client supports both query and document types [1][2]. It is not restricted to documents only [1][2]. The method signature for voyageai.Client.contextualized_embed is as follows [1][2]: def contextualized_embed( self, inputs: Union[List[List[str]], List[str]], model: str, input_type: Optional[str] = None, output_dimension: Optional[int] = None, output_dtype: Optional[str] = "float", enable_auto_chunking: Optional[bool] = False, chunk_size: Optional[int] = 512, chunk_overlap: Optional[int] = 0, chunk_fn: Optional[Callable[[str], List[str]]] = None,) The input_type parameter accepts the values None, "query", and "document" [1][2]. When using "query", each inner list should typically contain a single query string [3][4]. When using "document", each inner list typically contains chunks from a document [3][4]. The input_type argument is optional, but setting it to "query" or "document" is recommended for retrieval tasks as it prompts the model appropriately [1][2].
Citations:
🏁 Script executed:
Repository: crewAIInc/crewAI
Length of output: 3654
Respect
input_typeforvoyage-contextmodelscontextualized_embed()accepts bothqueryanddocument, so hardcoding"document"here ignores the configured value. Either forwardself._config.get("input_type")or reject non-document configs explicitly.🤖 Prompt for AI Agents