Learn how to combine prompts and templates to generate new, structured content from your existing knowledge base using the /generate/prompt endpoint.

Overview

The generate with prompt feature allows you to:

  • Use pre-defined prompts for consistent content generation
  • Apply templates to format output as JSON or structured text
  • Filter content by categories and topics
  • Save generated content back to your knowledge base

Prerequisites

Before generating content, you’ll need:

  1. Content in your knowledge base (see Hello World)
  2. A prompt created (see Managing Prompts)
  3. Optional: A template for structured output (see Managing Templates)

Generate Content Example

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

async function generateProductContent() {
  try {
    // First, ensure we have a prompt and template
    const promptResponse = await fetch(`${API_URL}/prompts`, {
      method: 'POST',
      headers: {
        'X-API-Key': API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: 'Product Overview Generator',
        text: 'Based on the {{content_type}} information, generate a comprehensive overview including features, benefits, pricing, and target audience. Make it suitable for {{audience}}.'
      })
    });
    const prompt = await promptResponse.json();

    const templateResponse = await fetch(`${API_URL}/templates`, {
      method: 'POST',
      headers: {
        'X-API-Key': API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: 'Product Overview JSON',
        text: `{
  "product_name": "{{product_name}}",
  "overview": {
    "summary": "{{summary}}",
    "key_features": {{features}},
    "benefits": {{benefits}},
    "pricing": "{{pricing}}",
    "target_audience": "{{target_audience}}"
  },
  "generated_at": "{{timestamp}}"
}`,
        output_type: 'json'
      })
    });
    const template = await templateResponse.json();

    // Generate content using the prompt and template
    const generateResponse = await fetch(`${API_URL}/generate/prompt`, {
      method: 'POST',
      headers: {
        'X-API-Key': API_KEY,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        prompt_id: prompt.prompt_id,
        template_id: template.template_id,
        content_type: 'product documentation and specifications',
        category_id: 'your-category-id', // Optional: filter by category
        max_results: 10, // Use up to 10 source chunks
        save: true // Save the generated content
      })
    });
    
    const generatedContent = await generateResponse.json();
    console.log('Generated content:', generatedContent.generated_text);
    console.log('Content saved with ID:', generatedContent.content_id);
    console.log('Sources used:', generatedContent.sources.length);
    
  } catch (error) {
    console.error('Error generating content:', error);
  }
}

generateProductContent();

Advanced Examples

Generate FAQ Content

// Create an FAQ generation setup
const faqPromptResponse = await fetch(`${API_URL}/prompts`, {
  method: 'POST',
  headers: {
    'X-API-Key': API_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'FAQ Generator',
    text: 'Generate {{num_questions}} frequently asked questions about {{content_type}}. Include detailed answers that address common concerns.'
  })
});
const faqPrompt = await faqPromptResponse.json();

const faqTemplateResponse = await fetch(`${API_URL}/templates`, {
  method: 'POST',
  headers: {
    'X-API-Key': API_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'FAQ List',
    text: `# Frequently Asked Questions

{{faq_content}}

---
*Last updated: {{date}}*`,
    output_type: 'text'
  })
});
const faqTemplate = await faqTemplateResponse.json();

// Generate FAQs for a specific topic
const faqsResponse = await fetch(`${API_URL}/generate/prompt`, {
  method: 'POST',
  headers: {
    'X-API-Key': API_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    prompt_id: faqPrompt.prompt_id,
    template_id: faqTemplate.template_id,
    content_type: 'mortgage application process',
    topic_id: 'your-topic-id', // Filter by specific topic
    max_results: 15
  })
});
const faqs = await faqsResponse.json();

Generate Summary Reports

// Without a template - get raw generated text
const summaryResponse = await fetch(`${API_URL}/generate/prompt`, {
  method: 'POST',
  headers: {
    'X-API-Key': API_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    prompt_id: 'existing-summary-prompt-id',
    content_type: 'quarterly financial reports',
    category_id: 'finance-category-id',
    save: false // Don't save, just return the generated content
  })
});

const summary = await summaryResponse.json();
console.log('Generated summary:', summary.generated_text);
console.log('Based on', summary.sources.length, 'sources');

Working with Sources

The response includes source information showing which content chunks were used:

{
  "generated_text": "...",
  "sources": [
    {
      "content_id": "abc123",
      "version_id": "def456",
      "chunk_index": 0,
      "chunk_text": "The mortgage application process typically...",
      "score": 0.92,
      "title": "Mortgage Application Guide"
    }
  ]
}

Best Practices

  1. Be specific with content_type: The more specific your content type description, the better the results
  2. Use filters wisely: Category and topic filters help focus the generation on relevant content
  3. Adjust max_results: More sources can provide comprehensive content but may increase processing time
  4. Save strategically: Only save generated content that you’ll reuse or need to reference later
  5. Monitor sources: Review which content chunks are being used to ensure quality

Error Handling

try:
    response = requests.post(
        f'{API_URL}/generate/prompt',
        headers={'X-API-Key': API_KEY},
        json={
            'prompt_id': prompt_id,
            'template_id': template_id,
            'content_type': 'product specs'
        }
    )
    result = response.json()
    
    if response.status_code == 404:
        print('Prompt or template not found')
    elif response.status_code == 400:
        print('Invalid request parameters')
    else:
        print(f"Generated: {result['generated_text']}")
        
except Exception as error:
    print(f'Error: {error}')

Next Steps

  • Experiment with different prompt variations for better results
  • Create multiple templates for different output formats
  • Use the saved generated content for further processing
  • Combine with the search API for a complete knowledge base solution