Skip to content

Commit

Permalink
chore: use-ai-lib makes life easy (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmAzing129 authored Sep 9, 2024
1 parent fbd4f2e commit 85f7815
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 29 deletions.
50 changes: 25 additions & 25 deletions app/smart-form/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useToast } from '../components/ui/use-toast';
import { Textarea } from '../components/ui/textarea';
import { Toaster } from '../components/ui/toaster';
import { LoaderCircle, ClipboardPaste } from 'lucide-react';
import { useAIModel, AIModelProvider } from 'use-ai-lib';

const schema = z.object({
name: z.string({ description: 'Name' }),
Expand All @@ -44,7 +45,7 @@ Please let me know once the update has been completed. Thank you for your assist
Best regards,
Jeason`;

const SmartFormPage: React.FC<unknown> = () => {
const SmartForm: React.FC<unknown> = () => {
const [emailContent, setEmailContent] = React.useState(defaultEmailContent);
const { toast } = useToast();

Expand Down Expand Up @@ -74,7 +75,19 @@ const SmartFormPage: React.FC<unknown> = () => {
content: '',
});
const model = useSettingsModel(settingsForm);
const [loading, setLoading] = React.useState(false);

const [messages, setMessages] = React.useState<CoreMessage[]>([]);

const { isGenerating } = useAIModel(model, {
schema,
messages,
stream: true,
onSuccess: (chunk) => {
console.log(chunk);
form.reset(chunk);
}
});

const onGenerate = async () => {
const content = await navigator.clipboard.readText();

Expand All @@ -94,26 +107,7 @@ const SmartFormPage: React.FC<unknown> = () => {

const messages = [systemMessage, userMessage];

try {
setLoading(true);
const startTimestamp = Date.now();
const { partialObjectStream } = await streamObject({
model,
schema,
messages,
});
for await (const partialObject of partialObjectStream) {
console.log(partialObject);
form.reset(partialObject);
}
setLoading(false);
const cost = Date.now() - startTimestamp;
console.log('cost:', cost, 'ms');
} catch (error) {
console.warn(error);
} finally {
setLoading(false);
}
setMessages(messages);
};

return (
Expand Down Expand Up @@ -201,10 +195,10 @@ const SmartFormPage: React.FC<unknown> = () => {
Submit
</Button>

{loading ? (
{isGenerating ? (
<Button
className="ml-auto"
disabled={loading}
disabled={isGenerating}
type="button"
>
<LoaderCircle className="animate-spin size-3.5 mr-1" />{' '}
Expand All @@ -215,7 +209,7 @@ const SmartFormPage: React.FC<unknown> = () => {
type="button"
className="ml-auto"
onClick={onGenerate}
disabled={loading}
disabled={isGenerating}
>
<ClipboardPaste className="size-3.5 mr-1" /> Paste with AI
</Button>
Expand All @@ -230,4 +224,10 @@ const SmartFormPage: React.FC<unknown> = () => {
);
};

const SmartFormPage = () => {
return <AIModelProvider>
<SmartForm />
</AIModelProvider>
}

export default SmartFormPage;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
"@radix-ui/react-dropdown-menu": "^2.1.1",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-slider": "^1.2.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-tooltip": "^1.1.2",
"@tailwindcss/typography": "^0.5.14",
"@types/debug": "^4.1.12",
Expand Down Expand Up @@ -92,6 +92,7 @@
"tailwindcss-animate": "^1.0.7",
"tsup": "^8.2.4",
"typescript": "^5",
"use-ai-lib": "0.0.3-alpha",
"vaul": "^0.9.1",
"vercel": "^35.2.3",
"vitest": "^2.0.5",
Expand Down
Loading

0 comments on commit 85f7815

Please sign in to comment.