minimax

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 12 Imported by: 0

README

Go MiniMax

Go Reference Go Report Card

The MiniMax unofficial Go library.

Installation

go get github.com/Twacqwq/go-minimax

go-minimax requires Go version 1.18 or greater.

Features

  • Chatcompletion pro
  • Chatcompletion
  • Embeddings
  • T2A
  • T2A pro
  • T2A large
  • T2A Stream
  • Voice Cloning
  • Assistants API
  • Files
  • Retrieval
  • Finetune API
  • Role Classification
  • Role Audio Generation

Usage

Minimax ChatCompletion Example:
package main

import (
	"context"
	"fmt"
	
	minimax "github.com/Twacqwq/go-minimax"
)

func main() {
    client := minimax.NewClient("your token", "your group id")
	resp, err := client.CreateCompletion(context.Background(), &minimax.ChatCompletionRequest{
		Model:            minimax.Abab5Dot5,
		TokensToGenerate: 1024,
		RoleMeta: &minimax.RoleMeta{
			UserName: "我",
			BotName:  "专家",
		},
		Prompt: "你是一个擅长发现故事中蕴含道理的专家,你很善于基于我给定的故事发现其中蕴含的道理。",
		Messages: []minimax.Message{
			{
				SenderType: minimax.ChatMessageRoleUser,
				Text:       "Please introduce yourself.",
			},
		},
	})
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v\n", resp.Reply)
}

Minimax ChatCompletionStream Example:
package main

import (
	"context"
	"errors"
	"fmt"
	"io"

	minimax "github.com/Twacqwq/go-minimax"
)

func main() {
    client := minimax.NewClient("your token", "your group id")
	stream, err := client.CreateCompletionStream(context.Background(), &minimax.ChatCompletionRequest{
		Model:            minimax.Abab5Dot5,
		TokensToGenerate: 1024,
		RoleMeta: &minimax.RoleMeta{
			UserName: "我",
			BotName:  "专家",
		},
		Prompt: "你是一个擅长发现故事中蕴含道理的专家,你很善于基于我给定的故事发现其中蕴含的道理。",
		Messages: []minimax.Message{
			{
				SenderType: minimax.ChatMessageRoleUser,
				Text:       "Please introduce yourself.",
			},
		},
	})
	if err != nil {
		panic(err)
	}
	defer stream.Close()

	for {
		resp, err := stream.Recv()
		if err != nil {
			if errors.Is(err, io.EOF) {
				break
			}
			panic(err)
		}
		fmt.Printf("%#v\n", resp)
	}
}

Minimax ChatCompletionPro Example:
package main

import (
	"context"
	"errors"
	"fmt"
	"io"

	minimax "github.com/Twacqwq/go-minimax"
)

func main() {
    client := minimax.NewClient("your token", "your group id")
	res, err := client.CreateCompletionPro(context.Background(), &minimax.ChatCompletionProRequest{
		Model:            minimax.Abab6,
		TokensToGenerate: 1024,
		Messages: []minimax.ProMessage{
			{
				SenderType: minimax.ChatMessageRoleUser,
				SenderName: "Twac",
				Text:       "请介绍一下你自己",
			},
		},
	})
	if err != nil {
		t.Fatal(err)
	}

	fmt.Printf("%#v\n", res)
}

Minimax ChatCompletionProStream Example:
package main

import (
	"context"
	"errors"
	"fmt"
	"io"

	minimax "github.com/Twacqwq/go-minimax"
)

func main() {
    client := minimax.NewClient("your token", "your group id")
	stream, err := client.CreateCompletionProStream(context.Background(), &minimax.ChatCompletionProRequest{
		Model:            minimax.Abab6,
		TokensToGenerate: 1024,
		Messages: []minimax.ProMessage{
			{
				SenderType: minimax.ChatMessageRoleUser,
				SenderName: "Twac",
				Text:       "请介绍一下你自己",
			},
		},
	})
	if err != nil {
		panic(err)
	}
	defer stream.Close()

	for {
		resp, err := stream.Recv()
		if err != nil {
			if errors.Is(err, io.EOF) {
				break
			}
			panic(err)
		}
		fmt.Printf("%#v\n", resp)
	}
}

Minimax CreateEmbeddings Example:
package main

import (
	"context"
	"fmt"
	
	minimax "github.com/Twacqwq/go-minimax"
)

func main() {
    client := minimax.NewClient("your token", "your group id")
	resp, err := client.CreateEmbeddings(context.Background(), &minimax.CreateEmbeddingsRequest{
		Texts: []string{"hello"},
		// Type: minimax.EmbeddingsDbType,
		Type:  minimax.EmbeddingsQueryType,
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", resp)
}

Minimax CreateTextToSpeech Example:
package main

import (
	"context"
	"fmt"

	minimax "github.com/Twacqwq/go-minimax"
)

func main() {
	client := minimax.NewClient("your token", "your group id")
	resp, err := client.CreateTextToSpeech(context.Background(), &minimax.CreateT2ARequest{
		Text:    "hello",
		VoiceId: "female-yujie",
		Path:    "./",
		Name:    "hello.mp3",
	})
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", resp)
}

Minimax CreateTextToSpeechPro Example:
package main

import (
	"context"
	"fmt"

	minimax "github.com/Twacqwq/go-minimax"
)

func main() {
	client := minimax.NewClient("your token", "your group id")
	resp, err := client.CreateTextToSpeechPro(context.Background(), &minimax.CreateT2ARequest{
		Text:    "hello",
		VoiceId: "female-yujie",
	})
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", resp)
}

Documentation

Index

Constants

View Source
const (
	Abab5       = "abab5-chat"
	Abab5Dot5   = "abab5.5-chat"
	Abab5Dot5s  = "abab5.5s-chat"
	Abab6       = "abab6-chat"
	Embo01      = "embo-01"
	Speech01    = "speech-01"
	Speech01Pro = "speech-01-pro" // alias speech-01=speech-01-pro

	ModelBot            = "MM智能助理"
	ChatMessageRoleUser = "USER"
	ChatMessageRoleBot  = "BOT"
	EmbeddingsDbType    = "db"
	EmbeddingsQueryType = "query"
)
View Source
const (
	APIV1 = "https://api.minimax.chat/v1"
)

Variables

View Source
var (
	ErrCompletionUnsupportedModel        = errors.New("this model is not supported with this method, please use CreateChatCompletion client method instead") //nolint:lll
	ErrCompletionStreamNotSupported      = errors.New("streaming is not supported with this method, please use CreateCompletionStream")                      //nolint:lll
	ErrTooManyEmptyStreamMessages        = errors.New("too many empty messages")
	ErrCreateTextToSpeechProNotSupported = errors.New("params is not supported, please use CreateTextToSpeechPro")
)

Functions

This section is empty.

Types

type BaseResp

type BaseResp struct {
	StatusCode int    `json:"status_code"`
	StatusMsg  string `json:"status_msg"`
}

type BotSetting

type BotSetting struct {
	BotName string `json:"bot_name"`
	Content string `json:"content"`
}

type ChatCompletionProRequest added in v1.1.0

type ChatCompletionProRequest struct {
	Model             string           `json:"model"`
	Messages          []ProMessage     `json:"messages"`
	BotSetting        []BotSetting     `json:"bot_setting"`
	SampleMessages    []Message        `json:"sample_messages,omitempty"`
	Stream            bool             `json:"stream,omitempty"`
	TokensToGenerate  int64            `json:"tokens_to_generate,omitempty"`
	Temperature       float32          `json:"temperature,omitempty"`
	TopP              float32          `json:"top_p,omitempty"`
	MaskSensitiveInfo bool             `json:"mask_sensitive_info,omitempty"`
	Functions         []*Function      `json:"functions,omitempty"`
	FunctionCall      *FunctionCall    `json:"function_call,omitempty"`
	ReplyConstraints  ReplyConstraints `json:"reply_constraints"`
	Plugins           []string         `json:"plugins"`
}

type ChatCompletionProResponse added in v1.1.0

type ChatCompletionProResponse struct {
	ID                  string                 `json:"id"`
	Created             int64                  `json:"created"`
	Model               string                 `json:"model"`
	Reply               string                 `json:"reply"`
	Choices             []ChatMessageProChoice `json:"choices"`
	Usage               Usage                  `json:"usage"`
	InputSensitive      bool                   `json:"input_sensitive,omitempty"`
	InputSensitiveType  int64                  `json:"input_sensitive_type,omitempty"`
	OutputSensitive     bool                   `json:"output_sensitive,omitempty"`
	OutputSensitiveType int64                  `json:"output_sensitive_type"`
	BaseResp            BaseResp               `json:"base_resp,omitempty"`
}

type ChatCompletionProStream added in v1.1.0

type ChatCompletionProStream struct {
	// contains filtered or unexported fields
}

func (ChatCompletionProStream) Close added in v1.1.0

func (stream ChatCompletionProStream) Close()

func (ChatCompletionProStream) Recv added in v1.1.0

func (stream ChatCompletionProStream) Recv() (T, error)

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model               string    `json:"model"`
	Messages            []Message `json:"messages"`
	Stream              bool      `json:"stream,omitempty"`
	Prompt              string    `json:"prompt"`
	TokensToGenerate    int64     `json:"tokens_to_generate,omitempty"`
	Temperature         float32   `json:"temperature,omitempty"`
	TopP                float32   `json:"top_p,omitempty"`
	UseStandardSSE      bool      `json:"use_standard_sse,omitempty"`
	BeamWidth           int       `json:"beam_width,omitempty"`
	RoleMeta            *RoleMeta `json:"role_meta"`
	ContinueLastMessage bool      `json:"continue_last_message"`
	SkipInfoMask        bool      `json:"skip_info_mask"`
}

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID                  string              `json:"id"`
	Created             int64               `json:"created"`
	Model               string              `json:"model"`
	Reply               string              `json:"reply"`
	Choices             []ChatMessageChoice `json:"choices"`
	Usage               Usage               `json:"usage"`
	InputSensitive      bool                `json:"input_sensitive,omitempty"`
	InputSensitiveType  int64               `json:"input_sensitive_type,omitempty"`
	OutputSensitive     bool                `json:"output_sensitive,omitempty"`
	OutputSensitiveType int64               `json:"output_sensitive_type"`
	BaseResp            BaseResp            `json:"base_resp,omitempty"`
}

type ChatCompletionStream

type ChatCompletionStream struct {
	// contains filtered or unexported fields
}

func (ChatCompletionStream) Close

func (stream ChatCompletionStream) Close()

func (ChatCompletionStream) Recv

func (stream ChatCompletionStream) Recv() (T, error)

type ChatMessageChoice

type ChatMessageChoice struct {
	Text         string `json:"text"`
	Index        int64  `json:"index"`
	FinishReason string `json:"finish_reason,omitempty"`
	Delta        string `json:"delta"`
}

type ChatMessageProChoice added in v1.1.0

type ChatMessageProChoice struct {
	FinishReason string       `json:"finish_reason,omitempty"`
	Index        int64        `json:"index"`
	Messages     []ProMessage `json:"messages"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(apiToken, groupId string) *Client

func NewClientWithConfig

func NewClientWithConfig(config *Config) *Client

func (*Client) CreateCompletion

func (c *Client) CreateCompletion(ctx context.Context, request *ChatCompletionRequest) (*ChatCompletionResponse, error)

func (*Client) CreateCompletionPro added in v1.1.0

func (c *Client) CreateCompletionPro(ctx context.Context, request *ChatCompletionProRequest, opts ...CompletionProOption) (*ChatCompletionProResponse, error)

func (*Client) CreateCompletionProStream added in v1.1.0

func (c *Client) CreateCompletionProStream(ctx context.Context, request *ChatCompletionProRequest, opts ...CompletionProOption) (*ChatCompletionProStream, error)

func (*Client) CreateCompletionStream

func (c *Client) CreateCompletionStream(ctx context.Context, request *ChatCompletionRequest) (*ChatCompletionStream, error)

func (*Client) CreateEmbeddings

func (c *Client) CreateEmbeddings(ctx context.Context, request *CreateEmbeddingsRequest) (*CreateEmbeddingsResponse, error)

func (*Client) CreateTextToSpeech

func (c *Client) CreateTextToSpeech(ctx context.Context, request *CreateT2ARequest) (*CreateT2AResponse, error)

func (*Client) CreateTextToSpeechPro

func (c *Client) CreateTextToSpeechPro(ctx context.Context, request *CreateT2ARequest) (*CreateT2AResponse, error)

type CompletionProOption added in v1.1.0

type CompletionProOption func(*ChatCompletionProRequest)

func WithBotSetting

func WithBotSetting(rolePrompt string, settings ...[]BotSetting) CompletionProOption

func WithReplyConstraints

func WithReplyConstraints(v ReplyConstraints) CompletionProOption

type Config

type Config struct {
	BaseURL           string
	HTTPClient        *http.Client
	EmptyMessageLimit uint
	// contains filtered or unexported fields
}

func DefaultConfig

func DefaultConfig(apiToken, groupId string) *Config

type CreateEmbeddingsRequest

type CreateEmbeddingsRequest struct {
	Model string `json:"model"`

	Texts []string `json:"texts"`
	Type  string   `json:"type"`
}

type CreateEmbeddingsResponse

type CreateEmbeddingsResponse struct {
	Vectors  [][]float32 `json:"vectors"`
	BaseResp BaseResp    `json:"base_resp"`
}

type CreateT2ARequest

type CreateT2ARequest struct {
	Model           string         `json:"model"`
	Text            string         `json:"text"`
	TimberWeights   []TimberWeight `json:"timber_weights,omitempty"`
	VoiceId         string         `json:"voice_id,omitempty"`
	Speed           float32        `json:"speed,omitempty"`
	Vol             float32        `json:"vol,omitempty"`
	Pitch           int            `json:"pitch,omitempty"`
	AudioSampleRate int            `json:"audio_sample_rate,omitempty"`
	Bitrate         int            `json:"bitrate,omitempty"`

	Path string `json:"-"`
	Name string `json:"-"`
}

type CreateT2AResponse

type CreateT2AResponse struct {
	TraceId      string    `json:"trace_id,omitempty"`
	BaseResp     BaseResp  `json:"base_resp,omitempty"`
	AudioFile    string    `json:"audio_file,omitempty"`
	SubtitleFile string    `json:"subtitle_file,omitempty"`
	ExtraInfo    ExtraInfo `json:"extra_info,omitempty"`
}

type ExtraInfo

type ExtraInfo struct {
	AudioLength     int64 `json:"audio_length,omitempty"`
	AudioSampleRate int64 `json:"audio_sample_rate,omitempty"`
	AudioSize       int64 `json:"audio_size,omitempty"`
	Bitrate         int64 `json:"bitrate,omitempty"`
	WordCount       int64 `json:"word_count,omitempty"`
}

type Function

type Function struct {
	Name        string     `json:"name"`
	Description string     `json:"description"`
	Parameters  Parameters `json:"parameters"`
}

type FunctionCall

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

type Glyph

type Glyph struct {
	Type           string `json:"type"`
	RawGlyph       string `json:"raw_glyph"`
	JsonProperties any    `json:"json_properties,omitempty"`
}

type Message

type Message struct {
	SenderType string `json:"sender_type"`
	Text       string `json:"text"`
}

type Parameters

type Parameters struct {
	Type       string   `json:"type"`
	Required   []string `json:"required"`
	Properties any      `json:"properties"`
}

type ProMessage added in v1.1.0

type ProMessage struct {
	SenderType string `json:"sender_type"`
	SenderName string `json:"sender_name"`
	Text       string `json:"text"`
}

type ReplyConstraints

type ReplyConstraints struct {
	SenderType string `json:"sender_type"`
	SenderName string `json:"sender_name"`
	Glyph      *Glyph `json:"glyph,omitempty"`
}

type RoleMeta added in v1.1.0

type RoleMeta struct {
	UserName string `json:"user_name"`
	BotName  string `json:"bot_name"`
}

type TimberWeight

type TimberWeight struct {
	VoiceId string `json:"voice_id"`
	Weight  int    `json:"weight"`
}

type Usage

type Usage struct {
	TotalTokens           int64 `json:"total_tokens"`
	TokensWithAddedPlugin int64 `json:"tokens_with_added_plugin"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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