Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add crewAI cookbook #546

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cookbook/_routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"notebook": "integration_llama-index.ipynb",
"docsPath": "docs/integrations/llama-index/example-python"
},
{
"notebook": "integration_crew_ai.ipynb",
"docsPath": null
},
{
"notebook": "evaluation_with_langchain.ipynb",
"docsPath": null
Expand Down
237 changes: 237 additions & 0 deletions cookbook/integration_crew_ai.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
{
RichardKruemmel marked this conversation as resolved.
Show resolved Hide resolved
RichardKruemmel marked this conversation as resolved.
Show resolved Hide resolved
"cells": [
{
"cell_type": "markdown",
"id": "27543dcb-8057-4e1c-840f-088ff050d348",
"metadata": {},
"source": [
"---\n",
"description: Cookbook with examples of the Langfuse Integration for crewAI.\n",
"category: Integrations\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "138ab723-ffcc-4737-a975-35f2ff5c5647",
"metadata": {},
"source": [
"# Cookbook: crewAI Integration\n",
"\n",
"This is a cookbook with examples of the Langfuse Integration for [crewAI](https://docs.crewai.com/) ([GitHub](https://github.com/joaomdmoura/crewai/)).<br>\n",
"It utilizes the Langfuse [OpenAI Integration](https://langfuse.com/docs/integrations/openai/python/get-started) and the [@observe() decorator](https://langfuse.com/docs/sdk/python/decorators). The OpenAI Integration ensures that each step executed by your crew is logged as a new generation in the system. The @observe() decorator automatically and asynchronously groups these logs into a single trace.\n",
"\n",
"\n",
"_Note: crewAI is compatible with Python >=3.10, <=3.13._"
]
},
{
"cell_type": "markdown",
"id": "612c578f-6fb0-4bd6-be5b-c9d8a2cbe92b",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "95f85c1c-38cf-4219-8a0e-7da10718db8a",
"metadata": {},
"outputs": [],
"source": [
"!pip install crewai langfuse"
]
},
{
"cell_type": "markdown",
"id": "70454872-cf6f-4d61-b9ad-e469dc025dfb",
"metadata": {},
"source": [
"Initialize the Langfuse client with your API keys from the project settings in the Langfuse UI and add them to your environment."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d051611-708d-4ebf-b82a-bc825cf8fbf3",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"LANGFUSE_PUBLIC_KEY\"] = \"\"\n",
"os.environ[\"LANGFUSE_SECRET_KEY\"] = \"\"\n",
"os.environ[\"LANGFUSE_HOST\"] = \"https://cloud.langfuse.com\" # For US data region, set this to \"https://us.cloud.langfuse.com\"\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\""
]
},
{
"cell_type": "markdown",
"id": "79689da5-0e4e-466f-9a76-70e2bd1fca1c",
"metadata": {},
"source": [
"Import the Langfuse OpenAI integration and check the server connection."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f008bd03-1571-4800-84da-11726d0e9da3",
"metadata": {},
"outputs": [],
"source": [
"from langfuse.openai import auth_check\n",
"\n",
"auth_check()"
]
},
{
"cell_type": "markdown",
"id": "af27daeb-9db3-4f46-a509-21d1586652db",
"metadata": {},
"source": [
"### Create Custom Tool\n",
"\n",
"For more information on how to create custom tools for the crewAI framework, please visit the [crewAI docs](https://docs.crewai.com/how-to/Create-Custom-Tools)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab4b002a-f8ad-46cf-8ad9-efbf7c24f1fb",
"metadata": {},
"outputs": [],
"source": [
"from crewai_tools import tool\n",
"\n",
"@tool\n",
"def multiplication_tool(first_number: int, second_number: int) -> str:\n",
" \"\"\"Useful for when you need to multiply two numbers together.\"\"\"\n",
" return first_number * second_number"
]
},
{
"cell_type": "markdown",
"id": "2e39f73a-62d8-4207-8cf2-6396fc275c42",
"metadata": {},
"source": [
"### Assemble Crew\n",
"\n",
"For detailed instructions on setting up your crew, please refer to the [crewAI documentation](https://docs.crewai.com/how-to/Creating-a-Crew-and-kick-it-off/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d094b88-7d58-44c3-8679-41f90e70d8ad",
"metadata": {},
"outputs": [],
"source": [
"from crewai import Agent, Task, Crew\n",
"\n",
"writer = Agent(\n",
" role=\"Writer\",\n",
" goal=\"You make math engaging and understandable for young children through poetry\",\n",
" backstory=\"You're an expert in writing haikus but you know nothing of math.\",\n",
" tools=[multiplication_tool], \n",
" )\n",
"\n",
"task = Task(description=(\"What is {multiplication}?\"),\n",
" expected_output=(\"Compose a haiku that includes the answer.\"),\n",
" agent=writer)\n",
"\n",
"crew = Crew(\n",
" agents=[writer],\n",
" tasks=[task],\n",
" share_crew=False\n",
")"
]
},
{
"cell_type": "markdown",
"id": "00d8bf44-1398-4f28-bcda-c51d3acc08e4",
"metadata": {},
"source": [
"## Examples\n",
"\n",
"### Simple Example using OpenAI Integration and @observe() decorator\n",
"\n",
"You can use this integration in combination with the `@observe()` decorator from the Langfuse Python SDK. Thereby, you can trace non-Langchain code, combine multiple Langchain invocations in a single trace, and use the full functionality of the Langfuse Python SDK.\n",
"\n",
"Learn more about Langfuse Tracing [here](https://langfuse.com/docs/tracing)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7998dbc-726e-4f23-bf0b-9789671f2b43",
"metadata": {},
"outputs": [],
"source": [
"from langfuse.decorators import observe\n",
"\n",
"@observe()\n",
"def create_simple_haiku(input):\n",
" result = crew.kickoff(inputs={\"multiplication\": input})\n",
" return result\n",
"\n",
"create_simple_haiku(\"3 * 3\")"
]
},
{
"cell_type": "markdown",
"id": "ef43f4c8-9be1-44ea-a874-4e8676a7be33",
"metadata": {},
"source": [
"### Example with additional trace attributes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68b9f0de-ada2-4b72-a9a1-6a4472d8ead9",
"metadata": {},
"outputs": [],
"source": [
"from langfuse.decorators import langfuse_context\n",
"\n",
"@observe()\n",
"def create_custom_haiku(input):\n",
" result = crew.kickoff(inputs={\"multiplication\": input})\n",
" langfuse_context.update_current_observation(\n",
" name=\"Crew AI Trace\",\n",
" session_id=\"session_id\",\n",
" user_id=\"user_id\",\n",
" tags=[\"haiku\"],\n",
" metadata={\"env\": \"prod\"}\n",
" )\n",
" return result\n",
"\n",
"create_custom_haiku(\"5 * 3\")"
]
}
],
"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.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
122 changes: 122 additions & 0 deletions pages/guides/cookbook/integration_crew_ai.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
description: Cookbook with examples of the Langfuse Integration for crewAI.
category: Integrations
---

# Cookbook: crewAI Integration

This is a cookbook with examples of the Langfuse Integration for [crewAI](https://docs.crewai.com/) ([GitHub](https://github.com/joaomdmoura/crewai/)).<br>
It utilizes the Langfuse [OpenAI Integration](https://langfuse.com/docs/integrations/openai/python/get-started) and the [@observe() decorator](https://langfuse.com/docs/sdk/python/decorators). The OpenAI Integration ensures that each step executed by your crew is logged as a new generation in the system. The @observe() decorator automatically and asynchronously groups these logs into a single trace.


_Note: crewAI is compatible with Python >=3.10, <=3.13._

## Setup


```python
!pip install crewai langfuse
```

Initialize the Langfuse client with your API keys from the project settings in the Langfuse UI and add them to your environment.


```python
import os

os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # For US data region, set this to "https://us.cloud.langfuse.com"

os.environ["OPENAI_API_KEY"] = ""
```

Import the Langfuse OpenAI integration and check the server connection.


```python
from langfuse.openai import auth_check

auth_check()
```

### Create Custom Tool

For more information on how to create custom tools for the crewAI framework, please visit the [crewAI docs](https://docs.crewai.com/how-to/Create-Custom-Tools).


```python
from crewai_tools import tool

@tool
def multiplication_tool(first_number: int, second_number: int) -> str:
"""Useful for when you need to multiply two numbers together."""
return first_number * second_number
```

### Assemble Crew

For detailed instructions on setting up your crew, please refer to the [crewAI documentation](https://docs.crewai.com/how-to/Creating-a-Crew-and-kick-it-off/).


```python
from crewai import Agent, Task, Crew

writer = Agent(
role="Writer",
goal="You make math engaging and understandable for young children through poetry",
backstory="You're an expert in writing haikus but you know nothing of math.",
tools=[multiplication_tool],
)

task = Task(description=("What is {multiplication}?"),
expected_output=("Compose a haiku that includes the answer."),
agent=writer)

crew = Crew(
agents=[writer],
tasks=[task],
share_crew=False
)
```

## Examples

### Simple Example using OpenAI Integration and @observe() decorator

You can use this integration in combination with the `@observe()` decorator from the Langfuse Python SDK. Thereby, you can trace non-Langchain code, combine multiple Langchain invocations in a single trace, and use the full functionality of the Langfuse Python SDK.

Learn more about Langfuse Tracing [here](https://langfuse.com/docs/tracing).


```python
from langfuse.decorators import observe

@observe()
def create_simple_haiku(input):
result = crew.kickoff(inputs={"multiplication": input})
return result

create_simple_haiku("3 * 3")
```

### Example with additional trace attributes


```python
from langfuse.decorators import langfuse_context

@observe()
def create_custom_haiku(input):
result = crew.kickoff(inputs={"multiplication": input})
langfuse_context.update_current_observation(
name="Crew AI Trace",
session_id="session_id",
user_id="user_id",
tags=["haiku"],
metadata={"env": "prod"}
)
return result

create_custom_haiku("5 * 3")
```
Loading