File tree Expand file tree Collapse file tree 2 files changed +108
-0
lines changed Expand file tree Collapse file tree 2 files changed +108
-0
lines changed Original file line number Diff line number Diff line change 1+ import { formatChat } from '../chat'
2+
3+ describe ( 'formatChat' , ( ) => {
4+ it ( 'should format chat messages' , ( ) => {
5+ const messages = [
6+ {
7+ role : 'user' ,
8+ content : 'Hello, world!' ,
9+ } ,
10+ {
11+ role : 'bot' ,
12+ content : [
13+ {
14+ text : 'Hello, user!' ,
15+ } ,
16+ {
17+ text : 'How are you?' ,
18+ } ,
19+ ] ,
20+ } ,
21+ ]
22+
23+ const expected = [
24+ {
25+ role : 'user' ,
26+ content : 'Hello, world!' ,
27+ } ,
28+ {
29+ role : 'bot' ,
30+ content : 'Hello, user!\nHow are you?' ,
31+ } ,
32+ ]
33+
34+ expect ( formatChat ( messages ) ) . toEqual ( expected )
35+ } )
36+
37+ it ( 'should throw an error if the content is missing' , ( ) => {
38+ const messages = [
39+ {
40+ role : 'user' ,
41+ } ,
42+ ]
43+
44+ expect ( ( ) => formatChat ( messages ) ) . toThrowError (
45+ "Missing 'content' (ref: https://github.com/ggerganov/llama.cpp/issues/8367)" ,
46+ )
47+ } )
48+
49+ it ( 'should throw an error if the content type is invalid' , ( ) => {
50+ const messages = [
51+ {
52+ role : 'user' ,
53+ content : 42 ,
54+ } ,
55+ ]
56+
57+ expect ( ( ) => formatChat ( messages ) ) . toThrowError (
58+ "Invalid 'content' type (ref: https://github.com/ggerganov/llama.cpp/issues/8367)" ,
59+ )
60+ } )
61+ } )
Original file line number Diff line number Diff line change 1+ export type RNLlamaMessagePart = {
2+ text ?: string
3+ }
4+
5+ export type RNLlamaOAICompatibleMessage = {
6+ role : string
7+ content ?: string | RNLlamaMessagePart [ ] | any // any for check invalid content type
8+ }
9+
10+ export type RNLlamaChatMessage = {
11+ role : string
12+ content : string
13+ }
14+
15+ export function formatChat (
16+ messages : RNLlamaOAICompatibleMessage [ ] ,
17+ ) : RNLlamaChatMessage [ ] {
18+ const chat : RNLlamaChatMessage [ ] = [ ]
19+
20+ messages . forEach ( ( currMsg ) => {
21+ const role : string = currMsg . role || ''
22+
23+ let content : string = ''
24+ if ( 'content' in currMsg ) {
25+ if ( typeof currMsg . content === 'string' ) {
26+ ; ( { content } = currMsg )
27+ } else if ( Array . isArray ( currMsg . content ) ) {
28+ currMsg . content . forEach ( ( part ) => {
29+ if ( 'text' in part ) {
30+ content += `${ content ? '\n' : '' } ${ part . text } `
31+ }
32+ } )
33+ } else {
34+ throw new TypeError (
35+ "Invalid 'content' type (ref: https://github.com/ggerganov/llama.cpp/issues/8367)" ,
36+ )
37+ }
38+ } else {
39+ throw new Error (
40+ "Missing 'content' (ref: https://github.com/ggerganov/llama.cpp/issues/8367)" ,
41+ )
42+ }
43+
44+ chat . push ( { role, content } )
45+ } )
46+ return chat
47+ }
You can’t perform that action at this time.
0 commit comments