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

# Generate Answer

> Generate AI answers using the Python client

## answer.generate

Generate AI-powered answers with RAG (retrieve from a text namespace) or direct LLM calls. Configure the LLM provider (Ollama, OpenAI, or Cohere) with `moorcheh configure` before use.

```python theme={null}
client.answer.generate(
    *,
    namespace: str,
    query: str,
    top_k: int | None = None,
    threshold: float | None = None,
    kiosk_mode: bool = False,
    temperature: float | None = None,
    ai_model: str | None = None,
    header_prompt: str | None = None,
    footer_prompt: str | None = None,
    chat_history: list[dict[str, str]] | None = None,
    structured_response: dict | None = None,
) -> dict[str, Any]
```

**API:** `POST /answer` — see [Generate AI Answer](/on-prem/api-references/ai/generate)

### Search Mode (RAG)

```python theme={null}
from moorcheh import MoorchehClient

with MoorchehClient("http://localhost:8080") as client:
    response = client.answer.generate(
        namespace="my-documents",
        query="What are the main benefits?",
        top_k=5,
    )
    print(response["answer"])
    print(response["context_count"])
```

### Direct AI Mode

Use an empty namespace string:

```python theme={null}
with MoorchehClient("http://localhost:8080") as client:
    response = client.answer.generate(
        namespace="",
        query="Explain vector search in one paragraph",
        temperature=0.5,
        header_prompt="You are a concise technical writer.",
    )
```

### With chat history

```python theme={null}
with MoorchehClient("http://localhost:8080") as client:
    response = client.answer.generate(
        namespace="",
        query="Can you give an example?",
        chat_history=[
            {"role": "user", "content": "What is RAG?"},
            {"role": "assistant", "content": "RAG combines retrieval with generation..."},
        ],
    )
```

### Structured output

```python theme={null}
with MoorchehClient("http://localhost:8080") as client:
    response = client.answer.generate(
        namespace="docs",
        query="Summarize the return policy",
        structured_response={"enabled": True},
    )
    print(response.get("structured_data"))
```

### Parameters

| Field                 | Type    | Required | Description                                    |
| --------------------- | ------- | -------- | ---------------------------------------------- |
| `query`               | string  | Yes      | User question                                  |
| `namespace`           | string  | Yes      | Text namespace for RAG, or `""` for direct LLM |
| `top_k`               | number  | No       | Chunks to retrieve (default `10`)              |
| `temperature`         | number  | No       | `0.0`–`2.0` (default `0.7`)                    |
| `ai_model`            | string  | No       | Override configured LLM model                  |
| `chat_history`        | array   | No       | Prior turns                                    |
| `header_prompt`       | string  | No       | System instruction                             |
| `footer_prompt`       | string  | No       | Trailing instruction                           |
| `kiosk_mode`          | boolean | No       | Filter by `threshold`                          |
| `threshold`           | number  | No       | Required if `kiosk_mode` is true               |
| `structured_response` | object  | No       | `{ "enabled": true, "schema": {...} }`         |

### Response

| Field             | Type   | Description                       |
| ----------------- | ------ | --------------------------------- |
| `answer`          | string | Generated text                    |
| `model`           | string | Model ID used                     |
| `context_count`   | number | Chunks used for RAG               |
| `query`           | string | Echo of input query               |
| `structured_data` | object | When structured output is enabled |

## Related Operations

* [API: Generate AI Answer](/on-prem/api-references/ai/generate)
* [CLI: moorcheh answer](/on-prem/cli/ai/generate)
