> This is a page from the ElevenLabs documentation. For a complete page index, fetch https://elevenlabs.io/docs/llms.txt. For the full documentation in a single file, fetch https://elevenlabs.io/docs/llms-full.txt.

# Python SDK

> **Info**
>
> Zie ook het [ElevenAgents-overzicht](/docs/nl/eleven-agents/overview)

## Installatie

Installeer het Python-pakket `elevenlabs` in je project:

```shell
pip install elevenlabs
# or
poetry add elevenlabs
```

Als je de standaardimplementatie voor audio-invoer/-uitvoer wilt gebruiken, heb je ook de extra `pyaudio` nodig:

```shell
pip install "elevenlabs[pyaudio]"
# or
poetry add "elevenlabs[pyaudio]"
```

> **Info**
>
> Voor de installatie van het pakket `pyaudio` zijn mogelijk aanvullende systeemdependencies nodig.
>
> Zie de [README van het PyAudio-pakket](https://pypi.org/project/PyAudio/) voor meer informatie.
>
> #### Linux
>
> Op Debian-gebaseerde systemen kun je de dependencies installeren met:
>
> ```shell
> sudo apt-get update
> sudo apt-get install libportaudio2 libportaudiocpp0 portaudio19-dev libasound-dev libsndfile1-dev -y
> ```
>
> #### macOS
>
> Op macOS met Homebrew kun je de dependencies installeren met:
>
> ```shell
> brew install portaudio
> ```

## Gebruik

In dit voorbeeld maken we een eenvoudig script dat een conversatie uitvoert met de ElevenLabs Agents-agent.

Importeer eerst de benodigde dependencies:

```python
import os
import signal

from elevenlabs.client import ElevenLabs
from elevenlabs.conversational_ai.conversation import Conversation
from elevenlabs.conversational_ai.default_audio_interface import DefaultAudioInterface
```

Laad vervolgens de agent-ID en API-sleutel uit omgevingsvariabelen:

```python
agent_id = os.getenv("AGENT_ID")
api_key = os.getenv("ELEVENLABS_API_KEY")
```

De API-sleutel is alleen vereist voor niet-openbare agents waarvoor authenticatie is ingeschakeld.
Je hoeft deze niet in te stellen voor openbare agents; de code werkt ook zonder.

Maak vervolgens de clientinstantie `ElevenLabs`:

```python
elevenlabs = ElevenLabs(api_key=api_key)
```

Nu initialiseren we de instantie `Conversation`:

```python
conversation = Conversation(
    # API client and agent ID.
    elevenlabs,
    agent_id,

    # Assume auth is required when API_KEY is set.
    requires_auth=bool(api_key),

    # Use the default audio interface.
    audio_interface=DefaultAudioInterface(),

    # Simple callbacks that print the conversation to the console.
    callback_agent_response=lambda response: print(f"Agent: {response}"),
    callback_agent_response_correction=lambda original, corrected: print(f"Agent: {original} -> {corrected}"),
    callback_user_transcript=lambda transcript: print(f"User: {transcript}"),

    # Uncomment if you want to see latency measurements.
    # callback_latency_measurement=lambda latency: print(f"Latency: {latency}ms"),

    # Uncomment if you want to receive audio alignment data with character-level timing.
    # callback_audio_alignment=lambda alignment: print(f"Alignment: {alignment.chars}"),
)
```

We gebruiken de `DefaultAudioInterface`, die de standaardaudio-invoer-/uitvoerapparaten van het systeem gebruikt voor de conversatie.
Je kunt ook je eigen audio-interface implementeren door `elevenlabs.conversational_ai.conversation.AudioInterface` als subklasse te gebruiken.

Nu kunnen we de conversatie starten. Optioneel raden we aan je eigen eindgebruikers-ID's door te geven om conversaties aan je gebruikers te koppelen.

```python
conversation.start_session(
    user_id=user_id # optional field
)
```

Om netjes af te sluiten wanneer de gebruiker op `Ctrl+C` drukt, kunnen we een signal handler toevoegen die `end_session()` aanroept:

```python
signal.signal(signal.SIGINT, lambda sig, frame: conversation.end_session())
```

Tot slot wachten we tot de conversatie eindigt en drukken we de conversatie-ID af (die je kunt gebruiken om de conversatiegeschiedenis te bekijken en voor debugging):

```python
conversation_id = conversation.wait_for_session_end()
print(f"Conversation ID: {conversation_id}")
```

Het enige wat nog rest, is het script uitvoeren en met de agent praten:

```shell
# For public agents:
AGENT_ID=youragentid python demo.py

# For private agents:
AGENT_ID=youragentid ELEVENLABS_API_KEY=yourapikey python demo.py
```