dashscopego

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 10 Imported by: 0

README

dashscopego

阿里云平台 dashscope api 的 golang 封装 (非官方)

开通DashScope并创建API-KEY

Install:

go get -u github.com/devinyf/dashscopego

Examples:

通义千问

通义万相

Paraformer(语音识别)

模型插件调用

langchaingo-Agent

  • TODO...

开发中...

import (
	"context"
	"fmt"
	"os"

	"github.com/devinyf/dashscopego"
	"github.com/devinyf/dashscopego/qwen"
)

func main() {
	model := qwen.QwenTurbo
	token := os.Getenv("DASHSCOPE_API_KEY")

	if token == "" {
		panic("token is empty")
	}

	cli := dashscopego.NewTongyiClient(model, token)

	content := qwen.TextContent{Text: "讲个冷笑话"}

	input := dashscopego.TextInput{
		Messages: []dashscopego.TextMessage{
			{Role: "user", Content: &content},
		},
	}

	// (可选 SSE开启) 需要流式输出时 通过该 Callback Function 获取实时显示的结果
	// 开启 SSE 时的 request_id/finish_reason/token usage 等信息在调用完成统一返回(resp)
	streamCallbackFn := func(ctx context.Context, chunk []byte) error {
		fmt.Print(string(chunk))
		return nil
	}
	req := &dashscopego.TextRequest{
		Input:       input,
		StreamingFn: streamCallbackFn,
	}

	ctx := context.TODO()
	resp, err := cli.CreateCompletion(ctx, req)
	if err != nil {
		panic(err)
	}

	fmt.Println("\nnon-stream result: ")
	fmt.Println(resp.Output.Choices[0].Message.Content.ToString())

	// request_id, finish_reason, token usage
	fmt.Println(resp.RequestID)
	fmt.Println(resp.Output.Choices[0].FinishReason)
	fmt.Println(resp.Usage.TotalTokens)
}

Documentation

Index

Constants

View Source
const (
	DashscopeTokenEnvName = "DASHSCOPE_API_KEY" //nolint:gosec
)

Variables

View Source
var (
	ErrModelNotSet     = errors.New("model is not set")
	ErrEmptyResponse   = errors.New("empty response")
	ErrImageFilePrefix = errors.New("file prefix is not supported, must be one of: file://, https://, http://")
)

Functions

func NewQwenMessage

func NewQwenMessage[T qwen.IQwenContent](role string, content T) *qwen.Message[T]

Types

type AudioInput

type AudioInput = qwen.Input[*qwen.AudioContentList]

type AudioMessage

type AudioMessage = qwen.Message[*qwen.AudioContentList]

type AudioQwenResponse

type AudioQwenResponse = qwen.OutputResponse[*qwen.AudioContentList]

type AudioRequest

type AudioRequest = qwen.Request[*qwen.AudioContentList]

type FileInput added in v0.0.4

type FileInput = qwen.Input[*qwen.FileContentList]

type FileMessage added in v0.0.4

type FileMessage = qwen.Message[*qwen.FileContentList]

type FileQwenResponse added in v0.0.4

type FileQwenResponse = qwen.OutputResponse[*qwen.TextContent] // PDF 文件解析返回的是纯文本格式.

type FileRequest added in v0.0.4

type FileRequest = qwen.Request[*qwen.FileContentList]

type TextInput

type TextInput = qwen.Input[*qwen.TextContent]

type TextMessage

type TextMessage = qwen.Message[*qwen.TextContent]

type TextQwenResponse

type TextQwenResponse = qwen.OutputResponse[*qwen.TextContent]

type TextRequest

type TextRequest = qwen.Request[*qwen.TextContent]

type TongyiClient

type TongyiClient struct {
	Model string
	// contains filtered or unexported fields
}

func NewTongyiClient

func NewTongyiClient(model string, token string) *TongyiClient

func (*TongyiClient) CreateAudioCompletion

func (q *TongyiClient) CreateAudioCompletion(ctx context.Context, payload *qwen.Request[*qwen.AudioContentList]) (*AudioQwenResponse, error)

func (*TongyiClient) CreateCompletion

func (q *TongyiClient) CreateCompletion(ctx context.Context, payload *qwen.Request[*qwen.TextContent]) (*TextQwenResponse, error)

duplicate: CreateCompletion and CreateVLCompletion are the same but with different payload types. maybe this can be change in the future.

nolint:lll

func (*TongyiClient) CreateEmbedding

func (q *TongyiClient) CreateEmbedding(ctx context.Context, r *embedding.Request) ([][]float32, error)

func (*TongyiClient) CreateFileCompletion added in v0.0.4

func (q *TongyiClient) CreateFileCompletion(ctx context.Context, payload *qwen.Request[*qwen.FileContentList]) (*FileQwenResponse, error)

used for pdf_extracter plugin.

func (*TongyiClient) CreateImageGeneration

func (q *TongyiClient) CreateImageGeneration(ctx context.Context, payload *wanx.ImageSynthesisRequest) ([]*wanx.ImgBlob, error)

TODO: intergrate wanx.Request into qwen.IQwenContent(or should rename to ITongyiContent)

func (*TongyiClient) CreateSpeechToTextGeneration

func (q *TongyiClient) CreateSpeechToTextGeneration(ctx context.Context, request *paraformer.Request, reader *bufio.Reader) error

func (*TongyiClient) CreateVLCompletion

func (q *TongyiClient) CreateVLCompletion(ctx context.Context, payload *qwen.Request[*qwen.VLContentList]) (*VLQwenResponse, error)

type VLInput

type VLInput = qwen.Input[*qwen.VLContentList]

type VLMessage

type VLMessage = qwen.Message[*qwen.VLContentList]

type VLQwenResponse

type VLQwenResponse = qwen.OutputResponse[*qwen.VLContentList]

type VLRequest

type VLRequest = qwen.Request[*qwen.VLContentList]

type WrapMessageError

type WrapMessageError struct {
	Message string
	Cause   error
}

func (*WrapMessageError) Error

func (e *WrapMessageError) Error() string

Directories

Path Synopsis
example
Code generated by MockGen.
Code generated by MockGen.

Jump to

Keyboard shortcuts

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