# Usage Examples

### 1. Basic Text Conversations

```typescript
// Method 1: One-liner approach
const response1 = await ai.sendTextMessage('Tell me a joke about programming');

// Method 2: Conversation chain
const response2 = await ai
  .addUserMessage('Hello!')
  .addAssistantMessage('Hi there! How can I help you?')
  .addUserMessage('What is the capital of France?')
  .send();

// Method 3: Multiple messages at once
const response3 = await ai.sendMultipleMessages([
  { role: 'user', content: 'Hello' },
  { role: 'assistant', content: 'Hi! How can I help?' },
  { role: 'user', content: 'Tell me about TypeScript' }
]);
```

### 2. Image Analysis

```typescript
// Analyze image with text question
const imageResponse = await ai.sendImageMessage(
  'What do you see in this image? Describe the main elements.',
  'https://example.com/image.jpg'
);

// Or use the chain method
const imageAnalysis = await ai
  .addImageMessage('Analyze this image', 'https://example.com/photo.jpg')
  .send();
```

### 3. Streaming Responses

```typescript
// Stream response in real-time
const fullResponse = await ai.stream((chunk) => {
  process.stdout.write(chunk); // Print as it comes
});

// Streaming with custom options
const streamedText = await ai.stream(
  (chunk) => console.log('Chunk:', chunk),
  { temperature: 0.9, maxTokens: 500 }
);
```

### 4. System Prompts and Context

```typescript
// Set AI personality/behavior
const aiWithPersonality = new AIModel({
  apiKey: 'your-key',
  provider: 'openai'
}).setSystemPrompt(`
  You are a knowledgeable historian specializing in European history.
  Provide detailed, accurate information with dates and context.
  Be engaging but factual in your responses.
`);

const historyResponse = await aiWithPersonality
  .addUserMessage('Tell me about the Renaissance period')
  .send();
```

### 5. Configuration Management

```typescript
import { ConfigManager } from './ai-model-lib';

// Global configuration
const config = ConfigManager.getInstance();
config.setDefaultConfig({
  temperature: 0.7,
  maxTokens: 1500,
  debug: process.env.NODE_ENV === 'development',
  retryAttempts: 5
});

// Create pre-configured models
const ai = config.createModel({
  apiKey: process.env.OPENAI_API_KEY,
  model: 'gpt-4'
});
```

### 6. Error Handling and Retries

```typescript
try {
  const response = await ai
    .addUserMessage('Generate a long story about space exploration')
    .send({ maxTokens: 2000 });
    
  console.log('Success:', response.content);
  
} catch (error) {
  if (error.message.includes('API key')) {
    console.error('Authentication failed - check your API key');
  } else if (error.message.includes('rate limit')) {
    console.error('Rate limit exceeded - try again later');
  } else if (error.message.includes('timeout')) {
    console.error('Request timed out - check your connection');
  } else {
    console.error('Unexpected error:', error.message);
  }
}
```

### 7. Advanced Conversation Management

```typescript
// Complex conversation with context
const conversation = await ai
  .setSystemPrompt('You are a helpful travel assistant. Provide concise, practical advice.')
  .addUserMessage('I want to visit Japan next month')
  .addAssistantMessage('Great choice! Japan is wonderful. What cities are you thinking of visiting?')
  .addUserMessage('Tokyo and Kyoto for 10 days')
  .addAssistantMessage('Excellent itinerary. Tokyo for modern experiences, Kyoto for tradition.')
  .addUserMessage('What should I pack for April?')
  .send();

console.log('Travel advice:', conversation.content);

// Check conversation history
const messages = ai.getMessages();
messages.forEach((msg, index) => {
  console.log(`${index + 1}. ${msg.role}: ${msg.content}`);
});
```