# LLM API

## Chat Completions

Spice provides an OpenAI compatible chat completion AI at <https://data.spiceai.io/v1/chat/completions>. Authorize with the endpoint using an [App API key](https://github.com/spicehq/docs/blob/trunk/portal/apps/api-keys.md).

The App requires a configured and deployed model to respond to chat completion requests.

For more information about using chat completions, refer to the [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat).

## Create Chat Completion

> Creates a model response for the given chat conversation.

```json
{"openapi":"3.1.0","info":{"title":"Spice.ai Data-Platform","version":"1.0.0"},"tags":[],"servers":[{"description":"AWS Stamp, US East 1 region","url":"https://us-east-1-prod-aws-data.spiceai.io"},{"description":"AWS Stamp, US West 2 region","url":"https://us-west-2-prod-aws-data.spiceai.io"}],"security":[{"ApiKeyAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"in":"header","name":"X-API-KEY","type":"apiKey"}},"schemas":{}},"paths":{"/v1/chat/completions":{"post":{"tags":["AI"],"summary":"Create Chat Completion","description":"Creates a model response for the given chat conversation.","operationId":"post_chat_completions","requestBody":{"description":"Create a chat completion request using a language model.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/CreateChatCompletionRequest"}}},"required":true},"responses":{"200":{"description":"Chat completion generated successfully","content":{"application/json":{"schema":{"$ref":"#/components/schemas/CreateChatCompletionResponse"}}}},"404":{"description":"The specified model was not found"},"500":{"description":"An internal server error occurred while processing the chat completion","content":{"application/json":{"schema":{}}}}}}}}}
```

## Text-to-SQL (NSQL)

> Generate and optionally execute a natural-language text-to-SQL (NSQL) query.\
> \
> This endpoint generates a SQL query using a natural language query (NSQL) and optionally executes it.\
> The SQL query is generated by the specified model and executed if the \`Accept\` header is not set to \`application/sql\`.\
> When \`stream\` is true, the response is streamed as Server-Sent Events (SSE).

```json
{"openapi":"3.1.0","info":{"title":"Spice.ai Data-Platform","version":"1.0.0"},"tags":[],"servers":[{"description":"AWS Stamp, US East 1 region","url":"https://us-east-1-prod-aws-data.spiceai.io"},{"description":"AWS Stamp, US West 2 region","url":"https://us-west-2-prod-aws-data.spiceai.io"}],"security":[{"ApiKeyAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"in":"header","name":"X-API-KEY","type":"apiKey"}},"schemas":{}},"paths":{"/v1/nsql":{"post":{"tags":["SQL"],"summary":"Text-to-SQL (NSQL)","description":"Generate and optionally execute a natural-language text-to-SQL (NSQL) query.\n\nThis endpoint generates a SQL query using a natural language query (NSQL) and optionally executes it.\nThe SQL query is generated by the specified model and executed if the `Accept` header is not set to `application/sql`.\nWhen `stream` is true, the response is streamed as Server-Sent Events (SSE).","operationId":"post_nsql","parameters":[{"name":"Accept","in":"header","description":"The format of the response, one of 'application/json' (default), 'application/vnd.spiceai.nsql.v1+json', 'application/sql', 'text/csv' or 'text/plain'. 'application/sql' will only return the SQL query generated by the model.","required":true,"schema":{"type":"string"}}],"requestBody":{"description":"Request body to generate an NSQL query","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Request"}}},"required":true},"responses":{"200":{"description":"SQL query executed successfully","content":{"application/json":{"schema":{"type":"array","items":{}}},"application/sql":{"schema":{"type":"string"}},"application/vnd.spiceai.nsql.v1+json":{"schema":{}},"text/event-stream":{"schema":{"type":"string"}}}},"400":{"description":"Invalid request parameters","content":{"application/json":{"schema":{"type":"string"}}}},"500":{"description":"Internal server error","content":{"text/plain":{"schema":{"type":"string"}}}}}}}}}
```

## List Models

> List all models, both machine learning and language models, available in the runtime.

```json
{"openapi":"3.1.0","info":{"title":"Spice.ai Data-Platform","version":"1.0.0"},"tags":[],"servers":[{"description":"AWS Stamp, US East 1 region","url":"https://us-east-1-prod-aws-data.spiceai.io"},{"description":"AWS Stamp, US West 2 region","url":"https://us-west-2-prod-aws-data.spiceai.io"}],"security":[{"ApiKeyAuth":[]}],"components":{"securitySchemes":{"ApiKeyAuth":{"in":"header","name":"X-API-KEY","type":"apiKey"}},"schemas":{}},"paths":{"/v1/models":{"get":{"tags":["AI"],"summary":"List Models","description":"List all models, both machine learning and language models, available in the runtime.","operationId":"get_models","parameters":[{"name":"format","in":"query","description":"The format of the response (e.g., `json` or `csv`).","required":false,"schema":{"$ref":"#/components/schemas/Format"}},{"name":"status","in":"query","description":"If true, includes the status of each model in the response.","required":false,"schema":{"type":"boolean"}},{"name":"metadata_fields","in":"query","description":"A comma-separated list of metadata fields to include in the response (e.g., `supports_responses_api`)","required":false,"schema":{"type":"string"}}],"responses":{"200":{"description":"List of models in JSON format","content":{"application/json":{"schema":{"$ref":"#/components/schemas/OpenAIModelResponse"}},"text/csv":{"schema":{"type":"string"}}}},"500":{"description":"Internal server error occurred while processing models","content":{"application/json":{"schema":{}}}}}}}}}
```


---

# 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.spice.ai/api/runtime-apis/openai-api.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.
