Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
81 changes: 81 additions & 0 deletions api-reference/pgai/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: pgai API reference
description: Complete API reference for pgai functions and AI operations in PostgreSQL
products: [cloud, mst, self_hosted]
keywords: [API, reference, AI, pgai, embeddings, LLM, vectorizer]
mode: "wide"
---

<CardGroup cols={2}>
<Card
title="OpenAI"
icon="openai"
href="/api-reference/pgai/model-calling/openai/"
>
GPT models, embeddings, moderation, and tokenization
</Card>

<Card
title="Ollama"
icon="server"
href="/api-reference/pgai/model-calling/ollama/"
>
Local LLM hosting with embedding and chat support
</Card>

<Card
title="Anthropic"
icon="brain"
href="/api-reference/pgai/model-calling/anthropic/"
>
Claude models for advanced reasoning
</Card>

<Card
title="Cohere"
icon="sparkles"
href="/api-reference/pgai/model-calling/cohere/"
>
Embeddings, classification, and reranking
</Card>

<Card
title="Voyage AI"
icon="rocket"
href="/api-reference/pgai/model-calling/voyageai/"
>
Specialized embedding models for retrieval
</Card>

<Card
title="LiteLLM"
icon="layer-group"
href="/api-reference/pgai/model-calling/litellm/"
>
Unified interface for 100+ LLM providers
</Card>

<Card
title="Vectorizer"
icon="vector-square"
href="/api-reference/pgai/vectorizer/"
>
Automatically create and sync embeddings for your data
</Card>

<Card
title="Semantic catalog"
icon="book"
href="/api-reference/pgai/semantic-catalog/"
>
Enable natural language to SQL with AI-powered database descriptions
</Card>

<Card
title="Utilities"
icon="wrench"
href="/api-reference/pgai/utilities/"
>
Helper functions for chunking, secrets, and dataset loading
</Card>
</CardGroup>
169 changes: 169 additions & 0 deletions api-reference/pgai/model-calling/anthropic/anthropic_generate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
---
title: anthropic_generate()
description: Generate text completions using Claude models for sophisticated reasoning and analysis
keywords: [Anthropic, Claude, generation, reasoning, AI]
tags: [AI, Anthropic, Claude, generation]
license: community
type: function
---

Generate text completions using Anthropic's Claude models. This function supports multi-turn conversations, system
prompts, tool use, and vision capabilities for sophisticated reasoning and analysis tasks.

## Samples

### Basic text generation

Generate a simple response:

```sql
SELECT ai.anthropic_generate(
'claude-3-5-sonnet-20241022',
jsonb_build_array(
jsonb_build_object('role', 'user', 'content', 'Explain PostgreSQL in one sentence')
)
)->'content'->0->>'text';
```

### Multi-turn conversation

Continue a conversation with message history:

```sql
SELECT ai.anthropic_generate(
'claude-3-5-sonnet-20241022',
jsonb_build_array(
jsonb_build_object('role', 'user', 'content', 'What is PostgreSQL?'),
jsonb_build_object('role', 'assistant', 'content', 'PostgreSQL is a powerful open-source relational database.'),
jsonb_build_object('role', 'user', 'content', 'What makes it different from MySQL?')
)
)->'content'->0->>'text';
```

### Use a system prompt

Guide Claude's behavior:

```sql
SELECT ai.anthropic_generate(
'claude-3-5-sonnet-20241022',
jsonb_build_array(
jsonb_build_object('role', 'user', 'content', 'Explain databases')
),
system_prompt => 'You are a helpful database expert. Give concise, technical answers with code examples.'
)->'content'->0->>'text';
```

### Control creativity with temperature

Adjust the randomness of responses:

```sql
SELECT ai.anthropic_generate(
'claude-3-5-sonnet-20241022',
jsonb_build_array(
jsonb_build_object('role', 'user', 'content', 'Write a creative story about databases')
),
temperature => 0.9,
max_tokens => 2000
)->'content'->0->>'text';
```

### Use tools (function calling)

Enable Claude to call functions:

```sql
SELECT ai.anthropic_generate(
'claude-3-5-sonnet-20241022',
jsonb_build_array(
jsonb_build_object('role', 'user', 'content', 'What is the weather in Paris?')
),
tools => '[
{
"name": "get_weather",
"description": "Get current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
}
},
"required": ["location"]
}
}
]'::jsonb,
tool_choice => '{"type": "auto"}'::jsonb
);
```

### Control stop sequences

Stop generation at specific sequences:

```sql
SELECT ai.anthropic_generate(
'claude-3-5-sonnet-20241022',
jsonb_build_array(
jsonb_build_object('role', 'user', 'content', 'List three database types')
),
stop_sequences => ARRAY['4.', 'Fourth']
)->'content'->0->>'text';
```

### Use with API key name

Reference a stored API key:

```sql
SELECT ai.anthropic_generate(
'claude-3-5-sonnet-20241022',
jsonb_build_array(
jsonb_build_object('role', 'user', 'content', 'Hello, Claude!')
),
api_key_name => 'ANTHROPIC_API_KEY'
)->'content'->0->>'text';
```

## Arguments

| Name | Type | Default | Required | Description |
|------|------|---------|----------|-------------|
| `model` | `TEXT` | - | ✔ | The Claude model to use (e.g., `claude-3-5-sonnet-20241022`) |
| `messages` | `JSONB` | - | ✔ | Array of message objects with `role` and `content` |
| `max_tokens` | `INT` | `1024` | ✖ | Maximum tokens to generate (required by Anthropic API) |
| `api_key` | `TEXT` | `NULL` | ✖ | Anthropic API key. If not provided, uses configured secret |
| `api_key_name` | `TEXT` | `NULL` | ✖ | Name of the secret containing the API key |
| `base_url` | `TEXT` | `NULL` | ✖ | Custom API base URL |
| `timeout` | `FLOAT8` | `NULL` | ✖ | Request timeout in seconds |
| `max_retries` | `INT` | `NULL` | ✖ | Maximum number of retry attempts |
| `system_prompt` | `TEXT` | `NULL` | ✖ | System prompt to guide model behavior |
| `user_id` | `TEXT` | `NULL` | ✖ | Unique identifier for the end user |
| `stop_sequences` | `TEXT[]` | `NULL` | ✖ | Sequences that stop generation |
| `temperature` | `FLOAT8` | `NULL` | ✖ | Sampling temperature (0.0 to 1.0) |
| `tool_choice` | `JSONB` | `NULL` | ✖ | How the model should use tools (e.g., `{"type": "auto"}`) |
| `tools` | `JSONB` | `NULL` | ✖ | Function definitions for tool use |
| `top_k` | `INT` | `NULL` | ✖ | Only sample from top K options |
| `top_p` | `FLOAT8` | `NULL` | ✖ | Nucleus sampling threshold (0.0 to 1.0) |
| `verbose` | `BOOLEAN` | `FALSE` | ✖ | Enable verbose logging for debugging |

## Returns

`JSONB`: The complete API response including:
- `id`: Unique message identifier
- `type`: Response type (always `"message"`)
- `role`: Role of the responder (always `"assistant"`)
- `content`: Array of content blocks (text, tool use, etc.)
- `model`: Model used for generation
- `stop_reason`: Why generation stopped (e.g., `"end_turn"`, `"max_tokens"`)
- `usage`: Token usage statistics

## Related functions

- [`anthropic_list_models()`][anthropic_list_models]: list available Claude models
- [`openai_chat_complete()`][openai_chat_complete]: alternative with OpenAI models

[anthropic_list_models]: /api-reference/pgai/model-calling/anthropic/anthropic_list_models
[openai_chat_complete]: /api-reference/pgai/model-calling/openai/openai_chat_complete
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
title: anthropic_list_models()
description: List available Anthropic Claude models
keywords: [Anthropic, Claude, models, list]
tags: [AI, Anthropic, models, management]
license: community
type: function
---

List all Claude models available through the Anthropic API. This function returns basic information about each model
including its ID, name, and creation date.

## Samples

### List all models

See all available Claude models:

```sql
SELECT * FROM ai.anthropic_list_models();
```

Returns:

```text
id | name | created
------------------------------------+-------------------------+-------------------
claude-3-5-sonnet-20241022 | Claude 3.5 Sonnet | 2024-10-22 ...
claude-3-opus-20240229 | Claude 3 Opus | 2024-02-29 ...
claude-3-sonnet-20240229 | Claude 3 Sonnet | 2024-02-29 ...
claude-3-haiku-20240307 | Claude 3 Haiku | 2024-03-07 ...
```

### Use with API key name

Reference a stored API key:

```sql
SELECT * FROM ai.anthropic_list_models(
api_key_name => 'ANTHROPIC_API_KEY'
);
```

### Filter by model name

Find specific models:

```sql
SELECT id, name
FROM ai.anthropic_list_models()
WHERE name LIKE '%Sonnet%';
```

### Get the latest model

Find the most recently released model:

```sql
SELECT id, name, created
FROM ai.anthropic_list_models()
ORDER BY created DESC
LIMIT 1;
```

## Arguments

| Name | Type | Default | Required | Description |
|------|------|---------|----------|-------------|
| `api_key` | `TEXT` | `NULL` | ✖ | Anthropic API key. If not provided, uses configured secret |
| `api_key_name` | `TEXT` | `NULL` | ✖ | Name of the secret containing the API key |
| `base_url` | `TEXT` | `NULL` | ✖ | Custom API base URL |
| `verbose` | `BOOLEAN` | `FALSE` | ✖ | Enable verbose logging for debugging |

## Returns

`TABLE`: A table with the following columns:

| Column | Type | Description |
|--------|------|-------------|
| `id` | `TEXT` | Model identifier (e.g., `claude-3-5-sonnet-20241022`) |
| `name` | `TEXT` | Human-readable model name (e.g., `Claude 3.5 Sonnet`) |
| `created` | `TIMESTAMPTZ` | When the model was released |

## Related functions

- [`anthropic_generate()`][anthropic_generate]: generate text with Claude models
- [`openai_list_models()`][openai_list_models]: list OpenAI models

[anthropic_generate]: /api-reference/pgai/model-calling/anthropic/anthropic_generate
[openai_list_models]: /api-reference/pgai/model-calling/openai/openai_list_models
Loading