-
Notifications
You must be signed in to change notification settings - Fork 5
Add CrewAI Render agent example #15
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
rpdevmp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ### 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": "..." | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = "" | ||
rpdevmp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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", | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "reportMissingImports": "none", | ||
| "reportMissingModuleSource": "none", | ||
| "exclude": [ | ||
| "examples/crewai-render-agent" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| fastapi | ||
| uvicorn[standard] | ||
| crewai | ||
| python-dotenv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.