Skip to content

The agent engineering platform.

Harrison/better async (#2112)

Harrison Chasec58932e8fddf
diff --git a/langchain/chat_models/base.py b/langchain/chat_models/base.py
index 1bf9d4fab6..a9a0a6d673 100644
--- a/langchain/chat_models/base.py
+++ b/langchain/chat_models/base.py
@@ -1,3 +1,4 @@
+import asyncio
from abc import ABC, abstractmethod
from typing import List, Optional
@@ -59,7 +60,9 @@ class BaseChatModel(BaseLanguageModel, BaseModel, ABC):
self, messages: List[List[BaseMessage]], stop: Optional[List[str]] = None
) -> LLMResult:
"""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])
generations = [res.generations for res in results]
return LLMResult(generations=generations, llm_output=llm_output)