Skip to content

Commit

Permalink
Sidekick: Show citations (#373)
Browse files Browse the repository at this point in the history
Previously, this was hardcoded in. Now it's configurable.
  • Loading branch information
NickHeiner committed Oct 5, 2023
1 parent 7bfcd4f commit e6064f5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 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.0",
"version": "0.21.1",
"volta": {
"extends": "../../package.json"
},
Expand Down
8 changes: 5 additions & 3 deletions packages/ai-jsx/src/batteries/sidekick/platform/gen-ui.tsx
@@ -1,7 +1,7 @@
import { SidekickProps } from './sidekick.js';

// prettier-ignore
export function MdxUsageExamples({includeNextStepsRecommendations}: Pick<SidekickProps, 'includeNextStepsRecommendations'>) {
export function MdxUsageExamples({includeNextStepsRecommendations, useCitationCard}: Pick<SidekickProps, 'includeNextStepsRecommendations' | 'useCitationCard'>) {
return <>
Respond concisely, using MDX formatting to make your response
more readable and structured.
Expand All @@ -11,8 +11,10 @@ export function MdxUsageExamples({includeNextStepsRecommendations}: Pick<Sidekic
When you show the user a link, only show them links that you found from
searching the knowledge base. Do not make links up.

When you show users links to knowledge base, use the {'<Citation />'} component. Its props are:
{' interface CitationProps { title: string; href: string } '}
{useCitationCard && <>
When you show users links to knowledge base, use the {'<Citation />'} component. Its props are:
{' interface CitationProps { title: string; href: string } '}
</>}

{includeNextStepsRecommendations && <>
You may suggest follow-up ideas to the user, if they fall within the scope of
Expand Down
20 changes: 12 additions & 8 deletions packages/ai-jsx/src/batteries/sidekick/platform/sidekick.tsx
Expand Up @@ -29,21 +29,17 @@ type OutputFormatSidekickProps = MergeExclusive<
* A set of examples to the Sidekick instructing it how to emit MDX responses, when
* `outputFormat` is `text/mdx`.
*
* By default, the Sidekick will only be able to use the default set of
* MDX components in its vocabulary: `Citation` and `NextStepsButton`.
* If you wish to support additional MDX components, you must provide examples
* of how to use them here.
* Separately from that's passed here, the Sidekick will emit MDX components if the
* `includeNextStepsRecommendations` and `useCitationCard` props are set.
*/
genUIExamples?: AI.Node;

/**
* A set of component names that the Sidekick should be able to use in its
* MDX output, when `outputFormat` is `text/mdx`.
*
* By default, the Sidekick will only be able to use the default set of
* MDX components in its vocabulary: `Citation` and `NextStepsButton`.
* If you wish to support additional MDX components, you must provide their
* names here.
* Separately from that's passed here, the Sidekick will emit MDX components if the
* `includeNextStepsRecommendations` and `useCitationCard` props are set.
*/
genUIComponentNames?: string[];

Expand All @@ -61,6 +57,13 @@ type OutputFormatSidekickProps = MergeExclusive<
* Defaults to true.
*/
includeNextStepsRecommendations?: boolean;

/**
* If true, the Sidekick will emit a `Citation` component when it wants to cite a source. For example:
*
* <Citation title='How to cancel an order' href='https://docs.example.com/how-to-cancel-order' />
*/
useCitationCard?: boolean;
},
{
/**
Expand All @@ -81,6 +84,7 @@ export function Sidekick(props: SidekickProps) {
<SidekickSystemMessage
timeZone="America/Los_Angeles"
includeNextStepsRecommendations={props.includeNextStepsRecommendations ?? true}
useCitationCard={props.useCitationCard ?? true}
outputFormat={props.outputFormat ?? 'text/mdx'}
userProvidedGenUIUsageExamples={props.genUIExamples}
userProvidedGenUIComponentNames={props.genUIComponentNames}
Expand Down
40 changes: 33 additions & 7 deletions packages/ai-jsx/src/batteries/sidekick/platform/system-message.tsx
Expand Up @@ -5,7 +5,7 @@ import { Node } from '../../../index.js';
import { SidekickProps } from './sidekick.js';

export interface SidekickSystemMessageProps
extends Pick<SidekickProps, 'outputFormat' | 'includeNextStepsRecommendations'> {
extends Pick<SidekickProps, 'outputFormat' | 'includeNextStepsRecommendations' | 'useCitationCard'> {
timeZone: string;
userProvidedGenUIUsageExamples?: Node;
userProvidedGenUIComponentNames?: string[];
Expand All @@ -16,6 +16,7 @@ export function SidekickSystemMessage({
userProvidedGenUIUsageExamples,
userProvidedGenUIComponentNames,
includeNextStepsRecommendations,
useCitationCard,
outputFormat,
}: SidekickSystemMessageProps) {
const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
Expand All @@ -37,23 +38,48 @@ export function SidekickSystemMessage({
</SystemMessage>
);

const baseComponentNames = ['Citation'];
const baseComponentNames = [];
if (includeNextStepsRecommendations) {
baseComponentNames.push('NextStepsButton');
}
if (useCitationCard) {
baseComponentNames.push('Citation');
}

const allComponents = [...baseComponentNames, ...(userProvidedGenUIComponentNames ?? [])];

if (allComponents.length && outputFormat !== 'text/mdx') {
throw new Error(
`If you specify components for the Sidekick to use, you must set outputFormat to text/mdx. You specified components "${allComponents.join(
'", "'
)}", and outputFormat "${outputFormat}`
);
}

/**
* If the user passed text/mdx but did not pass any config that results in components being
* available, we'll set the outputFormat to text/markdown instead, so we can omit the MDX
* system message.
*/
let outputFormatToUse = outputFormat;
if (!allComponents.length && outputFormat === 'text/mdx') {
outputFormatToUse = 'text/markdown';
}

return (
<>
{dateTimeSystemMessage}
{responseFramingSystemMessage}
{outputFormat === 'text/plain' && <SystemMessage>Respond with plain text, without any markup.</SystemMessage>}
{outputFormat === 'text/markdown' && <SystemMessage>Respond with Markdown.</SystemMessage>}
{outputFormat === 'text/mdx' && (
{outputFormatToUse === 'text/markdown' && <SystemMessage>Respond with Markdown.</SystemMessage>}
{outputFormatToUse === 'text/mdx' && (
<MdxSystemMessage
componentNames={[...baseComponentNames, ...(userProvidedGenUIComponentNames ?? [])]}
componentNames={allComponents}
usageExamples={
<>
<MdxUsageExamples includeNextStepsRecommendations={includeNextStepsRecommendations} />
<MdxUsageExamples
includeNextStepsRecommendations={includeNextStepsRecommendations}
useCitationCard={useCitationCard}
/>
{userProvidedGenUIUsageExamples}
</>
}
Expand Down
6 changes: 5 additions & 1 deletion packages/docs/docs/changelog.md
@@ -1,6 +1,10 @@
# Changelog

## 0.21.0
## 0.21.1

- `Sidekick` now accepts a `useCitationCard` prop, which controls whether it will emit `<Citation />` MDX components.

## [0.21.0](https://github.com/fixie-ai/ai-jsx/commit/7e1bba39f37300363216e564448b640a12de8db1)

- `Sidekick` is no longer locked to GPT-4-32k. Now, it'll run with whatever model is set by the AI.JSX context.
- If you pass tools, make sure that the model supports native function calling, or you'll get an error.
Expand Down

3 comments on commit e6064f5

@vercel
Copy link

@vercel vercel bot commented on e6064f5 Oct 5, 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

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

@vercel
Copy link

@vercel vercel bot commented on e6064f5 Oct 5, 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-fixie-ai.vercel.app
ai-jsx-tutorial-nextjs.vercel.app
ai-jsx-tutorial-nextjs-git-main-fixie-ai.vercel.app

@vercel
Copy link

@vercel vercel bot commented on e6064f5 Oct 5, 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.