mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-30 18:33:40 +00:00
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)
This commit is contained in:
parent
62505043be
commit
1ea48a31da
@ -26,7 +26,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 1,
|
||||||
"id": "d3e893bf",
|
"id": "d3e893bf",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@ -44,19 +44,24 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 2,
|
||||||
"id": "dfdd8bf5",
|
"id": "dfdd8bf5",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from unittest.mock import patch\n",
|
"from unittest.mock import patch\n",
|
||||||
"\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",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 3,
|
||||||
"id": "e6fdffc1",
|
"id": "e6fdffc1",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@ -69,7 +74,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 27,
|
"execution_count": 4,
|
||||||
"id": "584461ab",
|
"id": "584461ab",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@ -83,7 +88,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Let's use just the OpenAI LLm first, to show that we run into an error\n",
|
"# 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",
|
" try:\n",
|
||||||
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
|
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
|
||||||
" except:\n",
|
" except:\n",
|
||||||
@ -106,7 +111,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Now let's try with fallbacks to Anthropic\n",
|
"# 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",
|
" try:\n",
|
||||||
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
|
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
|
||||||
" except:\n",
|
" except:\n",
|
||||||
@ -148,7 +153,7 @@
|
|||||||
" ]\n",
|
" ]\n",
|
||||||
")\n",
|
")\n",
|
||||||
"chain = prompt | llm\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",
|
" try:\n",
|
||||||
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
|
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
|
||||||
" except:\n",
|
" except:\n",
|
||||||
@ -286,7 +291,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.1"
|
"version": "3.11.5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 18,
|
"execution_count": 1,
|
||||||
"id": "d3e893bf",
|
"id": "d3e893bf",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@ -46,19 +46,24 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 21,
|
"execution_count": 2,
|
||||||
"id": "dfdd8bf5",
|
"id": "dfdd8bf5",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from unittest.mock import patch\n",
|
"from unittest.mock import patch\n",
|
||||||
"\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",
|
"cell_type": "code",
|
||||||
"execution_count": 24,
|
"execution_count": 3,
|
||||||
"id": "e6fdffc1",
|
"id": "e6fdffc1",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@ -71,7 +76,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 27,
|
"execution_count": 4,
|
||||||
"id": "584461ab",
|
"id": "584461ab",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@ -85,7 +90,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Let's use just the OpenAI LLm first, to show that we run into an error\n",
|
"# 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",
|
" try:\n",
|
||||||
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
|
" print(openai_llm.invoke(\"Why did the chicken cross the road?\"))\n",
|
||||||
" except:\n",
|
" except:\n",
|
||||||
@ -108,7 +113,7 @@
|
|||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# Now let's try with fallbacks to Anthropic\n",
|
"# 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",
|
" try:\n",
|
||||||
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
|
" print(llm.invoke(\"Why did the chicken cross the road?\"))\n",
|
||||||
" except:\n",
|
" except:\n",
|
||||||
@ -150,7 +155,7 @@
|
|||||||
" ]\n",
|
" ]\n",
|
||||||
")\n",
|
")\n",
|
||||||
"chain = prompt | llm\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",
|
" try:\n",
|
||||||
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
|
" print(chain.invoke({\"animal\": \"kangaroo\"}))\n",
|
||||||
" except:\n",
|
" except:\n",
|
||||||
@ -431,7 +436,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.10.12"
|
"version": "3.11.5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
Loading…
Reference in New Issue
Block a user