Skip to content

Commit

Permalink
Tweak <Sidekick> behavior (#400)
Browse files Browse the repository at this point in the history
- Move user system message to the start of the prompt
- Make MDX formatting logic conditional on using MDX
  • Loading branch information
petersalas committed Oct 12, 2023
1 parent 12a08e6 commit b533717
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/ai-jsx/package.json
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.21.2",
"version": "0.22.0",
"volta": {
"extends": "../../package.json"
},
Expand Down
18 changes: 9 additions & 9 deletions packages/ai-jsx/src/batteries/sidekick/platform/conversation.tsx
Expand Up @@ -15,6 +15,7 @@ import {
import { LargeFunctionResponseWrapper, redactedFunctionTools } from './large-response-handler.js';
import { ExecuteFunction, UseToolsProps } from '../../use-tools.js';
import _ from 'lodash';
import { SidekickOutputFormat } from './sidekick.js';

/**
* This function defines the shrinking policy. It's activated when the conversation history overflows the context
Expand Down Expand Up @@ -54,8 +55,8 @@ export function getShrinkableConversation(messages: ConversationMessage[], fullC
});
}

export function present(conversationElement: ConversationMessage) {
if (conversationElement.type === 'assistant') {
export function present(conversationElement: ConversationMessage, outputFormat: SidekickOutputFormat) {
if (conversationElement.type === 'assistant' && outputFormat === 'text/mdx') {
return (
<AssistantMessage>
<LimitToValidMdx>{conversationElement.element}</LimitToValidMdx>
Expand All @@ -78,6 +79,7 @@ export function present(conversationElement: ConversationMessage) {
export function getNextConversationStep(
messages: ConversationMessage[],
fullConversation: ConversationMessage[],
outputFormat: SidekickOutputFormat,
tools?: UseToolsProps['tools']
) {
const shrinkableConversation = getShrinkableConversation(messages, fullConversation);
Expand Down Expand Up @@ -118,14 +120,12 @@ export function getNextConversationStep(
*/
case 'system':
case 'user':
case 'functionResponse':
return (
<RepairMdxInConversation>
<ChatCompletion functionDefinitions={tools ? updatedTools : undefined}>
{shrinkableConversation}
</ChatCompletion>
</RepairMdxInConversation>
case 'functionResponse': {
const generation = (
<ChatCompletion functionDefinitions={tools ? updatedTools : undefined}>{shrinkableConversation}</ChatCompletion>
);
return outputFormat === 'text/mdx' ? <RepairMdxInConversation>{generation}</RepairMdxInConversation> : generation;
}
default:
return null;
}
Expand Down
35 changes: 24 additions & 11 deletions packages/ai-jsx/src/batteries/sidekick/platform/sidekick.tsx
@@ -1,14 +1,18 @@
import { present } from './conversation.js';
import { UseTools } from './use-tools-eject.js';
import { getNextConversationStep, present } from './conversation.js';
import { SidekickSystemMessage } from './system-message.js';
import { UseToolsProps } from '../../use-tools.js';
import * as AI from '../../../index.js';
import { ConversationHistory, ShowConversation } from '../../../core/conversation.js';
import { ConversationHistory, Converse, ShowConversation } from '../../../core/conversation.js';
import { MergeExclusive } from 'type-fest';

interface UniversalSidekickProps {
tools?: UseToolsProps['tools'];
systemMessage?: AI.Node;

/**
* The conversation to act on. If not specified, uses the <ConversationHistory /> component.
*/
children?: AI.Node;
}

type OutputFormatSidekickProps = MergeExclusive<
Expand Down Expand Up @@ -77,21 +81,30 @@ type OutputFormatSidekickProps = MergeExclusive<

export type SidekickProps = UniversalSidekickProps & OutputFormatSidekickProps;

export type SidekickOutputFormat = Exclude<SidekickProps['outputFormat'], undefined>;

export function Sidekick(props: SidekickProps) {
const outputFormat = props.outputFormat ?? 'text/mdx';
return (
<ShowConversation present={present}>
<UseTools tools={props.tools ?? undefined} showSteps>
<ShowConversation present={(msg) => present(msg, outputFormat)}>
<Converse
reply={(messages, fullConversation) =>
getNextConversationStep(messages, fullConversation, outputFormat, props.tools)
}
>
{props.systemMessage}
<SidekickSystemMessage
timeZone="America/Los_Angeles"
includeNextStepsRecommendations={props.includeNextStepsRecommendations ?? true}
useCitationCard={props.useCitationCard ?? true}
outputFormat={props.outputFormat ?? 'text/mdx'}
includeNextStepsRecommendations={
outputFormat === 'text/mdx' && (props.includeNextStepsRecommendations ?? true)
}
useCitationCard={outputFormat === 'text/mdx' && (props.useCitationCard ?? true)}
outputFormat={outputFormat}
userProvidedGenUIUsageExamples={props.genUIExamples}
userProvidedGenUIComponentNames={props.genUIComponentNames}
/>
<ConversationHistory />
{props.systemMessage}
</UseTools>
{props.children ?? <ConversationHistory />}
</Converse>
</ShowConversation>
);
}
Expand Up @@ -2,10 +2,12 @@ import { SystemMessage } from '../../../core/conversation.js';
import { MdxSystemMessage } from '../../../react/jit-ui/mdx.js';
import { MdxUsageExamples } from './gen-ui.js';
import { Node } from '../../../index.js';
import { SidekickProps } from './sidekick.js';
import { SidekickOutputFormat } from './sidekick.js';

export interface SidekickSystemMessageProps
extends Pick<SidekickProps, 'outputFormat' | 'includeNextStepsRecommendations' | 'useCitationCard'> {
export interface SidekickSystemMessageProps {
outputFormat: SidekickOutputFormat;
includeNextStepsRecommendations: boolean;
useCitationCard: boolean;
timeZone: string;
userProvidedGenUIUsageExamples?: Node;
userProvidedGenUIComponentNames?: string[];
Expand Down

This file was deleted.

4 comments on commit b533717

@vercel
Copy link

@vercel vercel bot commented on b533717 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-voice – ./packages/voice

ai-jsx-voice-fixie-ai.vercel.app
ai-jsx-voice-git-main-fixie-ai.vercel.app
voice.fixie.ai
ai-jsx-voice.vercel.app

@vercel
Copy link

@vercel vercel bot commented on b533717 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-docs – ./packages/docs

docs.ai-jsx.com
ai-jsx-docs-git-main-fixie-ai.vercel.app
ai-jsx-docs-fixie-ai.vercel.app
ai-jsx-docs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on b533717 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-tutorial-nextjs – ./packages/tutorial-nextjs

ai-jsx-tutorial-nextjs-git-main-fixie-ai.vercel.app
ai-jsx-tutorial-nextjs.vercel.app
ai-jsx-tutorial-nextjs-fixie-ai.vercel.app

@vercel
Copy link

@vercel vercel bot commented on b533717 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ai-jsx-nextjs-demo – ./packages/nextjs-demo

ai-jsx-nextjs-demo.vercel.app
ai-jsx-nextjs-demo-fixie-ai.vercel.app
ai-jsx-nextjs-demo-git-main-fixie-ai.vercel.app

Please sign in to comment.