langchain-ollama 0.1.0rc0__py3-none-any.whl → 0.1.2__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.
- langchain_ollama/chat_models.py +109 -37
- langchain_ollama/embeddings.py +139 -19
- langchain_ollama/llms.py +56 -24
- {langchain_ollama-0.1.0rc0.dist-info → langchain_ollama-0.1.2.dist-info}/METADATA +6 -5
- langchain_ollama-0.1.2.dist-info/RECORD +9 -0
- {langchain_ollama-0.1.0rc0.dist-info → langchain_ollama-0.1.2.dist-info}/WHEEL +1 -1
- langchain_ollama-0.1.0rc0.dist-info/RECORD +0 -9
- {langchain_ollama-0.1.0rc0.dist-info → langchain_ollama-0.1.2.dist-info}/LICENSE +0 -0
langchain_ollama/chat_models.py
CHANGED
@@ -17,7 +17,6 @@ from typing import (
|
|
17
17
|
)
|
18
18
|
from uuid import uuid4
|
19
19
|
|
20
|
-
import ollama
|
21
20
|
from langchain_core.callbacks import (
|
22
21
|
CallbackManagerForLLMRun,
|
23
22
|
)
|
@@ -36,11 +35,11 @@ from langchain_core.messages import (
|
|
36
35
|
from langchain_core.messages.ai import UsageMetadata
|
37
36
|
from langchain_core.messages.tool import tool_call
|
38
37
|
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
|
39
|
-
from langchain_core.pydantic_v1 import
|
38
|
+
from langchain_core.pydantic_v1 import Field, root_validator
|
40
39
|
from langchain_core.runnables import Runnable
|
41
40
|
from langchain_core.tools import BaseTool
|
42
41
|
from langchain_core.utils.function_calling import convert_to_openai_tool
|
43
|
-
from ollama import AsyncClient, Message, Options
|
42
|
+
from ollama import AsyncClient, Client, Message, Options
|
44
43
|
|
45
44
|
|
46
45
|
def _get_usage_metadata_from_generation_info(
|
@@ -218,9 +217,32 @@ class ChatOllama(BaseChatModel):
|
|
218
217
|
.. code-block:: python
|
219
218
|
|
220
219
|
'{"location": "Pune, India", "time_of_day": "morning"}'
|
220
|
+
|
221
|
+
Tool Calling:
|
222
|
+
.. warning::
|
223
|
+
Ollama currently does not support streaming for tools
|
224
|
+
|
225
|
+
.. code-block:: python
|
226
|
+
|
227
|
+
from langchain_ollama import ChatOllama
|
228
|
+
from langchain_core.pydantic_v1 import BaseModel, Field
|
229
|
+
|
230
|
+
class Multiply(BaseModel):
|
231
|
+
a: int = Field(..., description="First integer")
|
232
|
+
b: int = Field(..., description="Second integer")
|
233
|
+
|
234
|
+
ans = await chat.invoke("What is 45*67")
|
235
|
+
ans.tool_calls
|
236
|
+
|
237
|
+
.. code-block:: python
|
238
|
+
|
239
|
+
[{'name': 'Multiply',
|
240
|
+
'args': {'a': 45, 'b': 67},
|
241
|
+
'id': '420c3f3b-df10-4188-945f-eb3abdb40622',
|
242
|
+
'type': 'tool_call'}]
|
221
243
|
""" # noqa: E501
|
222
244
|
|
223
|
-
model: str
|
245
|
+
model: str
|
224
246
|
"""Model name to use."""
|
225
247
|
|
226
248
|
mirostat: Optional[int] = None
|
@@ -269,6 +291,11 @@ class ChatOllama(BaseChatModel):
|
|
269
291
|
"""The temperature of the model. Increasing the temperature will
|
270
292
|
make the model answer more creatively. (Default: 0.8)"""
|
271
293
|
|
294
|
+
seed: Optional[int] = None
|
295
|
+
"""Sets the random number seed to use for generation. Setting this
|
296
|
+
to a specific number will make the model generate the same text for
|
297
|
+
the same prompt."""
|
298
|
+
|
272
299
|
stop: Optional[List[str]] = None
|
273
300
|
"""Sets the stop tokens to use."""
|
274
301
|
|
@@ -293,6 +320,24 @@ class ChatOllama(BaseChatModel):
|
|
293
320
|
keep_alive: Optional[Union[int, str]] = None
|
294
321
|
"""How long the model will stay loaded into memory."""
|
295
322
|
|
323
|
+
base_url: Optional[str] = None
|
324
|
+
"""Base url the model is hosted under."""
|
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
|
+
|
296
341
|
@property
|
297
342
|
def _default_params(self) -> Dict[str, Any]:
|
298
343
|
"""Get the default parameters for calling Ollama."""
|
@@ -310,6 +355,7 @@ class ChatOllama(BaseChatModel):
|
|
310
355
|
"repeat_last_n": self.repeat_last_n,
|
311
356
|
"repeat_penalty": self.repeat_penalty,
|
312
357
|
"temperature": self.temperature,
|
358
|
+
"seed": self.seed,
|
313
359
|
"stop": self.stop,
|
314
360
|
"tfs_z": self.tfs_z,
|
315
361
|
"top_k": self.top_k,
|
@@ -318,12 +364,21 @@ class ChatOllama(BaseChatModel):
|
|
318
364
|
"keep_alive": self.keep_alive,
|
319
365
|
}
|
320
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
|
+
|
321
376
|
def _convert_messages_to_ollama_messages(
|
322
377
|
self, messages: List[BaseMessage]
|
323
378
|
) -> Sequence[Message]:
|
324
379
|
ollama_messages: List = []
|
325
380
|
for message in messages:
|
326
|
-
role
|
381
|
+
role: Literal["user", "assistant", "system", "tool"]
|
327
382
|
tool_call_id: Optional[str] = None
|
328
383
|
tool_calls: Optional[List[Dict[str, Any]]] = None
|
329
384
|
if isinstance(message, HumanMessage):
|
@@ -360,11 +415,13 @@ class ChatOllama(BaseChatModel):
|
|
360
415
|
image_url = None
|
361
416
|
temp_image_url = content_part.get("image_url")
|
362
417
|
if isinstance(temp_image_url, str):
|
363
|
-
image_url =
|
418
|
+
image_url = temp_image_url
|
364
419
|
elif (
|
365
|
-
isinstance(temp_image_url, dict)
|
420
|
+
isinstance(temp_image_url, dict)
|
421
|
+
and "url" in temp_image_url
|
422
|
+
and isinstance(temp_image_url["url"], str)
|
366
423
|
):
|
367
|
-
image_url = temp_image_url
|
424
|
+
image_url = temp_image_url["url"]
|
368
425
|
else:
|
369
426
|
raise ValueError(
|
370
427
|
"Only string image_url or dict with string 'url' "
|
@@ -385,15 +442,16 @@ class ChatOllama(BaseChatModel):
|
|
385
442
|
"Must either have type 'text' or type 'image_url' "
|
386
443
|
"with a string 'image_url' field."
|
387
444
|
)
|
388
|
-
|
445
|
+
# Should convert to ollama.Message once role includes tool, and tool_call_id is in Message # noqa: E501
|
446
|
+
msg: dict = {
|
389
447
|
"role": role,
|
390
448
|
"content": content,
|
391
449
|
"images": images,
|
392
450
|
}
|
451
|
+
if tool_calls:
|
452
|
+
msg["tool_calls"] = tool_calls # type: ignore
|
393
453
|
if tool_call_id:
|
394
454
|
msg["tool_call_id"] = tool_call_id
|
395
|
-
if tool_calls:
|
396
|
-
msg["tool_calls"] = tool_calls
|
397
455
|
ollama_messages.append(msg)
|
398
456
|
|
399
457
|
return ollama_messages
|
@@ -415,15 +473,26 @@ class ChatOllama(BaseChatModel):
|
|
415
473
|
params[key] = kwargs[key]
|
416
474
|
|
417
475
|
params["options"]["stop"] = stop
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
476
|
+
if "tools" in kwargs:
|
477
|
+
yield await self._async_client.chat(
|
478
|
+
model=params["model"],
|
479
|
+
messages=ollama_messages,
|
480
|
+
stream=False,
|
481
|
+
options=Options(**params["options"]),
|
482
|
+
keep_alive=params["keep_alive"],
|
483
|
+
format=params["format"],
|
484
|
+
tools=kwargs["tools"],
|
485
|
+
) # type:ignore
|
486
|
+
else:
|
487
|
+
async for part in await self._async_client.chat(
|
488
|
+
model=params["model"],
|
489
|
+
messages=ollama_messages,
|
490
|
+
stream=True,
|
491
|
+
options=Options(**params["options"]),
|
492
|
+
keep_alive=params["keep_alive"],
|
493
|
+
format=params["format"],
|
494
|
+
): # type:ignore
|
495
|
+
yield part
|
427
496
|
|
428
497
|
def _create_chat_stream(
|
429
498
|
self,
|
@@ -443,25 +512,17 @@ class ChatOllama(BaseChatModel):
|
|
443
512
|
|
444
513
|
params["options"]["stop"] = stop
|
445
514
|
if "tools" in kwargs:
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
"messages": ollama_messages,
|
450
|
-
"stream": False,
|
451
|
-
"format": params["format"],
|
452
|
-
"options": Options(**params["options"]),
|
453
|
-
"keep_alive": params["keep_alive"],
|
454
|
-
"tools": kwargs["tools"],
|
455
|
-
}
|
456
|
-
it = ollama._client._request_stream(
|
457
|
-
"POST",
|
458
|
-
"/api/chat",
|
459
|
-
json=req,
|
515
|
+
yield self._client.chat(
|
516
|
+
model=params["model"],
|
517
|
+
messages=ollama_messages,
|
460
518
|
stream=False,
|
519
|
+
options=Options(**params["options"]),
|
520
|
+
keep_alive=params["keep_alive"],
|
521
|
+
format=params["format"],
|
522
|
+
tools=kwargs["tools"],
|
461
523
|
)
|
462
|
-
yield cast(Mapping[str, Any], it)
|
463
524
|
else:
|
464
|
-
yield from
|
525
|
+
yield from self._client.chat(
|
465
526
|
model=params["model"],
|
466
527
|
messages=ollama_messages,
|
467
528
|
stream=True,
|
@@ -686,8 +747,19 @@ class ChatOllama(BaseChatModel):
|
|
686
747
|
|
687
748
|
def bind_tools(
|
688
749
|
self,
|
689
|
-
tools: Sequence[Union[Dict[str, Any], Type
|
750
|
+
tools: Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]],
|
690
751
|
**kwargs: Any,
|
691
752
|
) -> Runnable[LanguageModelInput, BaseMessage]:
|
753
|
+
"""Bind tool-like objects to this chat model.
|
754
|
+
|
755
|
+
Assumes model is compatible with OpenAI tool-calling API.
|
756
|
+
|
757
|
+
Args:
|
758
|
+
tools: A list of tool definitions to bind to this chat model.
|
759
|
+
Supports any tool definition handled by
|
760
|
+
:meth:`langchain_core.utils.function_calling.convert_to_openai_tool`.
|
761
|
+
kwargs: Any additional parameters are passed directly to
|
762
|
+
``self.bind(**kwargs)``.
|
763
|
+
""" # noqa: E501
|
692
764
|
formatted_tools = [convert_to_openai_tool(tool) for tool in tools]
|
693
765
|
return super().bind(tools=formatted_tools, **kwargs)
|
langchain_ollama/embeddings.py
CHANGED
@@ -1,36 +1,158 @@
|
|
1
|
-
from typing import
|
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,
|
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
|
-
"""
|
12
|
+
"""Ollama embedding model integration.
|
11
13
|
|
12
|
-
|
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
|
-
|
18
|
-
|
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])
|
20
85
|
|
21
|
-
|
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
|
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 =
|
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 = []
|
32
|
-
for doc in texts:
|
33
|
-
embedded_docs.append(list(ollama.embeddings(self.model, doc)["embedding"]))
|
155
|
+
embedded_docs = self._client.embed(self.model, texts)["embeddings"]
|
34
156
|
return embedded_docs
|
35
157
|
|
36
158
|
def embed_query(self, text: str) -> List[float]:
|
@@ -39,11 +161,9 @@ class OllamaEmbeddings(BaseModel, Embeddings):
|
|
39
161
|
|
40
162
|
async def aembed_documents(self, texts: List[str]) -> List[List[float]]:
|
41
163
|
"""Embed search docs."""
|
42
|
-
embedded_docs = [
|
43
|
-
|
44
|
-
|
45
|
-
list((await AsyncClient().embeddings(self.model, doc))["embedding"])
|
46
|
-
)
|
164
|
+
embedded_docs = (await self._async_client.embed(self.model, texts))[
|
165
|
+
"embeddings"
|
166
|
+
]
|
47
167
|
return embedded_docs
|
48
168
|
|
49
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
|
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):
|
@@ -34,7 +34,7 @@ class OllamaLLM(BaseLLM):
|
|
34
34
|
model.invoke("Come up with 10 names for a song about parrots")
|
35
35
|
"""
|
36
36
|
|
37
|
-
model: str
|
37
|
+
model: str
|
38
38
|
"""Model name to use."""
|
39
39
|
|
40
40
|
mirostat: Optional[int] = None
|
@@ -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,
|
@@ -205,9 +241,9 @@ class OllamaLLM(BaseLLM):
|
|
205
241
|
if not isinstance(stream_resp, str):
|
206
242
|
chunk = GenerationChunk(
|
207
243
|
text=stream_resp["response"] if "response" in stream_resp else "",
|
208
|
-
generation_info=
|
209
|
-
|
210
|
-
|
244
|
+
generation_info=(
|
245
|
+
dict(stream_resp) if stream_resp.get("done") is True else None
|
246
|
+
),
|
211
247
|
)
|
212
248
|
if final_chunk is None:
|
213
249
|
final_chunk = chunk
|
@@ -237,9 +273,9 @@ class OllamaLLM(BaseLLM):
|
|
237
273
|
if not isinstance(stream_resp, str):
|
238
274
|
chunk = GenerationChunk(
|
239
275
|
text=stream_resp["response"] if "response" in stream_resp else "",
|
240
|
-
generation_info=
|
241
|
-
|
242
|
-
|
276
|
+
generation_info=(
|
277
|
+
dict(stream_resp) if stream_resp.get("done") is True else None
|
278
|
+
),
|
243
279
|
)
|
244
280
|
if final_chunk is None:
|
245
281
|
final_chunk = chunk
|
@@ -304,12 +340,10 @@ class OllamaLLM(BaseLLM):
|
|
304
340
|
for stream_resp in self._create_generate_stream(prompt, stop, **kwargs):
|
305
341
|
if not isinstance(stream_resp, str):
|
306
342
|
chunk = GenerationChunk(
|
307
|
-
text=stream_resp
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
if stream_resp.get("done") is True
|
312
|
-
else None,
|
343
|
+
text=(stream_resp.get("response", "")),
|
344
|
+
generation_info=(
|
345
|
+
dict(stream_resp) if stream_resp.get("done") is True else None
|
346
|
+
),
|
313
347
|
)
|
314
348
|
if run_manager:
|
315
349
|
run_manager.on_llm_new_token(
|
@@ -328,12 +362,10 @@ class OllamaLLM(BaseLLM):
|
|
328
362
|
async for stream_resp in self._acreate_generate_stream(prompt, stop, **kwargs):
|
329
363
|
if not isinstance(stream_resp, str):
|
330
364
|
chunk = GenerationChunk(
|
331
|
-
text=stream_resp
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
if stream_resp.get("done") is True
|
336
|
-
else None,
|
365
|
+
text=(stream_resp.get("response", "")),
|
366
|
+
generation_info=(
|
367
|
+
dict(stream_resp) if stream_resp.get("done") is True else None
|
368
|
+
),
|
337
369
|
)
|
338
370
|
if run_manager:
|
339
371
|
await run_manager.on_llm_new_token(
|
@@ -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,9 +11,10 @@ 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 (
|
15
|
-
Requires-Dist: ollama (>=0.
|
14
|
+
Requires-Dist: langchain-core (==0.2.36)
|
15
|
+
Requires-Dist: ollama (>=0.3.0,<1)
|
16
16
|
Project-URL: Repository, https://github.com/langchain-ai/langchain
|
17
|
+
Project-URL: Release Notes, https://github.com/langchain-ai/langchain/releases?q=tag%3A%22langchain-ollama%3D%3D0%22&expanded=true
|
17
18
|
Project-URL: Source Code, https://github.com/langchain-ai/langchain/tree/master/libs/partners/ollama
|
18
19
|
Description-Content-Type: text/markdown
|
19
20
|
|
@@ -37,7 +38,7 @@ You can download it [here](https://ollama.com/download).
|
|
37
38
|
```python
|
38
39
|
from langchain_ollama import ChatOllama
|
39
40
|
|
40
|
-
llm = ChatOllama(model="llama3")
|
41
|
+
llm = ChatOllama(model="llama3-groq-tool-use")
|
41
42
|
llm.invoke("Sing a ballad of LangChain.")
|
42
43
|
```
|
43
44
|
|
@@ -58,7 +59,7 @@ embeddings.embed_query("What is the meaning of life?")
|
|
58
59
|
```python
|
59
60
|
from langchain_ollama import OllamaLLM
|
60
61
|
|
61
|
-
llm = OllamaLLM()
|
62
|
+
llm = OllamaLLM(model="llama3")
|
62
63
|
llm.invoke("The meaning of life is")
|
63
64
|
```
|
64
65
|
|
@@ -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.2.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
|
7
|
+
langchain_ollama-0.1.2.dist-info/METADATA,sha256=hzrdbivZX2tDz7Vy4sqf2LBBcJ7e-c22qiyJZILYNuc,1820
|
8
|
+
langchain_ollama-0.1.2.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
9
|
+
langchain_ollama-0.1.2.dist-info/RECORD,,
|
@@ -1,9 +0,0 @@
|
|
1
|
-
langchain_ollama/__init__.py,sha256=HhQZqbCjhrbr2dC_9Dkw12pg4HPjnDXUoInROMNJKqA,518
|
2
|
-
langchain_ollama/chat_models.py,sha256=HgKG2MJ-ORJHC9ootpZNq9Gi507awQWAhWqJYkz-w9U,27680
|
3
|
-
langchain_ollama/embeddings.py,sha256=_T8N0rh3z9mZHwwUFYNlJngjYmHt5GiQZVOy7l4JNMc,1551
|
4
|
-
langchain_ollama/llms.py,sha256=rbpupb4cx0dG692Drs5Verm1Tz48U9gdcLuxkZcxBpo,12071
|
5
|
-
langchain_ollama/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
-
langchain_ollama-0.1.0rc0.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
|
7
|
-
langchain_ollama-0.1.0rc0.dist-info/METADATA,sha256=uVl4yXYxTEAZykbX5bxB36iJTeBfnlLvkQClaJhT2nw,1671
|
8
|
-
langchain_ollama-0.1.0rc0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
9
|
-
langchain_ollama-0.1.0rc0.dist-info/RECORD,,
|
File without changes
|