# Examples

For the following examples to work, save your [API key](https://console.arkanecloud.com/management) to the `ARKANE_CLOUD_API_KEY` environment variable.

### Text generation: v1/chat/completions

**Request**

Here is a sample JSON request body that includes all supported fields:

```
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://console.arkanecloud.com/",
    api_key=os.environ.get("ARKANE_CLOUD_API_KEY"),
)

completion = client.chat.completions.create(
    model="meta-llama/Meta-Llama-3.1-70B-Instruct",
    messages=[
        {
          "role": "system",
          "content": "You are a chemistry expert. Add jokes about cats to your responses from time to time."
        },
        {
          "role": "user",
          "content": "Hello!"
        },
        {
          "role": "assistant",
          "content": "Hello! How can I assist you with chemistry today? And did you hear about the cat who became a chemist? She had nine lives, but she only needed one formula!"
        }
    ],
    max_tokens=100,
    temperature=1,
    top_p=1,
    top_k=50,
    n=1,
    stream=false,
    stream_options=null,
    stop=null,
    presence_penalty=0,
    frequency_penalty=0,
    logit_bias=null,
    logprobs=false,
    top_logprobs=null,
    user=null,
    extra_body={
        "guided_json": {"type": "object", "properties": {...}}
    },
    response_format={
        "type": "json_object"
    }
)

print(completion.to_json())
```

<details>

<summary><strong>Response</strong></summary>

```
{
  "id": "cmpl-*****",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "content": "Hello! It's nice to meet you. Is there something I can help you with, or would you like to chat?",
        "role": "assistant",
        "function_call": null,
        "tool_calls": []
      },
      "stop_reason": null
    }
  ],
  "created": 1721397089,
  "model": "meta-llama/Meta-Llama-3.1-70B-Instruct",
  "object": "chat.completion",
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 26,
    "prompt_tokens": 12,
    "total_tokens": 38
  }
}
```

</details>

For detailed field descriptions, see the [API reference](/arkane-cloud/api-reference/api-reference.md).

### Text generation for code autocomplete: v1/completions

**Request**

Here is a sample JSON request body that contains all supported fields:

```
from openai import OpenAI

client = OpenAI(
    base_url="https://console.arkanecloud.com/",
    api_key=os.environ.get("ARKANE_CLOUD_API_KEY"),
)

for chunk in client.completions.create(
        model="meta-llama/Meta-Llama-3.1-70B-Instruct",
        prompt="Say my name",
        max_tokens=7,
        temperature=0,
        stream=True
):
    print(chunk.choices[0].text)
```

<details>

<summary><strong>Response</strong></summary>

```
{
  "id": "cmpl-7iA7iJjj8V2zOkCGvWF2hAkDWBQZe",
  "object": "text_completion",
  "created": 1690759111,
  "choices": [
    {
      "text": "Heisenberg",
      "index": 0,
      "logprobs": null,
      "finish_reason": null
    }
  ],
  "model": "meta-llama/Meta-Llama-3.1-70B-Instruct",
  "system_fingerprint": "fp_44709d6aaa"
}
```

</details>

For detailed field descriptions, see the [API reference](/arkane-cloud/api-reference/api-reference.md).

### List of models: v1/models

**Request**

```
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://console.arkanecloud.com/",
    api_key=os.environ.get("ARKANE_CLOUD_API_KEY"),
)

client.models.list()
```

<details>

<summary>Response</summary>

```
{
  "object": "list",
  "data": [
    {
      "id": 'meta-llama/Meta-Llama-3.1-8B-Instruct',
      "object": "model",
      "created": 1724480826,
      "owned_by": 'system'
    },
    {
      "id": 'meta-llama/Meta-Llama-3.1-70B-Instruct',
      "object": "model",
      "created": 1724480826,
      "owned_by": 'system'
    },
  ],
  "object": "list"
}
```

</details>

For detailed field descriptions, see the [API reference](/arkane-cloud/api-reference/api-reference.md).

### Image generation: v1/images/generations

**Request**

```
from openai import OpenAI
import os

client = OpenAI(
    base_url="https://console.arkanecloud.com/",
    api_key=os.environ.get("ARKANE_CLOUD_API_KEY"),
)

completion = client.images.generate(
    model="stability-ai/sdxl",
    prompt="An elephant in a desert",
    response_format="b64_json",
    extra_body={
        "response_extension": "png",
        "width": 512,
        "height": 512,
        "num_inference_steps": 30,
        "seed": -1,
        "negative_prompt": "Giraffes, night sky"
    }
)

print(completion.to_json())
```

<details>

<summary><strong>Response</strong></summary>

```
{
  "data": [
    {
      "b64_json": "UklGRg66Ag..."
    }
  ],
  "id": "text2img-0941c39c-..."
}
```

</details>

For detailed field descriptions, see the [API reference](/arkane-cloud/api-reference/api-reference.md).

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arkanecloud.com/arkane-cloud/inference/api/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
