qwen

package
v0.0.0-...-e1deba5 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PluginCodeInterpreter = "code_interpreter"
	PluginPDFExtracter    = "pdf_extracter"
)
View Source
const (
	DashScopeBaseURL     = "https://dashscope.aliyuncs.com/api"
	DashScopeIntlBaseURL = "https://dashscope-intl.aliyuncs.com/api"
	QwenSubURL           = "/v1/services/aigc/text-generation/generation"
	QwenVLSubURL         = "/v1/services/aigc/multimodal-generation/generation"
	QwenAudioSubURL      = QwenVLSubURL
)
View Source
const DefaultTemperature = 1.0

Variables

View Source
var ErrEmptyResponse = errors.New("empty response")
View Source
var ErrModelNotSet = errors.New("model is not set")

Functions

func URLQwen

func URLQwen(baseURL string) string

text-generation only.

func URLQwenAudio

func URLQwenAudio(baseURL string) string

func URLQwenVL

func URLQwenVL(baseURL string) string

multimodal.

func UploadFileFromURL

func UploadFileFromURL(ctx context.Context, fileURL, model, apiKey string, uploadCacher UploadCacher) (string, error)

download and uploading a online image to aliyun oss, a oss url will be returned.

func UploadLocalFile

func UploadLocalFile(ctx context.Context, filePath, model, apiKey string, uploadCacher UploadCacher) (string, error)

uploading local image to aliyun oss, a oss url will be returned.

Types

type AudioContent

type AudioContent struct {
	Audio string `json:"audio,omitempty"`
	Text  string `json:"text,omitempty"`
}

func (AudioContent) GetBlob

func (ac AudioContent) GetBlob() string

type AudioContentList

type AudioContentList []AudioContent

AudioContentList is used for multi-modal generation.

func NewAudioContentList

func NewAudioContentList() *AudioContentList

func (*AudioContentList) AppendText

func (acList *AudioContentList) AppendText(s string)

func (*AudioContentList) ConvertBackFromBlobList

func (acList *AudioContentList) ConvertBackFromBlobList(list []IBlobContent)

func (*AudioContentList) ConvertToBlobList

func (acList *AudioContentList) ConvertToBlobList() []IBlobContent

func (*AudioContentList) PopAudioContent

func (acList *AudioContentList) PopAudioContent() (AudioContent, bool)

func (*AudioContentList) SetAudio

func (acList *AudioContentList) SetAudio(url string)

func (*AudioContentList) SetBlob

func (acList *AudioContentList) SetBlob(url string)

func (*AudioContentList) SetText

func (acList *AudioContentList) SetText(s string)

func (*AudioContentList) ToBytes

func (acList *AudioContentList) ToBytes() []byte

func (*AudioContentList) ToString

func (acList *AudioContentList) ToString() string

type CertOutput

type CertOutput struct {
	Policy              string `json:"policy"`
	Signature           string `json:"signature"`
	UploadDir           string `json:"upload_dir"`
	UploadHost          string `json:"upload_host"`
	ExpireInSeconds     int    `json:"expire_in_seconds"`
	MaxFileSizeMB       int    `json:"max_file_size_mb"`
	CapacityLimitMB     int    `json:"capacity_limit_mb"`
	OSSAccessKeyID      string `json:"oss_access_key_id"`
	XOSSObjectACL       string `json:"x_oss_object_acl"`
	XOSSForbidOverwrite string `json:"x_oss_forbid_overwrite"`
}

type CertResponse

type CertResponse struct {
	RequestID string     `json:"request_id"`
	Data      CertOutput `json:"data"`
}

func (*CertResponse) JSONString

func (c *CertResponse) JSONString() string

type Choice

type Choice[T IQwenContent] struct {
	Message      Message[T]   `json:"message,omitempty"`
	Messages     []Message[T] `json:"messages,omitempty"` // TODO: 部分 plugin 会返回message列表.
	FinishReason string       `json:"finish_reason"`
}

type FileCacheInfo

type FileCacheInfo struct {
	URL        string
	UploadTime int64
}

==================== Sample MemoryFileCache ====================.

type FileContent

type FileContent struct {
	File string `json:"file,omitempty"`
	Text string `json:"text,omitempty"`
}

func (FileContent) GetBlob

func (vc FileContent) GetBlob() string

type FileContentList

type FileContentList []FileContent

FileContent currently used for pdf_extracter plugin only.

func NewFileContentList

func NewFileContentList() *FileContentList

func (*FileContentList) AppendText

func (fclist *FileContentList) AppendText(s string)

func (*FileContentList) ConvertBackFromBlobList

func (fclist *FileContentList) ConvertBackFromBlobList(list []IBlobContent)

func (*FileContentList) ConvertToBlobList

func (fclist *FileContentList) ConvertToBlobList() []IBlobContent

func (*FileContentList) PopFileContent

func (fclist *FileContentList) PopFileContent() (FileContent, bool)

func (*FileContentList) SetBlob

func (fclist *FileContentList) SetBlob(url string)

func (*FileContentList) SetFile

func (fclist *FileContentList) SetFile(url string)

func (*FileContentList) SetText

func (fclist *FileContentList) SetText(s string)

func (*FileContentList) ToBytes

func (fclist *FileContentList) ToBytes() []byte

func (*FileContentList) ToString

func (fclist *FileContentList) ToString() string

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

func (FunctionCall) GetArguments

func (f FunctionCall) GetArguments() map[string]map[string]string

API 接口返回的是 string, 这里转换为 map.

type IBlobContent

type IBlobContent interface {
	GetBlob() string
}

content with blob url: e.g. image, audio, file...

type IBlobListConvert

type IBlobListConvert interface {
	ConvertToBlobList() []IBlobContent
	ConvertBackFromBlobList(list []IBlobContent)
}

type IQwenContent

qwen(text-generation) and qwen-vl(multi-modal) have different data format so define generic interfaces for them.

type IQwenContentMethods

type IQwenContentMethods interface {
	ToBytes() []byte
	ToString() string
	SetText(text string)
	AppendText(text string)
	SetBlob(url string)
}

TODO: langchaingo 中有使用这个 interface, 稍后看看是否需要重新设计.

type Input

type Input[T IQwenContent] struct {
	Messages []Message[T] `json:"messages"`
}

type MemoryFileCache

type MemoryFileCache struct {
	MapFiles             map[string]*FileCacheInfo
	MaxFileCacheLifeTime time.Duration
}

MemoryFileCache is a simple in-memory-cache implementation for UploadCacher interface.

func NewMemoryFileCache

func NewMemoryFileCache() *MemoryFileCache

func (*MemoryFileCache) GetCache

func (mgr *MemoryFileCache) GetCache(buf []byte) string

func (*MemoryFileCache) SaveCache

func (mgr *MemoryFileCache) SaveCache(buf []byte, url string) error

type Message

type Message[T IQwenContent] struct {
	Role    string `json:"role"`
	Content T      `json:"content"`

	Name *string `json:"name,omitempty"` // plugin 和 function_call 中使用.
	// plugin parameters
	PluginCall *PluginCall `json:"plugin_call,omitempty"`
	// function call input parameters
	ToolCalls *[]ToolCalls `json:"tool_calls,omitempty"`
}

func (*Message[T]) HasToolCallInput

func (m *Message[T]) HasToolCallInput() bool

type ModelQwen

type ModelQwen = string
const (
	// text-generation model.
	QwenLong           ModelQwen = "qwen-long"
	QwenTurbo          ModelQwen = "qwen-turbo"
	QwenPlus           ModelQwen = "qwen-plus"
	QwenMax            ModelQwen = "qwen-max"
	QwenMax1201        ModelQwen = "qwen-max-1201"
	QwenMaxLongContext ModelQwen = "qwen-max-longcontext"

	// multi-modal model.
	QwenVLPlus     ModelQwen = "qwen-vl-plus"
	QwenVLMax      ModelQwen = "qwen-vl-max"
	QwenAudioTurbo ModelQwen = "qwen-audio-turbo"
)

type Output

type Output[T IQwenContent] struct {
	Choices []Choice[T] `json:"choices"`
}

new version response format.

type OutputResponse

type OutputResponse[T IQwenContent] struct {
	Output    Output[T] `json:"output"`
	Usage     Usage     `json:"usage"`
	RequestID string    `json:"request_id"`
}

func SendMessage

func SendMessage[T IQwenContent, U IQwenContent](ctx context.Context, payload *Request[T], cli httpclient.IHttpClient, url, token string) (*OutputResponse[U], error)

func SendMessageStream

func SendMessageStream[T IQwenContent, U IQwenContent](ctx context.Context, payload *Request[T], cli httpclient.IHttpClient, url, token string) (*OutputResponse[U], error)

func (*OutputResponse[T]) GetChoices

func (t *OutputResponse[T]) GetChoices() []Choice[T]

func (*OutputResponse[T]) GetRequestID

func (t *OutputResponse[T]) GetRequestID() string

func (*OutputResponse[T]) GetUsage

func (t *OutputResponse[T]) GetUsage() Usage

func (*OutputResponse[T]) HasToolCallInput

func (t *OutputResponse[T]) HasToolCallInput() bool

func (*OutputResponse[T]) ToJSONStr

func (t *OutputResponse[T]) ToJSONStr() string

type Parameters

type Parameters struct {
	ResultFormat      string  `json:"result_format,omitempty"`
	Seed              int     `json:"seed,omitempty"`
	MaxTokens         int     `json:"max_tokens,omitempty"`
	TopP              float64 `json:"top_p,omitempty"`
	TopK              int     `json:"top_k,omitempty"`
	Temperature       float64 `json:"temperature,omitempty"`
	EnableSearch      bool    `json:"enable_search,omitempty"`
	IncrementalOutput bool    `json:"incremental_output,omitempty"`
	Tools             []Tool  `json:"tools,omitempty"` // function call tools.
}

func DefaultParameters

func DefaultParameters() *Parameters

func NewParameters

func NewParameters() *Parameters

func (*Parameters) SetEnableSearch

func (p *Parameters) SetEnableSearch(value bool) *Parameters

func (*Parameters) SetIncrementalOutput

func (p *Parameters) SetIncrementalOutput(value bool) *Parameters

func (*Parameters) SetMaxTokens

func (p *Parameters) SetMaxTokens(value int) *Parameters

func (*Parameters) SetResultFormat

func (p *Parameters) SetResultFormat(value string) *Parameters

func (*Parameters) SetSeed

func (p *Parameters) SetSeed(value int) *Parameters

func (*Parameters) SetTemperature

func (p *Parameters) SetTemperature(value float64) *Parameters

func (*Parameters) SetTopK

func (p *Parameters) SetTopK(value int) *Parameters

func (*Parameters) SetTopP

func (p *Parameters) SetTopP(value float64) *Parameters

type PluginCall

type PluginCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // TODO: 临时使用string...后续设计通用 interface 方便自定义扩展.
}

func (*PluginCall) ToString

func (p *PluginCall) ToString() string

type Plugins

type Plugins map[string]map[string]any

type PropertieDefinition

type PropertieDefinition struct {
	Type        string `json:"type"`
	Description string `json:"description"`
}

type Request

type Request[T IQwenContent] struct {
	Model      string      `json:"model"`
	Input      Input[T]    `json:"input"`
	Parameters *Parameters `json:"parameters,omitempty"`
	// streaming callback function.
	StreamingFn StreamingFunc `json:"-"`
	// qwen-vl model need to upload image to oss for recognition.
	HasUploadOss bool `json:"-"`
	// plugin
	Plugins Plugins `json:"-"`
	// function_call
	Tools []Tool `json:"-"`
}

func (*Request[T]) SetInput

func (q *Request[T]) SetInput(value Input[T]) *Request[T]

func (*Request[T]) SetModel

func (q *Request[T]) SetModel(value string) *Request[T]

func (*Request[T]) SetParameters

func (q *Request[T]) SetParameters(value *Parameters) *Request[T]

func (*Request[T]) SetStreamingFunc

func (q *Request[T]) SetStreamingFunc(fn func(ctx context.Context, chunk []byte) error) *Request[T]

type RoleType

type RoleType = string
const (
	RoleSystem    RoleType = "system"
	RoleUser      RoleType = "user"
	RoleAssistant RoleType = "assistant"
	RolePlugin    RoleType = "plugin"
	RoleTool      RoleType = "tool"
)

type StreamOutput

type StreamOutput[T IQwenContent] struct {
	ID         string            `json:"id"`
	Event      string            `json:"event"`
	HTTPStatus int               `json:"http_status"`
	Output     OutputResponse[T] `json:"output"`
	Err        error             `json:"error"`
}

type StreamingFunc

type StreamingFunc func(ctx context.Context, chunk []byte) error

type TextContent

type TextContent struct {
	Text string
}

TextConent is used for text-generation only.

func NewTextContent

func NewTextContent() *TextContent

func (*TextContent) AppendText

func (t *TextContent) AppendText(text string)

func (TextContent) MarshalJSON

func (t TextContent) MarshalJSON() ([]byte, error)

redifine MarshalJSON and UnmarshalJSON.

func (*TextContent) SetBlob

func (t *TextContent) SetBlob(_ string)

func (*TextContent) SetText

func (t *TextContent) SetText(text string)

func (*TextContent) ToBytes

func (t *TextContent) ToBytes() []byte

func (*TextContent) ToString

func (t *TextContent) ToString() string

func (*TextContent) UnmarshalJSON

func (t *TextContent) UnmarshalJSON(data []byte) error

type Tool

type Tool struct {
	Type     string       `json:"type"`
	Function ToolFunction `json:"function"`
}

Tool represents a tool with its type and function details.

type ToolCallParameter

type ToolCallParameter struct {
	Type       string                         `json:"type"`
	Properties map[string]PropertieDefinition `json:"properties"`
}

type ToolCalls

type ToolCalls struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"`
	Function FunctionCall `json:"function"`
}

type ToolFunction

type ToolFunction struct {
	Name        string            `json:"name"`
	Description string            `json:"description"`
	Parameters  ToolCallParameter `json:"parameters,omitempty"` // Using interface{} to handle both empty parameters and structured ones
	Required    []string          `json:"required,omitempty"`
}

type UploadCacher

type UploadCacher interface {
	SaveCache(buf []byte, url string) error
	GetCache(buf []byte) string
}

UploadCacher is an interface for caching uploaded file url to prevent duplicate upload. By default we provide Sample MemoryFileCache as the implementation. Customize your own cache manager by implementing this interface.

type UploadRequest

type UploadRequest struct {
	File []byte `json:"file"`
}

type Usage

type Usage struct {
	TotalTokens  int `json:"total_tokens"`
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

type VLContent

type VLContent struct {
	Image string `json:"image,omitempty"`
	Text  string `json:"text,omitempty"`
}

func (VLContent) GetBlob

func (vc VLContent) GetBlob() string

type VLContentList

type VLContentList []VLContent

VLContentList is used for multi-modal generation.

func NewVLContentList

func NewVLContentList() *VLContentList

func (*VLContentList) AppendText

func (vlist *VLContentList) AppendText(s string)

func (*VLContentList) ConvertBackFromBlobList

func (vlist *VLContentList) ConvertBackFromBlobList(list []IBlobContent)

func (*VLContentList) ConvertToBlobList

func (vlist *VLContentList) ConvertToBlobList() []IBlobContent

func (*VLContentList) PopImageContent

func (vlist *VLContentList) PopImageContent() (VLContent, bool)

func (*VLContentList) SetBlob

func (vlist *VLContentList) SetBlob(url string)

func (*VLContentList) SetImage

func (vlist *VLContentList) SetImage(url string)

func (*VLContentList) SetText

func (vlist *VLContentList) SetText(s string)

func (*VLContentList) ToBytes

func (vlist *VLContentList) ToBytes() []byte

func (*VLContentList) ToString

func (vlist *VLContentList) ToString() string

type WrapMessageError

type WrapMessageError struct {
	Message string
	Cause   error
}

func (*WrapMessageError) Error

func (e *WrapMessageError) Error() string

Jump to

Keyboard shortcuts

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