-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkimi-chat.ts
More file actions
136 lines (111 loc) · 3.24 KB
/
Copy pathkimi-chat.ts
File metadata and controls
136 lines (111 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
//this is a simple command-line chat interface for Kimi K2.5 (for PS).
//It has a conversational format.
//The script also includes error handling.
import * as readline from 'readline';
import { InferenceClient } from '@huggingface/inference';
// Configuration
const MODEL_ID = 'moonshotai/Kimi-K2-Instruct-0905';
const HF_TOKEN = process.env.HF_TOKEN;
// Initialize Hugging Face Inference client
const hf = new InferenceClient(HF_TOKEN);
// Interface for chat history
interface ChatMessage {
role: 'user' | 'assistant' | 'system';
content: string;
[key: string]: unknown;
}
// Chat history to maintain context
const conversationHistory: ChatMessage[] = [];
// System prompt (optional - you can customize this)
const SYSTEM_PROMPT = 'You are a helpful AI assistant.';
/**
* Send a message to Kimi K2.5 and get a response
*/
async function chat(userMessage: string): Promise<string> {
try {
// Add user message to history
conversationHistory.push({
role: 'user',
content: userMessage
});
// Prepare messages for the API
const messages = [
{ role: 'system' as const, content: SYSTEM_PROMPT },
...conversationHistory
];
// Call the Hugging Face Inference API
const data = await hf.chatCompletion({
model: MODEL_ID,
messages,
max_tokens: 2048,
temperature: 0.7,
stream: false
});
// Extract assistant's response
const assistantMessage = data.choices?.[0]?.message?.content || 'No response received.';
// Add assistant's response to history
conversationHistory.push({
role: 'assistant',
content: assistantMessage
});
return assistantMessage;
} catch (error) {
if (error instanceof Error) {
return `Error: ${error.message}`;
}
return 'An unknown error occurred.';
}
}
/**
* Main CLI loop
*/
async function main() {
console.log('=== Kimi K2.5 Chat ===');
console.log('Type your message and press Enter. Type "exit" or "quit" to end the conversation.\n');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'You: '
});
rl.prompt();
rl.on('line', async (line: string) => {
const userInput = line.trim();
// Exit commands
if (userInput.toLowerCase() === 'exit' || userInput.toLowerCase() === 'quit') {
console.log('\nGoodbye!');
rl.close();
process.exit(0);
}
// Clear history command
if (userInput.toLowerCase() === 'clear') {
conversationHistory.length = 0;
console.log('\nConversation history cleared.\n');
rl.prompt();
return;
}
// Skip empty input
if (!userInput) {
rl.prompt();
return;
}
// Get and display response
console.log('\nKimi: Thinking...\n');
const response = await chat(userInput);
console.log(`Kimi: ${response}\n`);
rl.prompt();
});
rl.on('close', () => {
console.log('\nSession ended.');
process.exit(0);
});
}
// Check for HF_TOKEN
if (!HF_TOKEN) {
console.error('Error: HF_TOKEN environment variable not set.');
console.error('Please set your Hugging Face token:');
console.error(' PowerShell: $env:HF_TOKEN="your_token_here"');
process.exit(1);
}
// Run the chat
main().catch(console.error);