# createChat



# Overview

Creates a [DefaultChatRuntime](/docs/api-reference/server/default-chat-runtime) instance with a `.handler` function for HTTP requests.

## Type Definition

```ts
import type { LanguageModel } from 'ai';
import type { SearchEngine, SearchIndexStore } from '@peam-ai/search';
import type { ConversationSummarizer, DefaultChatRuntime, SummarizationOptions } from 'peam/server';

export interface ChatRuntimeOptions {
  /**
   * The language model to use for generating responses and summarization.
   * Defaults to OpenAI GPT-4o if not provided.
   */
  model?: LanguageModel;

  /**
   * Maximum allowed length for a single message.
   * @default 30000
   */
  maxMessageLength?: number;

  /**
   * Search index store to use for loading the search index.
   */
  searchIndexStore?: SearchIndexStore;

  /**
   * SearchEngine to use for retrieving relevant documents.
   */
  searchEngine?: SearchEngine;

  /**
   * Options for message summarization.
   */
  summarization?: SummarizationOptions | false;

  /**
   * Custom summarizer implementation.
   */
  summarizer?: ConversationSummarizer;
}

export declare function createChat(options?: ChatRuntimeOptions): DefaultChatRuntime;
```

## Request Body

The handler expects the following JSON payload:

```ts
import type { UIMessage } from 'ai';

export interface Summary {
  text: string;
  lastSummarizedMessageId: string;
}

export interface ChatRequestBody {
  messages: UIMessage[];
  summary?: Summary;
}
```

## Usage

### Basic Usage

```ts
import { createChat } from 'peam/server';

export const POST = createChat().handler;
```

### With Custom Model

```ts
import { createChat } from 'peam/server';
import { openai } from '@ai-sdk/openai';

export const POST = createChat({
  model: openai('gpt-4o'),
}).handler;
```

### With Custom Search Store

```ts
import { createChat } from 'peam/server';
import { FileBasedSearchIndexStore } from '@peam-ai/search';

export const POST = createChat({
  searchIndexStore: new FileBasedSearchIndexStore({
    indexPath: '.peam/index.json',
  }),
}).handler;
```

### Disable Summarization

```ts
import { createChat } from 'peam/server';

export const POST = createChat({
  summarization: false,
}).handler;
```

## Notes

* If `searchIndexStore` is not provided, [DefaultChatRuntime](/docs/api-reference/server/default-chat-runtime) uses a `FileBasedSearchIndexStore` pointing to `.peam/index.json`.
* If `searchEngine` is provided, it is used directly instead of loading from a store (see [getSearchEngine](/docs/api-reference/server/get-search-engine)).
* If `summarization` is not set to `false`, [WindowedConversationSummarizer](/docs/api-reference/server/windowed-conversation-summarizer) is used with `maxMessages` defaulting to 10.


## Sitemap
[Overview of all docs pages](/sitemap.md)
