streamtagparser

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

streamtagparser

Parse the tag data in the stream data returned by the LLM, such as the Artifact

codecov Sanity check

Installation

go get github.com/liushuangls/streamtagparser

Usage

package main

import (
	"github.com/liushuangls/streamtagparser"
)

// TagParser Non-concurrency safe, a TagParser can only be used for one stream
parser := streamtagparser.NewTagParser("Artifact")

mockStream := []string{
	"hello", "world <A", "rtifact", ` "id"=1>`,
	"local a", "=1", "</Ar", "tifact>end"
}

for _, data := range mockStream {
	tagStrams := parser.Parse(data)
	for _, tagStram := range tagStrams {
		fmt.Println(tagStram)
	}
}

// Sometimes the streaming data returned by the LLM may be incomplete, so finishing touches are needed
tagStreams := parser.ParseDone()
for _, tagStram := range tagStrams {
	fmt.Println(tagStram)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TagAttr

type TagAttr struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type TagParser

type TagParser struct {
	// contains filtered or unexported fields
}

TagParser Non-concurrency safe, a TagParser can only be used for one stream

func NewTagParser

func NewTagParser(needParsed ...string) *TagParser

func (*TagParser) Parse

func (p *TagParser) Parse(streamStr string) (tagsData []*TagStreamData)

func (*TagParser) ParseDone

func (p *TagParser) ParseDone() (tagsData []*TagStreamData)

type TagStreamData

type TagStreamData struct {
	Type TagStreamType `json:"type"`

	Text string `json:"text,omitempty"` // text is the content outside the tag

	TagName string    `json:"tag_name,omitempty"`
	Attrs   []TagAttr `json:"attrs,omitempty"`
	Content string    `json:"content,omitempty"` // content is the content of the tag
}

func NewContentTagStreamData

func NewContentTagStreamData(tagName, content string) *TagStreamData

func NewEndTagStreamData

func NewEndTagStreamData(tagName string, attrs []TagAttr, content string) *TagStreamData

func NewStartTagStreamData

func NewStartTagStreamData(tagName string, attrs []TagAttr) *TagStreamData

func NewTextTagStreamData

func NewTextTagStreamData(text string) *TagStreamData

type TagStreamType

type TagStreamType string
const (
	TagStreamTypeText    TagStreamType = "text"
	TagStreamTypeStart   TagStreamType = "start"
	TagStreamTypeContent TagStreamType = "content"
	TagStreamTypeEnd     TagStreamType = "end"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL