Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12b948a

Browse files
authored
models - ollama - init async client per request (strands-agents#433)
1 parent 11d2e7d commit 12b948a

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/strands/models/ollama.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,12 @@ def __init__(
6868
ollama_client_args: Additional arguments for the Ollama client.
6969
**model_config: Configuration options for the Ollama model.
7070
"""
71+
self.host = host
72+
self.client_args = ollama_client_args or {}
7173
self.config = OllamaModel.OllamaConfig(**model_config)
7274

7375
logger.debug("config=<%s> | initializing", self.config)
7476

75-
ollama_client_args = ollama_client_args if ollama_client_args is not None else {}
76-
77-
self.client = ollama.AsyncClient(host, **ollama_client_args)
78-
7977
@override
8078
def update_config(self, **model_config: Unpack[OllamaConfig]) -> None: # type: ignore
8179
"""Update the Ollama Model configuration with the provided arguments.
@@ -306,7 +304,8 @@ async def stream(
306304
logger.debug("invoking model")
307305
tool_requested = False
308306

309-
response = await self.client.chat(**request)
307+
client = ollama.AsyncClient(self.host, **self.client_args)
308+
response = await client.chat(**request)
310309

311310
logger.debug("got response from model")
312311
yield self.format_chunk({"chunk_type": "message_start"})
@@ -346,7 +345,9 @@ async def structured_output(
346345
formatted_request = self.format_request(messages=prompt)
347346
formatted_request["format"] = output_model.model_json_schema()
348347
formatted_request["stream"] = False
349-
response = await self.client.chat(**formatted_request)
348+
349+
client = ollama.AsyncClient(self.host, **self.client_args)
350+
response = await client.chat(**formatted_request)
350351

351352
try:
352353
content = response.message.content.strip()

0 commit comments

Comments
 (0)