Skip to content

Commit

Permalink
Run the Sidekick with different models (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickHeiner committed Oct 4, 2023
1 parent 96e2e4e commit 7e1bba3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 55 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.20.0",
"version": "0.21.0",
"volta": {
"extends": "../../package.json"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function present(conversationElement: ConversationMessage) {
export function getNextConversationStep(
messages: ConversationMessage[],
fullConversation: ConversationMessage[],
tools: UseToolsProps['tools']
tools?: UseToolsProps['tools']
) {
const shrinkableConversation = getShrinkableConversation(messages, fullConversation);
const lastMessage = messages[messages.length - 1];
Expand All @@ -101,7 +101,7 @@ export function getNextConversationStep(
/>
);
// If we are using a tool based on redacted functions, we don't want to redact it further
if (!(name in tools)) {
if (tools && !(name in tools)) {
return executedFunction;
}
// Function responses can potentially be very large. In that case, we need
Expand All @@ -121,7 +121,9 @@ export function getNextConversationStep(
case 'functionResponse':
return (
<RepairMdxInConversation>
<ChatCompletion functionDefinitions={updatedTools}>{shrinkableConversation}</ChatCompletion>
<ChatCompletion functionDefinitions={tools ? updatedTools : undefined}>
{shrinkableConversation}
</ChatCompletion>
</RepairMdxInConversation>
);
default:
Expand Down
61 changes: 13 additions & 48 deletions packages/ai-jsx/src/batteries/sidekick/platform/sidekick.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,11 @@
import { present } from './conversation.js';
import { UseTools } from './use-tools-eject.js';
import { SidekickSystemMessage } from './system-message.js';
import { OpenAI } from '../../../lib/openai.js';
import { UseToolsProps } from '../../use-tools.js';
import * as AI from '../../../index.js';
import { ConversationHistory, ShowConversation } from '../../../core/conversation.js';
import { MergeExclusive } from 'type-fest';

export type OpenAIChatModel = Exclude<Parameters<typeof OpenAI>[0]['chatModel'], undefined>;
export type ModelProvider = 'openai';
export type ChatModel = OpenAIChatModel;

/**
* This is not as type safe as it could be, but I'm fine with that because the type safety would have to be enforced
* at the API layer (e.g. req.body()), and even after we did that, I'm not convinceed we could actually assert to TS
* that we've validated the types.
*
* If the user passes a modelProvider that doesn't match the model, AI.JSX will throw an error at completion time.
*/
export function ModelProvider({
children,
modelProvider,
model,
}: {
children: AI.Node;
modelProvider: ModelProvider;
model: ChatModel;
}) {
switch (modelProvider) {
case 'openai':
return (
<OpenAI chatModel={model as OpenAIChatModel} temperature={0}>
{children}
</OpenAI>
);
default:
throw new Error(`Unknown model provider: ${modelProvider}`);
}
}

interface UniversalSidekickProps {
tools?: UseToolsProps['tools'];
systemMessage?: AI.Node;
Expand Down Expand Up @@ -109,20 +76,18 @@ export type SidekickProps = UniversalSidekickProps & OutputFormatSidekickProps;

export function Sidekick(props: SidekickProps) {
return (
<ModelProvider model="gpt-4-32k" modelProvider="openai">
<ShowConversation present={present}>
<UseTools tools={props.tools ?? {}} showSteps>
<SidekickSystemMessage
timeZone="America/Los_Angeles"
includeNextStepsRecommendations={props.includeNextStepsRecommendations ?? true}
outputFormat={props.outputFormat ?? 'text/mdx'}
userProvidedGenUIUsageExamples={props.genUIExamples}
userProvidedGenUIComponentNames={props.genUIComponentNames}
/>
<ConversationHistory />
{props.systemMessage}
</UseTools>
</ShowConversation>
</ModelProvider>
<ShowConversation present={present}>
<UseTools tools={props.tools ?? undefined} showSteps>
<SidekickSystemMessage
timeZone="America/Los_Angeles"
includeNextStepsRecommendations={props.includeNextStepsRecommendations ?? true}
outputFormat={props.outputFormat ?? 'text/mdx'}
userProvidedGenUIUsageExamples={props.genUIExamples}
userProvidedGenUIComponentNames={props.genUIComponentNames}
/>
<ConversationHistory />
{props.systemMessage}
</UseTools>
</ShowConversation>
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getNextConversationStep } from './conversation.js';
import { Converse } from '../../../core/conversation.js';
import { UseToolsProps } from '../../use-tools.js';
import { SetOptional } from 'type-fest';

export function UseTools(props: UseToolsProps) {
export function UseTools(props: SetOptional<UseToolsProps, 'tools'>) {
return (
<Converse reply={(messages, fullConversation) => getNextConversationStep(messages, fullConversation, props.tools)}>
{props.children}
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-jsx/src/lib/anthropic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function* AnthropicChatModel(
props: AnthropicChatModelProps,
{ render, getContext, logger, memo }: AI.ComponentContext
): AI.RenderableStream {
if ('functionDefinitions' in props) {
if ('functionDefinitions' in props && props.functionDefinitions) {
throw new AIJSXError(
'Anthropic does not support function calling, but function definitions were provided.',
ErrorCode.ChatModelDoesNotSupportFunctions,
Expand Down
8 changes: 7 additions & 1 deletion packages/docs/docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## 0.20.0
## 0.21.0

- `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.
- Fix bug in Anthropic's `ChatCompletion` where it was too aggressive in checking that `tools` don't exist.

## [0.20.0](https://github.com/fixie-ai/ai-jsx/commit/96e2e4e7ccca7d9bec7c417da42fb3eca26d2037)

- Remove `finalSystemMessageBeforeResponse` from `Sidekick` component. The `systemMessage` is now always given to the model as the last part of the context window.
- Remove other cruft from the built-in Sidekick system message.
Expand Down
1 change: 1 addition & 0 deletions packages/sidekick-github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"deploy": "fixie deploy"
},
"dependencies": {
"@types/node": "^20.8.2",
"ai-jsx": "*"
},
"files": [
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6602,6 +6602,13 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^20.8.2":
version: 20.8.2
resolution: "@types/node@npm:20.8.2"
checksum: 3da73e25d821bfcdb7de98589027e08bb4848e55408671c4a83ec0341e124b5313a0b20e1e4b4eff1168ea17a86f622ad73fcb04b761abd77496b9a27cbd5de5
languageName: node
linkType: hard

"@types/parse-json@npm:^4.0.0":
version: 4.0.0
resolution: "@types/parse-json@npm:4.0.0"
Expand Down Expand Up @@ -22995,6 +23002,7 @@ __metadata:
dependencies:
"@fixieai/sdk": "*"
"@tsconfig/node18": ^2.0.1
"@types/node": ^20.8.2
ai-jsx: "*"
typescript: ^5.1.3
languageName: unknown
Expand Down

3 comments on commit 7e1bba3

@vercel
Copy link

@vercel vercel bot commented on 7e1bba3 Oct 4, 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
ai-jsx-docs.vercel.app
docs.ai-jsx.com

@vercel
Copy link

@vercel vercel bot commented on 7e1bba3 Oct 4, 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 7e1bba3 Oct 4, 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-git-main-fixie-ai.vercel.app
ai-jsx-nextjs-demo.vercel.app
ai-jsx-nextjs-demo-fixie-ai.vercel.app

Please sign in to comment.