Skip to content

Commit f6b1da8

Browse files
SyedBaqarAbbasshinxi
authored andcommitted
[feat] Added backwards compatibility for OllamaEmbeddings initialization (migration from langchain_community.embeddings to langchain_ollama.embeddings (#29296)
- [feat] **Added backwards compatibility for OllamaEmbeddings initialization (migration from `langchain_community.embeddings` to `langchain_ollama.embeddings`**: "langchain_ollama" - **Description:** Given that `OllamaEmbeddings` from `langchain_community.embeddings` is deprecated, code is being shifted to ``langchain_ollama.embeddings`. However, this does not offer backward compatibility of initializing the parameters and `OllamaEmbeddings` object. - **Issue:** #29294 - **Dependencies:** None - **Twitter handle:** @BaqarAbbas2001 ## Additional Information Previously, `OllamaEmbeddings` from `langchain_community.embeddings` used to support the following options: https://github.com/langchain-ai/langchain/blob/e9abe583b2efe80b4c38f4b7548a473bcca4dde5/libs/community/langchain_community/embeddings/ollama.py#L125-L139 However, in the new package `from langchain_ollama import OllamaEmbeddings`, there is no method to set these options. I have added these parameters to resolve this issue. This issue was also discussed in https://github.com/langchain-ai/langchain/discussions/29113
1 parent 564bdae commit f6b1da8

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

libs/partners/ollama/langchain_ollama/embeddings.py

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
"""Ollama embeddings models."""
22

3-
from typing import (
4-
List,
5-
Optional,
6-
)
3+
from typing import Any, Dict, List, Optional
74

85
from langchain_core.embeddings import Embeddings
96
from ollama import AsyncClient, Client
@@ -144,10 +141,89 @@ class OllamaEmbeddings(BaseModel, Embeddings):
144141
The async client to use for making requests.
145142
"""
146143

144+
mirostat: Optional[int] = None
145+
"""Enable Mirostat sampling for controlling perplexity.
146+
(default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)"""
147+
148+
mirostat_eta: Optional[float] = None
149+
"""Influences how quickly the algorithm responds to feedback
150+
from the generated text. A lower learning rate will result in
151+
slower adjustments, while a higher learning rate will make
152+
the algorithm more responsive. (Default: 0.1)"""
153+
154+
mirostat_tau: Optional[float] = None
155+
"""Controls the balance between coherence and diversity
156+
of the output. A lower value will result in more focused and
157+
coherent text. (Default: 5.0)"""
158+
159+
num_ctx: Optional[int] = None
160+
"""Sets the size of the context window used to generate the
161+
next token. (Default: 2048) """
162+
163+
num_gpu: Optional[int] = None
164+
"""The number of GPUs to use. On macOS it defaults to 1 to
165+
enable metal support, 0 to disable."""
166+
167+
num_thread: Optional[int] = None
168+
"""Sets the number of threads to use during computation.
169+
By default, Ollama will detect this for optimal performance.
170+
It is recommended to set this value to the number of physical
171+
CPU cores your system has (as opposed to the logical number of cores)."""
172+
173+
repeat_last_n: Optional[int] = None
174+
"""Sets how far back for the model to look back to prevent
175+
repetition. (Default: 64, 0 = disabled, -1 = num_ctx)"""
176+
177+
repeat_penalty: Optional[float] = None
178+
"""Sets how strongly to penalize repetitions. A higher value (e.g., 1.5)
179+
will penalize repetitions more strongly, while a lower value (e.g., 0.9)
180+
will be more lenient. (Default: 1.1)"""
181+
182+
temperature: Optional[float] = None
183+
"""The temperature of the model. Increasing the temperature will
184+
make the model answer more creatively. (Default: 0.8)"""
185+
186+
stop: Optional[List[str]] = None
187+
"""Sets the stop tokens to use."""
188+
189+
tfs_z: Optional[float] = None
190+
"""Tail free sampling is used to reduce the impact of less probable
191+
tokens from the output. A higher value (e.g., 2.0) will reduce the
192+
impact more, while a value of 1.0 disables this setting. (default: 1)"""
193+
194+
top_k: Optional[int] = None
195+
"""Reduces the probability of generating nonsense. A higher value (e.g. 100)
196+
will give more diverse answers, while a lower value (e.g. 10)
197+
will be more conservative. (Default: 40)"""
198+
199+
top_p: Optional[float] = None
200+
"""Works together with top-k. A higher value (e.g., 0.95) will lead
201+
to more diverse text, while a lower value (e.g., 0.5) will
202+
generate more focused and conservative text. (Default: 0.9)"""
203+
147204
model_config = ConfigDict(
148205
extra="forbid",
149206
)
150207

208+
@property
209+
def _default_params(self) -> Dict[str, Any]:
210+
"""Get the default parameters for calling Ollama."""
211+
return {
212+
"mirostat": self.mirostat,
213+
"mirostat_eta": self.mirostat_eta,
214+
"mirostat_tau": self.mirostat_tau,
215+
"num_ctx": self.num_ctx,
216+
"num_gpu": self.num_gpu,
217+
"num_thread": self.num_thread,
218+
"repeat_last_n": self.repeat_last_n,
219+
"repeat_penalty": self.repeat_penalty,
220+
"temperature": self.temperature,
221+
"stop": self.stop,
222+
"tfs_z": self.tfs_z,
223+
"top_k": self.top_k,
224+
"top_p": self.top_p,
225+
}
226+
151227
@model_validator(mode="after")
152228
def _set_clients(self) -> Self:
153229
"""Set clients to use for ollama."""
@@ -158,7 +234,9 @@ def _set_clients(self) -> Self:
158234

159235
def embed_documents(self, texts: List[str]) -> List[List[float]]:
160236
"""Embed search docs."""
161-
embedded_docs = self._client.embed(self.model, texts)["embeddings"]
237+
embedded_docs = self._client.embed(
238+
self.model, texts, options=self._default_params
239+
)["embeddings"]
162240
return embedded_docs
163241

164242
def embed_query(self, text: str) -> List[float]:

0 commit comments

Comments
 (0)