langchain-ollama 0.2.2rc1__py3-none-any.whl → 0.2.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.
- langchain_ollama/chat_models.py +437 -13
- langchain_ollama/embeddings.py +83 -5
- {langchain_ollama-0.2.2rc1.dist-info → langchain_ollama-0.2.3.dist-info}/METADATA +4 -3
- langchain_ollama-0.2.3.dist-info/RECORD +9 -0
- {langchain_ollama-0.2.2rc1.dist-info → langchain_ollama-0.2.3.dist-info}/WHEEL +1 -1
- langchain_ollama-0.2.2rc1.dist-info/RECORD +0 -9
- {langchain_ollama-0.2.2rc1.dist-info → langchain_ollama-0.2.3.dist-info}/LICENSE +0 -0
langchain_ollama/chat_models.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
"""Ollama chat models."""
|
2
2
|
|
3
|
+
import json
|
4
|
+
from operator import itemgetter
|
3
5
|
from typing import (
|
4
6
|
Any,
|
5
7
|
AsyncIterator,
|
@@ -21,6 +23,7 @@ from langchain_core.callbacks import (
|
|
21
23
|
CallbackManagerForLLMRun,
|
22
24
|
)
|
23
25
|
from langchain_core.callbacks.manager import AsyncCallbackManagerForLLMRun
|
26
|
+
from langchain_core.exceptions import OutputParserException
|
24
27
|
from langchain_core.language_models import LanguageModelInput
|
25
28
|
from langchain_core.language_models.chat_models import BaseChatModel, LangSmithParams
|
26
29
|
from langchain_core.messages import (
|
@@ -34,13 +37,24 @@ from langchain_core.messages import (
|
|
34
37
|
)
|
35
38
|
from langchain_core.messages.ai import UsageMetadata
|
36
39
|
from langchain_core.messages.tool import tool_call
|
40
|
+
from langchain_core.output_parsers import (
|
41
|
+
JsonOutputKeyToolsParser,
|
42
|
+
JsonOutputParser,
|
43
|
+
PydanticOutputParser,
|
44
|
+
PydanticToolsParser,
|
45
|
+
)
|
37
46
|
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
|
38
|
-
from langchain_core.runnables import Runnable
|
47
|
+
from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough
|
39
48
|
from langchain_core.tools import BaseTool
|
49
|
+
from langchain_core.utils.function_calling import (
|
50
|
+
_convert_any_typed_dicts_to_pydantic as convert_any_typed_dicts_to_pydantic,
|
51
|
+
)
|
40
52
|
from langchain_core.utils.function_calling import convert_to_openai_tool
|
53
|
+
from langchain_core.utils.pydantic import TypeBaseModel, is_basemodel_subclass
|
41
54
|
from ollama import AsyncClient, Client, Message, Options
|
42
|
-
from pydantic import PrivateAttr, model_validator
|
43
|
-
from
|
55
|
+
from pydantic import BaseModel, PrivateAttr, model_validator
|
56
|
+
from pydantic.json_schema import JsonSchemaValue
|
57
|
+
from typing_extensions import Self, is_typeddict
|
44
58
|
|
45
59
|
|
46
60
|
def _get_usage_metadata_from_generation_info(
|
@@ -60,6 +74,72 @@ def _get_usage_metadata_from_generation_info(
|
|
60
74
|
return None
|
61
75
|
|
62
76
|
|
77
|
+
def _parse_json_string(
|
78
|
+
json_string: str, raw_tool_call: dict[str, Any], skip: bool
|
79
|
+
) -> Any:
|
80
|
+
"""Attempt to parse a JSON string for tool calling.
|
81
|
+
|
82
|
+
Args:
|
83
|
+
json_string: JSON string to parse.
|
84
|
+
skip: Whether to ignore parsing errors and return the value anyways.
|
85
|
+
raw_tool_call: Raw tool call to include in error message.
|
86
|
+
|
87
|
+
Returns:
|
88
|
+
The parsed JSON string.
|
89
|
+
|
90
|
+
Raises:
|
91
|
+
OutputParserException: If the JSON string wrong invalid and skip=False.
|
92
|
+
"""
|
93
|
+
try:
|
94
|
+
return json.loads(json_string)
|
95
|
+
except json.JSONDecodeError as e:
|
96
|
+
if skip:
|
97
|
+
return json_string
|
98
|
+
msg = (
|
99
|
+
f"Function {raw_tool_call['function']['name']} arguments:\n\n"
|
100
|
+
f"{raw_tool_call['function']['arguments']}\n\nare not valid JSON. "
|
101
|
+
f"Received JSONDecodeError {e}"
|
102
|
+
)
|
103
|
+
raise OutputParserException(msg) from e
|
104
|
+
except TypeError as e:
|
105
|
+
if skip:
|
106
|
+
return json_string
|
107
|
+
msg = (
|
108
|
+
f"Function {raw_tool_call['function']['name']} arguments:\n\n"
|
109
|
+
f"{raw_tool_call['function']['arguments']}\n\nare not a string or a "
|
110
|
+
f"dictionary. Received TypeError {e}"
|
111
|
+
)
|
112
|
+
raise OutputParserException(msg) from e
|
113
|
+
|
114
|
+
|
115
|
+
def _parse_arguments_from_tool_call(
|
116
|
+
raw_tool_call: dict[str, Any],
|
117
|
+
) -> Optional[dict[str, Any]]:
|
118
|
+
"""Parse arguments by trying to parse any shallowly nested string-encoded JSON.
|
119
|
+
|
120
|
+
Band-aid fix for issue in Ollama with inconsistent tool call argument structure.
|
121
|
+
Should be removed/changed if fixed upstream.
|
122
|
+
See https://github.com/ollama/ollama/issues/6155
|
123
|
+
"""
|
124
|
+
if "function" not in raw_tool_call:
|
125
|
+
return None
|
126
|
+
arguments = raw_tool_call["function"]["arguments"]
|
127
|
+
parsed_arguments = {}
|
128
|
+
if isinstance(arguments, dict):
|
129
|
+
for key, value in arguments.items():
|
130
|
+
if isinstance(value, str):
|
131
|
+
parsed_arguments[key] = _parse_json_string(
|
132
|
+
value, skip=True, raw_tool_call=raw_tool_call
|
133
|
+
)
|
134
|
+
else:
|
135
|
+
parsed_arguments[key] = value
|
136
|
+
else:
|
137
|
+
parsed_arguments = _parse_json_string(
|
138
|
+
arguments, skip=False, raw_tool_call=raw_tool_call
|
139
|
+
)
|
140
|
+
return parsed_arguments
|
141
|
+
|
142
|
+
|
63
143
|
def _get_tool_calls_from_response(
|
64
144
|
response: Mapping[str, Any],
|
65
145
|
) -> List[ToolCall]:
|
@@ -72,7 +152,7 @@ def _get_tool_calls_from_response(
|
|
72
152
|
tool_call(
|
73
153
|
id=str(uuid4()),
|
74
154
|
name=tc["function"]["name"],
|
75
|
-
args=tc
|
155
|
+
args=_parse_arguments_from_tool_call(tc) or {},
|
76
156
|
)
|
77
157
|
)
|
78
158
|
return tool_calls
|
@@ -89,6 +169,10 @@ def _lc_tool_call_to_openai_tool_call(tool_call: ToolCall) -> dict:
|
|
89
169
|
}
|
90
170
|
|
91
171
|
|
172
|
+
def _is_pydantic_class(obj: Any) -> bool:
|
173
|
+
return isinstance(obj, type) and is_basemodel_subclass(obj)
|
174
|
+
|
175
|
+
|
92
176
|
class ChatOllama(BaseChatModel):
|
93
177
|
r"""Ollama chat model integration.
|
94
178
|
|
@@ -222,8 +306,6 @@ class ChatOllama(BaseChatModel):
|
|
222
306
|
'{"location": "Pune, India", "time_of_day": "morning"}'
|
223
307
|
|
224
308
|
Tool Calling:
|
225
|
-
.. warning::
|
226
|
-
Ollama currently does not support streaming for tools
|
227
309
|
|
228
310
|
.. code-block:: python
|
229
311
|
|
@@ -317,8 +399,8 @@ class ChatOllama(BaseChatModel):
|
|
317
399
|
to more diverse text, while a lower value (e.g., 0.5) will
|
318
400
|
generate more focused and conservative text. (Default: 0.9)"""
|
319
401
|
|
320
|
-
format: Literal["", "json"] =
|
321
|
-
"""Specify the format of the output (options: json)"""
|
402
|
+
format: Optional[Union[Literal["", "json"], JsonSchemaValue]] = None
|
403
|
+
"""Specify the format of the output (options: "json", JSON schema)."""
|
322
404
|
|
323
405
|
keep_alive: Optional[Union[int, str]] = None
|
324
406
|
"""How long the model will stay loaded into memory."""
|
@@ -375,12 +457,9 @@ class ChatOllama(BaseChatModel):
|
|
375
457
|
},
|
376
458
|
)
|
377
459
|
|
378
|
-
tools = kwargs.get("tools")
|
379
|
-
default_stream = not bool(tools)
|
380
|
-
|
381
460
|
params = {
|
382
461
|
"messages": ollama_messages,
|
383
|
-
"stream": kwargs.pop("stream",
|
462
|
+
"stream": kwargs.pop("stream", True),
|
384
463
|
"model": kwargs.pop("model", self.model),
|
385
464
|
"format": kwargs.pop("format", self.format),
|
386
465
|
"options": Options(**options_dict),
|
@@ -388,7 +467,7 @@ class ChatOllama(BaseChatModel):
|
|
388
467
|
**kwargs,
|
389
468
|
}
|
390
469
|
|
391
|
-
if tools:
|
470
|
+
if tools := kwargs.get("tools"):
|
392
471
|
params["tools"] = tools
|
393
472
|
|
394
473
|
return params
|
@@ -747,3 +826,348 @@ class ChatOllama(BaseChatModel):
|
|
747
826
|
""" # noqa: E501
|
748
827
|
formatted_tools = [convert_to_openai_tool(tool) for tool in tools]
|
749
828
|
return super().bind(tools=formatted_tools, **kwargs)
|
829
|
+
|
830
|
+
def with_structured_output(
|
831
|
+
self,
|
832
|
+
schema: Union[Dict, type],
|
833
|
+
*,
|
834
|
+
method: Literal[
|
835
|
+
"function_calling", "json_mode", "json_schema"
|
836
|
+
] = "function_calling",
|
837
|
+
include_raw: bool = False,
|
838
|
+
**kwargs: Any,
|
839
|
+
) -> Runnable[LanguageModelInput, Union[Dict, BaseModel]]:
|
840
|
+
"""Model wrapper that returns outputs formatted to match the given schema.
|
841
|
+
|
842
|
+
Args:
|
843
|
+
schema:
|
844
|
+
The output schema. Can be passed in as:
|
845
|
+
|
846
|
+
- a Pydantic class,
|
847
|
+
- a JSON schema
|
848
|
+
- a TypedDict class
|
849
|
+
- an OpenAI function/tool schema.
|
850
|
+
|
851
|
+
If ``schema`` is a Pydantic class then the model output will be a
|
852
|
+
Pydantic instance of that class, and the model-generated fields will be
|
853
|
+
validated by the Pydantic class. Otherwise the model output will be a
|
854
|
+
dict and will not be validated. See :meth:`langchain_core.utils.function_calling.convert_to_openai_tool`
|
855
|
+
for more on how to properly specify types and descriptions of
|
856
|
+
schema fields when specifying a Pydantic or TypedDict class.
|
857
|
+
|
858
|
+
method: The method for steering model generation, one of:
|
859
|
+
|
860
|
+
- "function_calling":
|
861
|
+
Uses Ollama's tool-calling API
|
862
|
+
- "json_schema":
|
863
|
+
Uses Ollama's structured output API: https://ollama.com/blog/structured-outputs
|
864
|
+
- "json_mode":
|
865
|
+
Specifies ``format="json"``. Note that if using JSON mode then you
|
866
|
+
must include instructions for formatting the output into the
|
867
|
+
desired schema into the model call.
|
868
|
+
|
869
|
+
include_raw:
|
870
|
+
If False then only the parsed structured output is returned. If
|
871
|
+
an error occurs during model output parsing it will be raised. If True
|
872
|
+
then both the raw model response (a BaseMessage) and the parsed model
|
873
|
+
response will be returned. If an error occurs during output parsing it
|
874
|
+
will be caught and returned as well. The final output is always a dict
|
875
|
+
with keys "raw", "parsed", and "parsing_error".
|
876
|
+
|
877
|
+
kwargs: Additional keyword args aren't supported.
|
878
|
+
|
879
|
+
Returns:
|
880
|
+
A Runnable that takes same inputs as a :class:`langchain_core.language_models.chat.BaseChatModel`.
|
881
|
+
|
882
|
+
| If ``include_raw`` is False and ``schema`` is a Pydantic class, Runnable outputs an instance of ``schema`` (i.e., a Pydantic object). Otherwise, if ``include_raw`` is False then Runnable outputs a dict.
|
883
|
+
|
884
|
+
| If ``include_raw`` is True, then Runnable outputs a dict with keys:
|
885
|
+
|
886
|
+
- "raw": BaseMessage
|
887
|
+
- "parsed": None if there was a parsing error, otherwise the type depends on the ``schema`` as described above.
|
888
|
+
- "parsing_error": Optional[BaseException]
|
889
|
+
|
890
|
+
.. versionchanged:: 0.2.2
|
891
|
+
|
892
|
+
Added support for structured output API via ``format`` parameter.
|
893
|
+
|
894
|
+
.. dropdown:: Example: schema=Pydantic class, method="function_calling", include_raw=False
|
895
|
+
|
896
|
+
.. code-block:: python
|
897
|
+
|
898
|
+
from typing import Optional
|
899
|
+
|
900
|
+
from langchain_ollama import ChatOllama
|
901
|
+
from pydantic import BaseModel, Field
|
902
|
+
|
903
|
+
|
904
|
+
class AnswerWithJustification(BaseModel):
|
905
|
+
'''An answer to the user question along with justification for the answer.'''
|
906
|
+
|
907
|
+
answer: str
|
908
|
+
justification: Optional[str] = Field(
|
909
|
+
default=..., description="A justification for the answer."
|
910
|
+
)
|
911
|
+
|
912
|
+
|
913
|
+
llm = ChatOllama(model="llama3.1", temperature=0)
|
914
|
+
structured_llm = llm.with_structured_output(
|
915
|
+
AnswerWithJustification
|
916
|
+
)
|
917
|
+
|
918
|
+
structured_llm.invoke(
|
919
|
+
"What weighs more a pound of bricks or a pound of feathers"
|
920
|
+
)
|
921
|
+
|
922
|
+
# -> AnswerWithJustification(
|
923
|
+
# answer='They weigh the same',
|
924
|
+
# justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
|
925
|
+
# )
|
926
|
+
|
927
|
+
.. dropdown:: Example: schema=Pydantic class, method="function_calling", include_raw=True
|
928
|
+
|
929
|
+
.. code-block:: python
|
930
|
+
|
931
|
+
from langchain_ollama import ChatOllama
|
932
|
+
from pydantic import BaseModel
|
933
|
+
|
934
|
+
|
935
|
+
class AnswerWithJustification(BaseModel):
|
936
|
+
'''An answer to the user question along with justification for the answer.'''
|
937
|
+
|
938
|
+
answer: str
|
939
|
+
justification: str
|
940
|
+
|
941
|
+
|
942
|
+
llm = ChatOllama(model="llama3.1", temperature=0)
|
943
|
+
structured_llm = llm.with_structured_output(
|
944
|
+
AnswerWithJustification, include_raw=True
|
945
|
+
)
|
946
|
+
|
947
|
+
structured_llm.invoke(
|
948
|
+
"What weighs more a pound of bricks or a pound of feathers"
|
949
|
+
)
|
950
|
+
# -> {
|
951
|
+
# 'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),
|
952
|
+
# 'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),
|
953
|
+
# 'parsing_error': None
|
954
|
+
# }
|
955
|
+
|
956
|
+
.. dropdown:: Example: schema=Pydantic class, method="json_schema", include_raw=False
|
957
|
+
|
958
|
+
.. code-block:: python
|
959
|
+
|
960
|
+
from typing import Optional
|
961
|
+
|
962
|
+
from langchain_ollama import ChatOllama
|
963
|
+
from pydantic import BaseModel, Field
|
964
|
+
|
965
|
+
|
966
|
+
class AnswerWithJustification(BaseModel):
|
967
|
+
'''An answer to the user question along with justification for the answer.'''
|
968
|
+
|
969
|
+
answer: str
|
970
|
+
justification: Optional[str] = Field(
|
971
|
+
default=..., description="A justification for the answer."
|
972
|
+
)
|
973
|
+
|
974
|
+
|
975
|
+
llm = ChatOllama(model="llama3.1", temperature=0)
|
976
|
+
structured_llm = llm.with_structured_output(
|
977
|
+
AnswerWithJustification, method="json_schema"
|
978
|
+
)
|
979
|
+
|
980
|
+
structured_llm.invoke(
|
981
|
+
"What weighs more a pound of bricks or a pound of feathers"
|
982
|
+
)
|
983
|
+
|
984
|
+
# -> AnswerWithJustification(
|
985
|
+
# answer='They weigh the same',
|
986
|
+
# justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'
|
987
|
+
# )
|
988
|
+
|
989
|
+
.. dropdown:: Example: schema=TypedDict class, method="function_calling", include_raw=False
|
990
|
+
|
991
|
+
.. code-block:: python
|
992
|
+
|
993
|
+
# IMPORTANT: If you are using Python <=3.8, you need to import Annotated
|
994
|
+
# from typing_extensions, not from typing.
|
995
|
+
from typing_extensions import Annotated, TypedDict
|
996
|
+
|
997
|
+
from langchain_ollama import ChatOllama
|
998
|
+
|
999
|
+
|
1000
|
+
class AnswerWithJustification(TypedDict):
|
1001
|
+
'''An answer to the user question along with justification for the answer.'''
|
1002
|
+
|
1003
|
+
answer: str
|
1004
|
+
justification: Annotated[
|
1005
|
+
Optional[str], None, "A justification for the answer."
|
1006
|
+
]
|
1007
|
+
|
1008
|
+
|
1009
|
+
llm = ChatOllama(model="llama3.1", temperature=0)
|
1010
|
+
structured_llm = llm.with_structured_output(AnswerWithJustification)
|
1011
|
+
|
1012
|
+
structured_llm.invoke(
|
1013
|
+
"What weighs more a pound of bricks or a pound of feathers"
|
1014
|
+
)
|
1015
|
+
# -> {
|
1016
|
+
# 'answer': 'They weigh the same',
|
1017
|
+
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
|
1018
|
+
# }
|
1019
|
+
|
1020
|
+
.. dropdown:: Example: schema=OpenAI function schema, method="function_calling", include_raw=False
|
1021
|
+
|
1022
|
+
.. code-block:: python
|
1023
|
+
|
1024
|
+
from langchain_ollama import ChatOllama
|
1025
|
+
|
1026
|
+
oai_schema = {
|
1027
|
+
'name': 'AnswerWithJustification',
|
1028
|
+
'description': 'An answer to the user question along with justification for the answer.',
|
1029
|
+
'parameters': {
|
1030
|
+
'type': 'object',
|
1031
|
+
'properties': {
|
1032
|
+
'answer': {'type': 'string'},
|
1033
|
+
'justification': {'description': 'A justification for the answer.', 'type': 'string'}
|
1034
|
+
},
|
1035
|
+
'required': ['answer']
|
1036
|
+
}
|
1037
|
+
}
|
1038
|
+
|
1039
|
+
llm = ChatOllama(model="llama3.1", temperature=0)
|
1040
|
+
structured_llm = llm.with_structured_output(oai_schema)
|
1041
|
+
|
1042
|
+
structured_llm.invoke(
|
1043
|
+
"What weighs more a pound of bricks or a pound of feathers"
|
1044
|
+
)
|
1045
|
+
# -> {
|
1046
|
+
# 'answer': 'They weigh the same',
|
1047
|
+
# 'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
|
1048
|
+
# }
|
1049
|
+
|
1050
|
+
.. dropdown:: Example: schema=Pydantic class, method="json_mode", include_raw=True
|
1051
|
+
|
1052
|
+
.. code-block::
|
1053
|
+
|
1054
|
+
from langchain_ollama import ChatOllama
|
1055
|
+
from pydantic import BaseModel
|
1056
|
+
|
1057
|
+
class AnswerWithJustification(BaseModel):
|
1058
|
+
answer: str
|
1059
|
+
justification: str
|
1060
|
+
|
1061
|
+
llm = ChatOllama(model="llama3.1", temperature=0)
|
1062
|
+
structured_llm = llm.with_structured_output(
|
1063
|
+
AnswerWithJustification,
|
1064
|
+
method="json_mode",
|
1065
|
+
include_raw=True
|
1066
|
+
)
|
1067
|
+
|
1068
|
+
structured_llm.invoke(
|
1069
|
+
"Answer the following question. "
|
1070
|
+
"Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n"
|
1071
|
+
"What's heavier a pound of bricks or a pound of feathers?"
|
1072
|
+
)
|
1073
|
+
# -> {
|
1074
|
+
# 'raw': AIMessage(content='{\\n "answer": "They are both the same weight.",\\n "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'),
|
1075
|
+
# 'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'),
|
1076
|
+
# 'parsing_error': None
|
1077
|
+
# }
|
1078
|
+
""" # noqa: E501, D301
|
1079
|
+
if kwargs:
|
1080
|
+
raise ValueError(f"Received unsupported arguments {kwargs}")
|
1081
|
+
is_pydantic_schema = _is_pydantic_class(schema)
|
1082
|
+
if method == "function_calling":
|
1083
|
+
if schema is None:
|
1084
|
+
raise ValueError(
|
1085
|
+
"schema must be specified when method is not 'json_mode'. "
|
1086
|
+
"Received None."
|
1087
|
+
)
|
1088
|
+
formatted_tool = convert_to_openai_tool(schema)
|
1089
|
+
tool_name = formatted_tool["function"]["name"]
|
1090
|
+
llm = self.bind_tools(
|
1091
|
+
[schema],
|
1092
|
+
tool_choice=tool_name,
|
1093
|
+
structured_output_format={
|
1094
|
+
"kwargs": {"method": method},
|
1095
|
+
"schema": formatted_tool,
|
1096
|
+
},
|
1097
|
+
)
|
1098
|
+
if is_pydantic_schema:
|
1099
|
+
output_parser: Runnable = PydanticToolsParser(
|
1100
|
+
tools=[schema], # type: ignore[list-item]
|
1101
|
+
first_tool_only=True,
|
1102
|
+
)
|
1103
|
+
else:
|
1104
|
+
output_parser = JsonOutputKeyToolsParser(
|
1105
|
+
key_name=tool_name, first_tool_only=True
|
1106
|
+
)
|
1107
|
+
elif method == "json_mode":
|
1108
|
+
llm = self.bind(
|
1109
|
+
format="json",
|
1110
|
+
structured_output_format={
|
1111
|
+
"kwargs": {"method": method},
|
1112
|
+
"schema": schema,
|
1113
|
+
},
|
1114
|
+
)
|
1115
|
+
output_parser = (
|
1116
|
+
PydanticOutputParser(pydantic_object=schema) # type: ignore[arg-type]
|
1117
|
+
if is_pydantic_schema
|
1118
|
+
else JsonOutputParser()
|
1119
|
+
)
|
1120
|
+
elif method == "json_schema":
|
1121
|
+
if schema is None:
|
1122
|
+
raise ValueError(
|
1123
|
+
"schema must be specified when method is not 'json_mode'. "
|
1124
|
+
"Received None."
|
1125
|
+
)
|
1126
|
+
if is_pydantic_schema:
|
1127
|
+
schema = cast(TypeBaseModel, schema)
|
1128
|
+
llm = self.bind(
|
1129
|
+
format=schema.model_json_schema(),
|
1130
|
+
structured_output_format={
|
1131
|
+
"kwargs": {"method": method},
|
1132
|
+
"schema": schema,
|
1133
|
+
},
|
1134
|
+
)
|
1135
|
+
output_parser = PydanticOutputParser(pydantic_object=schema)
|
1136
|
+
else:
|
1137
|
+
if is_typeddict(schema):
|
1138
|
+
schema = cast(type, schema)
|
1139
|
+
response_format = convert_any_typed_dicts_to_pydantic(
|
1140
|
+
schema, visited={}
|
1141
|
+
).schema() # type: ignore[attr-defined]
|
1142
|
+
if "required" not in response_format:
|
1143
|
+
response_format["required"] = list(
|
1144
|
+
response_format["properties"].keys()
|
1145
|
+
)
|
1146
|
+
else:
|
1147
|
+
# is JSON schema
|
1148
|
+
response_format = schema
|
1149
|
+
llm = self.bind(
|
1150
|
+
format=response_format,
|
1151
|
+
structured_output_format={
|
1152
|
+
"kwargs": {"method": method},
|
1153
|
+
"schema": response_format,
|
1154
|
+
},
|
1155
|
+
)
|
1156
|
+
output_parser = JsonOutputParser()
|
1157
|
+
else:
|
1158
|
+
raise ValueError(
|
1159
|
+
f"Unrecognized method argument. Expected one of 'function_calling', "
|
1160
|
+
f"'json_schema', or 'json_mode'. Received: '{method}'"
|
1161
|
+
)
|
1162
|
+
|
1163
|
+
if include_raw:
|
1164
|
+
parser_assign = RunnablePassthrough.assign(
|
1165
|
+
parsed=itemgetter("raw") | output_parser, parsing_error=lambda _: None
|
1166
|
+
)
|
1167
|
+
parser_none = RunnablePassthrough.assign(parsed=lambda _: None)
|
1168
|
+
parser_with_fallback = parser_assign.with_fallbacks(
|
1169
|
+
[parser_none], exception_key="parsing_error"
|
1170
|
+
)
|
1171
|
+
return RunnableMap(raw=llm) | parser_with_fallback
|
1172
|
+
else:
|
1173
|
+
return llm | output_parser
|
langchain_ollama/embeddings.py
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
"""Ollama embeddings models."""
|
2
2
|
|
3
|
-
from typing import
|
4
|
-
List,
|
5
|
-
Optional,
|
6
|
-
)
|
3
|
+
from typing import Any, Dict, List, Optional
|
7
4
|
|
8
5
|
from langchain_core.embeddings import Embeddings
|
9
6
|
from ollama import AsyncClient, Client
|
@@ -144,10 +141,89 @@ class OllamaEmbeddings(BaseModel, Embeddings):
|
|
144
141
|
The async client to use for making requests.
|
145
142
|
"""
|
146
143
|
|
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
|
+
|
147
204
|
model_config = ConfigDict(
|
148
205
|
extra="forbid",
|
149
206
|
)
|
150
207
|
|
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
|
+
|
151
227
|
@model_validator(mode="after")
|
152
228
|
def _set_clients(self) -> Self:
|
153
229
|
"""Set clients to use for ollama."""
|
@@ -158,7 +234,9 @@ class OllamaEmbeddings(BaseModel, Embeddings):
|
|
158
234
|
|
159
235
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
160
236
|
"""Embed search docs."""
|
161
|
-
embedded_docs = self._client.embed(
|
237
|
+
embedded_docs = self._client.embed(
|
238
|
+
self.model, texts, options=self._default_params
|
239
|
+
)["embeddings"]
|
162
240
|
return embedded_docs
|
163
241
|
|
164
242
|
def embed_query(self, text: str) -> List[float]:
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: langchain-ollama
|
3
|
-
Version: 0.2.
|
3
|
+
Version: 0.2.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,8 +11,9 @@ 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
|
-
|
15
|
-
Requires-Dist:
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
15
|
+
Requires-Dist: langchain-core (>=0.3.33,<0.4.0)
|
16
|
+
Requires-Dist: ollama (>=0.4.4,<1)
|
16
17
|
Project-URL: Repository, https://github.com/langchain-ai/langchain
|
17
18
|
Project-URL: Release Notes, https://github.com/langchain-ai/langchain/releases?q=tag%3A%22langchain-ollama%3D%3D0%22&expanded=true
|
18
19
|
Project-URL: Source Code, https://github.com/langchain-ai/langchain/tree/master/libs/partners/ollama
|
@@ -0,0 +1,9 @@
|
|
1
|
+
langchain_ollama/__init__.py,sha256=SxPRrWcPayJpbwhheTtlqCaPp9ffiAAgZMM5Wc1yYpM,634
|
2
|
+
langchain_ollama/chat_models.py,sha256=YDaHyz5t4EfQrMIGJsNFdiPH9LJUOBdrBjlr0qAC8GM,48172
|
3
|
+
langchain_ollama/embeddings.py,sha256=rZLgMvuEVqMRo1kPr9pPPrGVpEOes76cwzkXJRged_4,8397
|
4
|
+
langchain_ollama/llms.py,sha256=ojnYU0efhN10xhUINu1dCR2Erw79J_mYS6_l45J7Vls,12778
|
5
|
+
langchain_ollama/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
+
langchain_ollama-0.2.3.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
|
7
|
+
langchain_ollama-0.2.3.dist-info/METADATA,sha256=BZ3HPeJJiDPaEhUjJIC-3SmIhQuNs6r97LS7EOVoPsE,1876
|
8
|
+
langchain_ollama-0.2.3.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
9
|
+
langchain_ollama-0.2.3.dist-info/RECORD,,
|
@@ -1,9 +0,0 @@
|
|
1
|
-
langchain_ollama/__init__.py,sha256=SxPRrWcPayJpbwhheTtlqCaPp9ffiAAgZMM5Wc1yYpM,634
|
2
|
-
langchain_ollama/chat_models.py,sha256=BS28WEnDBq0aUrlOyABbcMkvIk4C-oV_Zj6bnhQoJkM,29902
|
3
|
-
langchain_ollama/embeddings.py,sha256=svqdPF44qX5qbFpZmLiXrzTC-AldmMlZRS5wBfY-EmA,5056
|
4
|
-
langchain_ollama/llms.py,sha256=ojnYU0efhN10xhUINu1dCR2Erw79J_mYS6_l45J7Vls,12778
|
5
|
-
langchain_ollama/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
|
-
langchain_ollama-0.2.2rc1.dist-info/LICENSE,sha256=2btS8uNUDWD_UNjw9ba6ZJt_00aUjEw9CGyK-xIHY8c,1072
|
7
|
-
langchain_ollama-0.2.2rc1.dist-info/METADATA,sha256=E9wttWytUkVCrJtbUjYA0nMxIt8tTkZOQZDFCU6Z_nc,1828
|
8
|
-
langchain_ollama-0.2.2rc1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
9
|
-
langchain_ollama-0.2.2rc1.dist-info/RECORD,,
|
File without changes
|