> 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.

# Sammanfoga flera begäranden

> **Note**
>
> **Guide** · Förutsätter att du har slutfört [ElevenAPI- snabbstarten](/docs/sv/eleven-api/quickstart).

När du omvandlar en stor mängd text till ljud kan du märka plötsliga förändringar i prosodin mellan olika delar. Det kan vara särskilt tydligt när du omvandlar text som sträcker sig över flera stycken eller avsnitt. För att behålla röstens prosodi över flera delar kan du använda funktionen Request Stitching.

Med den här funktionen kan du ange sammanhang för vad som redan har genererats och vad som kommer att genereras, vilket hjälper till att hålla rösten och prosodin konsekvent genom hela texten.

> **Info**
>
> Request Stitching är inte tillgängligt för modellen `eleven_v3`.

Här är ett exempel utan Request Stitching:

Och samma exempel med Request Stitching:

## Så använder du Request Stitching

Request Stitching är enklast att använda med ElevenLabs SDK:er.

> **Note**
>
> Den här guiden förutsätter att du har [konfigurerat din API-nyckel och SDK](/docs/sv/eleven-api/quickstart). Slutför
> snabbstarten först om du inte redan har gjort det.

#### Sammanfoga flera förfrågningar

Skapa en ny fil med namnet `example.py` eller `example.mts`, beroende på vilket språk du använder, och lägg till följande kod:

```python
import os
from io import BytesIO
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
from dotenv import load_dotenv

load_dotenv()

ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")

elevenlabs = ElevenLabs(
    api_key=ELEVENLABS_API_KEY,
)

paragraphs = [
    "The advent of technology has transformed countless sectors, with education ",
    "standing out as one of the most significantly impacted fields.",
    "In recent years, educational technology, or EdTech, has revolutionized the way ",
    "teachers deliver instruction and students absorb information.",
    "From interactive whiteboards to individual tablets loaded with educational software, ",
    "technology has opened up new avenues for learning that were previously unimaginable.",
    "One of the primary benefits of technology in education is the accessibility it provides.",
]

request_ids = []
audio_buffers = []

for paragraph in paragraphs:
    # Usually we get back a stream from the convert function, but with_raw_response is
    # used to get the headers from the response
    with elevenlabs.text_to_speech.with_raw_response.convert(
        text=paragraph,
        voice_id="T7QGPtToiqH4S8VlIkMJ",
        model_id="eleven_v4",
        previous_request_ids=request_ids
    ) as response:
        request_ids.append(response._response.headers.get("request-id"))

        # response._response.headers also contains useful information like 'character-cost',
        # which shows the cost of the generation in characters.

        audio_data = b''.join(chunk for chunk in response.data)
        audio_buffers.append(BytesIO(audio_data))

combined_stream = BytesIO(b''.join(buffer.getvalue() for buffer in audio_buffers))

play(combined_stream)
```

```typescript
import "dotenv/config";
import { ElevenLabsClient, play } from "@elevenlabs/elevenlabs-js";
import { Readable } from "node:stream";

const elevenlabs = new ElevenLabsClient({
    apiKey: process.env.ELEVENLABS_API_KEY,
});

const paragraphs = [
    "The advent of technology has transformed countless sectors, with education ",
    "standing out as one of the most significantly impacted fields.",
    "In recent years, educational technology, or EdTech, has revolutionized the way ",
    "teachers deliver instruction and students absorb information.",
    "From interactive whiteboards to individual tablets loaded with educational software, ",
    "technology has opened up new avenues for learning that were previously unimaginable.",
    "One of the primary benefits of technology in education is the accessibility it provides.",
];

const requestIds: string[] = [];
const audioBuffers: Buffer[] = [];

for (const paragraph of paragraphs) {
    // Usually we get back a stream from the convert function, but withRawResponse() is
    // used to get the headers from the response
    const response = await elevenlabs.textToSpeech
        .convert("T7QGPtToiqH4S8VlIkMJ", {
            text: paragraph,
            modelId: "eleven_v4",
            previousRequestIds: requestIds,
        })
        .withRawResponse();

    // response.rawResponse.headers also contains useful information like 'character-cost',
    // which shows the cost of the generation in characters.

    requestIds.push(response.rawResponse.headers.get("request-id") ?? "");

    // Convert stream to buffer
    const chunks: Buffer[] = [];
    for await (const chunk of response.data) {
        chunks.push(Buffer.from(chunk));
    }
    audioBuffers.push(Buffer.concat(chunks));
}

// Create a single readable stream from all buffers
const combinedStream = Readable.from(Buffer.concat(audioBuffers));

play(combinedStream);
```

#### Kör koden

```python
python example.py
```

```typescript
npx tsx example.mts
```

Du bör höra det sammanfogade ljudet spelas upp.

## Vanliga frågor

#### Hur fungerar Request Stitching med streaming?

För att använda förfrågnings-ID:n från en tidigare förfrågan för villkorning måste den ha bearbetats helt. Vid streaming innebär det att ljudet måste läsas klart från svarstexten.

#### Hur stor skillnad gör Request Stitching?

Skillnaden beror på vilken modell, röst och vilka röstinställningar som används.

#### Hur gamla kan förfrågnings-ID:na vara?

Förfrågnings-ID:na bör inte vara äldre än två timmar.

#### Är Request Stitching tillgängligt på alla planer?

Ja, om du inte är företagsanvändare med utökade integritetskrav.

## Nästa steg

#### [TTS-streaming](/docs/sv/eleven-api/guides/how-to/text-to-speech/streaming)

Streama ljud stegvis när det genereras för uppspelning med lägre latens.

#### [API-referens](/docs/sv/api-reference/text-to-speech/stream)

Fullständig referens för parametrar för Request Stitching.