From 0cdb186302f1e8c49eefca0abfaf470e945fcd5d Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Sat, 25 Nov 2023 15:36:56 -0500 Subject: [PATCH 01/19] openai v1 conversion --- libs/langchain/langchain/chains/moderation.py | 5 +++-- libs/langchain/langchain/chat_models/jinachat.py | 4 ++-- libs/langchain/langchain/chat_models/openai.py | 5 +++-- .../langchain/document_loaders/parsers/audio.py | 8 +++++--- libs/langchain/langchain/embeddings/localai.py | 16 ++++++++++------ libs/langchain/langchain/embeddings/openai.py | 16 ++++++++++------ libs/langchain/langchain/llms/gooseai.py | 5 +++-- libs/langchain/langchain/llms/openai.py | 16 ++++++++++------ 8 files changed, 46 insertions(+), 29 deletions(-) diff --git a/libs/langchain/langchain/chains/moderation.py b/libs/langchain/langchain/chains/moderation.py index c2935f3feb317..e0e26279293ac 100644 --- a/libs/langchain/langchain/chains/moderation.py +++ b/libs/langchain/langchain/chains/moderation.py @@ -49,9 +49,10 @@ def validate_environment(cls, values: Dict) -> Dict: try: import openai - openai.api_key = openai_api_key + if openai_organization: - openai.organization = openai_organization + # TODO: The 'openai.organization' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(organization=openai_organization)' + # openai.organization = openai_organization values["client"] = openai.Moderation except ImportError: raise ImportError( diff --git a/libs/langchain/langchain/chat_models/jinachat.py b/libs/langchain/langchain/chat_models/jinachat.py index bdb77e25b20a0..f244373f335f5 100644 --- a/libs/langchain/langchain/chat_models/jinachat.py +++ b/libs/langchain/langchain/chat_models/jinachat.py @@ -66,7 +66,7 @@ def _create_retry_decorator(llm: JinaChat) -> Callable[[Any], Any]: stop=stop_after_attempt(llm.max_retries), wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( - retry_if_exception_type(openai.error.Timeout) + retry_if_exception_type(openai.Timeout) | retry_if_exception_type(openai.error.APIError) | retry_if_exception_type(openai.error.APIConnectionError) | retry_if_exception_type(openai.error.RateLimitError) @@ -262,7 +262,7 @@ def _create_retry_decorator(self) -> Callable[[Any], Any]: stop=stop_after_attempt(self.max_retries), wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( - retry_if_exception_type(openai.error.Timeout) + retry_if_exception_type(openai.Timeout) | retry_if_exception_type(openai.error.APIError) | retry_if_exception_type(openai.error.APIConnectionError) | retry_if_exception_type(openai.error.RateLimitError) diff --git a/libs/langchain/langchain/chat_models/openai.py b/libs/langchain/langchain/chat_models/openai.py index 74c24066cb8d2..94bbe41000331 100644 --- a/libs/langchain/langchain/chat_models/openai.py +++ b/libs/langchain/langchain/chat_models/openai.py @@ -80,7 +80,7 @@ def _create_retry_decorator( import openai errors = [ - openai.error.Timeout, + openai.Timeout, openai.error.APIError, openai.error.APIConnectionError, openai.error.RateLimitError, @@ -538,7 +538,8 @@ def _client_params(self) -> Dict[str, Any]: if self.openai_proxy: import openai - openai.proxy = {"http": self.openai_proxy, "https": self.openai_proxy} # type: ignore[assignment] # noqa: E501 + # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy})' + # openai.proxy = {"http": self.openai_proxy, "https": self.openai_proxy} # type: ignore[assignment] # noqa: E501 return {**self._default_params, **openai_creds} def _get_invocation_params( diff --git a/libs/langchain/langchain/document_loaders/parsers/audio.py b/libs/langchain/langchain/document_loaders/parsers/audio.py index 073eb82ea0883..312dada958d51 100644 --- a/libs/langchain/langchain/document_loaders/parsers/audio.py +++ b/libs/langchain/langchain/document_loaders/parsers/audio.py @@ -23,7 +23,9 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]: import io try: - import openai + from openai import OpenAI + + client = OpenAI(api_key=self.api_key) except ImportError: raise ImportError( "openai package not found, please install it with " @@ -38,7 +40,7 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]: # Set the API key if provided if self.api_key: - openai.api_key = self.api_key + # Audio file from disk audio = AudioSegment.from_file(blob.path) @@ -63,7 +65,7 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]: attempts = 0 while attempts < 3: try: - transcript = openai.Audio.transcribe("whisper-1", file_obj) + transcript = client.audio.transcribe("whisper-1", file_obj) break except Exception as e: attempts += 1 diff --git a/libs/langchain/langchain/embeddings/localai.py b/libs/langchain/langchain/embeddings/localai.py index 4a35d9e83c69f..48b9d99a092d2 100644 --- a/libs/langchain/langchain/embeddings/localai.py +++ b/libs/langchain/langchain/embeddings/localai.py @@ -44,7 +44,7 @@ def _create_retry_decorator(embeddings: LocalAIEmbeddings) -> Callable[[Any], An stop=stop_after_attempt(embeddings.max_retries), wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( - retry_if_exception_type(openai.error.Timeout) + retry_if_exception_type(openai.Timeout) | retry_if_exception_type(openai.error.APIError) | retry_if_exception_type(openai.error.APIConnectionError) | retry_if_exception_type(openai.error.RateLimitError) @@ -66,7 +66,7 @@ def _async_retry_decorator(embeddings: LocalAIEmbeddings) -> Any: stop=stop_after_attempt(embeddings.max_retries), wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( - retry_if_exception_type(openai.error.Timeout) + retry_if_exception_type(openai.Timeout) | retry_if_exception_type(openai.error.APIError) | retry_if_exception_type(openai.error.APIConnectionError) | retry_if_exception_type(openai.error.RateLimitError) @@ -253,10 +253,14 @@ def _invocation_params(self) -> Dict: if self.openai_proxy: import openai - openai.proxy = { - "http": self.openai_proxy, - "https": self.openai_proxy, - } # type: ignore[assignment] # noqa: E501 + # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={ + # "http": self.openai_proxy, + # "https": self.openai_proxy, + # })' + # openai.proxy = { + # "http": self.openai_proxy, + # "https": self.openai_proxy, + # } # type: ignore[assignment] # noqa: E501 return openai_args def _embedding_func(self, text: str, *, engine: str) -> List[float]: diff --git a/libs/langchain/langchain/embeddings/openai.py b/libs/langchain/langchain/embeddings/openai.py index d10da4b4e9713..99860f142b27b 100644 --- a/libs/langchain/langchain/embeddings/openai.py +++ b/libs/langchain/langchain/embeddings/openai.py @@ -50,7 +50,7 @@ def _create_retry_decorator(embeddings: OpenAIEmbeddings) -> Callable[[Any], Any stop=stop_after_attempt(embeddings.max_retries), wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( - retry_if_exception_type(openai.error.Timeout) + retry_if_exception_type(openai.Timeout) | retry_if_exception_type(openai.error.APIError) | retry_if_exception_type(openai.error.APIConnectionError) | retry_if_exception_type(openai.error.RateLimitError) @@ -72,7 +72,7 @@ def _async_retry_decorator(embeddings: OpenAIEmbeddings) -> Any: stop=stop_after_attempt(embeddings.max_retries), wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( - retry_if_exception_type(openai.error.Timeout) + retry_if_exception_type(openai.Timeout) | retry_if_exception_type(openai.error.APIError) | retry_if_exception_type(openai.error.APIConnectionError) | retry_if_exception_type(openai.error.RateLimitError) @@ -371,10 +371,14 @@ def _invocation_params(self) -> Dict[str, Any]: "Please install it with `pip install openai`." ) - openai.proxy = { - "http": self.openai_proxy, - "https": self.openai_proxy, - } # type: ignore[assignment] # noqa: E501 + # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={ + # "http": self.openai_proxy, + # "https": self.openai_proxy, + # })' + # openai.proxy = { + # "http": self.openai_proxy, + # "https": self.openai_proxy, + # } # type: ignore[assignment] # noqa: E501 return openai_args # please refer to diff --git a/libs/langchain/langchain/llms/gooseai.py b/libs/langchain/langchain/llms/gooseai.py index db08a360a91e6..3006de43c6a3e 100644 --- a/libs/langchain/langchain/llms/gooseai.py +++ b/libs/langchain/langchain/llms/gooseai.py @@ -98,8 +98,9 @@ def validate_environment(cls, values: Dict) -> Dict: try: import openai - openai.api_key = gooseai_api_key.get_secret_value() - openai.api_base = "https://api.goose.ai/v1" + + # TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(api_base="https://api.goose.ai/v1")' + # openai.api_base = "https://api.goose.ai/v1" values["client"] = openai.Completion except ImportError: raise ImportError( diff --git a/libs/langchain/langchain/llms/openai.py b/libs/langchain/langchain/llms/openai.py index eca554f55716b..37c2061d06142 100644 --- a/libs/langchain/langchain/llms/openai.py +++ b/libs/langchain/langchain/llms/openai.py @@ -94,7 +94,7 @@ def _create_retry_decorator( import openai errors = [ - openai.error.Timeout, + openai.Timeout, openai.error.APIError, openai.error.APIConnectionError, openai.error.RateLimitError, @@ -599,7 +599,8 @@ def _invocation_params(self) -> Dict[str, Any]: if self.openai_proxy: import openai - openai.proxy = {"http": self.openai_proxy, "https": self.openai_proxy} # type: ignore[assignment] # noqa: E501 + # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy})' + # openai.proxy = {"http": self.openai_proxy, "https": self.openai_proxy} # type: ignore[assignment] # noqa: E501 return {**openai_creds, **self._default_params} @property @@ -1020,13 +1021,16 @@ def validate_environment(cls, values: Dict) -> Dict: try: import openai - openai.api_key = openai_api_key + if openai_api_base: - openai.api_base = openai_api_base + # TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(api_base=openai_api_base)' + # openai.api_base = openai_api_base if openai_organization: - openai.organization = openai_organization + # TODO: The 'openai.organization' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(organization=openai_organization)' + # openai.organization = openai_organization if openai_proxy: - openai.proxy = {"http": openai_proxy, "https": openai_proxy} # type: ignore[assignment] # noqa: E501 + # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={"http": openai_proxy, "https": openai_proxy})' + # openai.proxy = {"http": openai_proxy, "https": openai_proxy} # type: ignore[assignment] # noqa: E501 except ImportError: raise ImportError( "Could not import openai python package. " From f678c1775b3d472d26efd911a10411f4c09a3afc Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Sat, 25 Nov 2023 15:57:54 -0500 Subject: [PATCH 02/19] openai migrate test changes --- .../integration_tests/adapters/test_openai.py | 28 +++++++++++++------ .../embeddings/test_azure_openai.py | 6 ++-- .../embeddings/test_openai.py | 6 ++-- .../tests/unit_tests/llms/test_openai.py | 4 +-- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/libs/langchain/tests/integration_tests/adapters/test_openai.py b/libs/langchain/tests/integration_tests/adapters/test_openai.py index b9279e8952e7c..e58de0de69ec5 100644 --- a/libs/langchain/tests/integration_tests/adapters/test_openai.py +++ b/libs/langchain/tests/integration_tests/adapters/test_openai.py @@ -4,9 +4,12 @@ def _test_no_stream(**kwargs: Any) -> None: - import openai + from openai import OpenAI, AsyncOpenAI + + client = OpenAI() + aclient = AsyncOpenAI() - result = openai.ChatCompletion.create(**kwargs) + result = client.chat.completions.create(**kwargs) lc_result = lcopenai.ChatCompletion.create(**kwargs) if isinstance(lc_result, dict): if isinstance(result, dict): @@ -17,10 +20,13 @@ def _test_no_stream(**kwargs: Any) -> None: def _test_stream(**kwargs: Any) -> None: - import openai + from openai import OpenAI, AsyncOpenAI + + client = OpenAI() + aclient = AsyncOpenAI() result = [] - for c in openai.ChatCompletion.create(**kwargs): + for c in client.chat.completions.create(**kwargs): result.append(c["choices"][0]["delta"].to_dict_recursive()) lc_result = [] @@ -30,9 +36,12 @@ def _test_stream(**kwargs: Any) -> None: async def _test_async(**kwargs: Any) -> None: - import openai + from openai import OpenAI, AsyncOpenAI + + client = OpenAI() + aclient = AsyncOpenAI() - result = await openai.ChatCompletion.acreate(**kwargs) + result = await aclient.chat.completions.create(**kwargs) lc_result = await lcopenai.ChatCompletion.acreate(**kwargs) if isinstance(lc_result, dict): if isinstance(result, dict): @@ -43,10 +52,13 @@ async def _test_async(**kwargs: Any) -> None: async def _test_astream(**kwargs: Any) -> None: - import openai + from openai import OpenAI, AsyncOpenAI + + client = OpenAI() + aclient = AsyncOpenAI() result = [] - async for c in await openai.ChatCompletion.acreate(**kwargs): + async for c in await aclient.chat.completions.create(**kwargs): result.append(c["choices"][0]["delta"].to_dict_recursive()) lc_result = [] diff --git a/libs/langchain/tests/integration_tests/embeddings/test_azure_openai.py b/libs/langchain/tests/integration_tests/embeddings/test_azure_openai.py index 45f5b48d39ab6..9146b440e4e4d 100644 --- a/libs/langchain/tests/integration_tests/embeddings/test_azure_openai.py +++ b/libs/langchain/tests/integration_tests/embeddings/test_azure_openai.py @@ -80,14 +80,16 @@ async def test_azure_openai_embedding_async_query() -> None: @pytest.mark.skip(reason="Unblock scheduled testing. TODO: fix.") def test_azure_openai_embedding_with_empty_string() -> None: """Test openai embeddings with empty string.""" - import openai + from openai import OpenAI + + client = OpenAI() document = ["", "abc"] embedding = _get_embeddings() output = embedding.embed_documents(document) assert len(output) == 2 assert len(output[0]) == 1536 - expected_output = openai.Embedding.create(input="", model="text-embedding-ada-002")[ + expected_output = client.embeddings.create(input="", model="text-embedding-ada-002")[ "data" ][0]["embedding"] assert np.allclose(output[0], expected_output) diff --git a/libs/langchain/tests/integration_tests/embeddings/test_openai.py b/libs/langchain/tests/integration_tests/embeddings/test_openai.py index 8fab5789ccf3e..ae080baea8cb4 100644 --- a/libs/langchain/tests/integration_tests/embeddings/test_openai.py +++ b/libs/langchain/tests/integration_tests/embeddings/test_openai.py @@ -63,14 +63,16 @@ async def test_openai_embedding_async_query() -> None: @pytest.mark.scheduled def test_openai_embedding_with_empty_string() -> None: """Test openai embeddings with empty string.""" - import openai + from openai import OpenAI + + client = OpenAI() document = ["", "abc"] embedding = OpenAIEmbeddings() output = embedding.embed_documents(document) assert len(output) == 2 assert len(output[0]) == 1536 - expected_output = openai.Embedding.create(input="", model="text-embedding-ada-002")[ + expected_output = client.embeddings.create(input="", model="text-embedding-ada-002")[ "data" ][0]["embedding"] assert np.allclose(output[0], expected_output) diff --git a/libs/langchain/tests/unit_tests/llms/test_openai.py b/libs/langchain/tests/unit_tests/llms/test_openai.py index ad9279daf8bfd..631d26684aa04 100644 --- a/libs/langchain/tests/unit_tests/llms/test_openai.py +++ b/libs/langchain/tests/unit_tests/llms/test_openai.py @@ -90,7 +90,7 @@ def raise_once(*args: Any, **kwargs: Any) -> Any: nonlocal completed, raised if not raised: raised = True - raise openai.error.APIError + raise openai.APIError completed = True return mock_completion @@ -126,7 +126,7 @@ async def araise_once(*args: Any, **kwargs: Any) -> Any: nonlocal completed, raised if not raised: raised = True - raise openai.error.APIError + raise openai.APIError await asyncio.sleep(0) completed = True return mock_completion From e3b2d9d10e278dfcf95fa8fc149fed117f4a7b7c Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Tue, 28 Nov 2023 14:48:10 -0500 Subject: [PATCH 03/19] auditing audio.py --- libs/langchain/langchain/document_loaders/parsers/audio.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libs/langchain/langchain/document_loaders/parsers/audio.py b/libs/langchain/langchain/document_loaders/parsers/audio.py index 312dada958d51..8f3bf078d32bb 100644 --- a/libs/langchain/langchain/document_loaders/parsers/audio.py +++ b/libs/langchain/langchain/document_loaders/parsers/audio.py @@ -24,8 +24,6 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]: try: from openai import OpenAI - - client = OpenAI(api_key=self.api_key) except ImportError: raise ImportError( "openai package not found, please install it with " @@ -40,7 +38,7 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]: # Set the API key if provided if self.api_key: - + client = OpenAI(api_key=self.api_key) # Audio file from disk audio = AudioSegment.from_file(blob.path) @@ -65,7 +63,7 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]: attempts = 0 while attempts < 3: try: - transcript = client.audio.transcribe("whisper-1", file_obj) + transcript = client.audio.transcriptions.create("whisper-1", file_obj) break except Exception as e: attempts += 1 From 46d6800d085c9e4813d7dfaf73477061cf3e58f3 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Tue, 28 Nov 2023 15:06:24 -0500 Subject: [PATCH 04/19] auditing moderation.py --- libs/langchain/langchain/chains/moderation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/langchain/langchain/chains/moderation.py b/libs/langchain/langchain/chains/moderation.py index e0e26279293ac..c1fbf119bab32 100644 --- a/libs/langchain/langchain/chains/moderation.py +++ b/libs/langchain/langchain/chains/moderation.py @@ -47,12 +47,15 @@ def validate_environment(cls, values: Dict) -> Dict: default="", ) try: + from openai import OpenAI import openai - if openai_organization: # TODO: The 'openai.organization' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(organization=openai_organization)' # openai.organization = openai_organization + client = OpenAI(organization=openai_organization, api_key=openai_api_key) + else: + client = OpenAI(api_key=openai_api_key) values["client"] = openai.Moderation except ImportError: raise ImportError( From 0dcd8a23e8579d496d8d1e08c8b0c1dbd87f4ed9 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 11:59:17 -0500 Subject: [PATCH 05/19] remove unnecessary comment --- libs/langchain/langchain/chains/moderation.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/libs/langchain/langchain/chains/moderation.py b/libs/langchain/langchain/chains/moderation.py index c1fbf119bab32..18d82d0ebe373 100644 --- a/libs/langchain/langchain/chains/moderation.py +++ b/libs/langchain/langchain/chains/moderation.py @@ -51,8 +51,6 @@ def validate_environment(cls, values: Dict) -> Dict: import openai if openai_organization: - # TODO: The 'openai.organization' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(organization=openai_organization)' - # openai.organization = openai_organization client = OpenAI(organization=openai_organization, api_key=openai_api_key) else: client = OpenAI(api_key=openai_api_key) From 6294d24546a021797eb3485854f978a23e71794f Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:06:02 -0500 Subject: [PATCH 06/19] fixing errors in jinachat --- libs/langchain/langchain/chat_models/jinachat.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libs/langchain/langchain/chat_models/jinachat.py b/libs/langchain/langchain/chat_models/jinachat.py index f244373f335f5..9bd1096d81ebb 100644 --- a/libs/langchain/langchain/chat_models/jinachat.py +++ b/libs/langchain/langchain/chat_models/jinachat.py @@ -67,10 +67,10 @@ def _create_retry_decorator(llm: JinaChat) -> Callable[[Any], Any]: wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( retry_if_exception_type(openai.Timeout) - | retry_if_exception_type(openai.error.APIError) - | retry_if_exception_type(openai.error.APIConnectionError) - | retry_if_exception_type(openai.error.RateLimitError) - | retry_if_exception_type(openai.error.ServiceUnavailableError) + | retry_if_exception_type(openai.APIError) + | retry_if_exception_type(openai.APIConnectionError) + | retry_if_exception_type(openai.RateLimitError) + | retry_if_exception_type(openai.APIStatusError) ), before_sleep=before_sleep_log(logger, logging.WARNING), ) @@ -263,10 +263,10 @@ def _create_retry_decorator(self) -> Callable[[Any], Any]: wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( retry_if_exception_type(openai.Timeout) - | retry_if_exception_type(openai.error.APIError) - | retry_if_exception_type(openai.error.APIConnectionError) - | retry_if_exception_type(openai.error.RateLimitError) - | retry_if_exception_type(openai.error.ServiceUnavailableError) + | retry_if_exception_type(openai.APIError) + | retry_if_exception_type(openai.APIConnectionError) + | retry_if_exception_type(openai.RateLimitError) + | retry_if_exception_type(openai.APIStatusError) ), before_sleep=before_sleep_log(logger, logging.WARNING), ) From 6ea19bc9da436b0c12b33fffda4f8cdf6232e83e Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:34:01 -0500 Subject: [PATCH 07/19] fixing new openai error paths & proxy --- .../expression_language/how_to/fallbacks.ipynb | 2 +- docs/docs/guides/fallbacks.ipynb | 2 +- libs/langchain/langchain/chat_models/openai.py | 13 ++++++------- libs/langchain/langchain/embeddings/localai.py | 18 +++++++++--------- libs/langchain/langchain/embeddings/openai.py | 18 +++++++++--------- libs/langchain/langchain/llms/openai.py | 8 ++++---- 6 files changed, 30 insertions(+), 31 deletions(-) diff --git a/docs/docs/expression_language/how_to/fallbacks.ipynb b/docs/docs/expression_language/how_to/fallbacks.ipynb index f788c0746dc3e..06ea740164eb5 100644 --- a/docs/docs/expression_language/how_to/fallbacks.ipynb +++ b/docs/docs/expression_language/how_to/fallbacks.ipynb @@ -51,7 +51,7 @@ "source": [ "from unittest.mock import patch\n", "\n", - "from openai.error import RateLimitError" + "from openai import RateLimitError" ] }, { diff --git a/docs/docs/guides/fallbacks.ipynb b/docs/docs/guides/fallbacks.ipynb index d9366bbeddfd4..af2a10038390c 100644 --- a/docs/docs/guides/fallbacks.ipynb +++ b/docs/docs/guides/fallbacks.ipynb @@ -53,7 +53,7 @@ "source": [ "from unittest.mock import patch\n", "\n", - "from openai.error import RateLimitError" + "from openai import RateLimitError" ] }, { diff --git a/libs/langchain/langchain/chat_models/openai.py b/libs/langchain/langchain/chat_models/openai.py index 94bbe41000331..0bd5459507107 100644 --- a/libs/langchain/langchain/chat_models/openai.py +++ b/libs/langchain/langchain/chat_models/openai.py @@ -81,10 +81,10 @@ def _create_retry_decorator( errors = [ openai.Timeout, - openai.error.APIError, - openai.error.APIConnectionError, - openai.error.RateLimitError, - openai.error.ServiceUnavailableError, + openai.APIError, + openai.APIConnectionError, + openai.RateLimitError, + openai.APIStatusError, ] return create_base_retry_decorator( error_types=errors, max_retries=llm.max_retries, run_manager=run_manager @@ -536,10 +536,9 @@ def _client_params(self) -> Dict[str, Any]: } ) if self.openai_proxy: - import openai + from openai import OpenAI - # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy})' - # openai.proxy = {"http": self.openai_proxy, "https": self.openai_proxy} # type: ignore[assignment] # noqa: E501 + client = OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy}) return {**self._default_params, **openai_creds} def _get_invocation_params( diff --git a/libs/langchain/langchain/embeddings/localai.py b/libs/langchain/langchain/embeddings/localai.py index 48b9d99a092d2..b3b5dc702c208 100644 --- a/libs/langchain/langchain/embeddings/localai.py +++ b/libs/langchain/langchain/embeddings/localai.py @@ -45,10 +45,10 @@ def _create_retry_decorator(embeddings: LocalAIEmbeddings) -> Callable[[Any], An wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( retry_if_exception_type(openai.Timeout) - | retry_if_exception_type(openai.error.APIError) - | retry_if_exception_type(openai.error.APIConnectionError) - | retry_if_exception_type(openai.error.RateLimitError) - | retry_if_exception_type(openai.error.ServiceUnavailableError) + | retry_if_exception_type(openai.APIError) + | retry_if_exception_type(openai.APIConnectionError) + | retry_if_exception_type(openai.RateLimitError) + | retry_if_exception_type(openai.APIStatusError) ), before_sleep=before_sleep_log(logger, logging.WARNING), ) @@ -67,10 +67,10 @@ def _async_retry_decorator(embeddings: LocalAIEmbeddings) -> Any: wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( retry_if_exception_type(openai.Timeout) - | retry_if_exception_type(openai.error.APIError) - | retry_if_exception_type(openai.error.APIConnectionError) - | retry_if_exception_type(openai.error.RateLimitError) - | retry_if_exception_type(openai.error.ServiceUnavailableError) + | retry_if_exception_type(openai.APIError) + | retry_if_exception_type(openai.APIConnectionError) + | retry_if_exception_type(openai.RateLimitError) + | retry_if_exception_type(openai.APIStatusError) ), before_sleep=before_sleep_log(logger, logging.WARNING), ) @@ -91,7 +91,7 @@ def _check_response(response: dict) -> dict: if any(len(d["embedding"]) == 1 for d in response["data"]): import openai - raise openai.error.APIError("LocalAI API returned an empty embedding") + raise openai.APIError("LocalAI API returned an empty embedding") return response diff --git a/libs/langchain/langchain/embeddings/openai.py b/libs/langchain/langchain/embeddings/openai.py index 99860f142b27b..217eac5341a60 100644 --- a/libs/langchain/langchain/embeddings/openai.py +++ b/libs/langchain/langchain/embeddings/openai.py @@ -51,10 +51,10 @@ def _create_retry_decorator(embeddings: OpenAIEmbeddings) -> Callable[[Any], Any wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( retry_if_exception_type(openai.Timeout) - | retry_if_exception_type(openai.error.APIError) - | retry_if_exception_type(openai.error.APIConnectionError) - | retry_if_exception_type(openai.error.RateLimitError) - | retry_if_exception_type(openai.error.ServiceUnavailableError) + | retry_if_exception_type(openai.APIError) + | retry_if_exception_type(openai.APIConnectionError) + | retry_if_exception_type(openai.RateLimitError) + | retry_if_exception_type(openai.APIStatusError) ), before_sleep=before_sleep_log(logger, logging.WARNING), ) @@ -73,10 +73,10 @@ def _async_retry_decorator(embeddings: OpenAIEmbeddings) -> Any: wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds), retry=( retry_if_exception_type(openai.Timeout) - | retry_if_exception_type(openai.error.APIError) - | retry_if_exception_type(openai.error.APIConnectionError) - | retry_if_exception_type(openai.error.RateLimitError) - | retry_if_exception_type(openai.error.ServiceUnavailableError) + | retry_if_exception_type(openai.APIError) + | retry_if_exception_type(openai.APIConnectionError) + | retry_if_exception_type(openai.RateLimitError) + | retry_if_exception_type(openai.APIStatusError) ), before_sleep=before_sleep_log(logger, logging.WARNING), ) @@ -97,7 +97,7 @@ def _check_response(response: dict, skip_empty: bool = False) -> dict: if any(len(d["embedding"]) == 1 for d in response["data"]) and not skip_empty: import openai - raise openai.error.APIError("OpenAI API returned an empty embedding") + raise openai.APIError("OpenAI API returned an empty embedding") return response diff --git a/libs/langchain/langchain/llms/openai.py b/libs/langchain/langchain/llms/openai.py index 37c2061d06142..50db50ac3e67a 100644 --- a/libs/langchain/langchain/llms/openai.py +++ b/libs/langchain/langchain/llms/openai.py @@ -95,10 +95,10 @@ def _create_retry_decorator( errors = [ openai.Timeout, - openai.error.APIError, - openai.error.APIConnectionError, - openai.error.RateLimitError, - openai.error.ServiceUnavailableError, + openai.APIError, + openai.APIConnectionError, + openai.RateLimitError, + openai.APIStatusError, ] return create_base_retry_decorator( error_types=errors, max_retries=llm.max_retries, run_manager=run_manager From 7771079520abf3b3a2eabd75b44b9bf58d64a4b5 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:39:10 -0500 Subject: [PATCH 08/19] fixing proxy --- libs/langchain/langchain/chat_models/openai.py | 1 + libs/langchain/langchain/embeddings/localai.py | 14 +++++--------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/libs/langchain/langchain/chat_models/openai.py b/libs/langchain/langchain/chat_models/openai.py index 0bd5459507107..938804d67d6c4 100644 --- a/libs/langchain/langchain/chat_models/openai.py +++ b/libs/langchain/langchain/chat_models/openai.py @@ -539,6 +539,7 @@ def _client_params(self) -> Dict[str, Any]: from openai import OpenAI client = OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy}) + # type: ignore[assignment] # noqa: E501 return {**self._default_params, **openai_creds} def _get_invocation_params( diff --git a/libs/langchain/langchain/embeddings/localai.py b/libs/langchain/langchain/embeddings/localai.py index b3b5dc702c208..035d8803651f6 100644 --- a/libs/langchain/langchain/embeddings/localai.py +++ b/libs/langchain/langchain/embeddings/localai.py @@ -251,16 +251,12 @@ def _invocation_params(self) -> Dict: **self.model_kwargs, } if self.openai_proxy: - import openai + from openai import OpenAI - # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={ - # "http": self.openai_proxy, - # "https": self.openai_proxy, - # })' - # openai.proxy = { - # "http": self.openai_proxy, - # "https": self.openai_proxy, - # } # type: ignore[assignment] # noqa: E501 + client = OpenAI(proxy={ + "http": self.openai_proxy, + "https": self.openai_proxy, + }) return openai_args def _embedding_func(self, text: str, *, engine: str) -> List[float]: From 4a2358f1cef145e54ed919a8f5e2da699d93ab4e Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:40:15 -0500 Subject: [PATCH 09/19] adding back removed comment --- libs/langchain/langchain/embeddings/localai.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain/langchain/embeddings/localai.py b/libs/langchain/langchain/embeddings/localai.py index 035d8803651f6..11e8248dc69df 100644 --- a/libs/langchain/langchain/embeddings/localai.py +++ b/libs/langchain/langchain/embeddings/localai.py @@ -256,7 +256,7 @@ def _invocation_params(self) -> Dict: client = OpenAI(proxy={ "http": self.openai_proxy, "https": self.openai_proxy, - }) + }) # type: ignore[assignment] # noqa: E501 return openai_args def _embedding_func(self, text: str, *, engine: str) -> List[float]: From 1f13c01d6f2ecb02d0731e38610babdfd1e4458e Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:44:45 -0500 Subject: [PATCH 10/19] embeddings openai proxy fix for v1 --- libs/langchain/langchain/embeddings/openai.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/libs/langchain/langchain/embeddings/openai.py b/libs/langchain/langchain/embeddings/openai.py index 217eac5341a60..b6b70fb27492a 100644 --- a/libs/langchain/langchain/embeddings/openai.py +++ b/libs/langchain/langchain/embeddings/openai.py @@ -364,21 +364,17 @@ def _invocation_params(self) -> Dict[str, Any]: # TODO: Look into proxy with openai v1. if self.openai_proxy: try: - import openai + from openai import OpenAI except ImportError: raise ImportError( "Could not import openai python package. " "Please install it with `pip install openai`." ) - - # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={ - # "http": self.openai_proxy, - # "https": self.openai_proxy, - # })' - # openai.proxy = { - # "http": self.openai_proxy, - # "https": self.openai_proxy, - # } # type: ignore[assignment] # noqa: E501 + + client = OpenAI(proxy={ + "http": self.openai_proxy, + "https": self.openai_proxy + }) # type: ignore[assignment] # noqa: E501 return openai_args # please refer to From 6f40304579aa2d07b43ac6ae933c56f52fca349e Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:48:17 -0500 Subject: [PATCH 11/19] gooseai baseurl fix --- libs/langchain/langchain/llms/gooseai.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libs/langchain/langchain/llms/gooseai.py b/libs/langchain/langchain/llms/gooseai.py index 3006de43c6a3e..9d1f61165e64c 100644 --- a/libs/langchain/langchain/llms/gooseai.py +++ b/libs/langchain/langchain/llms/gooseai.py @@ -98,9 +98,7 @@ def validate_environment(cls, values: Dict) -> Dict: try: import openai - - # TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(api_base="https://api.goose.ai/v1")' - # openai.api_base = "https://api.goose.ai/v1" + openai.base_url = "https://api.goose.ai/v1" values["client"] = openai.Completion except ImportError: raise ImportError( From a9df0049274a0e81ea78ab955a86f86b736a64b6 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:53:47 -0500 Subject: [PATCH 12/19] test_openai fix --- .../tests/integration_tests/adapters/test_openai.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/libs/langchain/tests/integration_tests/adapters/test_openai.py b/libs/langchain/tests/integration_tests/adapters/test_openai.py index e58de0de69ec5..8fb5fc4b07fcc 100644 --- a/libs/langchain/tests/integration_tests/adapters/test_openai.py +++ b/libs/langchain/tests/integration_tests/adapters/test_openai.py @@ -4,10 +4,9 @@ def _test_no_stream(**kwargs: Any) -> None: - from openai import OpenAI, AsyncOpenAI + from openai import OpenAI client = OpenAI() - aclient = AsyncOpenAI() result = client.chat.completions.create(**kwargs) lc_result = lcopenai.ChatCompletion.create(**kwargs) @@ -20,10 +19,9 @@ def _test_no_stream(**kwargs: Any) -> None: def _test_stream(**kwargs: Any) -> None: - from openai import OpenAI, AsyncOpenAI + from openai import OpenAI client = OpenAI() - aclient = AsyncOpenAI() result = [] for c in client.chat.completions.create(**kwargs): @@ -36,9 +34,8 @@ def _test_stream(**kwargs: Any) -> None: async def _test_async(**kwargs: Any) -> None: - from openai import OpenAI, AsyncOpenAI + from openai import AsyncOpenAI - client = OpenAI() aclient = AsyncOpenAI() result = await aclient.chat.completions.create(**kwargs) @@ -52,9 +49,8 @@ async def _test_async(**kwargs: Any) -> None: async def _test_astream(**kwargs: Any) -> None: - from openai import OpenAI, AsyncOpenAI + from openai import AsyncOpenAI - client = OpenAI() aclient = AsyncOpenAI() result = [] From 0b72386dfcdf20ae81f20942c5ff36521486ee15 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:55:46 -0500 Subject: [PATCH 13/19] dalle image generation api fix --- .../langchain/utilities/dalle_image_generator.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libs/langchain/langchain/utilities/dalle_image_generator.py b/libs/langchain/langchain/utilities/dalle_image_generator.py index c81027db700c0..5a3fa443a3535 100644 --- a/libs/langchain/langchain/utilities/dalle_image_generator.py +++ b/libs/langchain/langchain/utilities/dalle_image_generator.py @@ -43,12 +43,20 @@ def validate_environment(cls, values: Dict) -> Dict: ) try: from openai import OpenAI +<<<<<<< HEAD client = OpenAI( api_key=openai_api_key, # this is also the default, it can be omitted ) values["client"] = client +======= + import openai + + client = OpenAI(api_key=openai_api_key) + + values["client"] = openai.Image +>>>>>>> dalle image generation api fix except ImportError as e: raise ImportError( "Could not import openai python package. " From 6762d6cadb684c929de3692c04462d15861921fc Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 12:58:14 -0500 Subject: [PATCH 14/19] gooseai adding back api key --- libs/langchain/langchain/llms/gooseai.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libs/langchain/langchain/llms/gooseai.py b/libs/langchain/langchain/llms/gooseai.py index 9d1f61165e64c..642aa09c86928 100644 --- a/libs/langchain/langchain/llms/gooseai.py +++ b/libs/langchain/langchain/llms/gooseai.py @@ -96,9 +96,11 @@ def validate_environment(cls, values: Dict) -> Dict: ) values["gooseai_api_key"] = gooseai_api_key try: + from openai import OpenAI import openai - openai.base_url = "https://api.goose.ai/v1" + client = OpenAI(api_base="https://api.goose.ai/v1", api_key=gooseai_api_key.get_secret_value()) + values["client"] = openai.Completion except ImportError: raise ImportError( From bd4ed4ab451fca2feb8b250e142c49a6a4b3f7cb Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 13:14:42 -0500 Subject: [PATCH 15/19] fixing openai.py in llms --- libs/langchain/langchain/llms/openai.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/libs/langchain/langchain/llms/openai.py b/libs/langchain/langchain/llms/openai.py index 50db50ac3e67a..276b2d3b38685 100644 --- a/libs/langchain/langchain/llms/openai.py +++ b/libs/langchain/langchain/llms/openai.py @@ -597,10 +597,12 @@ def _invocation_params(self) -> Dict[str, Any]: } ) if self.openai_proxy: - import openai + from openai import OpenAI - # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy})' - # openai.proxy = {"http": self.openai_proxy, "https": self.openai_proxy} # type: ignore[assignment] # noqa: E501 + client = OpenAI(proxy={ + "http": self.openai_proxy, + "https": self.openai_proxy + }) # type: ignore[assignment] # noqa: E501 return {**openai_creds, **self._default_params} @property @@ -1019,18 +1021,18 @@ def validate_environment(cls, values: Dict) -> Dict: values, "openai_organization", "OPENAI_ORGANIZATION", default="" ) try: + from openai import OpenAI import openai + params = {} if openai_api_base: - # TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(api_base=openai_api_base)' - # openai.api_base = openai_api_base + params["api_base"]=openai_api_base if openai_organization: - # TODO: The 'openai.organization' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(organization=openai_organization)' - # openai.organization = openai_organization + params["organization"]=openai_organization if openai_proxy: - # TODO: The 'openai.proxy' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(proxy={"http": openai_proxy, "https": openai_proxy})' - # openai.proxy = {"http": openai_proxy, "https": openai_proxy} # type: ignore[assignment] # noqa: E501 + params["proxy"]={"http": openai_proxy, "https": openai_proxy} + client = OpenAI(**params) except ImportError: raise ImportError( "Could not import openai python package. " From adc8a2ffafec2f8cff6b85f8ae3b92cc11223f30 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 13:38:34 -0500 Subject: [PATCH 16/19] fixing return dictionaries --- libs/langchain/langchain/chains/moderation.py | 2 +- libs/langchain/langchain/llms/gooseai.py | 3 +-- .../langchain/utilities/dalle_image_generator.py | 8 -------- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/libs/langchain/langchain/chains/moderation.py b/libs/langchain/langchain/chains/moderation.py index 18d82d0ebe373..aee636ce39d56 100644 --- a/libs/langchain/langchain/chains/moderation.py +++ b/libs/langchain/langchain/chains/moderation.py @@ -54,7 +54,7 @@ def validate_environment(cls, values: Dict) -> Dict: client = OpenAI(organization=openai_organization, api_key=openai_api_key) else: client = OpenAI(api_key=openai_api_key) - values["client"] = openai.Moderation + values["client"] = client except ImportError: raise ImportError( "Could not import openai python package. " diff --git a/libs/langchain/langchain/llms/gooseai.py b/libs/langchain/langchain/llms/gooseai.py index 642aa09c86928..db1eefb8b8967 100644 --- a/libs/langchain/langchain/llms/gooseai.py +++ b/libs/langchain/langchain/llms/gooseai.py @@ -97,11 +97,10 @@ def validate_environment(cls, values: Dict) -> Dict: values["gooseai_api_key"] = gooseai_api_key try: from openai import OpenAI - import openai client = OpenAI(api_base="https://api.goose.ai/v1", api_key=gooseai_api_key.get_secret_value()) - values["client"] = openai.Completion + values["client"] = client except ImportError: raise ImportError( "Could not import openai python package. " diff --git a/libs/langchain/langchain/utilities/dalle_image_generator.py b/libs/langchain/langchain/utilities/dalle_image_generator.py index 5a3fa443a3535..c81027db700c0 100644 --- a/libs/langchain/langchain/utilities/dalle_image_generator.py +++ b/libs/langchain/langchain/utilities/dalle_image_generator.py @@ -43,20 +43,12 @@ def validate_environment(cls, values: Dict) -> Dict: ) try: from openai import OpenAI -<<<<<<< HEAD client = OpenAI( api_key=openai_api_key, # this is also the default, it can be omitted ) values["client"] = client -======= - import openai - - client = OpenAI(api_key=openai_api_key) - - values["client"] = openai.Image ->>>>>>> dalle image generation api fix except ImportError as e: raise ImportError( "Could not import openai python package. " From 1ba7aea5e4e69cc86736d1f7f24737c7ec114999 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 13:52:38 -0500 Subject: [PATCH 17/19] make format changes --- libs/langchain/langchain/chains/moderation.py | 6 ++++-- libs/langchain/langchain/chat_models/openai.py | 4 +++- .../langchain/document_loaders/parsers/audio.py | 4 +++- libs/langchain/langchain/embeddings/localai.py | 10 ++++++---- libs/langchain/langchain/embeddings/openai.py | 9 ++++----- libs/langchain/langchain/llms/gooseai.py | 5 ++++- libs/langchain/langchain/llms/openai.py | 17 ++++++++--------- .../integration_tests/adapters/test_openai.py | 8 ++++---- .../embeddings/test_azure_openai.py | 8 ++++---- .../integration_tests/embeddings/test_openai.py | 8 ++++---- 10 files changed, 44 insertions(+), 35 deletions(-) diff --git a/libs/langchain/langchain/chains/moderation.py b/libs/langchain/langchain/chains/moderation.py index aee636ce39d56..a430cb302a030 100644 --- a/libs/langchain/langchain/chains/moderation.py +++ b/libs/langchain/langchain/chains/moderation.py @@ -47,11 +47,13 @@ def validate_environment(cls, values: Dict) -> Dict: default="", ) try: - from openai import OpenAI import openai + from openai import OpenAI if openai_organization: - client = OpenAI(organization=openai_organization, api_key=openai_api_key) + client = OpenAI( + organization=openai_organization, api_key=openai_api_key + ) else: client = OpenAI(api_key=openai_api_key) values["client"] = client diff --git a/libs/langchain/langchain/chat_models/openai.py b/libs/langchain/langchain/chat_models/openai.py index 938804d67d6c4..5d510beff0615 100644 --- a/libs/langchain/langchain/chat_models/openai.py +++ b/libs/langchain/langchain/chat_models/openai.py @@ -538,7 +538,9 @@ def _client_params(self) -> Dict[str, Any]: if self.openai_proxy: from openai import OpenAI - client = OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy}) + client = OpenAI( + proxy={"http": self.openai_proxy, "https": self.openai_proxy} + ) # type: ignore[assignment] # noqa: E501 return {**self._default_params, **openai_creds} diff --git a/libs/langchain/langchain/document_loaders/parsers/audio.py b/libs/langchain/langchain/document_loaders/parsers/audio.py index 8f3bf078d32bb..e84d779fcbaf5 100644 --- a/libs/langchain/langchain/document_loaders/parsers/audio.py +++ b/libs/langchain/langchain/document_loaders/parsers/audio.py @@ -63,7 +63,9 @@ def lazy_parse(self, blob: Blob) -> Iterator[Document]: attempts = 0 while attempts < 3: try: - transcript = client.audio.transcriptions.create("whisper-1", file_obj) + transcript = client.audio.transcriptions.create( + "whisper-1", file_obj + ) break except Exception as e: attempts += 1 diff --git a/libs/langchain/langchain/embeddings/localai.py b/libs/langchain/langchain/embeddings/localai.py index 11e8248dc69df..15332202958c9 100644 --- a/libs/langchain/langchain/embeddings/localai.py +++ b/libs/langchain/langchain/embeddings/localai.py @@ -253,10 +253,12 @@ def _invocation_params(self) -> Dict: if self.openai_proxy: from openai import OpenAI - client = OpenAI(proxy={ - "http": self.openai_proxy, - "https": self.openai_proxy, - }) # type: ignore[assignment] # noqa: E501 + client = OpenAI( + proxy={ + "http": self.openai_proxy, + "https": self.openai_proxy, + } + ) # type: ignore[assignment] # noqa: E501 return openai_args def _embedding_func(self, text: str, *, engine: str) -> List[float]: diff --git a/libs/langchain/langchain/embeddings/openai.py b/libs/langchain/langchain/embeddings/openai.py index b6b70fb27492a..7ae624253bddf 100644 --- a/libs/langchain/langchain/embeddings/openai.py +++ b/libs/langchain/langchain/embeddings/openai.py @@ -370,11 +370,10 @@ def _invocation_params(self) -> Dict[str, Any]: "Could not import openai python package. " "Please install it with `pip install openai`." ) - - client = OpenAI(proxy={ - "http": self.openai_proxy, - "https": self.openai_proxy - }) # type: ignore[assignment] # noqa: E501 + + client = OpenAI( + proxy={"http": self.openai_proxy, "https": self.openai_proxy} + ) # type: ignore[assignment] # noqa: E501 return openai_args # please refer to diff --git a/libs/langchain/langchain/llms/gooseai.py b/libs/langchain/langchain/llms/gooseai.py index db1eefb8b8967..de694d6c9e1c5 100644 --- a/libs/langchain/langchain/llms/gooseai.py +++ b/libs/langchain/langchain/llms/gooseai.py @@ -98,7 +98,10 @@ def validate_environment(cls, values: Dict) -> Dict: try: from openai import OpenAI - client = OpenAI(api_base="https://api.goose.ai/v1", api_key=gooseai_api_key.get_secret_value()) + client = OpenAI( + api_base="https://api.goose.ai/v1", + api_key=gooseai_api_key.get_secret_value(), + ) values["client"] = client except ImportError: diff --git a/libs/langchain/langchain/llms/openai.py b/libs/langchain/langchain/llms/openai.py index 276b2d3b38685..bd7c0603d3e55 100644 --- a/libs/langchain/langchain/llms/openai.py +++ b/libs/langchain/langchain/llms/openai.py @@ -599,10 +599,9 @@ def _invocation_params(self) -> Dict[str, Any]: if self.openai_proxy: from openai import OpenAI - client = OpenAI(proxy={ - "http": self.openai_proxy, - "https": self.openai_proxy - }) # type: ignore[assignment] # noqa: E501 + client = OpenAI( + proxy={"http": self.openai_proxy, "https": self.openai_proxy} + ) # type: ignore[assignment] # noqa: E501 return {**openai_creds, **self._default_params} @property @@ -1021,17 +1020,17 @@ def validate_environment(cls, values: Dict) -> Dict: values, "openai_organization", "OPENAI_ORGANIZATION", default="" ) try: - from openai import OpenAI import openai + from openai import OpenAI params = {} - + if openai_api_base: - params["api_base"]=openai_api_base + params["api_base"] = openai_api_base if openai_organization: - params["organization"]=openai_organization + params["organization"] = openai_organization if openai_proxy: - params["proxy"]={"http": openai_proxy, "https": openai_proxy} + params["proxy"] = {"http": openai_proxy, "https": openai_proxy} client = OpenAI(**params) except ImportError: raise ImportError( diff --git a/libs/langchain/tests/integration_tests/adapters/test_openai.py b/libs/langchain/tests/integration_tests/adapters/test_openai.py index 8fb5fc4b07fcc..599a44d2a1ffe 100644 --- a/libs/langchain/tests/integration_tests/adapters/test_openai.py +++ b/libs/langchain/tests/integration_tests/adapters/test_openai.py @@ -5,7 +5,7 @@ def _test_no_stream(**kwargs: Any) -> None: from openai import OpenAI - + client = OpenAI() result = client.chat.completions.create(**kwargs) @@ -20,7 +20,7 @@ def _test_no_stream(**kwargs: Any) -> None: def _test_stream(**kwargs: Any) -> None: from openai import OpenAI - + client = OpenAI() result = [] @@ -35,7 +35,7 @@ def _test_stream(**kwargs: Any) -> None: async def _test_async(**kwargs: Any) -> None: from openai import AsyncOpenAI - + aclient = AsyncOpenAI() result = await aclient.chat.completions.create(**kwargs) @@ -50,7 +50,7 @@ async def _test_async(**kwargs: Any) -> None: async def _test_astream(**kwargs: Any) -> None: from openai import AsyncOpenAI - + aclient = AsyncOpenAI() result = [] diff --git a/libs/langchain/tests/integration_tests/embeddings/test_azure_openai.py b/libs/langchain/tests/integration_tests/embeddings/test_azure_openai.py index 9146b440e4e4d..4f0321f8c6bcf 100644 --- a/libs/langchain/tests/integration_tests/embeddings/test_azure_openai.py +++ b/libs/langchain/tests/integration_tests/embeddings/test_azure_openai.py @@ -81,7 +81,7 @@ async def test_azure_openai_embedding_async_query() -> None: def test_azure_openai_embedding_with_empty_string() -> None: """Test openai embeddings with empty string.""" from openai import OpenAI - + client = OpenAI() document = ["", "abc"] @@ -89,9 +89,9 @@ def test_azure_openai_embedding_with_empty_string() -> None: output = embedding.embed_documents(document) assert len(output) == 2 assert len(output[0]) == 1536 - expected_output = client.embeddings.create(input="", model="text-embedding-ada-002")[ - "data" - ][0]["embedding"] + expected_output = client.embeddings.create( + input="", model="text-embedding-ada-002" + )["data"][0]["embedding"] assert np.allclose(output[0], expected_output) assert len(output[1]) == 1536 diff --git a/libs/langchain/tests/integration_tests/embeddings/test_openai.py b/libs/langchain/tests/integration_tests/embeddings/test_openai.py index ae080baea8cb4..01b9161cbc6ed 100644 --- a/libs/langchain/tests/integration_tests/embeddings/test_openai.py +++ b/libs/langchain/tests/integration_tests/embeddings/test_openai.py @@ -64,7 +64,7 @@ async def test_openai_embedding_async_query() -> None: def test_openai_embedding_with_empty_string() -> None: """Test openai embeddings with empty string.""" from openai import OpenAI - + client = OpenAI() document = ["", "abc"] @@ -72,9 +72,9 @@ def test_openai_embedding_with_empty_string() -> None: output = embedding.embed_documents(document) assert len(output) == 2 assert len(output[0]) == 1536 - expected_output = client.embeddings.create(input="", model="text-embedding-ada-002")[ - "data" - ][0]["embedding"] + expected_output = client.embeddings.create( + input="", model="text-embedding-ada-002" + )["data"][0]["embedding"] assert np.allclose(output[0], expected_output) assert len(output[1]) == 1536 From 5790de0540849ac14b42dcd7f9301785e1afa101 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 13:58:31 -0500 Subject: [PATCH 18/19] make lint --- libs/langchain/langchain/chains/moderation.py | 1 - libs/langchain/langchain/chat_models/openai.py | 2 +- libs/langchain/langchain/embeddings/localai.py | 2 +- libs/langchain/langchain/embeddings/openai.py | 2 +- libs/langchain/langchain/llms/openai.py | 7 ++++--- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libs/langchain/langchain/chains/moderation.py b/libs/langchain/langchain/chains/moderation.py index a430cb302a030..dd5e31d0a83a4 100644 --- a/libs/langchain/langchain/chains/moderation.py +++ b/libs/langchain/langchain/chains/moderation.py @@ -47,7 +47,6 @@ def validate_environment(cls, values: Dict) -> Dict: default="", ) try: - import openai from openai import OpenAI if openai_organization: diff --git a/libs/langchain/langchain/chat_models/openai.py b/libs/langchain/langchain/chat_models/openai.py index 5d510beff0615..a1491317bac92 100644 --- a/libs/langchain/langchain/chat_models/openai.py +++ b/libs/langchain/langchain/chat_models/openai.py @@ -538,7 +538,7 @@ def _client_params(self) -> Dict[str, Any]: if self.openai_proxy: from openai import OpenAI - client = OpenAI( + OpenAI( proxy={"http": self.openai_proxy, "https": self.openai_proxy} ) # type: ignore[assignment] # noqa: E501 diff --git a/libs/langchain/langchain/embeddings/localai.py b/libs/langchain/langchain/embeddings/localai.py index 15332202958c9..03c66f1b0f23f 100644 --- a/libs/langchain/langchain/embeddings/localai.py +++ b/libs/langchain/langchain/embeddings/localai.py @@ -253,7 +253,7 @@ def _invocation_params(self) -> Dict: if self.openai_proxy: from openai import OpenAI - client = OpenAI( + OpenAI( proxy={ "http": self.openai_proxy, "https": self.openai_proxy, diff --git a/libs/langchain/langchain/embeddings/openai.py b/libs/langchain/langchain/embeddings/openai.py index 7ae624253bddf..dc54f7044cdfb 100644 --- a/libs/langchain/langchain/embeddings/openai.py +++ b/libs/langchain/langchain/embeddings/openai.py @@ -371,7 +371,7 @@ def _invocation_params(self) -> Dict[str, Any]: "Please install it with `pip install openai`." ) - client = OpenAI( + OpenAI( proxy={"http": self.openai_proxy, "https": self.openai_proxy} ) # type: ignore[assignment] # noqa: E501 return openai_args diff --git a/libs/langchain/langchain/llms/openai.py b/libs/langchain/langchain/llms/openai.py index bd7c0603d3e55..7062e410b026e 100644 --- a/libs/langchain/langchain/llms/openai.py +++ b/libs/langchain/langchain/llms/openai.py @@ -599,7 +599,7 @@ def _invocation_params(self) -> Dict[str, Any]: if self.openai_proxy: from openai import OpenAI - client = OpenAI( + OpenAI( proxy={"http": self.openai_proxy, "https": self.openai_proxy} ) # type: ignore[assignment] # noqa: E501 return {**openai_creds, **self._default_params} @@ -1020,11 +1020,12 @@ def validate_environment(cls, values: Dict) -> Dict: values, "openai_organization", "OPENAI_ORGANIZATION", default="" ) try: - import openai from openai import OpenAI params = {} + if openai_api_key: + params["api_key"] = openai_api_key if openai_api_base: params["api_base"] = openai_api_base if openai_organization: @@ -1038,7 +1039,7 @@ def validate_environment(cls, values: Dict) -> Dict: "Please install it with `pip install openai`." ) try: - values["client"] = openai.ChatCompletion + values["client"] = client except AttributeError: raise ValueError( "`openai` has no `ChatCompletion` attribute, this is likely " From f6b11b565ed622eb5b7d05693a7b0526eff056f0 Mon Sep 17 00:00:00 2001 From: JoshuaConcon Date: Thu, 30 Nov 2023 14:02:52 -0500 Subject: [PATCH 19/19] make format changes --- libs/langchain/langchain/chat_models/openai.py | 4 +--- libs/langchain/langchain/embeddings/openai.py | 4 +--- libs/langchain/langchain/llms/openai.py | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/libs/langchain/langchain/chat_models/openai.py b/libs/langchain/langchain/chat_models/openai.py index a1491317bac92..a83b0ab592324 100644 --- a/libs/langchain/langchain/chat_models/openai.py +++ b/libs/langchain/langchain/chat_models/openai.py @@ -538,9 +538,7 @@ def _client_params(self) -> Dict[str, Any]: if self.openai_proxy: from openai import OpenAI - OpenAI( - proxy={"http": self.openai_proxy, "https": self.openai_proxy} - ) + OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy}) # type: ignore[assignment] # noqa: E501 return {**self._default_params, **openai_creds} diff --git a/libs/langchain/langchain/embeddings/openai.py b/libs/langchain/langchain/embeddings/openai.py index dc54f7044cdfb..c2440ba9980a5 100644 --- a/libs/langchain/langchain/embeddings/openai.py +++ b/libs/langchain/langchain/embeddings/openai.py @@ -371,9 +371,7 @@ def _invocation_params(self) -> Dict[str, Any]: "Please install it with `pip install openai`." ) - OpenAI( - proxy={"http": self.openai_proxy, "https": self.openai_proxy} - ) # type: ignore[assignment] # noqa: E501 + OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy}) # type: ignore[assignment] # noqa: E501 return openai_args # please refer to diff --git a/libs/langchain/langchain/llms/openai.py b/libs/langchain/langchain/llms/openai.py index 7062e410b026e..29307b78eafaa 100644 --- a/libs/langchain/langchain/llms/openai.py +++ b/libs/langchain/langchain/llms/openai.py @@ -599,9 +599,7 @@ def _invocation_params(self) -> Dict[str, Any]: if self.openai_proxy: from openai import OpenAI - OpenAI( - proxy={"http": self.openai_proxy, "https": self.openai_proxy} - ) # type: ignore[assignment] # noqa: E501 + OpenAI(proxy={"http": self.openai_proxy, "https": self.openai_proxy}) # type: ignore[assignment] # noqa: E501 return {**openai_creds, **self._default_params} @property