Skip to content

Commit

Permalink
Add stream option to _agenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojciech-Rebisz committed Sep 11, 2024
1 parent 2988608 commit 9b29b64
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 13 additions & 5 deletions libs/ibm/langchain_ibm/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,25 @@ async def _agenerate(
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
stream: Optional[bool] = None,
**kwargs: Any,
) -> LLMResult:
"""Async run the LLM on the given prompt and input."""
params, kwargs = self._get_chat_params(stop=stop, **kwargs)
params = self._validate_chat_params(params)
responses = [
await self.watsonx_model.agenerate(prompt=prompt, params=params, **kwargs)
for prompt in prompts
]
if stream:
return await super()._agenerate(
prompts=prompts, stop=stop, run_manager=run_manager, **kwargs
)
else:
responses = [
await self.watsonx_model.agenerate(
prompt=prompt, params=params, **kwargs
)
for prompt in prompts
]

return self._create_llm_result(responses)
return self._create_llm_result(responses)

def _stream(
self,
Expand Down
10 changes: 10 additions & 0 deletions libs/ibm/tests/integration_tests/test_llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,16 @@ async def test_watsonx_agenerate() -> None:
assert response.llm_output["token_usage"]["generated_token_count"] != 0 # type: ignore


async def test_watsonx_agenerate_with_stream() -> None:
watsonxllm = WatsonxLLM(
model_id=MODEL_ID,
url="https://us-south.ml.cloud.ibm.com", # type: ignore[arg-type]
project_id=WX_PROJECT_ID,
)
response = await watsonxllm.agenerate(["What color sunflower is?"], stream=True)
assert "yellow" in response.generations[0][0].text.lower()


def test_get_num_tokens() -> None:
watsonxllm = WatsonxLLM(
model_id=MODEL_ID,
Expand Down

0 comments on commit 9b29b64

Please sign in to comment.