Skip to content

The agent engineering platform.

Update ollama.py with optional raw setting. (#21486)

Bitmonkey570d45b2a1f3
diff --git a/libs/community/langchain_community/llms/ollama.py b/libs/community/langchain_community/llms/ollama.py
index 4a81871efb..43e4fbd511 100644
--- a/libs/community/langchain_community/llms/ollama.py
+++ b/libs/community/langchain_community/llms/ollama.py
@@ -112,15 +112,16 @@ class _OllamaCommon(BaseLanguageModel):
"""Timeout for the request stream"""
keep_alive: Optional[Union[int, str]] = None
- """How long the model will stay loaded into memory.
+ """How long the model will stay loaded into memory."""
+ raw: Optional[bool] = None
+ """raw or not.""
The parameter (Default: 5 minutes) can be set to:
1. a duration string in Golang (such as "10m" or "24h");
2. a number in seconds (such as 3600);
3. any negative number which will keep the model loaded \
in memory (e.g. -1 or "-1m");
4. 0 which will unload the model immediately after generating a response;
-
See the [Ollama documents](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-do-i-keep-a-model-loaded-in-memory-or-make-it-unload-immediately)"""
headers: Optional[dict] = None
@@ -154,6 +155,7 @@ class _OllamaCommon(BaseLanguageModel):
"system": self.system,
"template": self.template,
"keep_alive": self.keep_alive,
+ "raw": self.raw,
}
@property
@@ -227,7 +229,6 @@ class _OllamaCommon(BaseLanguageModel):
"images": payload.get("images", []),
**params,
}
-
response = requests.post(
url=api_url,
headers={
@@ -369,12 +370,9 @@ class _OllamaCommon(BaseLanguageModel):
class Ollama(BaseLLM, _OllamaCommon):
"""Ollama locally runs large language models.
-
To use, follow the instructions at https://ollama.ai/.
-
Example:
.. code-block:: python
-
from langchain_community.llms import Ollama
ollama = Ollama(model="llama2")
"""
@@ -398,17 +396,13 @@ class Ollama(BaseLLM, _OllamaCommon):
**kwargs: Any,
) -> LLMResult:
"""Call out to Ollama's generate endpoint.
-
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
-
Returns:
The string generated by the model.
-
Example:
.. code-block:: python
-
response = ollama("Tell me a joke.")
"""
# TODO: add caching here.
@@ -434,17 +428,13 @@ class Ollama(BaseLLM, _OllamaCommon):
**kwargs: Any,
) -> LLMResult:
"""Call out to Ollama's generate endpoint.
-
Args:
prompt: The prompt to pass into the model.
stop: Optional list of stop words to use when generating.
-
Returns:
The string generated by the model.
-
Example:
.. code-block:: python
-
response = ollama("Tell me a joke.")
"""
# TODO: add caching here.
diff --git a/libs/community/tests/unit_tests/llms/test_ollama.py b/libs/community/tests/unit_tests/llms/test_ollama.py
index 0c8c4e9ace..6d6ce632a8 100644
--- a/libs/community/tests/unit_tests/llms/test_ollama.py
+++ b/libs/community/tests/unit_tests/llms/test_ollama.py
@@ -101,6 +101,7 @@ def test_handle_kwargs_top_level_parameters(monkeypatch: MonkeyPatch) -> None:
"system": "Test system prompt",
"template": None,
"keep_alive": None,
+ "raw": None,
}
assert stream is True
assert timeout == 300
@@ -149,6 +150,7 @@ def test_handle_kwargs_with_unknown_param(monkeypatch: MonkeyPatch) -> None:
"system": None,
"template": None,
"keep_alive": None,
+ "raw": None,
}
assert stream is True
assert timeout == 300
@@ -181,6 +183,7 @@ def test_handle_kwargs_with_options(monkeypatch: MonkeyPatch) -> None:
"system": None,
"template": None,
"keep_alive": None,
+ "raw": None,
}
assert stream is True
assert timeout == 300