# Azion AI Client module

import DocButton from '~/components/webkit/DocButton.vue';

The Azion **AI Client** library provides a simple interface to interact with the Azion AI API. This specialized AI has contextual knowledge about all Azion products, services, and technologies, allowing for precise and relevant responses about Azion Web Platform.

<DocButton href="/en/documentation/products/azion-lib/overview/" label="Go to Azion Libraries Overview" kind="secondary" target="_blank" size="medium" />

You can interact with the API using a `client` or calling the methods directly from the library. When making direct calls, you can use the environment variables to configure the `client` without passing the token and debug parameters directly.

This is an example of how a `.env` file with your environment variables may look like:

```sh
AZION_TOKEN=your-api-token
AZION_DEBUG=true
```

| Variable | Description |
|----------|-------------|
| `AZION_TOKEN` | Your Azion API token. |
| `AZION_DEBUG` | Enable debug mode (true/false). |

:::tip
Setting `AZION_DEBUG` to `true` enables **Debug mode**. This mode provides detailed logging of the API requests and responses.
:::

---

## Usage

### `createClient`

Creates an Azion AI client with methods to interact with AI services.

```typescript
import { createClient, AzionAIClient } from 'azion/ai';


const client: AzionAIClient = createClient({
    token: 'your-token-here', // Replace with your actual token
    options: {
        // Add any additional options here
    }
});
```

**Parameters**:

| Parameter | Type | Description |
|-----------|------|-------------|
| `config` | `Partial<{ token: string; options?: AzionClientOptions }>` | Optional configuration for the client. |

**Returns**:

| Return Type | Description |
|-------------|-------------|
| `AzionAIClient` | A client object with methods to interact with AI services.

### `chat`

Sends a chat request to the Azion AI service.

**TypeScript example**:

```typescript
import { chat } from 'azion/ai';
import type { AzionAIRequest, AzionAIResponse, AzionAIResult } from 'azion/ai';

const request: AzionAIRequest = {
messages: [{ role: 'user', content: 'Explain what Azion Edge Computing is.' }]
};
const { data: response, error }: AzionAIResult<AzionAIResponse> = await chat(request, { debug: true });
if (response) {
console.log('AI response:', response.choices[0].message.content);
} else {
console.error('Chat failed', error);
}
```

**Parameters**:

| Parameter | Type | Description |
|-----------|------|-------------|
| `request: AzionAIRequest` | `Partial<{ token: string; options?: OptionsParams }>` |  Request object containing chat parameters. |
| `options AzionClientOptions` | `Partial<{ token: string; options?: OptionsParams }>` | Additional client options. |

**Returns**:

| Return Type | Description |
|-------------|-------------|
| `Promise<AzionAIResult<AzionAIResponse>>` | A promise that resolves to the chat result or an error. |

### `streamChat`

Sends a streaming chat request to the Azion AI service.

**TypeScript example**:

```typescript
import { streamChat } from 'azion/ai';
import type { AzionAIRequest, AzionAIStreamResponse, AzionAIResult } from 'azion/ai';

const request: AzionAIRequest = {
messages: [{ role: 'user', content: 'List 5 use cases for Azion Functions.' }]
};
const stream = streamChat(request, { debug: true });
for await (const chunk: AzionAIResult<AzionAIStreamResponse> of stream) {
if (chunk.data) {
process.stdout.write(chunk.data.choices[0].delta.content || '');
} else {
console.error('Error:', chunk.error);
}
}
```

**Parameters**:

| Parameter | Type | Description |
|-----------|------|-------------|
| `request: AzionAIRequest` | `Partial<{ token: string; options?: OptionsParams }>` |  Request object containing chat parameters. |
| `options AzionClientOptions` | `Partial<{ token: string; options?: OptionsParams }>` | Additional client options. |

**Returns**:

| Return Type | Description |
|-------------|-------------|
| `AsyncGenerator<AzionAIResult<AzionAIStreamResponse>>` | An async generator that produces partial chat results. |

---