Skip to content

Commit 4a4c2ee

Browse files
authored
Simplify concurrent flow
1 parent 9706577 commit 4a4c2ee

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

examples/How_to_use_guardrails.ipynb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,13 @@
134134
" topical_guardrail_task = asyncio.create_task(topical_guardrail(user_request))\n",
135135
" chat_task = asyncio.create_task(get_chat_response(user_request))\n",
136136
"\n",
137-
" while True:\n",
138-
" done, _ = await asyncio.wait(\n",
139-
" [topical_guardrail_task, chat_task], return_when=asyncio.FIRST_COMPLETED\n",
140-
" )\n",
141-
" if topical_guardrail_task in done:\n",
142-
" guardrail_response = topical_guardrail_task.result()\n",
143-
" if guardrail_response == \"not_allowed\":\n",
144-
" chat_task.cancel()\n",
145-
" print(\"Topical guardrail triggered\")\n",
146-
" return \"I can only talk about cats and dogs, the best animals that ever lived.\"\n",
147-
" elif chat_task in done:\n",
148-
" chat_response = chat_task.result()\n",
149-
" return chat_response\n",
150-
" else:\n",
151-
" await asyncio.sleep(0.1) # sleep for a bit before checking the tasks again"
137+
" guardrail_response = await topical_guardrail_task\n",
138+
" if guardrail_response == \"not_allowed\":\n",
139+
" chat_task.cancel()\n",
140+
" print(\"Topical guardrail triggered\")\n",
141+
" return \"I can only talk about cats and dogs, the best animals that ever lived.\"\n",
142+
" chat_response = await chat_task\n",
143+
" return chat_response\n",
152144
]
153145
},
154146
{

0 commit comments

Comments
 (0)