Add lock for using global config enum weak map (#11920)

<!-- Thank you for contributing to LangChain!

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes (if applicable),
  - **Dependencies:** any dependencies required for this change,
- **Tag maintainer:** for a quicker response, tag the relevant
maintainer (see below),
- **Twitter handle:** we announce bigger features on Twitter. If your PR
gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` to check this
locally.

See contribution guidelines for more information on how to write/run
tests, lint, etc:

https://github.com/langchain-ai/langchain/blob/master/.github/CONTRIBUTING.md

If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in `docs/extras`
directory.

If no one reviews your PR within a few days, please @-mention one of
@baskaryan, @eyurtsev, @hwchase17.
 -->
This commit is contained in:
Nuno Campos 2023-10-17 15:50:35 +01:00 committed by GitHub
parent 2a8ded6c8c
commit 8b79cf9566
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import enum import enum
import threading
from abc import abstractmethod from abc import abstractmethod
from typing import ( from typing import (
Any, Any,
@ -270,6 +271,8 @@ _enums_for_spec: WeakValueDictionary[
Type[StrEnum], Type[StrEnum],
] = WeakValueDictionary() ] = WeakValueDictionary()
_enums_for_spec_lock = threading.Lock()
class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]): class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]):
which: ConfigurableField which: ConfigurableField
@ -280,14 +283,18 @@ class RunnableConfigurableAlternatives(DynamicRunnable[Input, Output]):
@property @property
def config_specs(self) -> Sequence[ConfigurableFieldSpec]: def config_specs(self) -> Sequence[ConfigurableFieldSpec]:
if which_enum := _enums_for_spec.get(self.which): with _enums_for_spec_lock:
pass if which_enum := _enums_for_spec.get(self.which):
else: pass
which_enum = StrEnum( # type: ignore[call-overload] else:
self.which.name or self.which.id, which_enum = StrEnum( # type: ignore[call-overload]
((v, v) for v in list(self.alternatives.keys()) + [self.default_key]), self.which.name or self.which.id,
) (
_enums_for_spec[self.which] = cast(Type[StrEnum], which_enum) (v, v)
for v in list(self.alternatives.keys()) + [self.default_key]
),
)
_enums_for_spec[self.which] = cast(Type[StrEnum], which_enum)
return [ return [
ConfigurableFieldSpec( ConfigurableFieldSpec(
id=self.which.id, id=self.which.id,
@ -325,14 +332,15 @@ def make_options_spec(
spec: Union[ConfigurableFieldSingleOption, ConfigurableFieldMultiOption], spec: Union[ConfigurableFieldSingleOption, ConfigurableFieldMultiOption],
description: Optional[str], description: Optional[str],
) -> ConfigurableFieldSpec: ) -> ConfigurableFieldSpec:
if enum := _enums_for_spec.get(spec): with _enums_for_spec_lock:
pass if enum := _enums_for_spec.get(spec):
else: pass
enum = StrEnum( # type: ignore[call-overload] else:
spec.name or spec.id, enum = StrEnum( # type: ignore[call-overload]
((v, v) for v in list(spec.options.keys())), spec.name or spec.id,
) ((v, v) for v in list(spec.options.keys())),
_enums_for_spec[spec] = cast(Type[StrEnum], enum) )
_enums_for_spec[spec] = cast(Type[StrEnum], enum)
if isinstance(spec, ConfigurableFieldSingleOption): if isinstance(spec, ConfigurableFieldSingleOption):
return ConfigurableFieldSpec( return ConfigurableFieldSpec(
id=spec.id, id=spec.id,