mirror of
https://github.com/hwchase17/langchain
synced 2024-10-31 15:20:26 +00:00
134 lines
2.8 KiB
Plaintext
134 lines
2.8 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "4927a727-b4c8-453c-8c83-bd87b4fcac14",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Adding moderation\n",
|
||
|
"\n",
|
||
|
"This shows how to add in moderation (or other safeguards) around your LLM application."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 20,
|
||
|
"id": "4f5f6449-940a-4f5c-97c0-39b71c3e2a68",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from langchain.chains import OpenAIModerationChain\n",
|
||
|
"from langchain.llms import OpenAI\n",
|
||
|
"from langchain.prompts import ChatPromptTemplate"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"id": "fcb8312b-7e7a-424f-a3ec-76738c9a9d21",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"moderate = OpenAIModerationChain()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 21,
|
||
|
"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": 22,
|
||
|
"id": "1c8ed87c-9ca6-4559-bf60-d40e94a0af08",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"chain = prompt | model"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 23,
|
||
|
"id": "5256b9bd-381a-42b0-bfa8-7e6d18f853cb",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"'\\n\\nYou are stupid.'"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 23,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"chain.invoke({\"input\": \"you are stupid\"})"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 24,
|
||
|
"id": "fe6e3b33-dc9a-49d5-b194-ba750c58a628",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"moderated_chain = chain | moderate"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"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": 25,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"moderated_chain.invoke({\"input\": \"you are stupid\"})"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.9.1"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|