langchain-ollama 0.1.1__tar.gz → 0.1.2__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.
- {langchain_ollama-0.1.1 → langchain_ollama-0.1.2}/PKG-INFO +2 -2
- {langchain_ollama-0.1.1 → langchain_ollama-0.1.2}/langchain_ollama/chat_models.py +29 -4
- langchain_ollama-0.1.2/langchain_ollama/embeddings.py +171 -0
- {langchain_ollama-0.1.1 → langchain_ollama-0.1.2}/langchain_ollama/llms.py +41 -5
- {langchain_ollama-0.1.1 → langchain_ollama-0.1.2}/pyproject.toml +2 -2
- langchain_ollama-0.1.1/langchain_ollama/embeddings.py +0 -45
- {langchain_ollama-0.1.1 → langchain_ollama-0.1.2}/LICENSE +0 -0
- {langchain_ollama-0.1.1 → langchain_ollama-0.1.2}/README.md +0 -0
- {langchain_ollama-0.1.1 → langchain_ollama-0.1.2}/langchain_ollama/__init__.py +0 -0
- {langchain_ollama-0.1.1 → langchain_ollama-0.1.2}/langchain_ollama/py.typed +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: langchain-ollama
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.2
|
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 (
|
14
|
+
Requires-Dist: langchain-core (==0.2.36)
|
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
|
@@ -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
|
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
|
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
|
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
|
525
|
+
yield from self._client.chat(
|
501
526
|
model=params["model"],
|
502
527
|
messages=ollama_messages,
|
503
528
|
stream=True,
|
@@ -0,0 +1,171 @@
|
|
1
|
+
from typing import (
|
2
|
+
List,
|
3
|
+
Optional,
|
4
|
+
)
|
5
|
+
|
6
|
+
from langchain_core.embeddings import Embeddings
|
7
|
+
from langchain_core.pydantic_v1 import BaseModel, Field, root_validator
|
8
|
+
from ollama import AsyncClient, Client
|
9
|
+
|
10
|
+
|
11
|
+
class OllamaEmbeddings(BaseModel, Embeddings):
|
12
|
+
"""Ollama embedding model integration.
|
13
|
+
|
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:
|
71
|
+
.. code-block:: python
|
72
|
+
|
73
|
+
from langchain_ollama import OllamaEmbeddings
|
74
|
+
|
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
|
117
|
+
|
118
|
+
model: str
|
119
|
+
"""Model name to use."""
|
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
|
+
|
139
|
+
class Config:
|
140
|
+
"""Configuration for this pydantic object."""
|
141
|
+
|
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
|
152
|
+
|
153
|
+
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
154
|
+
"""Embed search docs."""
|
155
|
+
embedded_docs = self._client.embed(self.model, texts)["embeddings"]
|
156
|
+
return embedded_docs
|
157
|
+
|
158
|
+
def embed_query(self, text: str) -> List[float]:
|
159
|
+
"""Embed query text."""
|
160
|
+
return self.embed_documents([text])[0]
|
161
|
+
|
162
|
+
async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
|
163
|
+
"""Embed search docs."""
|
164
|
+
embedded_docs = (await self._async_client.embed(self.model, texts))[
|
165
|
+
"embeddings"
|
166
|
+
]
|
167
|
+
return embedded_docs
|
168
|
+
|
169
|
+
async def aembed_query(self, text: str) -> List[float]:
|
170
|
+
"""Embed query text."""
|
171
|
+
return (await self.aembed_documents([text]))[0]
|
@@ -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
|
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
|
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
|
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
|
[tool.poetry]
|
2
2
|
name = "langchain-ollama"
|
3
|
-
version = "0.1.
|
3
|
+
version = "0.1.2"
|
4
4
|
description = "An integration package connecting Ollama and LangChain"
|
5
5
|
authors = []
|
6
6
|
readme = "README.md"
|
@@ -14,7 +14,7 @@ license = "MIT"
|
|
14
14
|
[tool.poetry.dependencies]
|
15
15
|
python = ">=3.8.1,<4.0"
|
16
16
|
ollama = ">=0.3.0,<1"
|
17
|
-
langchain-core = "
|
17
|
+
langchain-core = "0.2.36"
|
18
18
|
|
19
19
|
[tool.poetry.group.test]
|
20
20
|
optional = true
|
@@ -1,45 +0,0 @@
|
|
1
|
-
from typing import List
|
2
|
-
|
3
|
-
import ollama
|
4
|
-
from langchain_core.embeddings import Embeddings
|
5
|
-
from langchain_core.pydantic_v1 import BaseModel, Extra
|
6
|
-
from ollama import AsyncClient
|
7
|
-
|
8
|
-
|
9
|
-
class OllamaEmbeddings(BaseModel, Embeddings):
|
10
|
-
"""OllamaEmbeddings embedding model.
|
11
|
-
|
12
|
-
Example:
|
13
|
-
.. code-block:: python
|
14
|
-
|
15
|
-
from langchain_ollama import OllamaEmbeddings
|
16
|
-
|
17
|
-
embedder = OllamaEmbeddings(model="llama3")
|
18
|
-
embedder.embed_query("what is the place that jonathan worked at?")
|
19
|
-
"""
|
20
|
-
|
21
|
-
model: str
|
22
|
-
"""Model name to use."""
|
23
|
-
|
24
|
-
class Config:
|
25
|
-
"""Configuration for this pydantic object."""
|
26
|
-
|
27
|
-
extra = Extra.forbid
|
28
|
-
|
29
|
-
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
30
|
-
"""Embed search docs."""
|
31
|
-
embedded_docs = ollama.embed(self.model, texts)["embeddings"]
|
32
|
-
return embedded_docs
|
33
|
-
|
34
|
-
def embed_query(self, text: str) -> List[float]:
|
35
|
-
"""Embed query text."""
|
36
|
-
return self.embed_documents([text])[0]
|
37
|
-
|
38
|
-
async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
|
39
|
-
"""Embed search docs."""
|
40
|
-
embedded_docs = (await AsyncClient().embed(self.model, texts))["embeddings"]
|
41
|
-
return embedded_docs
|
42
|
-
|
43
|
-
async def aembed_query(self, text: str) -> List[float]:
|
44
|
-
"""Embed query text."""
|
45
|
-
return (await self.aembed_documents([text]))[0]
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|