Skip to content
Merged
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
85 changes: 85 additions & 0 deletions examples/crewai-render-agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# crewai-render-agent

Minimal example of hosting **CrewAI agents behind an HTTP API** using **FastAPI**, designed to be deployed on **Render** and consumed by **Redpanda ADP** (or any system expecting an OpenAI-compatible API).
The service exposes a `/v1/chat/completions` endpoint and returns responses generated by a CrewAI agent.

---

## Features

- CrewAI agent execution
- OpenAI-compatible `POST /v1/chat/completions`
- Works locally and on Render (free tier)
- Designed for ADP agent base URL integration

---

## Requirements

- Python 3.11 (recommended)
- pip
- OpenAI API key or Azure OpenAI credentials

---

## Local Setup

### Create virtual environment
```bash
python3.11 -m venv .venv
source .venv/bin/activate
```

### Install dependencies
```bash
pip install -r requirements.txt
```

### Create .env
```env
OPENAI_API_KEY=sk-xxxx
```

### Run Locally
```bash
uvicorn main:app --reload --host 0.0.0.0 --port 8000
```

### Health check:
```bash
curl http://localhost:8000/health
```

### Test agent:
```json
curl -s http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"hello"}]}'
```

## API
POST /v1/chat/completions


### Request:
```bash
{
"messages": [
{ "role": "user", "content": "hello" }
]
}
```

### Response:
```bash
{
"choices": [
{
"message": {
"role": "assistant",
"content": "..."
}
}
]
}
```
89 changes: 89 additions & 0 deletions examples/crewai-render-agent/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import traceback
import uuid

from crewai import Agent, Crew, Process, Task
from dotenv import load_dotenv
from fastapi import FastAPI
from pydantic import BaseModel

load_dotenv()

app = FastAPI()

@app.get("/health")
def health():
return {"ok": True}


class ChatCompletionRequest(BaseModel):
model: str | None = None
messages: list[dict] # [{"role":"user","content":"hello"}]
temperature: float | None = None
stream: bool | None = False


def run_crewai(user_text: str) -> str:
# Minimal CrewAI example: 1 agent, 1 task
agent = Agent(
role="Assistant",
goal="Reply helpfully and concisely.",
backstory="You are a helpful AI agent hosted for ADP integration.",
verbose=True,
allow_delegation=False,
)

task = Task(
description=f"User said: {user_text}\nReply to the user.",
expected_output="A helpful natural-language reply to the user.",
agent=agent,
)

crew = Crew(
agents=[agent],
tasks=[task],
process=Process.sequential,
verbose=1,
)

result = crew.kickoff()
return str(result)


@app.post("/v1/chat/completions")
async def chat_completions(req: ChatCompletionRequest):
try:
user_text = ""
for m in reversed(req.messages):
if m.get("role") == "user":
user_text = (m.get("content") or "").strip()
break

if not user_text:
return {
"error": {
"message": "No user message provided.",
"type": "invalid_request_error",
}
}

answer = run_crewai(user_text)

return {
"id": f"chatcmpl-{uuid.uuid4().hex}",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": answer},
"finish_reason": "stop",
}
],
}
except Exception:
traceback.print_exc()
return {
"error": {
"message": "An internal error occurred.",
"type": "internal_error",
}
}
7 changes: 7 additions & 0 deletions examples/crewai-render-agent/pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"reportMissingImports": "none",
"reportMissingModuleSource": "none",
"exclude": [
"examples/crewai-render-agent"
]
}
4 changes: 4 additions & 0 deletions examples/crewai-render-agent/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fastapi
uvicorn[standard]
crewai
python-dotenv
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ packages = ["src/redpanda"]
[tool.pyright]
exclude = [
"src/redpanda/runtime/v1alpha1/**",
"examples/crewai-render-agent/**",
"**/__pycache__",
"**/.*",
]
Expand Down