Skip to content
Merged
804 changes: 804 additions & 0 deletions docs/AI_INTEGRATION_GUIDE.md

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions examples/ai-analyst/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# AI Data Analyst

> **Natural Language Query system for business intelligence**

## Overview

AI-powered data analyst that transforms natural language questions into ObjectQL queries and generates insights.

## Features

- **Natural Language Queries**: Ask questions in plain English
- **Auto-generate Dashboards**: Create visualizations from queries
- **Insights Generation**: AI-powered data analysis
- **Query Templates**: Pre-built query patterns

## Example Queries

- "What's our total revenue by region for Q1?"
- "Show me the top 10 customers by lifetime value"
- "Compare this month's sales to last month"
- "Which products have declining sales?"

## NLQ Configuration

```typescript
export const AnalystNLQConfig: NLQModelConfig = {
modelId: 'gpt-4-turbo',
includeSchema: true,
includeExamples: true,
enableIntentDetection: true,
enableTimeframeDetection: true,
enableFuzzyMatching: true,
};
```

## Agent

```typescript
export const DataAnalystAgent: Agent = {
name: 'data_analyst_ai',
role: 'Business Intelligence Analyst',
tools: [
{ type: 'query', name: 'execute_sql' },
{ type: 'action', name: 'create_dashboard' },
{ type: 'action', name: 'generate_chart' },
],
};
```

## Use Cases

1. **Executive Dashboards**: Auto-generate KPI dashboards
2. **Ad-hoc Analysis**: Answer business questions instantly
3. **Report Generation**: Create formatted reports from queries
4. **Data Exploration**: Discover patterns and anomalies
14 changes: 14 additions & 0 deletions examples/ai-analyst/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@objectstack/example-ai-analyst",
"version": "1.0.0",
"description": "AI-powered data analyst with natural language query capabilities",
"private": true,
"main": "objectstack.config.ts",
"scripts": {
"dev": "tsx watch objectstack.config.ts",
"build": "tsc"
},
"dependencies": {
"@objectstack/spec": "workspace:*"
}
}
146 changes: 146 additions & 0 deletions examples/ai-analyst/src/ai-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import type { Agent, NLQModelConfig, QueryTemplate } from '@objectstack/spec';

/**
* AI Data Analyst Agent
*/
export const DataAnalystAgent: Agent = {
name: 'data_analyst_ai',
label: 'AI Data Analyst',
role: 'Business Intelligence Analyst',

instructions: `You are a data analyst helping users understand their business metrics.

Skills:
- Interpret natural language questions about data
- Generate ObjectQL queries
- Create visualizations
- Provide actionable insights

Be precise, data-driven, and clear in explanations.`,

model: {
provider: 'openai',
model: 'gpt-4',
temperature: 0.3,
maxTokens: 4096,
},

tools: [
{
type: 'query',
name: 'execute_objectql',
description: 'Execute ObjectQL queries on data',
},
{
type: 'action',
name: 'create_dashboard',
description: 'Generate dashboard from metrics',
},
{
type: 'action',
name: 'generate_chart',
description: 'Create charts and visualizations',
},
{
type: 'query',
name: 'get_schema',
description: 'Get object schema information',
},
],

access: ['analysts', 'executives', 'managers'],
active: true,
};

/**
* NLQ Configuration for Business Intelligence
*/
export const AnalystNLQConfig: NLQModelConfig = {
modelId: 'gpt-4',
systemPrompt: `You are an expert at converting business questions into ObjectQL queries.

Available objects: account, opportunity, task, product, order
Be precise with field names and support timeframes like "last quarter", "this year".`,

includeSchema: true,
includeExamples: true,
enableIntentDetection: true,
intentThreshold: 0.75,
enableEntityRecognition: true,
enableFuzzyMatching: true,
fuzzyMatchThreshold: 0.85,
enableTimeframeDetection: true,
defaultTimeframe: 'current_month',
enableCaching: true,
cacheTTL: 3600,
};

/**
* Common Query Templates
*/
export const AnalystQueryTemplates: QueryTemplate[] = [
{
id: 'top-n-by-field',
name: 'top_n_by_field',
label: 'Top N Records by Field',
pattern: 'top {n} {object} by {field}',
variables: [
{ name: 'n', type: 'value', required: true },
{ name: 'object', type: 'object', required: true },
{ name: 'field', type: 'field', required: true },
],
astTemplate: {
object: '{object}',
sort: [{ field: '{field}', order: 'desc' }],
limit: '{n}',
},
category: 'ranking',
examples: [
'top 10 accounts by revenue',
'top 5 products by sales',
],
},

{
id: 'aggregate-by-group',
name: 'aggregate_by_group',
label: 'Aggregate by Group',
pattern: 'total {field} by {group_field}',
variables: [
{ name: 'field', type: 'field', required: true },
{ name: 'group_field', type: 'field', required: true },
],
astTemplate: {
fields: [
'{group_field}',
{ function: 'sum', field: '{field}', alias: 'total' },
],
groupBy: ['{group_field}'],
},
category: 'aggregation',
examples: [
'total revenue by region',
'total orders by product',
],
},

{
id: 'time-comparison',
name: 'time_comparison',
label: 'Time Period Comparison',
pattern: 'compare {metric} for {period1} vs {period2}',
variables: [
{ name: 'metric', type: 'field', required: true },
{ name: 'period1', type: 'timeframe', required: true },
{ name: 'period2', type: 'timeframe', required: true },
],
astTemplate: {
// Complex comparison logic
},
category: 'comparison',
examples: [
'compare revenue for this month vs last month',
'compare sales for Q1 vs Q2',
],
},
];
61 changes: 61 additions & 0 deletions examples/ai-codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# AI Code Generator

> **Generate ObjectStack applications from natural language descriptions**

## Overview

AI-powered code generator that creates complete ObjectStack applications from high-level requirements.

## Features

- **App Generation**: Create full apps from descriptions
- **Schema Generation**: Generate object definitions
- **Validation**: Ensure generated code follows best practices
- **Test Generation**: Auto-generate tests for objects

## Example Usage

**Input**: "Create a project management app with projects, tasks, and team members. Projects should have milestones and budgets. Tasks can be assigned to team members with due dates and priorities."

**Output**: Complete ObjectStack application with:
- Object definitions (Project, Task, TeamMember, Milestone)
- Relationships (lookup fields)
- Validation rules
- List views and forms
- Navigation structure
- Reports and dashboards

## Agent Configuration

```typescript
export const CodeGenAgent: Agent = {
name: 'objectstack_code_generator',
role: 'Senior ObjectStack Developer',

tools: [
{ type: 'action', name: 'generate_object' },
{ type: 'action', name: 'generate_field' },
{ type: 'action', name: 'generate_view' },
{ type: 'action', name: 'validate_schema' },
],

knowledge: {
topics: ['objectstack_patterns', 'best_practices', 'examples'],
indexes: ['objectstack_docs'],
},
};
```

## Capabilities

1. **Object Schema Generation**: Field types, validation, relationships
2. **UI Generation**: Views, forms, dashboards
3. **Logic Generation**: Workflows, validations, triggers
4. **Best Practices**: Follows naming conventions and patterns

## Success Criteria

- Generated code passes all validations
- Follows ObjectStack conventions (camelCase, snake_case)
- Includes appropriate indexes and constraints
- Complete with UI configuration
10 changes: 10 additions & 0 deletions examples/ai-codegen/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@objectstack/example-ai-codegen",
"version": "1.0.0",
"description": "AI code generator - Generate ObjectStack apps from natural language",
"private": true,
"main": "objectstack.config.ts",
"dependencies": {
"@objectstack/spec": "workspace:*"
}
}
Loading
Loading