Skip to content

Commit

Permalink
feat: trace completion types (PL-909) (#520)
Browse files Browse the repository at this point in the history
Introduce completion trace types for use in LLM responses.
The start trace will indicate which type of trace should be used, as well as any additional metadata it needs (voice for speak, delay for text).
The end trace signals it is done.

Co-authored-by: Tyler Stewart <git@tylerstewart.ca>
  • Loading branch information
trs and trs committed May 1, 2024
1 parent 82c3c81 commit 8ce3420
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 7 deletions.
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"private": true,
"workspaces": {
"packages": [
"packages/*",
"meta/*"
]
},
"workspaces": [
"packages/*"
],
"scripts": {
"build": "turbo run build:cmd",
"build:all": "yarn build",
Expand Down
3 changes: 3 additions & 0 deletions packages/base-types/src/node/utils/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export enum TraceType {
ENTITY_FILLING = 'entity-filling',
CHANNEL_ACTION = 'channel-action',
KNOWLEDGE_BASE = 'knowledgeBase',
COMPLETION_START = 'completion-start',
COMPLETION_CONTINUE = 'completion-continue',
COMPLETION_END = 'completion-end',
}

export interface BaseTraceFramePath<Event extends BaseEvent = BaseEvent> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { BaseTraceFrame } from '@base-types/node/utils';
import { TraceType } from '@base-types/node/utils';

export interface CompletionContinueTrace extends BaseTraceFrame<CompletionContinueTracePayload> {
type: TraceType.COMPLETION_CONTINUE;
}

export interface CompletionContinueTracePayload {
completion: string;
tokens?: {
answer: number;
query: number;
total: number;
};
}

export const isCompletionContinueTrace = (trace: BaseTraceFrame): trace is CompletionContinueTrace =>
trace.type === TraceType.COMPLETION_CONTINUE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { BaseTraceFrame } from '@base-types/node/utils';
import { TraceType } from '@base-types/node/utils';

export interface CompletionEndTrace extends BaseTraceFrame {
type: TraceType.COMPLETION_END;
}

export const isCompletionEndTrace = (trace: BaseTraceFrame): trace is CompletionEndTrace =>
trace.type === TraceType.COMPLETION_END;
46 changes: 46 additions & 0 deletions packages/base-types/src/trace/completion/completion-start-trace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { BaseTraceFrame } from '@base-types/node/utils';
import { TraceType } from '@base-types/node/utils';

export interface CompletionStartTrace extends BaseTraceFrame<BaseCompletionStartTracePayload> {
type: TraceType.COMPLETION_START;
}

export interface CompletionStartTraceSpeak extends CompletionStartTrace {
payload: CompletionStartTraceSpeakPayload;
}

export interface CompletionStartTraceText extends CompletionStartTrace {
payload: CompletionStartTraceTextPayload;
}

export type CompletionStartTracePayload = CompletionStartTraceSpeakPayload | CompletionStartTraceTextPayload;

export interface CompletionStartTraceSpeakPayload extends BaseCompletionStartTracePayload {
type: TraceType.SPEAK;
voice?: string;
}

export interface CompletionStartTraceTextPayload extends BaseCompletionStartTracePayload {
type: TraceType.TEXT;
delay?: number;
}

export interface BaseCompletionStartTracePayload {
type: TraceType.TEXT | TraceType.SPEAK;
completion: string;
tokens?: {
model: string;
answer: number;
query: number;
total: number;
};
}

export const isCompletionStartTrace = (trace: BaseTraceFrame): trace is CompletionStartTrace =>
trace.type === TraceType.COMPLETION_START;

export const isCompletionStartTraceSpeak = (trace: CompletionStartTrace): trace is CompletionStartTraceSpeak =>
trace.payload.type === TraceType.SPEAK;

export const isCompletionStartTraceText = (trace: CompletionStartTrace): trace is CompletionStartTraceText =>
trace.payload.type === TraceType.TEXT;
3 changes: 3 additions & 0 deletions packages/base-types/src/trace/completion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './completion-continue-trace';
export * from './completion-end-trace';
export * from './completion-start-trace';
14 changes: 13 additions & 1 deletion packages/base-types/src/trace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ import type { IntentRequest } from '@base-types/request';
import type { Log as RuntimeLog } from '@base-types/runtimeLogs';
import type { AnyRecord } from '@voiceflow/common';

import type { CompletionContinueTrace, CompletionEndTrace, CompletionStartTrace } from './completion';

export {
CompletionContinueTrace,
CompletionEndTrace,
CompletionStartTrace,
CompletionStartTraceSpeak,
CompletionStartTraceText,
} from './completion';
export { TraceFrame as CardV2 } from '@base-types/node/cardV2';
export { TraceFrame as Carousel } from '@base-types/node/carousel';
export { TraceFrame as End } from '@base-types/node/exit';
Expand Down Expand Up @@ -121,4 +130,7 @@ export type AnyTrace =
| CarouselTrace
| CardV2Trace
| EntityFillingTrace
| ChannelActionTrace;
| ChannelActionTrace
| CompletionStartTrace
| CompletionContinueTrace
| CompletionEndTrace;

0 comments on commit 8ce3420

Please sign in to comment.