Skip to content

Commit

Permalink
Merge pull request langchain-ai#10 from shinxi/HAI-891
Browse files Browse the repository at this point in the history
HAI-891 The answer is regenerated once again after clicking the Stop Generation button.
  • Loading branch information
shinxi authored Nov 27, 2024
2 parents ff1e2a3 + c02f705 commit 5171c0a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@

logger = logging.getLogger(__name__)

class StreamingStatus:
aborted:bool = False

def abort(self):
self.aborted = True

def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
"""Convert a dictionary to a LangChain message.
Expand Down Expand Up @@ -484,6 +489,7 @@ def _stream(
kwargs["stream"] = True
payload = self._get_request_payload(messages, stop=stop, **kwargs)
default_chunk_class: Type[BaseMessageChunk] = AIMessageChunk
streaming_status = StreamingStatus()
with self.client.create(**payload) as response:
for chunk in response:
if not isinstance(chunk, dict):
Expand Down Expand Up @@ -527,8 +533,10 @@ def _stream(
)
if run_manager:
run_manager.on_llm_new_token(
generation_chunk.text, chunk=generation_chunk, logprobs=logprobs
generation_chunk.text, chunk=generation_chunk, logprobs=logprobs, status=streaming_status
)
if streaming_status.aborted is True:
break
yield generation_chunk

def _generate(
Expand Down

0 comments on commit 5171c0a

Please sign in to comment.