Skip to content

Commit

Permalink
[Opik-703] Add guardrails integration docs (#1086)
Browse files Browse the repository at this point in the history
* Add documentation page to python-sdk-docs

* Update index.rst

* Add guardrails example, make the integration work properly in jupyter notebooks without asyncio

* Add markdown file for guardrails

* Update docs

* Delete the markdown file

* Update overview.md

* Update README.md

* Add guardrails-ai.md

* Update guardrails-ai.md

* Update sidebars

* Fix lint errors

* Updae guardrails hub install command

* Update sidebars

* Update guardrails-ai.md

* Fix typo in guardrails-ai.md

* Add guardrails api key to codeblock tests

* skip guardrails code block

* add nltk installation to BLEU metric documentation code block

* Fix lint errors

* Fix typo
  • Loading branch information
alexkuzmik authored Jan 21, 2025
1 parent 0619865 commit 2319663
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 66 deletions.
1 change: 1 addition & 0 deletions .github/workflows/documentation_codeblock_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
OPIK_WORKSPACE: ${{ secrets.COMET_WORKSPACE }}
OPIK_API_KEY: ${{ secrets.COMET_API_KEY }}
GUARDRAILS_API_KEY: ${{ secrets.GUARDRAILS_API_KEY }}
OPIK_SENTRY_ENABLE: False
strategy:
matrix:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ The easiest way to get started is to use one of our integrations. Opik supports:
| DSPy | Log traces for all DSPy runs | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/dspy?utm_source=opik&utm_medium=github&utm_content=dspy_link&utm_campaign=opik) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/dspy.ipynb) |
| Gemini | Log traces for all Gemini LLM calls | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/gemini?utm_source=opik&utm_medium=github&utm_content=gemini_link&utm_campaign=opik) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/gemini.ipynb) |
| Groq | Log traces for all Groq LLM calls | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/groq?utm_source=opik&utm_medium=github&utm_content=groq_link&utm_campaign=opik) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/groq.ipynb) |
| Guardrails | Log traces for all Guardrails validations | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/guardrails/?utm_source=opik&utm_medium=github&utm_content=guardrails_link&utm_campaign=opik) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/guardrails-ai.ipynb) |
| LangGraph | Log traces for all LangGraph executions | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/langgraph/?utm_source=opik&utm_medium=github&utm_content=langchain_link&utm_campaign=opik) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/langgraph.ipynb) |
| LlamaIndex | Log traces for all LlamaIndex LLM calls | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/llama_index?utm_source=opik&utm_medium=github&utm_content=llama_index_link&utm_campaign=opik) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/llama-index.ipynb) |
| Ollama | Log traces for all Ollama LLM calls | [Documentation](https://www.comet.com/docs/opik/tracing/integrations/ollama?utm_source=opik&utm_medium=github&utm_content=ollama_link&utm_campaign=opik) | [![Open Quickstart In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/ollama.ipynb) |
Expand Down
84 changes: 43 additions & 41 deletions apps/opik-documentation/documentation/docs/cookbook/crewai.ipynb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"description: Cookbook that showcases Opik's integration with the CrewAI\n",
Expand Down Expand Up @@ -31,21 +31,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": "%pip install crewai crewai-tools opik --upgrade",
"outputs": [],
"execution_count": null
"source": [
"%pip install crewai crewai-tools opik --upgrade"
]
},
{
"metadata": {},
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import opik\n",
"\n",
"opik.configure(use_local=False)"
],
"outputs": [],
"execution_count": null
]
},
{
"cell_type": "markdown",
Expand All @@ -58,38 +60,40 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-13T22:43:44.846076Z",
"start_time": "2025-01-13T22:43:42.594413Z"
}
},
"outputs": [],
"source": [
"import os\n",
"import getpass\n",
"\n",
"if \"OPENAI_API_KEY\" not in os.environ:\n",
" os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter your OpenAI API key: \")"
],
"outputs": [],
"execution_count": 2
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using CrewAI\n",
"The first step is to create our project. We will use an example from CrewAI's documentation:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-13T22:43:24.541363Z",
"start_time": "2025-01-13T22:43:21.919368Z"
}
},
"outputs": [],
"source": [
"from crewai import Agent, Crew, Task, Process\n",
"\n",
Expand All @@ -108,35 +112,33 @@
" role=\"Market Researcher\",\n",
" goal=\"Gather information on market dynamics\",\n",
" backstory=\"A diligent researcher with a keen eye for detail\",\n",
" verbose=True\n",
" verbose=True,\n",
" )\n",
"\n",
" def task_one(self) -> Task:\n",
" return Task(\n",
" name=\"Collect Data Task\",\n",
" description=\"Collect recent market data and identify trends.\",\n",
" expected_output=\"A report summarizing key trends in the market.\",\n",
" agent=self.agent_one()\n",
" agent=self.agent_one(),\n",
" )\n",
"\n",
" def task_two(self) -> Task:\n",
" return Task(\n",
" name=\"Market Research Task\",\n",
" description=\"Research factors affecting market dynamics.\",\n",
" expected_output=\"An analysis of factors influencing the market.\",\n",
" agent=self.agent_two()\n",
" agent=self.agent_two(),\n",
" )\n",
"\n",
" def crew(self) -> Crew:\n",
" return Crew(\n",
" agents=[self.agent_one(), self.agent_two()],\n",
" tasks=[self.task_one(), self.task_two()],\n",
" process=Process.sequential,\n",
" verbose=True\n",
" )\n"
],
"outputs": [],
"execution_count": 1
" verbose=True,\n",
" )"
]
},
{
"cell_type": "markdown",
Expand All @@ -145,33 +147,24 @@
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2025-01-13T22:44:15.997951Z",
"start_time": "2025-01-13T22:43:48.508770Z"
}
},
"source": [
"from opik.integrations.crewai import track_crewai\n",
"\n",
"track_crewai(project_name=\"crewai-integration-demo\")\n",
"\n",
"my_crew = YourCrewName().crew()\n",
"result = my_crew.kickoff()\n",
"\n",
"print(result)"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001B[1m\u001B[95m# Agent:\u001B[00m \u001B[1m\u001B[92mData Analyst\u001B[00m\n",
"\u001B[95m## Task:\u001B[00m \u001B[92mCollect recent market data and identify trends.\u001B[00m\n",
"\u001b[1m\u001b[95m# Agent:\u001b[00m \u001b[1m\u001b[92mData Analyst\u001b[00m\n",
"\u001b[95m## Task:\u001b[00m \u001b[92mCollect recent market data and identify trends.\u001b[00m\n",
"\n",
"\n",
"\u001B[1m\u001B[95m# Agent:\u001B[00m \u001B[1m\u001B[92mData Analyst\u001B[00m\n",
"\u001B[95m## Final Answer:\u001B[00m \u001B[92m\n",
"\u001b[1m\u001b[95m# Agent:\u001b[00m \u001b[1m\u001b[92mData Analyst\u001b[00m\n",
"\u001b[95m## Final Answer:\u001b[00m \u001b[92m\n",
"**Market Trends Report – October 2023**\n",
"\n",
"**Executive Summary:**\n",
Expand Down Expand Up @@ -202,11 +195,11 @@
"\n",
"---\n",
"\n",
"This comprehensive report outlines the prevailing market trends critical for understanding the current economic landscape.\u001B[00m\n",
"This comprehensive report outlines the prevailing market trends critical for understanding the current economic landscape.\u001b[00m\n",
"\n",
"\n",
"\u001B[1m\u001B[95m# Agent:\u001B[00m \u001B[1m\u001B[92mMarket Researcher\u001B[00m\n",
"\u001B[95m## Task:\u001B[00m \u001B[92mResearch factors affecting market dynamics.\u001B[00m\n"
"\u001b[1m\u001b[95m# Agent:\u001b[00m \u001b[1m\u001b[92mMarket Researcher\u001b[00m\n",
"\u001b[95m## Task:\u001b[00m \u001b[92mResearch factors affecting market dynamics.\u001b[00m\n"
]
},
{
Expand All @@ -222,8 +215,8 @@
"text": [
"\n",
"\n",
"\u001B[1m\u001B[95m# Agent:\u001B[00m \u001B[1m\u001B[92mMarket Researcher\u001B[00m\n",
"\u001B[95m## Final Answer:\u001B[00m \u001B[92m\n",
"\u001b[1m\u001b[95m# Agent:\u001b[00m \u001b[1m\u001b[92mMarket Researcher\u001b[00m\n",
"\u001b[95m## Final Answer:\u001b[00m \u001b[92m\n",
"The current market dynamics are influenced by a multitude of interrelated factors that span consumer preferences, technological advancements, economic conditions, and social trends. An in-depth analysis of these influencing factors reveals a comprehensive picture of how they impact market behavior across various sectors. \n",
"\n",
"**1. Economic Factors:**\n",
Expand All @@ -246,7 +239,7 @@
" - **Rise of Green Initiatives:** The emphasis on green building practices and sustainability in real estate indicates regulatory trends pushing for environmentally responsible development. Properties aligning with these initiatives are finding favor in the market, often fetching higher prices and attracting eco-aware buyers.\n",
"\n",
"**Conclusion:**\n",
"Understanding the multifaceted dynamics affecting the market is crucial for stakeholders aiming to navigate today's economic landscape effectively. The convergence of economic pressures, consumer preferences, and technological advancements creates a complex environment that requires adaptive strategies. By aligning business practices with these influencing factors, companies can better position themselves for growth, enhance their competitiveness, and respond to the evolving market landscape. The insights from the current market trends report will provide a strategic foundation for informed decisions going forward.\u001B[00m\n",
"Understanding the multifaceted dynamics affecting the market is crucial for stakeholders aiming to navigate today's economic landscape effectively. The convergence of economic pressures, consumer preferences, and technological advancements creates a complex environment that requires adaptive strategies. By aligning business practices with these influencing factors, companies can better position themselves for growth, enhance their competitiveness, and respond to the evolving market landscape. The insights from the current market trends report will provide a strategic foundation for informed decisions going forward.\u001b[00m\n",
"\n",
"\n",
"The current market dynamics are influenced by a multitude of interrelated factors that span consumer preferences, technological advancements, economic conditions, and social trends. An in-depth analysis of these influencing factors reveals a comprehensive picture of how they impact market behavior across various sectors. \n",
Expand Down Expand Up @@ -275,11 +268,20 @@
]
}
],
"execution_count": 3
"source": [
"from opik.integrations.crewai import track_crewai\n",
"\n",
"track_crewai(project_name=\"crewai-integration-demo\")\n",
"\n",
"my_crew = YourCrewName().crew()\n",
"result = my_crew.kickoff()\n",
"\n",
"print(result)"
]
},
{
"metadata": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"You can now go to the Opik app to see the trace:\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"description: Cookbook that showcases Opik's integration with the Guardrails AI Python SDK\n",
"---\n",
"\n",
"# Using Opik with Guardrails AI\n",
"\n",
"[Guardrails AI](https://github.com/guardrails-ai/guardrails) is a framework for validating the inputs and outputs \n",
"\n",
"For this guide we will use a simple example that logs guardrails validation steps as traces to Opik, providing them with the validation result tags.\n",
"\n",
"## Creating an account on Comet.com\n",
"\n",
"[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai&utm_campaign=opik) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai&utm_campaign=opik) and grab you API Key.\n",
"\n",
"> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=openai&utm_campaign=opik) for more information."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install --upgrade opik guardrails-ai"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import opik\n",
"\n",
"opik.configure(use_local=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preparing our environment\n",
"\n",
"In order to use Guardrails AI, we will configure the OpenAI API Key, if you are using any other providers you can replace this with the required API key:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import getpass\n",
"\n",
"if \"OPENAI_API_KEY\" not in os.environ:\n",
" os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter your OpenAI API key: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will also need to install the guardrails check for politeness from the Guardrails Hub"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!guardrails hub install hub://guardrails/politeness_check"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Logging validation traces\n",
"\n",
"In order to log traces to Opik, you will need to call the track the Guard object with `track_guardrails` function."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from guardrails import Guard, OnFailAction\n",
"from guardrails.hub import PolitenessCheck\n",
"\n",
"from opik.integrations.guardrails import track_guardrails\n",
"\n",
"politeness_check = PolitenessCheck(\n",
" llm_callable=\"gpt-3.5-turbo\", on_fail=OnFailAction.NOOP\n",
")\n",
"\n",
"guard: Guard = Guard().use_many(politeness_check)\n",
"guard = track_guardrails(guard, project_name=\"guardrails-integration-example\")\n",
"\n",
"guard.validate(\n",
" \"Would you be so kind to pass me a cup of tea?\",\n",
")\n",
"guard.validate(\n",
" \"Shut your mouth up and give me the tea.\",\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Every validation will now be logged to Opik as a trace"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The trace will now be viewable in the Opik platform:\n",
"\n",
"![Guardrails AI Integration](https://raw.githubusercontent.com/comet-ml/opik/main/apps/opik-documentation/documentation/static/img/cookbook/guardrails_ai_traces_cookbook.png)"
]
}
],
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 2319663

Please sign in to comment.