Documentation
¶
Index ¶
- Constants
- Variables
- func AmrToMp3(amrData []byte) ([]byte, error)
- func AmrToOgg(amrData []byte) ([]byte, error)
- func CheckUserChatExceed(userId string) bool
- func ContainsSequence(bits MsgTypeFlagBits) bool
- func ConvertToPNGFile(imageContent []byte) (*os.File, error)
- func DecreaseUserChat(userId string)
- func DetectAudioFormat(data []byte) string
- func DetectImageFormat(data []byte) string
- func DetectVideoMimeType(data []byte) string
- func DownloadFile(url string) ([]byte, error)
- func Failure(ctx context.Context, w http.ResponseWriter, r *http.Request, code int, ...)
- func FileRecognize(audioContent []byte) (string, error)
- func FileToMd5(filePath string) (string, error)
- func GetAbsPath(relPath string) string
- func GetAudioData(encoding string, data []byte) ([]byte, error)
- func GetAudioDataDetail(encoding string, data []byte, sampleRate int, channels int) ([]byte, error)
- func GetAvailImgType() []string
- func GetAvailRecType() []string
- func GetAvailTTSType() []string
- func GetAvailTxtType() []string
- func GetAvailVideoType() []string
- func GetDefaultLLMConfig() string
- func GetImgModel(t string) string
- func GetImgType(llmConf *param.LLMConfig) string
- func GetLLMProxyClient() *http.Client
- func GetRecType(llmConf *param.LLMConfig) string
- func GetRobotProxyClient() *http.Client
- func GetTTSModel(t string) string
- func GetTTSType(llmConf *param.LLMConfig) string
- func GetTailStartOffset(filePath string, lines int) (int64, error)
- func GetTxtModel(t string) string
- func GetTxtType(llmConf *param.LLMConfig) string
- func GetUsingImgModel(ty string, model string) string
- func GetUsingRecModel(ty string, model string) string
- func GetUsingTTSModel(ty string, model string) string
- func GetUsingTxtModel(ty string, model string) string
- func GetUsingVideoModel(ty string, model string) string
- func GetVideoModel(t string) string
- func GetVideoType(llmConf *param.LLMConfig) string
- func HandleIncomingAudio(data []byte)
- func HandleJsonBody(r *http.Request, v interface{}) error
- func MD5(input string) string
- func MP3ToOpus(mp3Path string) ([]byte, error)
- func MP4ToMP3(mp4Bytes []byte) ([]byte, error)
- func MapKeysToString(input interface{}) string
- func NormalizeHTTP(addr string) string
- func OGGToMP3(oggBytes []byte) ([]byte, error)
- func PCMDuration(fileSize, sampleRate, channels, bitDepth int) int
- func PCMToAMR(pcmData []byte, sampleRate int, channels int) ([]byte, error)
- func PCMToMP3(pcmBytes []byte, sampleRate int, channels int) ([]byte, error)
- func PCMToOGG(pcmBytes []byte, sampleRate int) ([]byte, error)
- func PCMToOpus(pcmData []byte, sampleRate int, channels int) ([]byte, error)
- func ParseInt(str string) int
- func RandomFilename(ext string) string
- func ReplaceCommand(content string, command string, botName string) string
- func SendAudio(c *websocket.Conn, sessionID string, data []byte) error
- func SendAudioFromWav(ctx context.Context, c *websocket.Conn, sessionID string, content []byte) error
- func SendSilenceAudio(c *websocket.Conn, sessionID string) error
- func SetStructFieldByJSONTag(s interface{}, key string, value interface{}) error
- func SilkToMp3(silkBytes []byte) ([]byte, error)
- func SilkToWav(silkBytes []byte) ([]byte, error)
- func StartConnection(conn *websocket.Conn) error
- func StartSession(conn *websocket.Conn, sessionID string, req *StartSessionPayload) error
- func Success(ctx context.Context, w http.ResponseWriter, r *http.Request, data interface{})
- func TruncateText(text string, maxBytes int) string
- func Unmarshal(data []byte, containsSequence ContainsSequenceFunc) (*Message, *BinaryProtocol, error)
- func Utf16len(s string) int
- func ValueToString(value interface{}) string
- func WavToPCMBytes(wavData []byte) ([]byte, error)
- type ASRPayload
- type AsrClient
- type AsrResponse
- type AudioConfig
- type BinaryProtocol
- func (p *BinaryProtocol) Clone() *BinaryProtocol
- func (p *BinaryProtocol) Compression() CompressionBits
- func (p *BinaryProtocol) HeaderSize() int
- func (p *BinaryProtocol) Marshal(msg *Message) ([]byte, error)
- func (p *BinaryProtocol) Serialization() SerializationBits
- func (p *BinaryProtocol) SetCompression(c CompressionBits, f CompressFunc)
- func (p *BinaryProtocol) SetHeaderSize(s HeaderSizeBits)
- func (p *BinaryProtocol) SetSerialization(s SerializationBits)
- func (p *BinaryProtocol) SetVersion(v VersionBits)
- func (p *BinaryProtocol) Version() int
- type ChatTTSTextPayload
- type ChatTextQueryPayload
- type CompressFunc
- type CompressionBits
- type CompressionType
- type ContainsSequenceFunc
- type ContentBlock
- type DialogPayload
- type HeaderSizeBits
- type LocationInfo
- type MediaItem
- type Message
- type MessageType
- type MessageTypeSpecificFlags
- type MsgType
- type MsgTypeFlagBits
- type ProtocolVersion
- type RequestAsr
- type Response
- type Result
- type SayHelloPayload
- type SerializationBits
- type SerializationType
- type StartSessionPayload
- type TTSPayload
- type Usage
- type UsageResponse
- type Utterance
- type VersionBits
- type Word
- type WsHeader
Constants ¶
const ( SuccessCode = 1000 PROTOCOL_VERSION = ProtocolVersion(0b0001) DEFAULT_HEADER_SIZE = 0b0001 PROTOCOL_VERSION_BITS = 4 HEADER_BITS = 4 MESSAGE_TYPE_BITS = 4 MESSAGE_TYPE_SPECIFIC_FLAGS_BITS = 4 MESSAGE_SERIALIZATION_BITS = 4 MESSAGE_COMPRESSION_BITS = 4 RESERVED_BITS = 8 // Message Type: CLIENT_FULL_REQUEST = MessageType(0b0001) CLIENT_AUDIO_ONLY_REQUEST = MessageType(0b0010) SERVER_FULL_RESPONSE = MessageType(0b1001) SERVER_ACK = MessageType(0b1011) SERVER_ERROR_RESPONSE = MessageType(0b1111) // Message Type Specific Flags NO_SEQUENCE = MessageTypeSpecificFlags(0b0000) // no check sequence POS_SEQUENCE = MessageTypeSpecificFlags(0b0001) NEG_SEQUENCE = MessageTypeSpecificFlags(0b0010) NEG_SEQUENCE_1 = MessageTypeSpecificFlags(0b0011) // Message Serialization NO_SERIALIZATION = SerializationType(0b0000) JSON = SerializationType(0b0001) THRIFT = SerializationType(0b0011) CUSTOM_TYPE = SerializationType(0b1111) // Message Compression NO_COMPRESSION = CompressionType(0b0000) GZIP = CompressionType(0b0001) CUSTOM_COMPRESSION = CompressionType(0b1111) )
Variables ¶
var DefaultAudioOnlyWsHeader = []byte{0x11, 0x20, 0x11, 0x00}
var DefaultFullClientWsHeader = []byte{0x11, 0x10, 0x11, 0x00}
version: b0001 (4 bits) header size: b0001 (4 bits) message type: b0001 (Full client request) (4bits) message type specific flags: b0000 (none) (4bits) message serialization method: b0001 (JSON) (4 bits) message compression: b0001 (gzip) (4bits) reserved data: 0x00 (1 byte)
var DefaultLastAudioWsHeader = []byte{0x11, 0x22, 0x11, 0x00}
Functions ¶
func CheckUserChatExceed ¶
func ContainsSequence ¶ added in v1.0.25
func ContainsSequence(bits MsgTypeFlagBits) bool
ContainsSequence reports whether a message type specific flag indicates messages with this kind of flag contain a sequence number in its serialized value. This determiner function should be used for common binary
func DecreaseUserChat ¶
func DecreaseUserChat(userId string)
func DetectAudioFormat ¶
func DetectImageFormat ¶
func DetectVideoMimeType ¶
func DownloadFile ¶
func FileRecognize ¶
func GetAbsPath ¶ added in v1.0.24
func GetAudioData ¶ added in v1.0.25
func GetAudioDataDetail ¶ added in v1.0.29
func GetAvailImgType ¶ added in v1.0.31
func GetAvailImgType() []string
func GetAvailRecType ¶ added in v1.0.31
func GetAvailRecType() []string
func GetAvailTTSType ¶ added in v1.0.32
func GetAvailTTSType() []string
func GetAvailTxtType ¶ added in v1.0.31
func GetAvailTxtType() []string
func GetAvailVideoType ¶ added in v1.0.31
func GetAvailVideoType() []string
func GetDefaultLLMConfig ¶ added in v1.0.31
func GetDefaultLLMConfig() string
func GetImgModel ¶ added in v1.0.31
func GetImgType ¶ added in v1.0.31
func GetLLMProxyClient ¶
func GetRecType ¶ added in v1.0.31
func GetRobotProxyClient ¶
func GetTTSModel ¶ added in v1.0.32
func GetTTSType ¶ added in v1.0.32
func GetTailStartOffset ¶ added in v1.0.30
func GetTxtModel ¶ added in v1.0.31
func GetTxtType ¶ added in v1.0.31
func GetUsingImgModel ¶ added in v1.0.31
func GetUsingRecModel ¶ added in v1.0.31
func GetUsingTTSModel ¶ added in v1.0.32
func GetUsingTxtModel ¶ added in v1.0.31
func GetUsingVideoModel ¶ added in v1.0.31
func GetVideoModel ¶ added in v1.0.31
func GetVideoType ¶ added in v1.0.31
func HandleIncomingAudio ¶ added in v1.0.25
func HandleIncomingAudio(data []byte)
func HandleJsonBody ¶
func MapKeysToString ¶
func MapKeysToString(input interface{}) string
func NormalizeHTTP ¶ added in v1.0.37
func PCMDuration ¶ added in v1.0.24
func RandomFilename ¶
func SendAudioFromWav ¶ added in v1.0.25
func SendSilenceAudio ¶ added in v1.0.25
func SetStructFieldByJSONTag ¶
func StartConnection ¶ added in v1.0.25
func StartSession ¶ added in v1.0.25
func StartSession(conn *websocket.Conn, sessionID string, req *StartSessionPayload) error
func TruncateText ¶ added in v1.0.19
func Unmarshal ¶ added in v1.0.25
func Unmarshal(data []byte, containsSequence ContainsSequenceFunc) (*Message, *BinaryProtocol, error)
Unmarshal deserializes the binary `data` into a Message and also returns the Binary
func ValueToString ¶
func ValueToString(value interface{}) string
func WavToPCMBytes ¶ added in v1.0.29
Types ¶
type ASRPayload ¶ added in v1.0.25
type ASRPayload struct {
Extra map[string]interface{} `json:"extra"`
}
type AsrClient ¶
type AsrClient struct {
Appid string
Token string
Cluster string
Workflow string
Format string
Codec string
SegSize int
Url string
}
func BuildAsrClient ¶
func BuildAsrClient() AsrClient
func (*AsrClient) ConstructRequest ¶
func (*AsrClient) RequestAsr ¶
func (client *AsrClient) RequestAsr(audioData []byte) (AsrResponse, error)
type AsrResponse ¶
type AudioConfig ¶ added in v1.0.25
type BinaryProtocol ¶ added in v1.0.25
type BinaryProtocol struct {
// contains filtered or unexported fields
}
BinaryProtocol implements the binary protocol serialization and deserialization used in Lab-Speech MDD, TTS, ASR, etc. services. For more details, read: https://bytedance.feishu.cn/docs/doccnT0t71J4LCQCS0cnB4Eca8D
func NewBinaryProtocol ¶ added in v1.0.25
func NewBinaryProtocol() *BinaryProtocol
NewBinaryProtocol returns a new BinaryProtocol instance.
func (*BinaryProtocol) Clone ¶ added in v1.0.25
func (p *BinaryProtocol) Clone() *BinaryProtocol
Clone returns a clone of current BinaryProtocol
func (*BinaryProtocol) Compression ¶ added in v1.0.25
func (p *BinaryProtocol) Compression() CompressionBits
Compression returns the bits value of protocol compression method.
func (*BinaryProtocol) HeaderSize ¶ added in v1.0.25
func (p *BinaryProtocol) HeaderSize() int
HeaderSize returns the protocol header size.
func (*BinaryProtocol) Marshal ¶ added in v1.0.25
func (p *BinaryProtocol) Marshal(msg *Message) ([]byte, error)
Marshal serializes the message to a sequence of binary data.
func (*BinaryProtocol) Serialization ¶ added in v1.0.25
func (p *BinaryProtocol) Serialization() SerializationBits
Serialization returns the bits value of protocol serialization method.
func (*BinaryProtocol) SetCompression ¶ added in v1.0.25
func (p *BinaryProtocol) SetCompression(c CompressionBits, f CompressFunc)
SetCompression sets the compression method.
func (*BinaryProtocol) SetHeaderSize ¶ added in v1.0.25
func (p *BinaryProtocol) SetHeaderSize(s HeaderSizeBits)
SetHeaderSize sets the protocol header size.
func (*BinaryProtocol) SetSerialization ¶ added in v1.0.25
func (p *BinaryProtocol) SetSerialization(s SerializationBits)
SetSerialization sets the serialization method.
func (*BinaryProtocol) SetVersion ¶ added in v1.0.25
func (p *BinaryProtocol) SetVersion(v VersionBits)
SetVersion sets the protocol version.
func (*BinaryProtocol) Version ¶ added in v1.0.25
func (p *BinaryProtocol) Version() int
Version returns the integral version value.
type ChatTTSTextPayload ¶ added in v1.0.25
type ChatTextQueryPayload ¶ added in v1.0.25
type ChatTextQueryPayload struct {
Content string `json:"content"`
}
type CompressFunc ¶ added in v1.0.25
CompressFunc defines the functional type that does the compression operation.
type CompressionBits ¶ added in v1.0.25
type CompressionBits uint8
CompressionBits defines the 4-bit compression method type.
const ( CompressionNone CompressionBits = 0 CompressionGzip CompressionBits = 0b1 CompressionCustom CompressionBits = 0b1111 )
Values that a CompressionBits variable can take.
type CompressionType ¶
type CompressionType byte
type ContainsSequenceFunc ¶ added in v1.0.25
type ContainsSequenceFunc func(MsgTypeFlagBits) bool
ContainsSequenceFunc defines the functional type that checks whether the MsgTypeFlagBits indicates the existence of a sequence number in serialized data. The background is that not all responses contain a sequence number, and whether a response contains one depends on the the message type specific flag bits. What makes it more complicated is that this dependency varies in each use case (eg, TTS protocol has its own dependency specification, more details at: https://bytedance.feishu.cn/docs/doccn8MD4cZHQuvobbtouWfUVsV).
type ContentBlock ¶ added in v1.0.35
type ContentBlock struct {
Type string // "text" or "image" or "video"
Content string // For Type="text"
Media MediaItem // For Type="media"
}
func ExtractContentBlocks ¶ added in v1.0.35
type DialogPayload ¶ added in v1.0.25
type HeaderSizeBits ¶ added in v1.0.25
type HeaderSizeBits uint8
HeaderSizeBits defines the 4-bit header-size type.
const ( HeaderSize4 HeaderSizeBits = iota + 1 HeaderSize8 HeaderSize12 HeaderSize16 )
Values that a HeaderSizeBits variable can take.
type LocationInfo ¶ added in v1.0.25
type LocationInfo struct {
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
City string `json:"city"`
Country string `json:"country"`
Province string `json:"province"`
District string `json:"district"`
Town string `json:"town"`
CountryCode string `json:"country_code"`
Address string `json:"address"`
}
type MediaItem ¶ added in v1.0.35
MediaItem struct holds the information for an extracted media link.
type Message ¶ added in v1.0.25
type Message struct {
Type MsgType
Event int32
SessionID string
ConnectID string
Sequence int32
ErrorCode uint32
// Raw payload (not Gzip compressed). BinaryMarshal will do the
// compression for you.
Payload []byte
// contains filtered or unexported fields
}
Message defines the general message content type.
func NewMessage ¶ added in v1.0.25
func NewMessage(msgType MsgType, typeFlag MsgTypeFlagBits) (*Message, error)
NewMessage returns a new Message instance of the given message type with the specific flag.
func NewMessageFromByte ¶ added in v1.0.25
NewMessageFromByte reads the byte as the message type and specific flag bits and composes a new Message instance from them.
func ReceiveMessage ¶ added in v1.0.25
*
- 结合api接入文档对二进制协议进行理解,上下行统一理解
- - header(4bytes)
- - (4bits)version(v1) + (4bits)header_size
- - (4bits)messageType + (4bits)messageTypeFlags
- -- 0001 CompleteClient | -- 0001 optional has sequence
- -- 0010 AudioOnlyClient | -- 0100 optional has event
- -- 1001 CompleteServer | -- 1111 optional has error code
- -- 1011 AudioOnlyServer | --
- - (4bits)payloadFormat + (4bits)compression
- - (8bits) reserve
- - payload
- - [optional 4 bytes] event
- - [optional] session ID
- -- (4 bytes)session ID len
- -- session ID data
- - (4 bytes)data len
- - data
func (*Message) TypeFlag ¶ added in v1.0.25
func (m *Message) TypeFlag() MsgTypeFlagBits
TypeFlag returns the message type specific flag.
type MessageType ¶
type MessageType byte
type MessageTypeSpecificFlags ¶
type MessageTypeSpecificFlags byte
type MsgType ¶ added in v1.0.25
type MsgType int32
MsgType defines message type which determines how the message will be serialized with the
const ( MsgTypeInvalid MsgType = iota MsgTypeFullClient MsgTypeAudioOnlyClient MsgTypeFullServer MsgTypeAudioOnlyServer MsgTypeFrontEndResultServer MsgTypeError MsgTypeServerACK = MsgTypeAudioOnlyServer )
Values that a MsgType variable can take.
type MsgTypeFlagBits ¶ added in v1.0.25
type MsgTypeFlagBits uint8
MsgTypeFlagBits defines the 4-bit message-type specific flags. The specific values should be defined in each specific usage scenario.
const ( // For common MsgTypeFlagNoSeq MsgTypeFlagBits = 0 // Non-terminal packet with no sequence MsgTypeFlagPositiveSeq MsgTypeFlagBits = 0b1 // Non-terminal packet with sequence > 0 MsgTypeFlagLastNoSeq MsgTypeFlagBits = 0b10 // last packet with no sequence MsgTypeFlagNegativeSeq MsgTypeFlagBits = 0b11 // last packet with sequence < 0 MsgTypeFlagWithEvent MsgTypeFlagBits = 0b100 // Payload contains event number (int32) )
Values that a MsgTypeFlagBits variable can take.
type ProtocolVersion ¶
type ProtocolVersion byte
type RequestAsr ¶
type RequestAsr interface {
// contains filtered or unexported methods
}
type SayHelloPayload ¶ added in v1.0.25
type SayHelloPayload struct {
Content string `json:"content"`
}
type SerializationBits ¶ added in v1.0.25
type SerializationBits uint8
SerializationBits defines the 4-bit serialization method type.
const ( SerializationRaw SerializationBits = 0 SerializationJSON SerializationBits = 0b1 << 4 SerializationThrift SerializationBits = 0b11 << 4 SerializationCustom SerializationBits = 0b1111 << 4 )
Values that a SerializationBits variable can take.
type SerializationType ¶
type SerializationType byte
type StartSessionPayload ¶ added in v1.0.25
type StartSessionPayload struct {
ASR ASRPayload `json:"asr"`
TTS TTSPayload `json:"tts"`
Dialog DialogPayload `json:"dialog"`
}
type TTSPayload ¶ added in v1.0.25
type TTSPayload struct {
Speaker string `json:"speaker"`
AudioConfig AudioConfig `json:"audio_config"`
}
type Usage ¶ added in v1.0.25
type Usage struct {
InputTextTokens int `json:"input_text_tokens"`
InputAudioTokens int `json:"input_audio_tokens"`
CachedTextTokens int `json:"cached_text_tokens"`
CachedAudioTokens int `json:"cached_audio_tokens"`
OutputTextTokens int `json:"output_text_tokens"`
OutputAudioTokens int `json:"output_audio_tokens"`
}
type UsageResponse ¶ added in v1.0.25
type UsageResponse struct {
Usage *Usage `json:"usage"`
}
func GetDialogUsage ¶ added in v1.0.25
func GetDialogUsage(data []byte) *UsageResponse
type VersionBits ¶ added in v1.0.25
type VersionBits uint8
VersionBits defines the 4-bit version type.
const ( Version1 VersionBits = (iota + 1) << 4 Version2 Version3 Version4 )
Values that a VersionBits variable can take.
type WsHeader ¶
type WsHeader struct {
ProtocolVersion ProtocolVersion
DefaultHeaderSize int
MessageType MessageType
MessageTypeSpecificFlags MessageTypeSpecificFlags
SerializationType SerializationType
CompressionType CompressionType
}