string formatting - Streaming text from openai in next.js - Stack Overflow

"Streaming Text Formatting" – Formatting text that arrives in chunks from a stream."Rea

"Streaming Text Formatting" – Formatting text that arrives in chunks from a stream. "Real-time Text Structuring"

I am creating a web app using next.js and openai

When I use openai api stream false it sends me output with markdown which is easy to structure using package

But in streaming text from openai using API, how can I format my text?

Please help :(

This is my code:

const handleSendMessage = async (content: MessageContent) => {  
   
         // ****
         // code ....
         // ****

        // major logic 
        const response = await fetch(`${process.env.NEXT_PUBLIC_AI4ALL_SERVER}openai/stream`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data),
        });
        if (!response.body) {
          throw new Error('No response body');
        } 
    
        const reader = response.body.getReader(); 
        const decoder = new TextDecoder(); 
        let buffer = ''; 
        while (true) {
          const { value, done } = await reader.read();
          if (done) break;
          
          const chunk = decoder.decode(value, { stream: true });
          buffer += chunk;
    
          
          const lines = buffer.split('\n');
          buffer = lines.pop() || ''; 
    
          for (const line of lines) {
            if (line.trim()) {
              // Remove "data:" prefix and clean up the text
              const cleanLine = line 
                .replace(/id:\s*\d+\s*/g, '')
                .replace(/^data:\s*/, '')   
                .replace(/\\n/g, '\n')     // Handle escaped newlines
                .replace(/\\\"/g, '"')     // Handle escaped quotes
                .replace(/\\t/g, '\t')     // Handle escaped tabs
                .replace(/\d+\\\nid:\s*\d+\\ndata:\s*/g, '') // Remove id markers  
                .replace(/\*\*/g, '') 
                .replace(/\`\`\`/g, '')         // Remove occurrences of **


                .trim();
    
                if (cleanLine) {
                  processFormattedLine(cleanLine)
                } 
                
            
                if (cleanedText) {
                  dispatch(
                    updateBotMessage({
                      id: botMessageId,
                      chunk: cleanedText,
                    }),
                  )
                  responseTokens = countTokens(cleanedText) + responseTokens
                  cleanedText = "" 
                }            
            }
          
          
        
    
      } catch (error) {
        console.error('Error in message processing:', error);
        setIsTyping(false);
        setDisable(false);
      } 
    
    
   
     
    
  } 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744298025a4567357.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信