Harrison/better async (#2112)

Co-authored-by: Ammar Husain <ammo700@gmail.com>
This commit is contained in:
Harrison Chase 2023-03-28 13:28:04 -07:00 committed by GitHub
parent 6e85cbcce3
commit c58932e8fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,4 @@
import asyncio
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import List, Optional from typing import List, Optional
@ -59,7 +60,9 @@ class BaseChatModel(BaseLanguageModel, BaseModel, ABC):
self, messages: List[List[BaseMessage]], stop: Optional[List[str]] = None self, messages: List[List[BaseMessage]], stop: Optional[List[str]] = None
) -> LLMResult: ) -> LLMResult:
"""Top Level call""" """Top Level call"""
results = [await self._agenerate(m, stop=stop) for m in messages] results = await asyncio.gather(
*[self._agenerate(m, stop=stop) for m in messages]
)
llm_output = self._combine_llm_outputs([res.llm_output for res in results]) llm_output = self._combine_llm_outputs([res.llm_output for res in results])
generations = [res.generations for res in results] generations = [res.generations for res in results]
return LLMResult(generations=generations, llm_output=llm_output) return LLMResult(generations=generations, llm_output=llm_output)