langchain-ollama 0.1.1__py3-none-any.whl → 0.1.3__py3-none-any.whl

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.
@@ -35,6 +35,7 @@ from langchain_core.messages import (
35
35
  from langchain_core.messages.ai import UsageMetadata
36
36
  from langchain_core.messages.tool import tool_call
37
37
  from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
38
+ from langchain_core.pydantic_v1 import Field, root_validator
38
39
  from langchain_core.runnables import Runnable
39
40
  from langchain_core.tools import BaseTool
40
41
  from langchain_core.utils.function_calling import convert_to_openai_tool
@@ -322,6 +323,21 @@ class ChatOllama(BaseChatModel):
322
323
  base_url: Optional[str] = None
323
324
  """Base url the model is hosted under."""
324
325
 
326
+ client_kwargs: Optional[dict] = {}
327
+ """Additional kwargs to pass to the httpx Client.
328
+ For a full list of the params, see [this link](https://pydoc.dev/httpx/latest/httpx.Client.html)
329
+ """
330
+
331
+ _client: Client = Field(default=None)
332
+ """
333
+ The client to use for making requests.
334
+ """
335
+
336
+ _async_client: AsyncClient = Field(default=None)
337
+ """
338
+ The async client to use for making requests.
339
+ """
340
+
325
341
  @property
326
342
  def _default_params(self) -> Dict[str, Any]:
327
343
  """Get the default parameters for calling Ollama."""
@@ -348,6 +364,15 @@ class ChatOllama(BaseChatModel):
348
364
  "keep_alive": self.keep_alive,
349
365
  }
350
366
 
367
+ @root_validator(pre=False, skip_on_failure=True)
368
+ def _set_clients(cls, values: dict) -> dict:
369
+ """Set clients to use for ollama."""
370
+ values["_client"] = Client(host=values["base_url"], **values["client_kwargs"])
371
+ values["_async_client"] = AsyncClient(
372
+ host=values["base_url"], **values["client_kwargs"]
373
+ )
374
+ return values
375
+
351
376
  def _convert_messages_to_ollama_messages(
352
377
  self, messages: List[BaseMessage]
353
378
  ) -> Sequence[Message]:
@@ -449,7 +474,7 @@ class ChatOllama(BaseChatModel):
449
474
 
450
475
  params["options"]["stop"] = stop
451
476
  if "tools" in kwargs:
452
- yield await AsyncClient(host=self.base_url).chat(
477
+ yield await self._async_client.chat(
453
478
  model=params["model"],
454
479
  messages=ollama_messages,
455
480
  stream=False,
@@ -459,7 +484,7 @@ class ChatOllama(BaseChatModel):
459
484
  tools=kwargs["tools"],
460
485
  ) # type:ignore
461
486
  else:
462
- async for part in await AsyncClient(host=self.base_url).chat(
487
+ async for part in await self._async_client.chat(
463
488
  model=params["model"],
464
489
  messages=ollama_messages,
465
490
  stream=True,
@@ -487,7 +512,7 @@ class ChatOllama(BaseChatModel):
487
512
 
488
513
  params["options"]["stop"] = stop
489
514
  if "tools" in kwargs:
490
- yield Client(host=self.base_url).chat(
515
+ yield self._client.chat(
491
516
  model=params["model"],
492
517
  messages=ollama_messages,
493
518
  stream=False,
@@ -497,7 +522,7 @@ class ChatOllama(BaseChatModel):
497
522
  tools=kwargs["tools"],
498
523
  )
499
524
  else:
500
- yield from Client(host=self.base_url).chat(
525
+ yield from self._client.chat(
501
526
  model=params["model"],
502
527
  messages=ollama_messages,
503
528
  stream=True,
@@ -1,34 +1,158 @@
1
- from typing import List
1
+ from typing import (
2
+ List,
3
+ Optional,
4
+ )
2
5
 
3
- import ollama
4
6
  from langchain_core.embeddings import Embeddings
5
- from langchain_core.pydantic_v1 import BaseModel, Extra
6
- from ollama import AsyncClient
7
+ from langchain_core.pydantic_v1 import BaseModel, Field, root_validator
8
+ from ollama import AsyncClient, Client
7
9
 
8
10
 
9
11
  class OllamaEmbeddings(BaseModel, Embeddings):
10
- """OllamaEmbeddings embedding model.
12
+ """Ollama embedding model integration.
11
13
 
12
- Example:
14
+ Set up a local Ollama instance:
15
+ Install the Ollama package and set up a local Ollama instance
16
+ using the instructions here: https://github.com/ollama/ollama .
17
+
18
+ You will need to choose a model to serve.
19
+
20
+ You can view a list of available models via the model library (https://ollama.com/library).
21
+
22
+ To fetch a model from the Ollama model library use ``ollama pull <name-of-model>``.
23
+
24
+ For example, to pull the llama3 model:
25
+
26
+ .. code-block:: bash
27
+
28
+ ollama pull llama3
29
+
30
+ This will download the default tagged version of the model.
31
+ Typically, the default points to the latest, smallest sized-parameter model.
32
+
33
+ * On Mac, the models will be downloaded to ~/.ollama/models
34
+ * On Linux (or WSL), the models will be stored at /usr/share/ollama/.ollama/models
35
+
36
+ You can specify the exact version of the model of interest
37
+ as such ``ollama pull vicuna:13b-v1.5-16k-q4_0``.
38
+
39
+ To view pulled models:
40
+
41
+ .. code-block:: bash
42
+
43
+ ollama list
44
+
45
+ To start serving:
46
+
47
+ .. code-block:: bash
48
+
49
+ ollama serve
50
+
51
+ View the Ollama documentation for more commands.
52
+
53
+ .. code-block:: bash
54
+
55
+ ollama help
56
+
57
+ Install the langchain-ollama integration package:
58
+ .. code-block:: bash
59
+
60
+ pip install -U langchain_ollama
61
+
62
+ Key init args — completion params:
63
+ model: str
64
+ Name of Ollama model to use.
65
+ base_url: Optional[str]
66
+ Base url the model is hosted under.
67
+
68
+ See full list of supported init args and their descriptions in the params section.
69
+
70
+ Instantiate:
13
71
  .. code-block:: python
14
72
 
15
73
  from langchain_ollama import OllamaEmbeddings
16
74
 
17
- embedder = OllamaEmbeddings(model="llama3")
18
- embedder.embed_query("what is the place that jonathan worked at?")
19
- """
75
+ embed = OllamaEmbeddings(
76
+ model="llama3"
77
+ )
78
+
79
+ Embed single text:
80
+ .. code-block:: python
81
+
82
+ input_text = "The meaning of life is 42"
83
+ vector = embed.embed_query(input_text)
84
+ print(vector[:3])
85
+
86
+ .. code-block:: python
87
+
88
+ [-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
89
+
90
+ Embed multiple texts:
91
+ .. code-block:: python
92
+
93
+ input_texts = ["Document 1...", "Document 2..."]
94
+ vectors = embed.embed_documents(input_texts)
95
+ print(len(vectors))
96
+ # The first 3 coordinates for the first vector
97
+ print(vectors[0][:3])
98
+
99
+ .. code-block:: python
100
+
101
+ 2
102
+ [-0.024603435769677162, -0.007543657906353474, 0.0039630369283258915]
103
+
104
+ Async:
105
+ .. code-block:: python
106
+
107
+ vector = await embed.aembed_query(input_text)
108
+ print(vector[:3])
109
+
110
+ # multiple:
111
+ # await embed.aembed_documents(input_texts)
112
+
113
+ .. code-block:: python
114
+
115
+ [-0.009100092574954033, 0.005071679595857859, -0.0029193938244134188]
116
+ """ # noqa: E501
20
117
 
21
118
  model: str
22
119
  """Model name to use."""
23
120
 
121
+ base_url: Optional[str] = None
122
+ """Base url the model is hosted under."""
123
+
124
+ client_kwargs: Optional[dict] = {}
125
+ """Additional kwargs to pass to the httpx Client.
126
+ For a full list of the params, see [this link](https://pydoc.dev/httpx/latest/httpx.Client.html)
127
+ """
128
+
129
+ _client: Client = Field(default=None)
130
+ """
131
+ The client to use for making requests.
132
+ """
133
+
134
+ _async_client: AsyncClient = Field(default=None)
135
+ """
136
+ The async client to use for making requests.
137
+ """
138
+
24
139
  class Config:
25
140
  """Configuration for this pydantic object."""
26
141
 
27
- extra = Extra.forbid
142
+ extra = "forbid"
143
+
144
+ @root_validator(pre=False, skip_on_failure=True)
145
+ def _set_clients(cls, values: dict) -> dict:
146
+ """Set clients to use for ollama."""
147
+ values["_client"] = Client(host=values["base_url"], **values["client_kwargs"])
148
+ values["_async_client"] = AsyncClient(
149
+ host=values["base_url"], **values["client_kwargs"]
150
+ )
151
+ return values
28
152
 
29
153
  def embed_documents(self, texts: List[str]) -> List[List[float]]:
30
154
  """Embed search docs."""
31
- embedded_docs = ollama.embed(self.model, texts)["embeddings"]
155
+ embedded_docs = self._client.embed(self.model, texts)["embeddings"]
32
156
  return embedded_docs
33
157
 
34
158
  def embed_query(self, text: str) -> List[float]:
@@ -37,7 +161,9 @@ class OllamaEmbeddings(BaseModel, Embeddings):
37
161
 
38
162
  async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
39
163
  """Embed search docs."""
40
- embedded_docs = (await AsyncClient().embed(self.model, texts))["embeddings"]
164
+ embedded_docs = (await self._async_client.embed(self.model, texts))[
165
+ "embeddings"
166
+ ]
41
167
  return embedded_docs
42
168
 
43
169
  async def aembed_query(self, text: str) -> List[float]:
langchain_ollama/llms.py CHANGED
@@ -12,14 +12,14 @@ from typing import (
12
12
  Union,
13
13
  )
14
14
 
15
- import ollama
16
15
  from langchain_core.callbacks import (
17
16
  AsyncCallbackManagerForLLMRun,
18
17
  CallbackManagerForLLMRun,
19
18
  )
20
- from langchain_core.language_models import BaseLLM
19
+ from langchain_core.language_models import BaseLLM, LangSmithParams
21
20
  from langchain_core.outputs import GenerationChunk, LLMResult
22
- from ollama import AsyncClient, Options
21
+ from langchain_core.pydantic_v1 import Field, root_validator
22
+ from ollama import AsyncClient, Client, Options
23
23
 
24
24
 
25
25
  class OllamaLLM(BaseLLM):
@@ -107,6 +107,24 @@ class OllamaLLM(BaseLLM):
107
107
  keep_alive: Optional[Union[int, str]] = None
108
108
  """How long the model will stay loaded into memory."""
109
109
 
110
+ base_url: Optional[str] = None
111
+ """Base url the model is hosted under."""
112
+
113
+ client_kwargs: Optional[dict] = {}
114
+ """Additional kwargs to pass to the httpx Client.
115
+ For a full list of the params, see [this link](https://pydoc.dev/httpx/latest/httpx.Client.html)
116
+ """
117
+
118
+ _client: Client = Field(default=None)
119
+ """
120
+ The client to use for making requests.
121
+ """
122
+
123
+ _async_client: AsyncClient = Field(default=None)
124
+ """
125
+ The async client to use for making requests.
126
+ """
127
+
110
128
  @property
111
129
  def _default_params(self) -> Dict[str, Any]:
112
130
  """Get the default parameters for calling Ollama."""
@@ -137,6 +155,24 @@ class OllamaLLM(BaseLLM):
137
155
  """Return type of LLM."""
138
156
  return "ollama-llm"
139
157
 
158
+ def _get_ls_params(
159
+ self, stop: Optional[List[str]] = None, **kwargs: Any
160
+ ) -> LangSmithParams:
161
+ """Get standard params for tracing."""
162
+ params = super()._get_ls_params(stop=stop, **kwargs)
163
+ if max_tokens := kwargs.get("num_predict", self.num_predict):
164
+ params["ls_max_tokens"] = max_tokens
165
+ return params
166
+
167
+ @root_validator(pre=False, skip_on_failure=True)
168
+ def _set_clients(cls, values: dict) -> dict:
169
+ """Set clients to use for ollama."""
170
+ values["_client"] = Client(host=values["base_url"], **values["client_kwargs"])
171
+ values["_async_client"] = AsyncClient(
172
+ host=values["base_url"], **values["client_kwargs"]
173
+ )
174
+ return values
175
+
140
176
  async def _acreate_generate_stream(
141
177
  self,
142
178
  prompt: str,
@@ -155,7 +191,7 @@ class OllamaLLM(BaseLLM):
155
191
  params[key] = kwargs[key]
156
192
 
157
193
  params["options"]["stop"] = stop
158
- async for part in await AsyncClient().generate(
194
+ async for part in await self._async_client.generate(
159
195
  model=params["model"],
160
196
  prompt=prompt,
161
197
  stream=True,
@@ -183,7 +219,7 @@ class OllamaLLM(BaseLLM):
183
219
  params[key] = kwargs[key]
184
220
 
185
221
  params["options"]["stop"] = stop
186
- yield from ollama.generate(
222
+ yield from self._client.generate(
187
223
  model=params["model"],
188
224
  prompt=prompt,
189
225
  stream=True,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: langchain-ollama
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: An integration package connecting Ollama and LangChain
5
5
  Home-page: https://github.com/langchain-ai/langchain
6
6
  License: MIT
@@ -11,7 +11,7 @@ Classifier: Programming Language :: Python :: 3.9
11
11
  Classifier: Programming Language :: Python :: 3.10
12
12
  Classifier: Programming Language :: Python :: 3.11
13
13
  Classifier: Programming Language :: Python :: 3.12
14
- Requires-Dist: langchain-core (>=0.2.20,<0.3.0)
14
+ Requires-Dist: langchain-core (>=0.2.36,<0.3.0)
15
15
  Requires-Dist: ollama (>=0.3.0,<1)
16
16
  Project-URL: Repository, https://github.com/langchain-ai/langchain
17
17
  Project-URL: Release Notes, https://github.com/langchain-ai/langchain/releases?q=tag%3A%22langchain-ollama%3D%3D0%22&expanded=true
@@ -0,0 +1,9 @@
1
+ langchain_ollama/__init__.py,sha256=HhQZqbCjhrbr2dC_9Dkw12pg4HPjnDXUoInROMNJKqA,518
2
+ langchain_ollama/chat_models.py,sha256=M2q6WK8tvFN9Q_CVH_cAYvNbhx7HNtN2PIw5255cpxs,30451
3
+ langchain_ollama/embeddings.py,sha256=Sq30Q2lWJxc5BT1TLqDgrH7AW3tEezhGAJw_5dkr_8s,5005
4
+ langchain_ollama/llms.py,sha256=FObzYNIQJoE5kQY_sh12CGj8AQXiMkfCLqrzsM8VNE0,13269
5
+ langchain_ollama/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ langchain_ollama-0.1.3.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
7
+ langchain_ollama-0.1.3.dist-info/METADATA,sha256=MOgY4zrbTiykPqxq660QciasjgMkwZ8yWptXMmHd-Xw,1827
8
+ langchain_ollama-0.1.3.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
9
+ langchain_ollama-0.1.3.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- langchain_ollama/__init__.py,sha256=HhQZqbCjhrbr2dC_9Dkw12pg4HPjnDXUoInROMNJKqA,518
2
- langchain_ollama/chat_models.py,sha256=1lRveHDQxtYz3nNgXNaPQUAuo7WTHTf6jZarenXeCuM,29642
3
- langchain_ollama/embeddings.py,sha256=8_vd3sZaBCHMGZFTjUtDRUWrmbXNyBZSazrAGtBfFSU,1371
4
- langchain_ollama/llms.py,sha256=XT8daS4gRx8_w4a3s2pMGP-G7kBMHSYcQBUq4GfAou4,11940
5
- langchain_ollama/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- langchain_ollama-0.1.1.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
7
- langchain_ollama-0.1.1.dist-info/METADATA,sha256=F41rq4WnaphtdkyWuZkKnxEp3gRT70n_OSptvwQtYd4,1827
8
- langchain_ollama-0.1.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
9
- langchain_ollama-0.1.1.dist-info/RECORD,,