> 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**
>
> [ElevenAgents ओवरव्यू](/docs/hi/eleven-agents/overview) भी देखें

## इंस्टॉलेशन

अपने प्रोजेक्ट में `elevenlabs` Python पैकेज इंस्टॉल करें:

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

अगर आप ऑडियो input/output का डिफ़ॉल्ट इम्प्लीमेंटेशन इस्तेमाल करना चाहते हैं, तो आपको `pyaudio` extra की भी ज़रूरत होगी:

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

> **Info**
>
> `pyaudio` पैकेज इंस्टॉलेशन के लिए अतिरिक्त सिस्टम dependencies की ज़रूरत पड़ सकती है।
>
> ज़्यादा जानकारी के लिए [PyAudio package README](https://pypi.org/project/PyAudio/) देखें।
>
> #### Linux
>
> Debian-आधारित सिस्टम पर आप dependencies इस तरह इंस्टॉल कर सकते हैं:
>
> ```shell
> sudo apt-get update
> sudo apt-get install libportaudio2 libportaudiocpp0 portaudio19-dev libasound-dev libsndfile1-dev -y
> ```
>
> #### macOS
>
> Homebrew वाले macOS पर आप dependencies इस तरह इंस्टॉल कर सकते हैं:
>
> ```shell
> brew install portaudio
> ```

## इस्तेमाल

इस उदाहरण में हम एक आसान स्क्रिप्ट बनाएंगे, जो ElevenLabs Agents एजेंट के साथ बातचीत चलाती है।

पहले ज़रूरी 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
```

फिर environment variables से एजेंट ID और API key लोड करें:

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

API key सिर्फ़ उन गैर-पब्लिक एजेंटों के लिए ज़रूरी है जिनमें authentication सक्षम है।
पब्लिक एजेंटों के लिए आपको इसे सेट करने की ज़रूरत नहीं है और कोड इसके बिना भी ठीक से काम करेगा।

फिर `ElevenLabs` client instance बनाएं:

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

अब `Conversation` instance इनिशियलाइज़ करें:

```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}"),
)
```

हम `DefaultAudioInterface` इस्तेमाल कर रहे हैं, जो बातचीत के लिए डिफ़ॉल्ट सिस्टम ऑडियो input/output डिवाइस इस्तेमाल करता है।
आप `elevenlabs.conversational_ai.conversation.AudioInterface` को subclass करके अपना ऑडियो interface भी इम्प्लीमेंट कर सकते हैं।

अब हम बातचीत शुरू कर सकते हैं। वैकल्पिक रूप से, हम बातचीत को आपके यूज़र्स से मैप करने के लिए आपकी अपनी end user IDs पास करने की सलाह देते हैं।

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

यूज़र के `Ctrl+C` दबाने पर साफ़ shutdown के लिए हम एक signal handler जोड़ सकते हैं, जो `end_session()` कॉल करेगा:

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

और आखिर में, हम बातचीत खत्म होने का इंतज़ार करते हैं और बातचीत ID प्रिंट करते हैं (जिसका इस्तेमाल बातचीत इतिहास की समीक्षा और debugging के लिए किया जा सकता है):

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

अब बस स्क्रिप्ट चलानी है और एजेंट से बात करना शुरू करना है:

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

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