diff --git a/docs/extras/guides/expression_language/cookbook.ipynb b/docs/extras/guides/expression_language/cookbook.ipynb index a189926c3a..e4391397b3 100644 --- a/docs/extras/guides/expression_language/cookbook.ipynb +++ b/docs/extras/guides/expression_language/cookbook.ipynb @@ -171,9 +171,7 @@ "cell_type": "code", "execution_count": 9, "id": "decf7710", - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [ { "data": { @@ -424,9 +422,7 @@ "cell_type": "code", "execution_count": 18, "id": "f3040b0c", - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [ { "name": "stderr", @@ -477,9 +473,7 @@ "cell_type": "code", "execution_count": 20, "id": "7ee8b2d4", - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [ { "name": "stderr", @@ -1538,10 +1532,117 @@ "response" ] }, + { + "cell_type": "markdown", + "id": "4927a727-b4c8-453c-8c83-bd87b4fcac14", + "metadata": {}, + "source": [ + "## Moderation\n", + "\n", + "This shows how to add in moderation (or other safeguards) around your LLM application." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "4f5f6449-940a-4f5c-97c0-39b71c3e2a68", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.chains import OpenAIModerationChain\n", + "from langchain.llms import OpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "fcb8312b-7e7a-424f-a3ec-76738c9a9d21", + "metadata": {}, + "outputs": [], + "source": [ + "moderate = OpenAIModerationChain()" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "b24b9148-f6b0-4091-8ea8-d3fb281bd950", + "metadata": {}, + "outputs": [], + "source": [ + "model = OpenAI()\n", + "prompt = ChatPromptTemplate.from_messages([\n", + " (\"system\", \"repeat after me: {input}\")\n", + "])" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "1c8ed87c-9ca6-4559-bf60-d40e94a0af08", + "metadata": {}, + "outputs": [], + "source": [ + "chain = prompt | model" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "5256b9bd-381a-42b0-bfa8-7e6d18f853cb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'\\n\\nYou are stupid.'" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.invoke({\"input\": \"you are stupid\"})" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "fe6e3b33-dc9a-49d5-b194-ba750c58a628", + "metadata": {}, + "outputs": [], + "source": [ + "moderated_chain = chain | moderate" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "d8ba0cbd-c739-4d23-be9f-6ae092bd5ffb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'input': '\\n\\nYou are stupid.',\n", + " 'output': \"Text was found that violates OpenAI's content policy.\"}" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "moderated_chain.invoke({\"input\": \"you are stupid\"})" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "179d3c03", + "id": "a0a85ba4-f782-47b8-b16f-8b7a61d6dab7", "metadata": {}, "outputs": [], "source": [