Download react-query/examples/react/chat/src/chat.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 865 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/chat/src/chat.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/chat/src/chat.ts
-
curl -L -o chat.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/examples/react/chat/src/chat.ts
865 Bytes
| import { | |
| queryOptions, | |
| experimental_streamedQuery as streamedQuery, | |
| } from '@tanstack/react-query' | |
| const answers = [ | |
| "I'm just an example chat, I can't really answer any questions :(".split(' '), | |
| 'TanStack is great. Would you like to know more?'.split(' '), | |
| ] | |
| function chatAnswer(_question: string) { | |
| return { | |
| async *[Symbol.asyncIterator]() { | |
| const answer = answers[Math.floor(Math.random() * answers.length)] | |
| let index = 0 | |
| while (index < answer.length) { | |
| await new Promise((resolve) => | |
| setTimeout(resolve, 100 + Math.random() * 300), | |
| ) | |
| yield answer[index++] | |
| } | |
| }, | |
| } | |
| } | |
| export const chatQueryOptions = (question: string) => | |
| queryOptions({ | |
| queryKey: ['chat', question], | |
| queryFn: streamedQuery({ | |
| queryFn: () => chatAnswer(question), | |
| }), | |
| staleTime: Infinity, | |
| }) | |