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

# Completions

> Use OpenAI-compatible endpoints for chat and text completions via OpenAI clients, supporting streaming and custom parameters.

Provides completions for all open source models that are `text-generation`, it also supports completions models from the closed source providers, `openai`, `anthropic`, `mistral`, `cohere`, and `google`.

To specify a provider, prefix the model with the provider, e.g. `davinci-002` should be passed in as `openai/davinci-002`

We provide access to models from `openai`, `mistral`, and `google`.

You will need to supply a header `provider-key` in order to make requests to `cohere` models.

**NOTE:** Logprobs are supported for all models!

<AccordionGroup>
  <Accordion defaultOpen="true" title="Basic usage (Open Source)">
    <CodeGroup>
      ```javascript javascript theme={null}
      import OpenAI from "openai";

      const client = new OpenAI({
        apiKey: "BYTEZ_KEY",
        baseURL: "https://api.bytez.com/models/v2/openai/v1"
      });

      const response = await client.completions.create({
        model: "openai-community/gpt2",
        prompt: "Write a short poem about AI",
        temperature: 0.7,
        max_tokens: 150
      });

      console.log(response);
      ```

      ```python python theme={null}
      from openai import OpenAI

      client = OpenAI(
          api_key="BYTEZ_KEY",
          base_url="https://api.bytez.com/models/v2/openai/v1"
      )

      response = client.completions.create(
          model="openai-community/gpt2",
          prompt="Write a short poem about AI",
          temperature=0.7,
          max_tokens=150
      )
      print(response)
      ```

      ```bash http theme={null}
      curl -X POST 'https://api.bytez.com/models/v2/openai/v1/completions' \
      -H 'Authorization: BYTEZ_KEY' \
      -H 'Content-Type: application/json' \
      --data '{
        "model": "openai-community/gpt2",
        "prompt": "Write a short poem about AI",
        "temperature": 0.7,
        "max_tokens": 150
      }'
      ```
    </CodeGroup>
  </Accordion>

  <Accordion defaultOpen="false" title="Streaming (Open Source)">
    <CodeGroup>
      ```javascript javascript theme={null}
      import OpenAI from "openai";

      const client = new OpenAI({
        apiKey: "BYTEZ_KEY",
        baseURL: "https://api.bytez.com/models/v2/openai/v1"
      });

      const stream = await client.completions.create({
        model: "openai-community/gpt2",
        prompt: "Write a short poem about AI",
        max_tokens: 150,
        temperature: 0.7,
        stream: true
      });

      let text = '';
      for await (const event of stream) {
        if (event.choices[0].finish_reason) {
          break;
        }

        const content = event.choices[0].text;
        text += content;
        console.log(content);
      }

      console.log({ text });
      ```

      ```python python theme={null}
      from openai import OpenAI

      client = OpenAI(
          api_key="BYTEZ_KEY",
          base_url="https://api.bytez.com/models/v2/openai/v1"
      )

      stream = client.completions.create(
          model="openai-community/gpt2",
          prompt="Write a short poem about AI",
          max_tokens=150,
          temperature=0.7,
          stream=True
      )

      text = ""
      for event in stream:
          if event.choices[0].finish_reason:
              break

          content = event.choices[0].text

          text += content
          print(content)

      print({"text": text})
      ```

      ```bash http theme={null}
      curl -N -X POST 'https://api.bytez.com/models/v2/openai/v1/completions' \
      -H 'Authorization: BYTEZ_KEY' \
      -H 'Content-Type: application/json' \
      --data '{
        "model": "openai-community/gpt2",
        "prompt": "Write a short poem about AI",
        "max_tokens": 150,
        "temperature": 0.7,
        "stream": true
      }'
      ```
    </CodeGroup>
  </Accordion>

  <Accordion defaultOpen="false" title="Basic usage (Closed Source)">
    <CodeGroup>
      ```javascript javascript theme={null}
      import OpenAI from "openai";

      const client = new OpenAI({
        apiKey: "BYTEZ_KEY",
        baseURL: "https://api.bytez.com/models/v2/openai/v1"
      });

      const response = await client.completions.create({
        model: "openai/davinci-002",
        prompt: "Write a short poem about AI",
        temperature: 0.7,
        max_tokens: 150
      });

      console.log(response);
      ```

      ```python python theme={null}
      from openai import OpenAI

      client = OpenAI(
          api_key="BYTEZ_KEY",
          base_url="https://api.bytez.com/models/v2/openai/v1"
      )

      response = client.completions.create(
          model="openai/davinci-002",
          prompt="Write a short poem about AI",
          temperature=0.7,
          max_tokens=150
      )
      print(response)
      ```

      ```bash http theme={null}
      curl -X POST 'https://api.bytez.com/models/v2/openai/v1/completions' \
      -H 'Authorization: BYTEZ_KEY' \
      -H 'provider-key: PROVIDER_KEY' \
      -H 'Content-Type: application/json' \
      --data '{
        "model": "openai/davinci-002",
        "prompt": "Write a short poem about AI",
        "temperature": 0.7,
        "max_tokens": 150
      }'
      ```
    </CodeGroup>
  </Accordion>

  <Accordion defaultOpen="false" title="Streaming (Closed Source)">
    <CodeGroup>
      ```javascript javascript theme={null}
      import OpenAI from "openai";

      const client = new OpenAI({
        apiKey: "BYTEZ_KEY",
        baseURL: "https://api.bytez.com/models/v2/openai/v1"
      });

      const stream = await client.completions.create({
        model: "openai/davinci-002",
        prompt: "Write a short poem about AI",
        max_tokens: 150,
        temperature: 0.7,
        stream: true
      });

      let text = '';
      for await (const event of stream) {
        if (event.choices[0].finish_reason) {
          break;
        }

        const content = event.choices[0].text;
        text += content;
        console.log(content);
      }

      console.log({ text });
      ```

      ```python python theme={null}
      from openai import OpenAI

      client = OpenAI(
          api_key="BYTEZ_KEY",
          base_url="https://api.bytez.com/models/v2/openai/v1"
      )

      stream = client.completions.create(
          model="openai/davinci-002",
          prompt="Write a short poem about AI",
          max_tokens=150,
          temperature=0.7,
          stream=True
      )

      text = ""
      for event in stream:
          if event.choices[0].finish_reason:
              break

          content = event.choices[0].text

          text += content
          print(content)

      print({"text": text})
      ```

      ```bash http theme={null}
      curl -N -X POST 'https://api.bytez.com/models/v2/openai/v1/completions' \
      -H 'Authorization: BYTEZ_KEY' \
      -H 'provider-key: PROVIDER_KEY' \
      -H 'Content-Type: application/json' \
      --data '{
        "model": "openai/davinci-002",
        "prompt": "Write a short poem about AI",
        "max_tokens": 150,
        "temperature": 0.7,
        "stream": true
      }'
      ```
    </CodeGroup>
  </Accordion>
</AccordionGroup>
