β οΈ Note: Not all examples are tested yet. Please use with caution and report any issues.
Complete TypeScript examples for the Rahyana AI API with full type safety, comprehensive error handling, and modern development practices. Build production-ready AI applications with confidence.
# Install dependencies npm install # Run interactive examples npm run dev # Run specific example npm run test:individual chat-completions/basic-chat.ts # Build all examples npm run build
- Complete TypeScript type definitions
- Compile-time error checking
- IntelliSense support
- Interface-based API design
- ES modules with TypeScript
- Async/await patterns
- Comprehensive error handling
- Performance monitoring
- Enterprise-grade code quality
- Comprehensive testing
- Security best practices
- Scalable architecture
Conversational AI with type safety
basic-chat.ts- Simple chat completions with TypeScript typesstreaming-chat.ts- Real-time streaming with progress trackingimage-analysis.ts- Multimodal image analysis (coming soon)audio-processing.ts- Audio processing and transcription (coming soon)pdf-processing.ts- PDF document analysis (coming soon)web-search.ts- Web search integration (coming soon)tool-calling.ts- Function calling with type safety (coming soon)json-mode.ts- Structured JSON responses (coming soon)
Production-ready applications
ai-content-generator.ts- Complete content generation systemai-chatbot.ts- Advanced chatbot with TypeScript (coming soon)ai-code-assistant.ts- Code generation and review tool (coming soon)
Intelligent development tools
ai-testing/ai-test-generator.ts- Intelligent test generationai-monitoring/ai-performance-monitor.ts- Performance monitoring (coming soon)ai-docs/ai-documentation-generator.ts- Documentation generation (coming soon)ai-review/ai-code-reviewer.ts- Code review assistant (coming soon)ai-devops/ai-cicd-assistant.ts- CI/CD automation (coming soon)
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "node",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"exactOptionalPropertyTypes": true
}
}{
"type": "module",
"scripts": {
"build": "tsc",
"dev": "tsx index.ts",
"test": "tsx test-all-examples.ts"
}
}import { createRahyanaClient } from './lib/rahyana-client.js'; import { ChatMessage } from './types/rahyana-api.js'; const client = createRahyanaClient({ apiKey: 'YOUR_API_KEY', baseUrl: 'https://rahyana.ir/api/v1' }); const messages: ChatMessage[] = [ { role: 'user', content: 'Hello, TypeScript!' } ]; const response = await client.chatCompletions({ model: 'openai/gpt-4o', messages, max_tokens: 100 }); console.log(response.choices[0].message.content);
for await (const chunk of client.streamChatCompletions({ model: 'openai/gpt-4o', messages, max_tokens: 200 })) { if (chunk.choices[0]?.message?.content) { process.stdout.write(chunk.choices[0].message.content); } }
import { AIContentGenerator } from './real-world-projects/ai-content-generator.js'; const generator = new AIContentGenerator({ apiKey: 'YOUR_API_KEY', baseUrl: 'https://rahyana.ir/api/v1' }); const blogPost = await generator.generateBlogPost( 'TypeScript Best Practices', { language: 'en', tone: 'technical', seoOptimized: true } ); console.log(blogPost.title); console.log(blogPost.content);
- Node.js 18+
- TypeScript 5+
- npm or yarn
# Clone the repository git clone https://github.com/rahyana/examples.git cd examples/typescript # Install dependencies npm install # Set up environment cp .env.example .env # Edit .env with your API key
# Interactive mode npm run dev # Run specific example npx tsx chat-completions/basic-chat.ts # Run all examples npm test # Build for production npm run build
interface ChatMessage { role: 'system' | 'user' | 'assistant'; content: string | ChatContent[]; } interface ChatCompletionRequest { model: string; messages: ChatMessage[]; max_tokens?: number; temperature?: number; stream?: boolean; } interface ChatCompletionResponse { id: string; choices: ChatChoice[]; usage: Usage; }
interface ClientConfig { apiKey: string; baseUrl: string; timeout?: number; retries?: number; headers?: Record<string, string>; }
try { const response = await client.chatCompletions(request); } catch (error) { if (error instanceof RahyanaError) { console.error('API Error:', error.message); } else { console.error('Network Error:', error); } }
const client = createRahyanaClient({ apiKey: 'YOUR_API_KEY', baseUrl: 'https://rahyana.ir/api/v1', retries: 3, timeout: 30000 });
const client = createRahyanaClient({ apiKey: 'YOUR_API_KEY', baseUrl: 'https://rahyana.ir/api/v1', headers: { 'X-Custom-Header': 'TypeScript-Client', 'User-Agent': 'MyApp/1.0.0' } });
# Run all tests npm test # Run specific test npm run test:individual ai-testing/ai-test-generator.ts # Run with coverage npm run test:coverage
describe('Chat Completions', () => { it('should generate valid responses', async () => { const response = await client.chatCompletions({ model: 'openai/gpt-4o', messages: [{ role: 'user', content: 'Hello' }], max_tokens: 10 }); expect(response.choices).toHaveLength(1); expect(response.choices[0].message.content).toBeDefined(); }); });
- Response Time: ~100ms average
- Type Checking: <1s compile time
- Memory Usage: Optimized for production
- Bundle Size: Minimal overhead
// Use specific types instead of 'any' const response: ChatCompletionResponse = await client.chatCompletions(request); // Enable strict mode // tsconfig.json: "strict": true // Use const assertions const config = { model: 'openai/gpt-4o', temperature: 0.7 } as const;
- Never commit API keys
- Use environment variables
- Validate all inputs
- Implement rate limiting
- Use HTTPS only
# .env
RAHYANA_API_KEY=your_api_key_here
RAHYANA_BASE_URL=https://rahyana.ir/api/v1- Fork the repository
- Create a feature branch
- Add TypeScript examples
- Write tests
- Submit a pull request
- Use TypeScript strict mode
- Follow ESLint rules
- Write comprehensive tests
- Document all functions
- Use meaningful names
MIT License - see LICENSE for details.
- Documentation: rahyana.ir/docs
- Issues: GitHub Issues
- Email: support@rahyana.ir
- Telegram: @rahyanabot
Build amazing AI applications with TypeScript and Rahyana API! π