Skip to main content

Build Your First AI-Powered Application

This guide will walk you through creating your first Trainly-powered application that can answer questions about your documents.
By the end of this guide, you’ll have a working document Q&A system with less than 20 lines of code.

Step 1: Create Your Knowledge Base

1

Sign Up

Create a free account at trainlyai.com
2

Create a Chat

Click “New Chat” to create your first knowledge base
3

Upload Documents

Upload PDFs, DOCX, TXT, or other documents. Trainly will automatically:
  • Extract text content
  • Create semantic chunks
  • Generate embeddings
  • Build a searchable graph
4

Test It

Try asking questions in the chat interface to verify your documents are indexed correctly
Start with 2-3 documents to get familiar with the system. You can always add more later.

Step 2: Enable API Access

1

Open Settings

Click the settings icon in your chat
2

Configure Your Settings

  • Choose your preferred AI model (GPT-4o-mini recommended)
  • Set temperature and max tokens
  • Add a custom prompt (optional)
  • Select which documents to include
3

Publish Settings

Click “Publish Settings” - this makes your configuration available to the API
4

Enable API Access

Toggle “Enable API Access” and click “Generate API Key”
5

Copy Your Credentials

Save your API key (starts with tk_) and chat ID
Keep your API key secret! Store it in environment variables, never commit it to source control.

Step 3: Make Your First Query

Choose your preferred language and make your first API call:
# Replace with your actual credentials
export TRAINLY_API_KEY="tk_your_api_key"
export TRAINLY_CHAT_ID="chat_abc123"

curl -X POST https://api.trainlyai.com/v1/${TRAINLY_CHAT_ID}/answer_question \
  -H "Authorization: Bearer ${TRAINLY_API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "question": "What are the main points in my documents?"
  }'

Step 4: Add Streaming (Optional)

Make your app more responsive with real-time streaming:
// Stream responses in real-time
for await (const chunk of trainly.queryStream({
  question: "Explain the methodology in detail"
})) {
  if (chunk.type === 'content') {
    process.stdout.write(chunk.data);
  } else if (chunk.type === 'end') {
    console.log('\n\nComplete!');
  }
}

What You’ve Built

Congratulations! You now have:

Knowledge Base

Your documents indexed and searchable via semantic search

AI Integration

AI models that can answer questions about your content

Citation System

Answers backed by source citations with relevance scores

API Access

Full programmatic access to query your knowledge base

Next Steps

Now that you have the basics working, explore more advanced features:

Enhance Your Application

Complete Tutorials

Build a Document Chat InterfaceCreate a full-featured chat application with:
  • Real-time streaming responses
  • File upload with drag & drop
  • Citation display
  • Conversation history
View Tutorial →

Common First Steps

Upload More Documents

const result = await trainly.uploadFile({
  file: './research_paper.pdf',
  scopeValues: {
    category: 'research',
    project_id: 'proj_123'
  }
});

console.log('Uploaded:', result.filename);

List Your Files

const files = await trainly.listFiles();

console.log(`Total: ${files.total_files} files`);
files.files.forEach(file => {
  console.log(`- ${file.filename} (${file.chunk_count} chunks)`);
});

Try Different Models

// Fast and cheap
const response1 = await trainly.query({
  question: "Quick summary?",
  model: "gpt-4o-mini"  // 1x credits
});

// High quality
const response2 = await trainly.query({
  question: "Detailed analysis?",
  model: "gpt-4o"  // 15x credits
});

Troubleshooting

Check these:
  • API key starts with tk_
  • API access is enabled in chat settings
  • Chat settings are published
  • Using correct chat ID
Solution: Go to chat settings → Verify API access is enabled → Regenerate key if needed
Possible causes:
  • No documents uploaded
  • Documents not processed yet (takes 10-30 seconds)
  • Published settings don’t include your documents
  • Question not relevant to your content
Solution: Upload documents → Wait for processing → Publish settings with context files
Default limits:
  • 60 requests/minute per API key
Solution: Implement exponential backoff in your retry logic or upgrade your plan
Check:
  • API key is valid and not expired
  • Using correct authorization header format: Authorization: Bearer tk_your_key
  • Chat has API access enabled
Solution: Verify credentials and regenerate API key if needed

Example Projects

Get inspired by complete working examples:

Resources

Get Help

Join the Community

Ask questions, share projects, and get help from the Trainly team and community
Other Support Channels:
Pro Tip: Start with gpt-4o-mini for development (1x credits), then switch to more powerful models like gpt-4o or claude-3-opus for production when you need higher quality responses.