Skip to content

Commit

Permalink
release 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
AmAzing129 committed Aug 28, 2024
1 parent e350385 commit 7855b21
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 43 deletions.
7 changes: 5 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function Home() {
prompt: msg,
});

console.log('data', data);

return (
<div
style={{
Expand All @@ -43,8 +45,9 @@ function Home() {
helloMessage={
'欢迎使用 ProChat ,我是你的专属机器人,这是我们的 Github:[ProChat](https://github.com/ant-design/pro-chat)'
}
// request={async (messages) => {
// }}
request={async (messages) => {
setMsg(messages[messages.length - 1].content);
}}
/>
)}
</div>
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "use-ai-lib",
"version": "0.0.1-alpha.5",
"description": "A React hooks library for building AI-powered apps.",
"version": "0.0.1",
"description": "A React hooks library for building AI-powered apps as simple as possible.",
"type": "module",
"source": "src/index.ts",
"main": "./dist/index.umd.js",
Expand All @@ -15,11 +15,13 @@
],
"scripts": {
"build": "microbundle --tsconfig tsconfig.build.json",
"dev": "microbundle watch --no-sourcemap",
"dev": "microbundle watch --no-sourcemap --tsconfig tsconfig.build.json",
"demo": "next dev",
"ci:publish": "pnpm publish -r"
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"@ai-sdk/ui-utils": "^0.0.39",
"@tanstack/react-query": "^5.52.1",
"ai": "^3.3.17",
Expand All @@ -36,8 +38,6 @@
"chrome-ai": "^1.11.1",
"microbundle": "^0.15.1",
"next": "^14.2.7",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"typescript": "^5.5.4"
},
"repository": {
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/queries/useGenerateObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function useGenerateObject(params: any, options?: Options) {
const query = useQuery<GenerateObjectResult<any>>({
queryKey: [
'generateObject',
params.system,
params.prompt,
JSON.stringify(params.messages),
],
Expand Down
7 changes: 1 addition & 6 deletions src/queries/useGenerateText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ export function useGenerateText(
options?: Options
) {
const query = useQuery<GenerateTextResult<any>>({
queryKey: [
'generateText',
params.system,
params.prompt,
JSON.stringify(params.messages),
],
queryKey: ['generateText', params.prompt, JSON.stringify(params.messages)],
queryFn: async () => generateText(params),
...options,
});
Expand Down
10 changes: 1 addition & 9 deletions src/queries/useStreamObject.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useQuery } from '@tanstack/react-query';
import { streamObject } from 'ai';
import type { JSONValue } from 'ai';
import { useEffect } from 'react';

// use some of options
type Options = {
Expand All @@ -10,15 +9,8 @@ type Options = {
};

export function useStreamObject(params: any, options?: Options) {
useEffect(() => {}, []);

const query = useQuery<JSONValue>({
queryKey: [
'streamObject',
params.system,
params.prompt,
JSON.stringify(params.messages),
],
queryKey: ['streamObject', JSON.stringify(params.messages)],
// @ts-ignore
queryFn: async () => {
const { partialObjectStream, object } = await streamObject(params);
Expand Down
7 changes: 1 addition & 6 deletions src/queries/useStreamText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ export function useStreamText(
options?: Options
) {
const query = useQuery<StreamTextResult<any>>({
queryKey: [
'generateText',
params.system,
params.prompt,
JSON.stringify(params.messages),
],
queryKey: ['generateText', params.prompt, JSON.stringify(params.messages)],
queryFn: async () => streamText(params),
...options,
});
Expand Down
22 changes: 15 additions & 7 deletions src/useAIModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import {
import { useEffect, useMemo } from 'react';

interface Options<D> extends Prompt {
deps?: any[];
schema?: z.Schema<D, z.ZodTypeDef, D> | Schema<D>;
stream?: boolean;
/**
* Do something when AI data is generated.
* Use this callback to get the stream data rather than 'data'.
* Use this callback to get the stream data rather than through 'data'.
*/
onSuccess?: (data: DeepPartial<D> | string | D) => void | boolean;
}
Expand All @@ -40,12 +39,21 @@ function useAIModel<D = string>(
aiModel: LanguageModel,
options: Options<D> = {}
) {
const { schema, onSuccess, stream, deps, ...prompt } = options;
const { schema, onSuccess, stream, ...prompt } = options;

const { model: contextModel } = useModelContext();

if (!aiModel && !contextModel) {
throw new Error('Model is required');
}

// Empty input is not allowed to pass to the moedel, or it will throw an error
const emptyInput =
!prompt ||
(!prompt.prompt && !prompt.messages) ||
(prompt.prompt && prompt.prompt.length === 0) ||
(prompt.messages && prompt.messages.length === 0);

const model = aiModel ?? contextModel;

// TODO: add middleware if model is a request
Expand All @@ -62,7 +70,7 @@ function useAIModel<D = string>(
...prompt,
},
{
enabled: !stream && !schema,
enabled: !stream && !schema && !emptyInput,
}
);

Expand All @@ -77,7 +85,7 @@ function useAIModel<D = string>(
...prompt,
},
{
enabled: stream && !schema,
enabled: !!stream && !schema && !emptyInput,
}
);

Expand All @@ -94,7 +102,7 @@ function useAIModel<D = string>(
...prompt,
},
{
enabled: !stream && !!schema,
enabled: !stream && !!schema && !emptyInput,
}
);

Expand All @@ -111,7 +119,7 @@ function useAIModel<D = string>(
},
{
onSuccess,
enabled: stream && !!schema,
enabled: !!stream && !!schema && !emptyInput,
}
);

Expand Down
6 changes: 5 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
"jsx": "react-jsx",
// https://github.com/developit/microbundle/issues/857
"jsxFactory": "",
"jsxFragmentFactory": ""
}
}

0 comments on commit 7855b21

Please sign in to comment.