qwen

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 25, 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"
	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() string

text-generation only.

func URLQwenAudio

func URLQwenAudio() string

func URLQwenVL

func URLQwenVL() 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 added in v0.0.4

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 added in v0.0.6

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

func (*AudioContentList) ConvertToBlobList added in v0.0.4

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 added in v0.0.4

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 added in v0.0.6

type FileCacheInfo struct {
	URL        string
	UploadTime int64
}

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

type FileContent added in v0.0.4

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

func (FileContent) GetBlob added in v0.0.4

func (vc FileContent) GetBlob() string

type FileContentList added in v0.0.4

type FileContentList []FileContent

FileContent currently used for pdf_extracter plugin only.

func NewFileContentList added in v0.0.4

func NewFileContentList() *FileContentList

func (*FileContentList) AppendText added in v0.0.4

func (fclist *FileContentList) AppendText(s string)

func (*FileContentList) ConvertBackFromBlobList added in v0.0.6

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

func (*FileContentList) ConvertToBlobList added in v0.0.4

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

func (*FileContentList) PopFileContent added in v0.0.4

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

func (*FileContentList) SetBlob added in v0.0.4

func (fclist *FileContentList) SetBlob(url string)

func (*FileContentList) SetFile added in v0.0.4

func (fclist *FileContentList) SetFile(url string)

func (*FileContentList) SetText added in v0.0.4

func (fclist *FileContentList) SetText(s string)

func (*FileContentList) ToBytes added in v0.0.4

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

func (*FileContentList) ToString added in v0.0.4

func (fclist *FileContentList) ToString() string

type FunctionCall added in v0.0.7

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

func (FunctionCall) GetArguments added in v0.0.7

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

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

type IBlobContent added in v0.0.4

type IBlobContent interface {
	GetBlob() string
}

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

type IBlobListConvert added in v0.0.4

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 added in v0.0.6

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

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

func NewMemoryFileCache added in v0.0.6

func NewMemoryFileCache() *MemoryFileCache

func (*MemoryFileCache) GetCache added in v0.0.6

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

func (*MemoryFileCache) SaveCache added in v0.0.6

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

type Message

type Message[T IQwenContent] struct {
	Role    string `json:"role"`
	Content T      `json:"content"`
	// plugin parameters
	Name       *string     `json:"name,omitempty"`
	PluginCall *PluginCall `json:"plugin_call,omitempty"`
	// function call input parameters
	ToolCalls *[]ToolCalls `json:"tool_calls,omitempty"`
}

func (*Message[T]) HasToolCallInput added in v0.0.7

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 added in v0.0.7

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

func (*OutputResponse[T]) ToJSONStr added in v0.0.5

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 added in v0.0.5

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

func (*PluginCall) ToString added in v0.0.5

func (p *PluginCall) ToString() string

type Plugins added in v0.0.4

type Plugins map[string]map[string]any

type PropertieDefinition added in v0.0.7

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 added in v0.0.4

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 added in v0.0.3

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

redifine MarshalJSON and UnmarshalJSON.

func (*TextContent) SetBlob added in v0.0.4

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 added in v0.0.3

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

type Tool added in v0.0.7

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

Tool represents a tool with its type and function details.

type ToolCallParameter added in v0.0.7

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

type ToolCalls added in v0.0.7

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

type ToolFunction added in v0.0.7

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 added in v0.0.6

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 added in v0.0.4

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 added in v0.0.6

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

func (*VLContentList) ConvertToBlobList added in v0.0.4

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

func (*VLContentList) PopImageContent

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

func (*VLContentList) SetBlob added in v0.0.4

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