-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.js
More file actions
107 lines (88 loc) · 3.14 KB
/
Copy pathbasic-usage.js
File metadata and controls
107 lines (88 loc) · 3.14 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
/**
* Basic usage example for Harmony Protocol Node.js library
*/
import {
HarmonyEncoder,
HarmonyParser,
Conversation,
Message,
Role,
ReasoningLevel,
Channel
} from '../src/index.js';
// Create a basic conversation
const conversation = new Conversation([
Message.system("You are a helpful AI assistant with advanced reasoning capabilities."),
Message.developer("Show your reasoning process and provide structured responses."),
Message.user("What is 15% of 240? Show your work step by step.")
]);
console.log('🚀 Basic Harmony Protocol Example\n');
// 1. Display the conversation
console.log('📝 Conversation:');
console.log(conversation.toString());
console.log('\n');
// 2. Validate the conversation
const validation = conversation.validate();
console.log('✅ Validation:', validation.isValid ? 'PASSED' : 'FAILED');
if (!validation.isValid) {
console.log('Errors:', validation.errors);
}
console.log('\n');
// 3. Encode for gpt-oss model
const encoder = new HarmonyEncoder();
const encoded = encoder.encodeConversation(conversation, {
reasoning: ReasoningLevel.HIGH,
channels: [Channel.FINAL, Channel.ANALYSIS, Channel.COMMENTARY]
});
console.log('🔧 Encoded for gpt-oss:');
console.log('─'.repeat(60));
console.log(encoded);
console.log('─'.repeat(60));
console.log(`Token estimate: ~${encoder.estimateTokenCount(conversation)} tokens\n`);
// 4. Simulate model response (what would come back from gpt-oss)
const simulatedResponse = `<|start|>assistant<|message|>
<|reasoning|>high<|message|>
<|channel|>analysis<|message|>
Let me calculate 15% of 240 step by step.
First, I need to convert 15% to a decimal:
15% = 15/100 = 0.15
Now I multiply 240 by 0.15:
240 × 0.15 = 240 × (15/100) = (240 × 15)/100 = 3600/100 = 36
Let me verify this using an alternative method:
15% = 10% + 5%
10% of 240 = 24
5% of 240 = half of 10% = 12
So 15% = 24 + 12 = 36 ✓
<|end|>
<|channel|>commentary<|message|>
Used basic percentage calculation. Verified answer using decomposition method (10% + 5%). Both methods confirm the result.
<|end|>
<|channel|>final<|message|>
15% of 240 is 36.
**Solution:**
- Convert percentage: 15% = 0.15
- Multiply: 240 × 0.15 = 36
- Verification: 10% (24) + 5% (12) = 36 ✓
<|end|>
<|end|>`;
// 5. Parse the model response
const parser = new HarmonyParser();
const parsed = parser.parse(simulatedResponse);
console.log('📤 Parsed Response:');
console.log('Success:', parsed.success);
console.log('\nChannels:');
for (const [channel, content] of Object.entries(parsed.channels)) {
console.log(`\n${channel.toUpperCase()}:`);
console.log(content.trim());
}
console.log('\nMetadata:', parsed.metadata);
// 6. Get specific channel content
console.log('\n🎯 Quick Access:');
console.log('Final Answer:', parser.getFinalResponse(parsed));
console.log('Reasoning:', parser.getAnalysis(parsed) ? 'Available' : 'Not available');
console.log('Commentary:', parser.getCommentary(parsed) ? 'Available' : 'Not available');
// 7. Add the response to conversation
const assistantMessage = parser.toMessage(parsed);
conversation.addMessage(assistantMessage);
console.log('\n📊 Final Conversation Stats:');
console.log(conversation.getStats());