onFinish | React #638
-
const useFoo = () => {
const [keyword, setKeyword] = React.useState<string>();
const res = useCompletion({
onFinish: () => {
console.log({ keyword }); // undefined | stale value
},
});
const { complete } = res;
const onTabChange = () => {
setKeyword("a");
complete("...",{ body: {...} });
};
return res;
}; |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
The final message is included in the callback:
|
Beta Was this translation helpful? Give feedback.
-
I think @MaxLeiter for useCompletion, there are two variables. (I could be misunderstanding the question) So for instance this works fine: export default function Completion() {
const {
completion,
input,
handleInputChange,
handleSubmit,
} = useCompletion({
api: '/api/completion/openai',
onFinish: (prompt: string, completion: string) => {
console.log('onFinish is', completion);
}
}); Using the example ... I just get the prompt back. |
Beta Was this translation helpful? Give feedback.
-
I came up with a workaround solution that solved my issue. It might help others. const useFoo = () => {
const [keyword, setKeyword] = useState<string>()
const [isFinished, setIsFinished] = useState(false)
const res = useCompletion({
onFinish: () => {
setKeyword("...")
setIsFinished(true)
},
});
const onAfterFinish=(prompt, completion)=> {
// do something...
}
useEffect(() => {
if (!isFinished || !keyword) return;
else {
onAfterFinish(term, res.completion);
setIsFinished(false);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isFinished, keyword])
const { complete } = res;
const onTabChange = () => {
setKeyword("a");
complete("...");
};
return res;
}; |
Beta Was this translation helpful? Give feedback.
-
Ah, I see, the question is actually about React. For what it's worth, you can get the same fresh variable with more efficient code (fewer re-renders) with React So, making it about both React and const [keyword, setKeyword] = React.useState<string>();
const keywordRef = React.useRef<string>();
// Update the ref whenever the state changes
React.useEffect(() => {
keywordRef.current = keyword;
}, [keyword]);
export default function Completion() {
const {
completion,
// ... callback helpers
} = useCompletion({
onFinish: (prompt: string, completion: string) => {
console.log('onFinish is completion is', completion); // thanks ai/react! :-)
console.log('fresh Keyword is', keyword); // thanks react!
// doUponCompletionHandler and so on here
}
});
|
Beta Was this translation helpful? Give feedback.
I think @MaxLeiter for useCompletion, there are two variables. (I could be misunderstanding the question)
So for instance this works fine:
Using the example
onFinish: (message) => console.log(message)
... I just get the prompt back.