|
| 1 | +import os |
| 2 | +import types |
| 3 | +import pytest |
| 4 | +import mock |
| 5 | +from gptcache.utils import import_voyageai |
| 6 | +from gptcache.embedding import VoyageAI |
| 7 | + |
| 8 | +import_voyageai() |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +@mock.patch.dict(os.environ, {"VOYAGE_API_KEY": "API_KEY", "VOYAGE_API_KEY_PATH": "API_KEY_FILE_PATH_ENV"}) |
| 13 | +@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="API_KEY") |
| 14 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1024])) |
| 15 | +def test_voageai_without_api_key(mock_created, mock_file): |
| 16 | + dimension = 1024 |
| 17 | + vo = VoyageAI() |
| 18 | + |
| 19 | + assert vo.dimension == dimension |
| 20 | + assert len(vo.to_embeddings("foo")) == dimension |
| 21 | + |
| 22 | + mock_file.assert_called_once_with("API_KEY_FILE_PATH_ENV", "rt") |
| 23 | + mock_created.assert_called_once_with(texts=["foo"], model="voyage-3", input_type=None, truncation=True) |
| 24 | + |
| 25 | + |
| 26 | +@mock.patch.dict(os.environ, {"VOYAGE_API_KEY": "API_KEY", "VOYAGE_API_KEY_PATH": "API_KEY_FILE_PATH_ENV"}) |
| 27 | +@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="API_KEY") |
| 28 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1024])) |
| 29 | +def test_voageai_with_api_key_path(mock_create, mock_file): |
| 30 | + dimension = 1024 |
| 31 | + vo = VoyageAI(api_key_path="API_KEY_FILE_PATH") |
| 32 | + |
| 33 | + assert vo.dimension == dimension |
| 34 | + assert len(vo.to_embeddings("foo")) == dimension |
| 35 | + |
| 36 | + mock_file.assert_called_once_with("API_KEY_FILE_PATH", "rt") |
| 37 | + mock_create.assert_called_once_with(texts=["foo"], model="voyage-3", input_type=None, truncation=True) |
| 38 | + |
| 39 | + |
| 40 | +@mock.patch.dict(os.environ, {"VOYAGE_API_KEY": "API_KEY"}) |
| 41 | +@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="API_KEY") |
| 42 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1024])) |
| 43 | +def test_voageai_with_api_key_in_envrion(mock_create, mock_file): |
| 44 | + dimension = 1024 |
| 45 | + vo = VoyageAI() |
| 46 | + |
| 47 | + assert vo.dimension == dimension |
| 48 | + assert len(vo.to_embeddings("foo")) == dimension |
| 49 | + mock_file.assert_not_called() |
| 50 | + mock_create.assert_called_once_with(texts=["foo"], model="voyage-3", input_type=None, truncation=True) |
| 51 | + |
| 52 | + |
| 53 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1024])) |
| 54 | +def test_voageai_with_api_key(mock_create): |
| 55 | + dimension = 1024 |
| 56 | + vo = VoyageAI(api_key="API_KEY") |
| 57 | + |
| 58 | + assert vo.dimension == dimension |
| 59 | + assert len(vo.to_embeddings("foo")) == dimension |
| 60 | + mock_create.assert_called_once_with(texts=["foo"], model="voyage-3", input_type=None, truncation=True) |
| 61 | + |
| 62 | + |
| 63 | +@mock.patch.dict(os.environ) |
| 64 | +@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="API_KEY") |
| 65 | +def test_voageai_without_api_key_or_api_key_file_path(mock_file): |
| 66 | + with pytest.raises(Exception): |
| 67 | + VoyageAI() |
| 68 | + mock_file.assert_not_called() |
| 69 | + |
| 70 | + |
| 71 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 512])) |
| 72 | +def test_voageai_with_model_voyage_3_lite(mock_create): |
| 73 | + dimension = 512 |
| 74 | + model = "voyage-3-lite" |
| 75 | + vo = VoyageAI(api_key="API_KEY", model=model) |
| 76 | + |
| 77 | + assert vo.dimension == dimension |
| 78 | + assert len(vo.to_embeddings("foo")) == dimension |
| 79 | + mock_create.assert_called_once_with(texts=["foo"], model=model, input_type=None, truncation=True) |
| 80 | + |
| 81 | + |
| 82 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1024])) |
| 83 | +def test_voageai_with_model_voyage_finance_2(mock_create): |
| 84 | + dimension = 1024 |
| 85 | + model = "voyage-finance-2" |
| 86 | + vo = VoyageAI(api_key="API_KEY", model=model) |
| 87 | + |
| 88 | + assert vo.dimension == dimension |
| 89 | + assert len(vo.to_embeddings("foo")) == dimension |
| 90 | + mock_create.assert_called_once_with(texts=["foo"], model=model, input_type=None, truncation=True) |
| 91 | + |
| 92 | + |
| 93 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1024])) |
| 94 | +def test_voageai_with_model_voyage_multilingual_2(mock_create): |
| 95 | + dimension = 1024 |
| 96 | + model = "voyage-multilingual-2" |
| 97 | + vo = VoyageAI(api_key="API_KEY", model=model) |
| 98 | + |
| 99 | + assert vo.dimension == dimension |
| 100 | + assert len(vo.to_embeddings("foo")) == dimension |
| 101 | + mock_create.assert_called_once_with(texts=["foo"], model=model, input_type=None, truncation=True) |
| 102 | + |
| 103 | + |
| 104 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1024])) |
| 105 | +def test_voageai_with_model_voyage_law_2(mock_create): |
| 106 | + dimension = 1024 |
| 107 | + model = "voyage-law-2" |
| 108 | + vo = VoyageAI(api_key="API_KEY", model=model) |
| 109 | + |
| 110 | + assert vo.dimension == dimension |
| 111 | + assert len(vo.to_embeddings("foo")) == dimension |
| 112 | + mock_create.assert_called_once_with(texts=["foo"], model=model, input_type=None, truncation=True) |
| 113 | + |
| 114 | + |
| 115 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1536])) |
| 116 | +def test_voageai_with_model_voyage_code_2(mock_create): |
| 117 | + dimension = 1536 |
| 118 | + model = "voyage-code-2" |
| 119 | + vo = VoyageAI(api_key="API_KEY", model=model) |
| 120 | + |
| 121 | + assert vo.dimension == dimension |
| 122 | + assert len(vo.to_embeddings("foo")) == dimension |
| 123 | + mock_create.assert_called_once_with(texts=["foo"], model=model, input_type=None, truncation=True) |
| 124 | + |
| 125 | + |
| 126 | +@mock.patch("voyageai.Client.embed", return_value=types.SimpleNamespace(embeddings=[[0] * 1536])) |
| 127 | +def test_voageai_with_general_parameters(mock_create): |
| 128 | + dimension = 1536 |
| 129 | + model = "voyage-code-2" |
| 130 | + api_key = "API_KEY" |
| 131 | + input_type = "query" |
| 132 | + truncation = False |
| 133 | + |
| 134 | + mock_create.return_value = types.SimpleNamespace(embeddings=[[0] * dimension]) |
| 135 | + |
| 136 | + vo = VoyageAI(model=model, api_key=api_key, input_type=input_type, truncation=truncation) |
| 137 | + assert vo.dimension == dimension |
| 138 | + assert len(vo.to_embeddings(["foo"])) == dimension |
| 139 | + |
| 140 | + mock_create.assert_called_once_with(texts=["foo"], model=model, input_type=input_type, truncation=truncation) |
0 commit comments