-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev: implement message normalization
- Loading branch information
1 parent
e6ad54b
commit e9279ff
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { whatis } = require("../../../util/langutil"); | ||
|
||
module.exports = class Messages { | ||
static normalize_single_message (message, params = {}) { | ||
params = Object.assign({ | ||
role: 'user', | ||
}, params); | ||
|
||
if ( typeof message === 'string' ) { | ||
message = { | ||
content: [message], | ||
}; | ||
} | ||
if ( whatis(message) !== 'object' ) { | ||
throw new Error('each message must be a string or object'); | ||
} | ||
if ( ! message.role ) { | ||
message.role = params.role; | ||
} | ||
if ( ! message.content ) { | ||
throw new Error(`each message must have a 'content' property`); | ||
} | ||
if ( whatis(message.content) !== 'array' ) { | ||
message.content = [message.content]; | ||
} | ||
for ( let i=0 ; i < message.content.length ; i++ ) { | ||
if ( whatis(message.content[i]) === 'string' ) { | ||
message.content[i] = { | ||
type: 'text', | ||
text: message.content[i], | ||
}; | ||
} | ||
if ( whatis(message.content[i]) !== 'object' ) { | ||
throw new Error('each message content item must be a string or object'); | ||
} | ||
if ( ! message.content[i].type ) { | ||
message.content[i].type = 'text'; | ||
} | ||
} | ||
|
||
console.log('???', message) | ||
return message; | ||
} | ||
static normalize_messages (messages, params = {}) { | ||
for ( let i=0 ; i < messages.length ; i++ ) { | ||
messages[i] = this.normalize_single_message(messages[i], params); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { expect } = require('chai'); | ||
const Messages = require('./Messages.js'); | ||
|
||
describe('Messages', () => { | ||
describe('normalize_single_message', () => { | ||
const cases = [ | ||
{ | ||
name: 'string message', | ||
input: 'Hello, world!', | ||
output: { | ||
role: 'user', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: 'Hello, world!', | ||
} | ||
] | ||
} | ||
} | ||
]; | ||
for ( const tc of cases ) { | ||
it(`should normalize ${tc.name}`, () => { | ||
const output = Messages.normalize_single_message(tc.input); | ||
expect(output).to.deep.equal(tc.output); | ||
}); | ||
} | ||
}); | ||
}); |