-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
326 additions
and
24 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,7 @@ | ||
--- | ||
"@livekit/agents": patch | ||
"@livekit/agents-plugin-openai": patch | ||
"livekit-agents-examples": patch | ||
--- | ||
|
||
add ChatContext |
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,136 @@ | ||
// SPDX-FileCopyrightText: 2024 LiveKit, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import type { AudioFrame } from '@livekit/rtc-node'; | ||
import type { CallableFunctionResult, FunctionContext } from './function_context.js'; | ||
|
||
export enum ChatRole { | ||
SYSTEM, | ||
USER, | ||
ASSISTANT, | ||
TOOL, | ||
} | ||
|
||
export interface ChatImage { | ||
image: string | AudioFrame; | ||
inferenceWidth?: number; | ||
inferenceHeight?: number; | ||
/** | ||
* @internal | ||
* Used by LLM implementations to store a processed version of the image for later use. | ||
*/ | ||
cache: { [id: string | number | symbol]: any }; | ||
} | ||
|
||
export interface ChatAudio { | ||
frame: AudioFrame | AudioFrame[]; | ||
} | ||
|
||
export type ChatContent = string | ChatImage | ChatAudio; | ||
|
||
const defaultCreateChatMessage = { | ||
text: '', | ||
images: [], | ||
role: ChatRole.SYSTEM, | ||
}; | ||
|
||
export class ChatMessage { | ||
readonly role: ChatRole; | ||
readonly id?: string; | ||
readonly name?: string; | ||
readonly content?: ChatContent | ChatContent[]; | ||
readonly toolCalls?: FunctionContext; | ||
readonly toolCallId?: string; | ||
readonly toolException?: Error; | ||
|
||
/** @internal */ | ||
constructor({ | ||
role, | ||
id, | ||
name, | ||
content, | ||
toolCalls, | ||
toolCallId, | ||
toolException, | ||
}: { | ||
role: ChatRole; | ||
id?: string; | ||
name?: string; | ||
content?: ChatContent | ChatContent[]; | ||
toolCalls?: FunctionContext; | ||
toolCallId?: string; | ||
toolException?: Error; | ||
}) { | ||
this.role = role; | ||
this.id = id; | ||
this.name = name; | ||
this.content = content; | ||
this.toolCalls = toolCalls; | ||
this.toolCallId = toolCallId; | ||
this.toolException = toolException; | ||
} | ||
|
||
static createToolFromFunctionResult(func: CallableFunctionResult): ChatMessage { | ||
if (!func.result && !func.error) { | ||
throw new TypeError('CallableFunctionResult must include result or error'); | ||
} | ||
|
||
return new ChatMessage({ | ||
role: ChatRole.TOOL, | ||
name: func.name, | ||
content: func.result || `Error: ${func.error}`, | ||
toolCallId: func.toolCallId, | ||
toolException: func.error, | ||
}); | ||
} | ||
|
||
static createToolCalls(toolCalls: FunctionContext, text = '') { | ||
return new ChatMessage({ | ||
role: ChatRole.ASSISTANT, | ||
toolCalls, | ||
content: text, | ||
}); | ||
} | ||
|
||
static create( | ||
options: Partial<{ | ||
text?: string; | ||
images: ChatImage[]; | ||
role: ChatRole; | ||
}>, | ||
): ChatMessage { | ||
const { text, images, role } = { ...defaultCreateChatMessage, ...options }; | ||
|
||
if (!images.length) { | ||
return new ChatMessage({ | ||
role: ChatRole.ASSISTANT, | ||
content: text, | ||
}); | ||
} else { | ||
return new ChatMessage({ | ||
role, | ||
content: [...(text ? [text] : []), ...images], | ||
}); | ||
} | ||
} | ||
|
||
/** Returns a structured clone of this message. */ | ||
copy(): ChatMessage { | ||
return structuredClone(this); | ||
} | ||
} | ||
|
||
export class ChatContext { | ||
messages: ChatMessage[] = []; | ||
metadata: { [id: string]: any } = {}; | ||
|
||
append(msg: { text?: string; images: ChatImage[]; role: ChatRole }): ChatContext { | ||
this.messages.push(ChatMessage.create(msg)); | ||
return this; | ||
} | ||
|
||
/** Returns a structured clone of this context. */ | ||
copy(): ChatContext { | ||
return structuredClone(this); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,11 +1,19 @@ | ||
// SPDX-FileCopyrightText: 2024 LiveKit, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
import { | ||
export { | ||
type CallableFunction, | ||
type CallableFunctionResult, | ||
type FunctionContext, | ||
type inferParameters, | ||
oaiParams, | ||
} from './function_context.js'; | ||
|
||
export { CallableFunction, FunctionContext, inferParameters, oaiParams }; | ||
export { | ||
type ChatImage, | ||
type ChatAudio, | ||
type ChatContent, | ||
ChatRole, | ||
ChatMessage, | ||
ChatContext, | ||
} from './chat_context.js'; |
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
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
Oops, something went wrong.