> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Używanie CrewAI z CometAPI

> Użyj czterech konfiguracji transportu LLM CrewAI z CometAPI w jednym sekwencyjnym wieloagentowym Crew.

[CrewAI](https://docs.crewai.com/) to framework Python do koordynowania agentów AI i zadań. Ten przewodnik konfiguruje jedno sekwencyjne Crew do wywoływania CometAPI za pośrednictwem czterech konfiguracji transportu CrewAI: OpenAI Chat Completions, OpenAI Responses, Anthropic Messages i Gemini `generateContent`.

## Wymagania wstępne

* Python 3.10–3.13
* [`uv`](https://docs.astral.sh/uv/getting-started/installation/)
* Konto CometAPI z aktywnym kluczem API — [uzyskaj go w panelu](https://www.cometapi.com/console/token)
* Cztery identyfikatory modeli tekstowych dla transportów przedstawionych poniżej

## Skonfiguruj integrację

**1. Utwórz projekt i zainstaluj CrewAI**

Utwórz nowy projekt `uv` i zainstaluj CrewAI z dodatkami Anthropic oraz Google Gen AI:

```bash theme={null}
uv init crewai-cometapi
cd crewai-cometapi
uv add 'crewai[anthropic,google-genai]'
```

**2. Ustaw klucz API i identyfikatory modeli**

Ustaw klucz API CometAPI oraz jeden identyfikator modelu dla każdego transportu:

```bash theme={null}
read -rsp "CometAPI API key: " COMETAPI_KEY
printf '\n'
export COMETAPI_KEY

export COMETAPI_CHAT_MODEL_ID=your-model-id
export COMETAPI_RESPONSES_MODEL_ID=your-model-id
export COMETAPI_ANTHROPIC_MODEL_ID=your-model-id
export COMETAPI_GEMINI_MODEL_ID=your-model-id
```

Zastąp każdą wartość `your-model-id` identyfikatorem modelu ze [strony modeli CometAPI](https://apidoc.cometapi.com/pl/overview/models.md). Te cztery wartości mogą być różne. Przypisz każdy identyfikator modelu do odpowiadającego mu transportu.

**3. Utwórz sekwencyjne Crew**

Zapisz poniższy przykład jako `crew.py`:

```python theme={null}
import os

from crewai import Agent, Crew, LLM, Process, Task


api_key = os.environ["COMETAPI_KEY"]

chat_llm = LLM(
    model=os.environ["COMETAPI_CHAT_MODEL_ID"],
    custom_openai=True,
    api="completions",
    base_url="https://api.cometapi.com/v1",
    api_key=api_key,
    max_tokens=512,
    max_retries=0,
)

responses_llm = LLM(
    model=os.environ["COMETAPI_RESPONSES_MODEL_ID"],
    custom_openai=True,
    api="responses",
    base_url="https://api.cometapi.com/v1",
    api_key=api_key,
    max_completion_tokens=512,
    max_retries=0,
)

anthropic_llm = LLM(
    model=f"anthropic/{os.environ['COMETAPI_ANTHROPIC_MODEL_ID']}",
    base_url="https://api.cometapi.com",
    api_key=api_key,
    max_tokens=512,
    max_retries=0,
)

gemini_llm = LLM(
    model=f"gemini/{os.environ['COMETAPI_GEMINI_MODEL_ID']}",
    api_key=api_key,
    max_output_tokens=512,
    thinking_config={"thinking_budget": 0, "include_thoughts": False},
    client_params={
        "http_options": {
            "base_url": "https://api.cometapi.com",
            "api_version": "v1beta",
        }
    },
)

audience_agent = Agent(
    role="Audience researcher",
    goal="Define the audience for an API migration brief",
    backstory="You turn product goals into a precise audience statement.",
    llm=chat_llm,
    tools=[],
    allow_delegation=False,
    max_iter=1,
    max_retry_limit=0,
    verbose=False,
)

requirements_agent = Agent(
    role="Requirements planner",
    goal="Turn an audience statement into implementation requirements",
    backstory="You write concise, testable requirements for API teams.",
    llm=responses_llm,
    tools=[],
    allow_delegation=False,
    max_iter=1,
    max_retry_limit=0,
    verbose=False,
)

risk_agent = Agent(
    role="Risk reviewer",
    goal="Identify the most important migration risk",
    backstory="You review plans for practical delivery risks.",
    llm=anthropic_llm,
    tools=[],
    allow_delegation=False,
    max_iter=1,
    max_retry_limit=0,
    verbose=False,
)

editor_agent = Agent(
    role="Release brief editor",
    goal="Combine research, requirements, and risk into one brief",
    backstory="You preserve source findings while producing clear summaries.",
    llm=gemini_llm,
    tools=[],
    allow_delegation=False,
    max_iter=1,
    max_retry_limit=0,
    verbose=False,
)

audience_task = Task(
    description=(
        "Define one target audience for a team moving an existing OpenAI "
        "integration to a multi-provider API. Return only an Audience heading "
        "and one sentence of at most 20 words."
    ),
    expected_output="An Audience heading followed by one concise sentence.",
    agent=audience_agent,
)

requirements_task = Task(
    description=(
        "Using the audience statement in your context, return only a "
        "Requirements heading and exactly two numbered requirements. Keep each "
        "requirement to at most 18 words."
    ),
    expected_output="A Requirements heading followed by two numbered items.",
    agent=requirements_agent,
    context=[audience_task],
)

risk_task = Task(
    description=(
        "Using the audience and requirements in your context, return only a "
        "Risk heading, one risk sentence, and one mitigation sentence. Keep "
        "each sentence to at most 18 words."
    ),
    expected_output="A Risk heading with one risk and one mitigation.",
    agent=risk_agent,
    context=[audience_task, requirements_task],
)

brief_task = Task(
    description=(
        "Create a release brief from all prior task outputs. Return only these "
        "sections: Audience with one sentence; Requirements with two numbered "
        "items; Risk with one risk and one mitigation. Keep the exact headings "
        "Audience, Requirements, and Risk."
    ),
    expected_output=(
        "A concise release brief with Audience, Requirements, and Risk headings."
    ),
    agent=editor_agent,
    context=[audience_task, requirements_task, risk_task],
)

crew = Crew(
    agents=[
        audience_agent,
        requirements_agent,
        risk_agent,
        editor_agent,
    ],
    tasks=[audience_task, requirements_task, risk_task, brief_task],
    process=Process.sequential,
    verbose=False,
)

result = crew.kickoff()

for index, task_output in enumerate(result.tasks_output, start=1):
    print(f"\n--- Task {index} ---\n{task_output.raw}")
```

Agenci nie mają narzędzi, nie mogą delegować zadań i działają pojedynczo. Każde Zadanie po pierwszym deklaruje poprzedzające je Zadania w `context`, dzięki czemu CrewAI uwzględnia wcześniejsze wyniki w kolejnym Zadaniu.

**4. Uruchom Crew**

Uruchom przykład w tej samej sesji powłoki:

```bash theme={null}
uv run python crew.py
```

Skrypt wyświetla wyniki wszystkich czterech Zadań, w tym końcowy brief wydania.

## Mapowanie tras

| Konfiguracja CrewAI | Trasa CometAPI | Bazowy adres URL |
| - | - | - |
| `custom_openai=True`, `api="completions"` | `POST /v1/chat/completions` | `https://api.cometapi.com/v1` |
| `custom_openai=True`, `api="responses"` | `POST /v1/responses` | `https://api.cometapi.com/v1` |
| `anthropic/` prefiks modelu | `POST /v1/messages` | `https://api.cometapi.com` |
| `gemini/` prefiks modelu oraz `client_params.http_options` | `POST /v1beta/models/{model}:generateContent` | `https://api.cometapi.com` |

Dwie powyższe konfiguracje OpenAI wymagają przyrostka `/v1` w `base_url`. Konfiguracje Anthropic i Gemini dodają własną wersjonowaną trasę, dlatego ich bazowy adres URL to źródłowy adres CometAPI bez `/v1`.

## Wybierz identyfikatory modeli

Skorzystaj ze [strony modeli CometAPI](https://apidoc.cometapi.com/pl/overview/models.md) aby wybrać po jednym identyfikatorze modelu tekstowego dla każdego transportu. Przechowuj identyfikatory modeli w zmiennych środowiskowych, aby móc je zmieniać bez edytowania pliku Python. Nie dodawaj prefiksu dostawcy do zmiennej środowiskowej. Przykład dodaje `anthropic/` i `gemini/` tam, gdzie CrewAI używa tych prefiksów do wyboru dostawcy.

## Rozwiązywanie problemów

> [accordion] Importy dostawcy Anthropic lub Gemini kończą się niepowodzeniem
>
> Uruchom `uv add 'crewai[anthropic,google-genai]'` w projekcie. Bazowy pakiet CrewAI domyślnie nie instaluje obu opcjonalnych SDK dostawców.

> [accordion] Żądanie Responses zwraca błąd parametru
>
> Usuń opcjonalne parametry, które nie należą do interfejsu wybranego modelu. Przykład konfiguruje dla Responses wyłącznie limit wyjściowych Tokens.

> [accordion] Żądanie używa niewłaściwej trasy
>
> Porównaj konfigurację `LLM` z tabelą mapowania tras. W szczególności użyj `custom_openai=True` z poprawną wartością `api` i zachowaj bazowy adres URL Gemini wewnątrz `client_params.http_options`.

## Powiązane zasoby

* [Dokumentacja LLM CrewAI 1.15.12](https://docs.crewai.com/v1.15.12/en/concepts/llms)
* [Agenci CrewAI](https://docs.crewai.com/v1.15.12/en/concepts/agents)
* [Zadania CrewAI](https://docs.crewai.com/v1.15.12/en/concepts/tasks)
* [Crew CrewAI](https://docs.crewai.com/v1.15.12/en/concepts/crews)
* [CometAPI Chat Completions](https://apidoc.cometapi.com/pl/api/text/chat.md)
* [CometAPI Responses](https://apidoc.cometapi.com/pl/api/text/responses.md)
* [CometAPI Anthropic Messages](https://apidoc.cometapi.com/pl/api/text/anthropic-messages.md)
* [CometAPI Gemini generateContent](https://apidoc.cometapi.com/pl/api/text/gemini-generating-content.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.