-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Chat Model Serialization #1226
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,29 +6,64 @@ import { | |
SystemChatMessage, | ||
} from "../../schema/index.js"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type AdditionalKwargs = Record<string, any>; | ||
export interface StoredMessageData { | ||
content: string; | ||
role: string | undefined; | ||
additional_kwargs?: AdditionalKwargs; | ||
} | ||
|
||
export interface StoredMessage { | ||
type: string; | ||
data: StoredMessageData; | ||
} | ||
|
||
interface StoredMessageV1 { | ||
type: string; | ||
role: string | undefined; | ||
text: string; | ||
} | ||
|
||
export function mapV1MessageToStoredMessage( | ||
message: StoredMessage | StoredMessageV1 | ||
): StoredMessage { | ||
if ("data" in message) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be some weird edge case where message has the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool thank you! |
||
return message as StoredMessage; | ||
} else { | ||
const v1Message = message as StoredMessageV1; | ||
return { | ||
type: v1Message.type, | ||
data: { | ||
content: v1Message.text, | ||
role: v1Message.role, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
export function mapStoredMessagesToChatMessages( | ||
messages: StoredMessage[] | ||
): BaseChatMessage[] { | ||
return messages.map((message) => { | ||
switch (message.type) { | ||
const stored_msg = mapV1MessageToStoredMessage(message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Camel case to match JS variable name style |
||
switch (stored_msg.type) { | ||
case "human": | ||
return new HumanChatMessage(message.text); | ||
return new HumanChatMessage(stored_msg.data.content); | ||
case "ai": | ||
return new AIChatMessage(message.text); | ||
return new AIChatMessage(stored_msg.data.content); | ||
case "system": | ||
return new SystemChatMessage(message.text); | ||
default: { | ||
if (message.role === undefined) { | ||
throw new Error("Role must be defined for generic messages"); | ||
return new SystemChatMessage(stored_msg.data.content); | ||
case "chat": | ||
if (stored_msg.data?.additional_kwargs?.role === undefined) { | ||
throw new Error("Role must be defined for chat messages"); | ||
} | ||
return new ChatMessage(message.text, message.role); | ||
} | ||
return new ChatMessage( | ||
stored_msg.data.content, | ||
stored_msg.data.additional_kwargs.role | ||
); | ||
default: | ||
throw new Error(`Got unexpected type: ${stored_msg.type}`); | ||
} | ||
}); | ||
} | ||
|
@@ -38,7 +73,9 @@ export function mapChatMessagesToStoredMessages( | |
): StoredMessage[] { | ||
return messages.map((message) => ({ | ||
type: message._getType(), | ||
role: "role" in message ? (message.role as string) : undefined, | ||
text: message.text, | ||
data: { | ||
content: message.text, | ||
role: "role" in message ? (message.role as string) : undefined, | ||
}, | ||
})); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { test, expect } from "@jest/globals"; | ||
import { mapV1MessageToStoredMessage } from "../message/utils.js"; | ||
|
||
test("mapV1MessageToStoredMessage", () => { | ||
// Test that a V1 message is upgraded. | ||
// Test that a v2 message is not changed. | ||
const v1Message = { | ||
type: "human", | ||
role: "user", | ||
text: "Hello, world!", | ||
}; | ||
const v2Message = { | ||
type: "human", | ||
data: { | ||
content: "Hello, world!", | ||
role: "user", | ||
}, | ||
}; | ||
expect(mapV1MessageToStoredMessage(v1Message)).toEqual(v2Message); | ||
|
||
const v2Message2 = { | ||
type: "human", | ||
data: { | ||
content: "Hello, world!", | ||
role: "user", | ||
additional_kwargs: { | ||
foo: "bar", | ||
}, | ||
}, | ||
}; | ||
expect(mapV1MessageToStoredMessage(v2Message2)).toEqual(v2Message2); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do feel like I'd rather shim this, mark it, then remove the shim when 1.0.0 comes out as a breaking change - I know all this is recent but have seen a few people using it already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jacoblee93 thoughts on the mapV1MessageToStoredMessage ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm for it! Just would add a comment saying to remove when ready to make a breaking change so we don't forget about it