Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Observability + make streaming demo look better (#168)
Last night, I noticed the streaming demo wouldn't display any results until the whole thing had finished. I wanted to make the demo have an immediately-streaming text result, following by the UI, but that didn't work because everything appeared at the end. This PR includes a number of fixes I needed to make that work, as well as observability I added along the way. Gif of the UI changes: https://github.com/fixie-ai/ai-jsx/assets/829827/055baf67-b63c-4a31-be5d-e2ad7bc48401 To push the UI further, I think we'll want to do more engine-level stuff, like add the ability for AI to pass props. What I did here is pretty hacky.
- Loading branch information
1 parent
183f4bb
commit e2735fd
Showing
22 changed files
with
538 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* To make this demo work, comment out the `import 'server-only'` line in `ai-jsx/experimental/next`. | ||
*/ | ||
|
||
/** @jsxImportSource ai-jsx/react */ | ||
import * as AI from 'ai-jsx/experimental/next'; | ||
import { memo } from 'ai-jsx/core/memoize'; | ||
import { ChatCompletion, UserMessage } from 'ai-jsx/core/completion'; | ||
import { makeComponentMap } from 'ai-jsx/react/map'; | ||
import { pino } from 'pino'; | ||
import { PinoLogger } from 'ai-jsx/core/log'; | ||
|
||
function App() { | ||
const chatCompletion = memo( | ||
<ChatCompletion temperature={1}> | ||
<UserMessage>List five dog names</UserMessage> | ||
</ChatCompletion> | ||
); | ||
|
||
return ( | ||
<> | ||
{chatCompletion} | ||
<Slow delay={2000} /> | ||
</> | ||
); | ||
} | ||
|
||
async function Slow({ delay }: { delay: number }) { | ||
// By default, this demo will show that the tree stream waits for `Slow` to complete before rendering anything. | ||
// If we `yield ''`, the problem is solved. | ||
|
||
// yield '' | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
return ` returned after ${delay}`; | ||
} | ||
|
||
const pinoStdoutLogger = pino({ | ||
name: 'ai-jsx', | ||
level: process.env.loglevel ?? 'trace', | ||
transport: { | ||
target: 'pino-pretty', | ||
options: { | ||
colorize: true, | ||
}, | ||
}, | ||
}); | ||
|
||
const decoder = new TextDecoder(); | ||
|
||
const response = await AI.toReactStream(makeComponentMap({}), <App />, { | ||
logger: new PinoLogger(pinoStdoutLogger), | ||
}); | ||
const body = await response.body; | ||
const reader = body.getReader(); | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
process.stdout.write(decoder.decode(value)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
e2735fd
There was a problem hiding this comment.
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
e2735fd
There was a problem hiding this comment.
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
e2735fd
There was a problem hiding this comment.
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-git-main-fixie-ai.vercel.app
ai-jsx-docs-fixie-ai.vercel.app
docs.ai-jsx.com
ai-jsx-docs.vercel.app