From 1ea48a31da4132de1a4c14462030e4e4e9bf4146 Mon Sep 17 00:00:00 2001 From: Bob Lin Date: Sun, 3 Dec 2023 10:56:07 -0600 Subject: [PATCH] Update fallback cases (#14164) ### Description The `RateLimitError` initialization method has changed after openai v1, and the usage of `patch` needs to be changed. ### Twitter handle [lin_bob57617](https://twitter.com/lin_bob57617) --- .../how_to/fallbacks.ipynb | 23 +++++++++++-------- docs/docs/guides/fallbacks.ipynb | 23 +++++++++++-------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/docs/docs/expression_language/how_to/fallbacks.ipynb b/docs/docs/expression_language/how_to/fallbacks.ipynb index f788c0746d..c5c241b7bf 100644 --- a/docs/docs/expression_language/how_to/fallbacks.ipynb +++ b/docs/docs/expression_language/how_to/fallbacks.ipynb @@ -26,7 +26,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "d3e893bf", "metadata": {}, "outputs": [], @@ -44,19 +44,24 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "dfdd8bf5", "metadata": {}, "outputs": [], "source": [ "from unittest.mock import patch\n", "\n", - "from openai.error import RateLimitError" + "import httpx\n", + "from openai import RateLimitError\n", + "\n", + "request = httpx.Request(\"GET\", \"/\")\n", + "response = httpx.Response(200, request=request)\n", + "error = RateLimitError(\"rate limit\", response=response, body=\"\")" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "id": "e6fdffc1", "metadata": {}, "outputs": [], @@ -69,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 4, "id": "584461ab", "metadata": {}, "outputs": [ @@ -83,7 +88,7 @@ ], "source": [ "# Let's use just the OpenAI LLm first, to show that we run into an error\n", - "with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError()):\n", + "with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n", " try:\n", " print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n", " except:\n", @@ -106,7 +111,7 @@ ], "source": [ "# Now let's try with fallbacks to Anthropic\n", - "with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError()):\n", + "with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n", " try:\n", " print(llm.invoke(\"Why did the chicken cross the road?\"))\n", " except:\n", @@ -148,7 +153,7 @@ " ]\n", ")\n", "chain = prompt | llm\n", - "with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError()):\n", + "with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n", " try:\n", " print(chain.invoke({\"animal\": \"kangaroo\"}))\n", " except:\n", @@ -286,7 +291,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.1" + "version": "3.11.5" } }, "nbformat": 4, diff --git a/docs/docs/guides/fallbacks.ipynb b/docs/docs/guides/fallbacks.ipynb index d9366bbedd..93666dd7ab 100644 --- a/docs/docs/guides/fallbacks.ipynb +++ b/docs/docs/guides/fallbacks.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 1, "id": "d3e893bf", "metadata": {}, "outputs": [], @@ -46,19 +46,24 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 2, "id": "dfdd8bf5", "metadata": {}, "outputs": [], "source": [ "from unittest.mock import patch\n", "\n", - "from openai.error import RateLimitError" + "import httpx\n", + "from openai import RateLimitError\n", + "\n", + "request = httpx.Request(\"GET\", \"/\")\n", + "response = httpx.Response(200, request=request)\n", + "error = RateLimitError(\"rate limit\", response=response, body=\"\")" ] }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 3, "id": "e6fdffc1", "metadata": {}, "outputs": [], @@ -71,7 +76,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 4, "id": "584461ab", "metadata": {}, "outputs": [ @@ -85,7 +90,7 @@ ], "source": [ "# Let's use just the OpenAI LLm first, to show that we run into an error\n", - "with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError()):\n", + "with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n", " try:\n", " print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n", " except:\n", @@ -108,7 +113,7 @@ ], "source": [ "# Now let's try with fallbacks to Anthropic\n", - "with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError()):\n", + "with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n", " try:\n", " print(llm.invoke(\"Why did the chicken cross the road?\"))\n", " except:\n", @@ -150,7 +155,7 @@ " ]\n", ")\n", "chain = prompt | llm\n", - "with patch(\"openai.ChatCompletion.create\", side_effect=RateLimitError()):\n", + "with patch(\"openai.resources.chat.completions.Completions.create\", side_effect=error):\n", " try:\n", " print(chain.invoke({\"animal\": \"kangaroo\"}))\n", " except:\n", @@ -431,7 +436,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.11.5" } }, "nbformat": 4,