diff --git a/docs/docs/apis-generate/chat.md b/docs/docs/apis-generate/chat.md index d5a147c..768615e 100644 --- a/docs/docs/apis-generate/chat.md +++ b/docs/docs/apis-generate/chat.md @@ -3,6 +3,7 @@ sidebar_position: 7 --- import CodeEmbed from '@site/src/components/CodeEmbed'; +import TypewriterTextarea from '@site/src/components/TypewriterTextarea'; # Chat @@ -52,7 +53,7 @@ You will get a response similar to: -::::tip[LLM Response] + + + ### Using a simple Console Output Stream Handler diff --git a/docs/docs/apis-generate/generate.md b/docs/docs/apis-generate/generate.md index f8f438b..463b7bb 100644 --- a/docs/docs/apis-generate/generate.md +++ b/docs/docs/apis-generate/generate.md @@ -3,6 +3,7 @@ sidebar_position: 1 --- import CodeEmbed from '@site/src/components/CodeEmbed'; +import TypewriterTextarea from '@site/src/components/TypewriterTextarea'; # Generate (Sync) @@ -25,14 +26,14 @@ You will get a response similar to: > I am a large language model created by Alibaba Cloud. My purpose is to assist users in generating text, answering > questions, and completing tasks. I aim to be user-friendly and easy to understand for everyone who interacts with me. :::: -> + ### Try asking a question, receiving the answer streamed You will get a response similar to: -::::tip[LLM Response] + + + ## Generate structured output diff --git a/docs/src/components/TypewriterTextarea/index.js b/docs/src/components/TypewriterTextarea/index.js new file mode 100644 index 0000000..78eb0a3 --- /dev/null +++ b/docs/src/components/TypewriterTextarea/index.js @@ -0,0 +1,66 @@ +import React, { useEffect, useState, useRef } from 'react'; + +const TypewriterTextarea = ({ textContent, typingSpeed = 50, pauseBetweenSentences = 1000, height = '200px', width = '100%' }) => { + const [text, setText] = useState(''); + const [sentenceIndex, setSentenceIndex] = useState(0); + const [charIndex, setCharIndex] = useState(0); + const sentences = textContent ? textContent.split('\n') : []; + const isTyping = useRef(false); + + useEffect(() => { + if (!textContent) return; + + if (!isTyping.current) { + isTyping.current = true; + } + + if (sentenceIndex >= sentences.length) { + // Reset to start from the beginning + setSentenceIndex(0); + setCharIndex(0); + setText(''); + return; + } + + const currentSentence = sentences[sentenceIndex]; + + if (charIndex < currentSentence.length) { + const timeout = setTimeout(() => { + setText((prevText) => prevText + currentSentence[charIndex]); + setCharIndex((prevCharIndex) => prevCharIndex + 1); + }, typingSpeed); + + return () => clearTimeout(timeout); + } else { + // Wait a bit, then go to the next sentence + const timeout = setTimeout(() => { + setSentenceIndex((prev) => prev + 1); + setCharIndex(0); + }, pauseBetweenSentences); + + return () => clearTimeout(timeout); + } + }, [charIndex, sentenceIndex, sentences, typingSpeed, pauseBetweenSentences, textContent]); + + return ( +