Learn how to use the Senso generate API to create new content based on your existing knowledge base.

What You’ll Learn

This guide covers:

  • Using the /generate endpoint to create content
  • Filtering generation by categories and topics
  • Saving generated content for future use
  • Understanding source attribution

How Generation Works

The generate API:

  1. Searches your knowledge base for relevant content
  2. Uses AI to synthesize information from multiple sources
  3. Creates new content following your instructions
  4. Optionally saves the result as new content

Basic Content Generation

const API_URL = 'https://sdk.senso.ai/api/v1';
const API_KEY = 'YOUR_API_KEY';

async function generateContent() {
  try {
    // Generate a product overview
    const overviewResponse = await fetch(`${API_URL}/generate`, {
      method: 'POST',
      headers: {
        'X-API-Key': API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        content_type: 'product features and benefits',
        instructions: 'Create a comprehensive product overview suitable for new customers',
        max_results: 10, // Use up to 10 source chunks
        save: false // Don't save this generation
      })
    });
    
    const overview = await overviewResponse.json();
    console.log('Generated Content:');
    console.log(overview.generated_text);
    console.log('\nBased on', overview.sources.length, 'sources');
    
    // Generate and save FAQ content
    const faqResponse = await fetch(`${API_URL}/generate`, {
      method: 'POST',
      headers: {
        'X-API-Key': API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        content_type: 'common customer questions',
        instructions: 'Generate a FAQ section with at least 10 questions and detailed answers',
        save: true // Save this as new content
      })
    });
    
    const faq = await faqResponse.json();
    console.log('\nFAQ Generated and Saved!');
    console.log('Content ID:', faq.content_id);
    console.log('Preview:', faq.generated_text.substring(0, 200) + '...');
    
  } catch (error) {
    console.error('Error generating content:', error);
  }
}

generateContent();

Filtered Generation

Generate content from specific categories or topics:

// Generate content from a specific category
const categoryResponse = await fetch(`${API_URL}/generate`, {
  method: 'POST',
  headers: {
    'X-API-Key': API_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    content_type: 'technical specifications',
    instructions: 'Create a technical datasheet with all specifications in a structured format',
    category_id: 'tech-docs-category-id',
    max_results: 20
  })
});

const categoryContent = await categoryResponse.json();

// Generate content from a specific topic
const topicResponse = await fetch(`${API_URL}/generate`, {
  method: 'POST',
  headers: {
    'X-API-Key': API_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    content_type: 'installation procedures',
    instructions: 'Write step-by-step installation instructions for beginners',
    topic_id: 'installation-topic-id',
    save: true
  })
});

const topicContent = await topicResponse.json();

Advanced Generation Examples

Email Template Generation

email_response = requests.post(
    f'{API_URL}/generate',
    headers={'X-API-Key': API_KEY},
    json={
        'content_type': 'product announcement information',
        'instructions': '''Create a professional email template for announcing our new product. 
                         Include: subject line, greeting, key benefits (3-5 points), 
                         call-to-action, and sign-off.''',
        'save': True
    }
)
email_template = email_response.json()

Comparison Chart

comparison_response = requests.post(
    f'{API_URL}/generate',
    headers={'X-API-Key': API_KEY},
    json={
        'content_type': 'product features across different tiers',
        'instructions': 'Create a comparison table showing features available in Basic, Pro, and Enterprise tiers',
        'max_results': 15
    }
)
comparison = comparison_response.json()

Training Material

training_response = requests.post(
    f'{API_URL}/generate',
    headers={'X-API-Key': API_KEY},
    json={
        'content_type': 'user guide and best practices',
        'instructions': 'Develop a training module for new users covering basic operations and tips',
        'category_id': 'user-docs-category',
        'save': True
    }
)
training = training_response.json()

Understanding the Response

The generate API returns detailed information about the generation:

{
  "content_type": "product features and benefits",
  "instructions": "Create a comprehensive product overview...",
  "generated_text": "# Product Overview\n\nOur product offers...",
  "content_id": "550e8400-e29b-41d4-a716-446655440000",
  "sources": [
    {
      "content_id": "abc123",
      "version_id": "def456",
      "chunk_index": 2,
      "chunk_text": "Key features include...",
      "score": 0.89,
      "title": "Product Documentation"
    }
  ],
  "processing_time_ms": 1250
}

Best Practices

  1. Be specific with content_type: Describe exactly what kind of content you’re looking for
  2. Clear instructions: Provide detailed instructions for better results
  3. Optimize max_results: More sources provide comprehensive content but take longer
  4. Save strategically: Only save generated content you’ll reuse
  5. Review sources: Check which content was used to ensure accuracy

Error Handling

try:
    response = requests.post(
        f'{API_URL}/generate',
        headers={'X-API-Key': API_KEY},
        json={
            'content_type': 'technical specs',
            'instructions': 'Create a spec sheet'
        }
    )
    result = response.json()
    
    if response.status_code == 400:
        print(f'Invalid parameters: {result.get("error")}')
    elif response.status_code == 404:
        print('No relevant content found')
    else:
        print(f'Generated: {result["generated_text"]}')
        
except Exception as error:
    print(f'Error: {error}')

Use Cases

Dynamic Documentation

Generate up-to-date documentation that reflects your latest content:

docs_response = requests.post(
    f'{API_URL}/generate',
    headers={'X-API-Key': API_KEY},
    json={
        'content_type': 'API endpoints and parameters',
        'instructions': 'Create comprehensive API documentation with examples',
        'category_id': 'api-docs',
        'save': True
    }
)
docs = docs_response.json()

Content Summarization

Create executive summaries from detailed content:

summary_response = requests.post(
    f'{API_URL}/generate',
    headers={'X-API-Key': API_KEY},
    json={
        'content_type': 'quarterly reports and analyses',
        'instructions': 'Summarize key findings and metrics in 5 bullet points',
        'max_results': 20
    }
)
summary = summary_response.json()

Next Steps