llm-gemini 0.13.1__tar.gz → 0.14__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: llm-gemini
3
- Version: 0.13.1
3
+ Version: 0.14
4
4
  Summary: LLM plugin to access Google's Gemini family of models
5
5
  Author: Simon Willison
6
6
  License: Apache-2.0
@@ -17,6 +17,7 @@ Requires-Dist: ijson
17
17
  Provides-Extra: test
18
18
  Requires-Dist: pytest; extra == "test"
19
19
  Requires-Dist: pytest-recording; extra == "test"
20
+ Requires-Dist: pytest-asyncio; extra == "test"
20
21
  Requires-Dist: nest-asyncio; extra == "test"
21
22
 
22
23
  # llm-gemini
@@ -145,7 +146,7 @@ llm chat -m gemini-1.5-pro-latest
145
146
 
146
147
  ## Embeddings
147
148
 
148
- The plugin also adds support for the `text-embedding-004` embedding model.
149
+ The plugin also adds support for the `gemini-embedding-exp-03-07` and `text-embedding-004` embedding models.
149
150
 
150
151
  Run that against a single string like this:
151
152
  ```bash
@@ -153,10 +154,20 @@ llm embed -m text-embedding-004 -c 'hello world'
153
154
  ```
154
155
  This returns a JSON array of 768 numbers.
155
156
 
157
+ The `gemini-embedding-exp-03-07` model is larger, returning 3072 numbers. You can also use variants of it that are truncated down to smaller sizes:
158
+
159
+ - `gemini-embedding-exp-03-07` - 3072 numbers
160
+ - `gemini-embedding-exp-03-07-2048` - 2048 numbers
161
+ - `gemini-embedding-exp-03-07-1024` - 1024 numbers
162
+ - `gemini-embedding-exp-03-07-512` - 512 numbers
163
+ - `gemini-embedding-exp-03-07-256` - 256 numbers
164
+ - `gemini-embedding-exp-03-07-128` - 128 numbers
165
+
156
166
  This command will embed every `README.md` file in child directories of the current directory and store the results in a SQLite database called `embed.db` in a collection called `readmes`:
157
167
 
158
168
  ```bash
159
- llm embed-multi readmes --files . '*/README.md' -d embed.db -m text-embedding-004
169
+ llm embed-multi readmes -d embed.db -m gemini-embedding-exp-03-07-128 \
170
+ --files . '*/README.md'
160
171
  ```
161
172
  You can then run similarity searches against that collection like this:
162
173
  ```bash
@@ -124,7 +124,7 @@ llm chat -m gemini-1.5-pro-latest
124
124
 
125
125
  ## Embeddings
126
126
 
127
- The plugin also adds support for the `text-embedding-004` embedding model.
127
+ The plugin also adds support for the `gemini-embedding-exp-03-07` and `text-embedding-004` embedding models.
128
128
 
129
129
  Run that against a single string like this:
130
130
  ```bash
@@ -132,10 +132,20 @@ llm embed -m text-embedding-004 -c 'hello world'
132
132
  ```
133
133
  This returns a JSON array of 768 numbers.
134
134
 
135
+ The `gemini-embedding-exp-03-07` model is larger, returning 3072 numbers. You can also use variants of it that are truncated down to smaller sizes:
136
+
137
+ - `gemini-embedding-exp-03-07` - 3072 numbers
138
+ - `gemini-embedding-exp-03-07-2048` - 2048 numbers
139
+ - `gemini-embedding-exp-03-07-1024` - 1024 numbers
140
+ - `gemini-embedding-exp-03-07-512` - 512 numbers
141
+ - `gemini-embedding-exp-03-07-256` - 256 numbers
142
+ - `gemini-embedding-exp-03-07-128` - 128 numbers
143
+
135
144
  This command will embed every `README.md` file in child directories of the current directory and store the results in a SQLite database called `embed.db` in a collection called `readmes`:
136
145
 
137
146
  ```bash
138
- llm embed-multi readmes --files . '*/README.md' -d embed.db -m text-embedding-004
147
+ llm embed-multi readmes -d embed.db -m gemini-embedding-exp-03-07-128 \
148
+ --files . '*/README.md'
139
149
  ```
140
150
  You can then run similarity searches against that collection like this:
141
151
  ```bash
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: llm-gemini
3
- Version: 0.13.1
3
+ Version: 0.14
4
4
  Summary: LLM plugin to access Google's Gemini family of models
5
5
  Author: Simon Willison
6
6
  License: Apache-2.0
@@ -17,6 +17,7 @@ Requires-Dist: ijson
17
17
  Provides-Extra: test
18
18
  Requires-Dist: pytest; extra == "test"
19
19
  Requires-Dist: pytest-recording; extra == "test"
20
+ Requires-Dist: pytest-asyncio; extra == "test"
20
21
  Requires-Dist: nest-asyncio; extra == "test"
21
22
 
22
23
  # llm-gemini
@@ -145,7 +146,7 @@ llm chat -m gemini-1.5-pro-latest
145
146
 
146
147
  ## Embeddings
147
148
 
148
- The plugin also adds support for the `text-embedding-004` embedding model.
149
+ The plugin also adds support for the `gemini-embedding-exp-03-07` and `text-embedding-004` embedding models.
149
150
 
150
151
  Run that against a single string like this:
151
152
  ```bash
@@ -153,10 +154,20 @@ llm embed -m text-embedding-004 -c 'hello world'
153
154
  ```
154
155
  This returns a JSON array of 768 numbers.
155
156
 
157
+ The `gemini-embedding-exp-03-07` model is larger, returning 3072 numbers. You can also use variants of it that are truncated down to smaller sizes:
158
+
159
+ - `gemini-embedding-exp-03-07` - 3072 numbers
160
+ - `gemini-embedding-exp-03-07-2048` - 2048 numbers
161
+ - `gemini-embedding-exp-03-07-1024` - 1024 numbers
162
+ - `gemini-embedding-exp-03-07-512` - 512 numbers
163
+ - `gemini-embedding-exp-03-07-256` - 256 numbers
164
+ - `gemini-embedding-exp-03-07-128` - 128 numbers
165
+
156
166
  This command will embed every `README.md` file in child directories of the current directory and store the results in a SQLite database called `embed.db` in a collection called `readmes`:
157
167
 
158
168
  ```bash
159
- llm embed-multi readmes --files . '*/README.md' -d embed.db -m text-embedding-004
169
+ llm embed-multi readmes -d embed.db -m gemini-embedding-exp-03-07-128 \
170
+ --files . '*/README.md'
160
171
  ```
161
172
  You can then run similarity searches against that collection like this:
162
173
  ```bash
@@ -5,4 +5,5 @@ ijson
5
5
  [test]
6
6
  pytest
7
7
  pytest-recording
8
+ pytest-asyncio
8
9
  nest-asyncio
@@ -378,9 +378,19 @@ class AsyncGeminiPro(_SharedGemini, llm.AsyncKeyModel):
378
378
 
379
379
  @llm.hookimpl
380
380
  def register_embedding_models(register):
381
+ register(GeminiEmbeddingModel("text-embedding-004", "text-embedding-004"))
382
+ # gemini-embedding-exp-03-07 in different truncation sizes
381
383
  register(
382
- GeminiEmbeddingModel("text-embedding-004", "text-embedding-004"),
384
+ GeminiEmbeddingModel(
385
+ "gemini-embedding-exp-03-07", "gemini-embedding-exp-03-07"
386
+ ),
383
387
  )
388
+ for i in (128, 256, 512, 1024, 2048):
389
+ register(
390
+ GeminiEmbeddingModel(
391
+ f"gemini-embedding-exp-03-07-{i}", f"gemini-embedding-exp-03-07", i
392
+ ),
393
+ )
384
394
 
385
395
 
386
396
  class GeminiEmbeddingModel(llm.EmbeddingModel):
@@ -388,9 +398,10 @@ class GeminiEmbeddingModel(llm.EmbeddingModel):
388
398
  key_env_var = "LLM_GEMINI_KEY"
389
399
  batch_size = 20
390
400
 
391
- def __init__(self, model_id, gemini_model_id):
401
+ def __init__(self, model_id, gemini_model_id, truncate=None):
392
402
  self.model_id = model_id
393
403
  self.gemini_model_id = gemini_model_id
404
+ self.truncate = truncate
394
405
 
395
406
  def embed_batch(self, items):
396
407
  headers = {
@@ -416,4 +427,7 @@ class GeminiEmbeddingModel(llm.EmbeddingModel):
416
427
  )
417
428
 
418
429
  response.raise_for_status()
419
- return [item["values"] for item in response.json()["embeddings"]]
430
+ values = [item["values"] for item in response.json()["embeddings"]]
431
+ if self.truncate:
432
+ values = [value[: self.truncate] for value in values]
433
+ return values
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "llm-gemini"
3
- version = "0.13.1"
3
+ version = "0.14"
4
4
  description = "LLM plugin to access Google's Gemini family of models"
5
5
  readme = "README.md"
6
6
  authors = [{name = "Simon Willison"}]
@@ -24,4 +24,4 @@ CI = "https://github.com/simonw/llm-gemini/actions"
24
24
  gemini = "llm_gemini"
25
25
 
26
26
  [project.optional-dependencies]
27
- test = ["pytest", "pytest-recording", "nest-asyncio"]
27
+ test = ["pytest", "pytest-recording", "pytest-asyncio", "nest-asyncio"]
@@ -102,3 +102,24 @@ async def test_prompt_with_pydantic_schema():
102
102
  "modelVersion": "gemini-1.5-flash-latest",
103
103
  }
104
104
  assert response.input_tokens == 10
105
+
106
+
107
+ @pytest.mark.vcr
108
+ @pytest.mark.parametrize(
109
+ "model_id",
110
+ (
111
+ "gemini-embedding-exp-03-07",
112
+ "gemini-embedding-exp-03-07-128",
113
+ "gemini-embedding-exp-03-07-512",
114
+ ),
115
+ )
116
+ def test_embedding(model_id, monkeypatch):
117
+ monkeypatch.setenv("LLM_GEMINI_KEY", GEMINI_API_KEY)
118
+ model = llm.get_embedding_model(model_id)
119
+ response = model.embed("Some text goes here")
120
+ expected_length = 3072
121
+ if model_id.endswith("-128"):
122
+ expected_length = 128
123
+ elif model_id.endswith("-512"):
124
+ expected_length = 512
125
+ assert len(response) == expected_length
File without changes
File without changes