mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
ollama[patch]: ruff fixes and rules (#31924)
* bump ruff deps * add more thorough ruff rules * fix said rules
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"""Ollama large language models."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import AsyncIterator, Iterator, Mapping
|
||||
from typing import (
|
||||
Any,
|
||||
@@ -132,22 +134,22 @@ class OllamaLLM(BaseLLM):
|
||||
"""Base url the model is hosted under."""
|
||||
|
||||
client_kwargs: Optional[dict] = {}
|
||||
"""Additional kwargs to pass to the httpx clients.
|
||||
"""Additional kwargs to pass to the httpx clients.
|
||||
These arguments are passed to both synchronous and async clients.
|
||||
Use sync_client_kwargs and async_client_kwargs to pass different arguments
|
||||
to synchronous and asynchronous clients.
|
||||
"""
|
||||
|
||||
async_client_kwargs: Optional[dict] = {}
|
||||
"""Additional kwargs to merge with client_kwargs before passing to the HTTPX
|
||||
"""Additional kwargs to merge with client_kwargs before passing to the HTTPX
|
||||
AsyncClient.
|
||||
|
||||
|
||||
For a full list of the params, see the `HTTPX documentation <https://www.python-httpx.org/api/#asyncclient>`__.
|
||||
"""
|
||||
|
||||
sync_client_kwargs: Optional[dict] = {}
|
||||
"""Additional kwargs to merge with client_kwargs before passing to the HTTPX Client.
|
||||
|
||||
|
||||
For a full list of the params, see the `HTTPX documentation <https://www.python-httpx.org/api/#client>`__.
|
||||
"""
|
||||
|
||||
@@ -168,7 +170,8 @@ class OllamaLLM(BaseLLM):
|
||||
**kwargs: Any,
|
||||
) -> dict[str, Any]:
|
||||
if self.stop is not None and stop is not None:
|
||||
raise ValueError("`stop` found in both the input and default params.")
|
||||
msg = "`stop` found in both the input and default params."
|
||||
raise ValueError(msg)
|
||||
if self.stop is not None:
|
||||
stop = self.stop
|
||||
|
||||
@@ -193,7 +196,7 @@ class OllamaLLM(BaseLLM):
|
||||
},
|
||||
)
|
||||
|
||||
params = {
|
||||
return {
|
||||
"prompt": prompt,
|
||||
"stream": kwargs.pop("stream", True),
|
||||
"model": kwargs.pop("model", self.model),
|
||||
@@ -204,8 +207,6 @@ class OllamaLLM(BaseLLM):
|
||||
**kwargs,
|
||||
}
|
||||
|
||||
return params
|
||||
|
||||
@property
|
||||
def _llm_type(self) -> str:
|
||||
"""Return type of LLM."""
|
||||
@@ -267,14 +268,14 @@ class OllamaLLM(BaseLLM):
|
||||
prompt: str,
|
||||
stop: Optional[list[str]] = None,
|
||||
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
|
||||
verbose: bool = False,
|
||||
verbose: bool = False, # noqa: FBT001, FBT002
|
||||
**kwargs: Any,
|
||||
) -> GenerationChunk:
|
||||
final_chunk = None
|
||||
async for stream_resp in self._acreate_generate_stream(prompt, stop, **kwargs):
|
||||
if not isinstance(stream_resp, str):
|
||||
chunk = GenerationChunk(
|
||||
text=stream_resp["response"] if "response" in stream_resp else "",
|
||||
text=stream_resp.get("response", ""),
|
||||
generation_info=(
|
||||
dict(stream_resp) if stream_resp.get("done") is True else None
|
||||
),
|
||||
@@ -290,7 +291,8 @@ class OllamaLLM(BaseLLM):
|
||||
verbose=verbose,
|
||||
)
|
||||
if final_chunk is None:
|
||||
raise ValueError("No data received from Ollama stream.")
|
||||
msg = "No data received from Ollama stream."
|
||||
raise ValueError(msg)
|
||||
|
||||
return final_chunk
|
||||
|
||||
@@ -299,14 +301,14 @@ class OllamaLLM(BaseLLM):
|
||||
prompt: str,
|
||||
stop: Optional[list[str]] = None,
|
||||
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
||||
verbose: bool = False,
|
||||
verbose: bool = False, # noqa: FBT001, FBT002
|
||||
**kwargs: Any,
|
||||
) -> GenerationChunk:
|
||||
final_chunk = None
|
||||
for stream_resp in self._create_generate_stream(prompt, stop, **kwargs):
|
||||
if not isinstance(stream_resp, str):
|
||||
chunk = GenerationChunk(
|
||||
text=stream_resp["response"] if "response" in stream_resp else "",
|
||||
text=stream_resp.get("response", ""),
|
||||
generation_info=(
|
||||
dict(stream_resp) if stream_resp.get("done") is True else None
|
||||
),
|
||||
@@ -322,7 +324,8 @@ class OllamaLLM(BaseLLM):
|
||||
verbose=verbose,
|
||||
)
|
||||
if final_chunk is None:
|
||||
raise ValueError("No data received from Ollama stream.")
|
||||
msg = "No data received from Ollama stream."
|
||||
raise ValueError(msg)
|
||||
|
||||
return final_chunk
|
||||
|
||||
|
||||
Reference in New Issue
Block a user