mirror of
https://github.com/MIDORIBIN/langchain-gpt4free.git
synced 2024-12-24 11:34:39 +03:00
Merge pull request #5 from Ludobico/Ludobico/AsyncStreaming
feat : AsyncStreaming
This commit is contained in:
commit
25c435aa3f
@ -1,9 +1,10 @@
|
|||||||
from typing import Any, List, Mapping, Optional, Union
|
from typing import Any, List, Mapping, Optional, Union
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
from g4f import ChatCompletion
|
from g4f import ChatCompletion
|
||||||
from g4f.models import Model
|
from g4f.models import Model
|
||||||
from g4f.Provider.base_provider import BaseProvider
|
from g4f.Provider.base_provider import BaseProvider
|
||||||
from langchain.callbacks.manager import CallbackManagerForLLMRun
|
from langchain.callbacks.manager import CallbackManagerForLLMRun, AsyncCallbackManagerForLLMRun
|
||||||
from langchain.llms.base import LLM
|
from langchain.llms.base import LLM
|
||||||
from langchain.llms.utils import enforce_stop_tokens
|
from langchain.llms.utils import enforce_stop_tokens
|
||||||
|
|
||||||
@ -42,6 +43,24 @@ class G4FLLM(LLM):
|
|||||||
if stop is not None:
|
if stop is not None:
|
||||||
text = enforce_stop_tokens(text, stop)
|
text = enforce_stop_tokens(text, stop)
|
||||||
return text
|
return text
|
||||||
|
async def _acall(self, prompt: str, stop: Optional[List[str]] = None, run_manager: Optional[AsyncCallbackManagerForLLMRun] = None, **kwargs: Any) -> str:
|
||||||
|
create_kwargs = {} if self.create_kwargs is None else self.create_kwargs.copy()
|
||||||
|
create_kwargs["model"] = self.model
|
||||||
|
if self.provider is not None:
|
||||||
|
create_kwargs["provider"] = self.provider
|
||||||
|
if self.auth is not None:
|
||||||
|
create_kwargs["auth"] = self.auth
|
||||||
|
|
||||||
|
text_callback = None
|
||||||
|
if run_manager:
|
||||||
|
text_callback = partial(run_manager.on_llm_new_token)
|
||||||
|
|
||||||
|
text = ""
|
||||||
|
for token in ChatCompletion.create(messages=[{"role": "user", "content": prompt}], stream=True, **create_kwargs):
|
||||||
|
if text_callback:
|
||||||
|
await text_callback(token)
|
||||||
|
text += token
|
||||||
|
return text
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _identifying_params(self) -> Mapping[str, Any]:
|
def _identifying_params(self) -> Mapping[str, Any]:
|
||||||
|
Loading…
Reference in New Issue
Block a user