Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: CTransformers: Add _astream implementation #15640

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion libs/community/langchain_community/llms/ctransformers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from functools import partial
from typing import Any, Dict, List, Optional, Sequence
from typing import Any, AsyncIterator, Dict, List, Optional, Sequence

from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
)
from langchain_core.language_models.llms import LLM
from langchain_core.outputs import GenerationChunk
from langchain_core.pydantic_v1 import root_validator


Expand Down Expand Up @@ -138,3 +139,34 @@ async def _acall(
text += token

return text

async def _astream(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> AsyncIterator[GenerationChunk]:
"""Internal method called for "streaming" response from LLM using `astream`

Args:
prompt: The prompt to pass into the model.
stop: A list of strings to stop generation when encountered.

Returns:
The asynchronous iterator of type generation chunk.

Example:
.. code-block:: python
async for text in llm.astream("Once upon a time, ")
print(f"text: {text}")
"""
text_callback = None
if run_manager:
text_callback = partial(run_manager.on_llm_new_token, verbose=self.verbose)

for token in self.client(prompt, stop=stop, stream=True):
token_chunk = GenerationChunk(text=token)
if text_callback:
await text_callback(token)
yield token_chunk
Loading