Skip to content

Commit

Permalink
Improve prompt/completion logging
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalas committed Nov 20, 2023
1 parent b5e4366 commit 8f77198
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/ai-jsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"repository": "fixie-ai/ai-jsx",
"bugs": "https://github.com/fixie-ai/ai-jsx/issues",
"homepage": "https://ai-jsx.com",
"version": "0.27.1",
"version": "0.28.0",
"volta": {
"extends": "../../package.json"
},
Expand Down
47 changes: 40 additions & 7 deletions packages/ai-jsx/src/core/conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,45 @@ function toConversationMessages(partialRendering: AI.PartiallyRendered[]): Conve
});
}

async function loggableMessage(
message: ConversationMessage,
render: AI.RenderContext['render'],
measureCost?: (message: ConversationMessage, render: AI.ComponentContext['render']) => Promise<number>
) {
let textPromise: PromiseLike<string> | undefined = undefined;
switch (message.type) {
case 'user':
case 'assistant':
case 'system':
textPromise = render(message.element);
break;
case 'functionResponse':
textPromise = render(message.element.props.children);
break;
case 'functionCall':
break;
default: {
const neverMessage: never = message;
throw new Error(`Unexpected message type ${(neverMessage as any).type}`);
}
}

const costPromise = measureCost?.(message, render);
const loggableProps = { ...message.element.props };
if ('children' in loggableProps) {
delete loggableProps.children;
}

return {
// Use a function so that it doesn't serialize to JSON, but can be accessed if needed.
getElement: () => message.element,
type: message.type,
props: loggableProps,
text: await textPromise,
cost: await costPromise,
};
}

/** @hidden */
export async function renderToConversation(
conversation: AI.Node,
Expand All @@ -258,13 +297,7 @@ export async function renderToConversation(
const messages = toConversationMessages(await render(conversationToUse, { stop: isConversationalComponent }));

if (logger && logType) {
const loggableMessages = await Promise.all(
messages.map(async (m) => ({
element: debug(m.element, true),
...(cost && { cost: await cost(m, render) }),
}))
);

const loggableMessages = await Promise.all(messages.map((m) => loggableMessage(m, render, cost)));
logger.setAttribute(`ai.jsx.${logType}`, JSON.stringify(loggableMessages));
logger.info({ [logType]: { messages: loggableMessages } }, `Got ${logType} conversation`);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/docs/docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## 0.27.1
## 0.28.0

- Improved completion/prompt logging to include explicit message text

## [0.27.1](https://github.com/fixie-ai/ai-jsx/commit/b5e436615df37c7b68986059892d6043b684df18)

- Fix bug where memoized components could duplicate content
- Refactor `<Converse>` to allow rounds to progress in parallel when content allows
Expand Down

0 comments on commit 8f77198

Please sign in to comment.