-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #670 from LorenzoPaleari/576-exec-info-misses-nest…
…ed-graphs Added CustomOpenaiCallback to ensure exclusive access to nested data.
- Loading branch information
Showing
3 changed files
with
39 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import threading | ||
from contextlib import contextmanager | ||
from langchain_community.callbacks import get_openai_callback | ||
|
||
class CustomOpenAiCallbackManager: | ||
_lock = threading.Lock() | ||
|
||
@contextmanager | ||
def exclusive_get_openai_callback(self): | ||
if CustomOpenAiCallbackManager._lock.acquire(blocking=False): | ||
try: | ||
with get_openai_callback() as cb: | ||
yield cb | ||
finally: | ||
CustomOpenAiCallbackManager._lock.release() | ||
else: | ||
yield None |