@yk1028-test/ai-chat-supporter
Version:
AI Chat Supporter - TypeScript library for intelligent chat processing with LangChain integration
194 lines (153 loc) • 4.96 kB
Markdown
# AI Chat Supporter
A TypeScript AI chat library providing streamlined prompts and token-based length control.
## Key Features
- **Streamlined Prompts**: Compressed instructions for AI efficiency
- **Token-based Length Control**: max_tokens + automatic retry (minimum 30 characters)
- **Diverse Personas**: tech, expert, business, and personality-based personas
- **Enhanced Language Control**: Consistent Korean/English response guarantee
- **Multi-participant Conversations**: Handle conversations with multiple participants
- **LangChain Integration**: Leveraging the latest AI technology stack
## Installation
```bash
npm install @yk1028-test/ai-chat-supporter
```
## Basic Usage
### 1. Initialize ChatSupporter
```typescript
import { ChatSupporterFactory, InputDataFactory } from '@yk1028-test/ai-chat-supporter';
const chatSupporter = await ChatSupporterFactory.createInitialized({
provider: 'ollama',
providerConfig: {
model: 'gemma3:1b',
baseUrl: 'http://localhost:11434'
},
defaultLanguage: 'korean'
});
```
### 2. Basic Chat
```typescript
const input = InputDataFactory.createChatInput('What is artificial intelligence?');
const result = await chatSupporter.process(input, {
outputType: 'chat',
language: 'english'
});
console.log(result.message);
```
### 3. Length Limitation
```typescript
// 30 character limit (minimum value)
const shortResult = await chatSupporter.process(input, {
outputType: 'chat',
chatLength: 30
});
// 100 character limit
const longResult = await chatSupporter.process(input, {
outputType: 'chat',
chatLength: 100
});
```
### 4. Persona Usage
```typescript
// Technical expert tone
const techResult = await chatSupporter.process(input, {
outputType: 'chat',
persona: 'tech'
});
// Humorous tone
const humorousResult = await chatSupporter.process(input, {
outputType: 'chat',
persona: 'humorous'
});
```
### 5. Language Settings
```typescript
// Force Korean response
const koreanResult = await chatSupporter.process(input, {
outputType: 'chat',
language: 'korean'
});
// Force English response
const englishResult = await chatSupporter.process(input, {
outputType: 'chat',
language: 'english'
});
```
### 6. Multi-participant Conversations
```typescript
const multiInput = InputDataFactory.createMultiChatInput([
{ participantName: 'John', message: 'What should we have for lunch?' },
{ participantName: 'Alice', message: 'How about Korean food?' },
{ participantName: 'Bob', message: 'I prefer Chinese food' }
], { context: 'Lunch menu decision' });
const response = await chatSupporter.process(multiInput, {
outputType: 'chat'
});
```
### 7. Output Types
```typescript
// Concise chat response
const chatResult = await chatSupporter.process(input, {
outputType: 'chat'
});
// Detailed analysis response
const analysisResult = await chatSupporter.process(input, {
outputType: 'analysis'
});
```
## Processing Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `outputType` | `'chat' \| 'analysis'` | `'chat'` | Response format |
| `language` | `'korean' \| 'english'` | `'korean'` | Response language |
| `chatLength` | `number` | `50` | Maximum character count (minimum 30) |
| `persona` | `string` | `'default'` | Response tone/style |
| `useRAG` | `boolean` | `false` | Document reference usage |
## Supported Personas
### Role-based Personas
- `default`: General AI assistant
- `tech`: Technical expert
- `expert`: Domain expert
- `creative`: Creative writer
- `business`: Business consultant
- `educator`: Educator
- `counselor`: Counselor
### Personality-based Personas
- `energetic`: Energetic Supporter - High energy and active motivation
- `humorous`: Humorous Companion - Witty jokes and entertaining conversations
- `calm`: Calm Mentor - Stable and peaceful attitude
- `passionate`: Passionate Advocate - Strong convictions and active communication
- `gentle`: Gentle Guide - Soft and warm care
- `analytical`: Analytical Thinker - Logical reasoning and systematic analysis
- `optimistic`: Optimistic Dreamer - Positive perspective and hopeful thinking
- `thoughtful`: Thoughtful Advisor - Careful review and meticulous advice
## Length Control System
```typescript
// Processing flow:
// 1st: "Maximum 30 characters" (initial attempt)
// 2nd: "Length exceeded. Rewrite within 30 characters"
// 3rd: "Summarize to 30 characters: [previous response]"
// 4-5th: "More concise" (maximum 5 retries)
const result = await chatSupporter.process(input, {
outputType: 'chat',
chatLength: 30
});
console.log(result.metadata);
// {
// retryCount: 2,
// summarized: true,
// originalLength: 84
// }
```
## Error Handling
```typescript
try {
await chatSupporter.process(input, {
outputType: 'chat',
chatLength: 20 // Below 30 causes error
});
} catch (error) {
console.log(error.message); // "chatLength must be at least 30 characters."
}
```
## License
MIT