qwen

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

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) (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) (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"`
}

type AudioContentList

type AudioContentList []AudioContent

func NewAudioContentList

func NewAudioContentList() *AudioContentList

func (*AudioContentList) AppendText

func (acList *AudioContentList) AppendText(s string)

func (*AudioContentList) PopAudioContent

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

func (*AudioContentList) SetAudio

func (acList *AudioContentList) SetAudio(url string)

func (*AudioContentList) SetImage

func (acList *AudioContentList) SetImage(_ 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"`
	FinishReason string     `json:"finish_reason"`
}

type IQwenContent

type IQwenContent interface {
	*TextContent | *VLContentList | *AudioContentList
	IQwenContentMethods
}

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)
	SetImage(url string)
	SetAudio(url string)
}

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

type Input

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

type Message

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

type ModelQwen

type ModelQwen = string
const (
	// text-generation model.
	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"
	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](ctx context.Context, payload *Request[T], cli httpclient.IHttpClient, url, token string) (*OutputResponse[T], error)

func SendMessageStream

func SendMessageStream[T IQwenContent](ctx context.Context, payload *Request[T], cli httpclient.IHttpClient, url, token string) (*OutputResponse[T], 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

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"`
}

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 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:"-"`
}

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 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) SetAudio

func (t *TextContent) SetAudio(_ string)

func (*TextContent) SetImage

func (t *TextContent) SetImage(_ 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 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"`
}

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) PopImageContent

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

func (*VLContentList) SetAudio

func (vlist *VLContentList) SetAudio(_ 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