chat

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AUTHORIZATION = "Authorization"
	CONTENT_TYPE  = "Content-Type"
)
View Source
const (
	RoleUser      = "user"      //
	RoleSystem    = "system"    //
	RoleAssistant = "assistant" //
)
View Source
const ENV_AI_API_KEY = "AI_API_KEY"
View Source
const GITEE_AI = "https://ai.gitee.com/v1"

服务地址

View Source
const GITEE_AI_API = "/chat/completions"

服务接口

Variables

View Source
var Debug bool
View Source
var DefaultGiteeAIMetaData = MetaData{
	Model:    GITEE_AI_DEEPSEEK_R1_DISTILL_QWEN_32B.Value(),
	Messages: make(Messages, 0),
}

DefaultGiteeAIMetaData 默认模型参数配置

View Source
var DefaultGiteeAIOptions = Options{
	BaseURL: GITEE_AI,
	API:     GITEE_AI_API,
	APIKey:  os.Getenv(ENV_AI_API_KEY),
}

DefaultGiteeAIOptions 默认选项配置

Functions

func GiteeAIModels added in v1.0.1

func GiteeAIModels() []string

func HttpPost

func HttpPost(ai IAI)

HttpPost 发送 Http Post 请求

Types

type AI added in v1.0.2

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

AI 基础实现

func (*AI) History added in v1.0.2

func (m *AI) History() *Messages

func (*AI) OnFail added in v1.0.2

func (m *AI) OnFail() TOnFail

func (*AI) OnReceive added in v1.0.2

func (m *AI) OnReceive() TOnReceive

func (*AI) SetOnFail added in v1.0.2

func (m *AI) SetOnFail(fn TOnFail)

func (*AI) SetOnReceive added in v1.0.2

func (m *AI) SetOnReceive(fn TOnReceive)

func (*AI) System added in v1.0.2

func (m *AI) System(content string)

type Choices

type Choices []TChoice

func (Choices) ToString

func (m Choices) ToString() string

func (Choices) ToStringArray

func (m Choices) ToStringArray() (result []string)

type FuncParameter

type FuncParameter struct {
	Type        FuncParameterType `json:"type,omitempty"`
	Description string            `json:"description,omitempty"`
}

FuncParameter 模型调用的工具函数参数

type FuncParameterType

type FuncParameterType string

FuncParameterType 工具函数参数类型

const (
	FptInteger FuncParameterType = "integer"
	FptString  FuncParameterType = "string"
	FptFloat   FuncParameterType = "float"
)

type FuncParameters

type FuncParameters map[string]FuncParameter

FuncParameters 模型调用的工具函数参数列表

func (*FuncParameters) Add

func (m *FuncParameters) Add(name string, type_ FuncParameterType, description string)

func (*FuncParameters) AddFloat

func (m *FuncParameters) AddFloat(name string, description string)

func (*FuncParameters) AddInteger

func (m *FuncParameters) AddInteger(name string, description string)

func (*FuncParameters) AddString

func (m *FuncParameters) AddString(name string, description string)

type Function

type Function struct {
	Name        string         `json:"name,omitempty"`        // 函数的名称 required
	Description string         `json:"description,omitempty"` // 函数的描述
	Parameters  FuncParameters `json:"parameters,omitempty"`  // 函数接受的参数
}

Function 模型调用的工具函数

type GiteeAI

type GiteeAI struct {
	AI
	// contains filtered or unexported fields
}

GiteeAI Gitee AI 实现

func (*GiteeAI) API

func (m *GiteeAI) API() string

func (*GiteeAI) APIKey

func (m *GiteeAI) APIKey() string

func (*GiteeAI) Chat

func (m *GiteeAI) Chat(content string)

func (*GiteeAI) ChatRole

func (m *GiteeAI) ChatRole(content string, role Role)

func (*GiteeAI) ChatStream

func (m *GiteeAI) ChatStream(content string)

func (*GiteeAI) ChatStreamRole

func (m *GiteeAI) ChatStreamRole(content string, role Role)

func (*GiteeAI) Header

func (m *GiteeAI) Header() http.Header

func (*GiteeAI) IsSupportTool

func (m *GiteeAI) IsSupportTool() bool

func (*GiteeAI) MetaData

func (m *GiteeAI) MetaData() *MetaData

func (*GiteeAI) Model

func (m *GiteeAI) Model() string

func (*GiteeAI) Options added in v1.0.1

func (m *GiteeAI) Options() *Options

func (*GiteeAI) Request

func (m *GiteeAI) Request()

func (*GiteeAI) SetModel

func (m *GiteeAI) SetModel(name GiteeAIModelNameEnum)

type GiteeAIModelNameEnum

type GiteeAIModelNameEnum string

gitee ai 模型列表

const (
	GITEE_AI_YI_34B_CHAT                   GiteeAIModelNameEnum = "Yi-34B-Chat"
	GITEE_AI_INTERNVL2_8B                  GiteeAIModelNameEnum = "InternVL2-8B"
	GITEE_AI_INTERNVL2_5_78B               GiteeAIModelNameEnum = "InternVL2.5-78B"
	GITEE_AI_DEEPSEEK_CODER_33B_INSTRUCT   GiteeAIModelNameEnum = "deepseek-coder-33B-instruct"
	GITEE_AI_INTERNVL2_5_26B               GiteeAIModelNameEnum = "InternVL2.5-26B"
	GITEE_AI_DEEPSEEK_R1_DISTILL_QWEN_1_5B GiteeAIModelNameEnum = "DeepSeek-R1-Distill-Qwen-1.5B"
	GITEE_AI_QWEN2_VL_72B                  GiteeAIModelNameEnum = "Qwen2-VL-72B"
	GITEE_AI_QWEN2_5_32B_INSTRUCT          GiteeAIModelNameEnum = "Qwen2.5-32B-Instruct"
	GITEE_AI_GLM_4_9B_CHAT                 GiteeAIModelNameEnum = "glm-4-9b-chat"
	GITEE_AI_QWQ_32B_PREVIEW               GiteeAIModelNameEnum = "QwQ-32B-Preview"
	GITEE_AI_CODEGEEX4_ALL_9B              GiteeAIModelNameEnum = "codegeex4-all-9b"
	GITEE_AI_QWEN2_5_CODER_32B_INSTRUCT    GiteeAIModelNameEnum = "Qwen2.5-Coder-32B-Instruct"
	GITEE_AI_DEEPSEEK_R1                   GiteeAIModelNameEnum = "DeepSeek-R1"
	GITEE_AI_QWEN2_5_72B_INSTRUCT          GiteeAIModelNameEnum = "Qwen2.5-72B-Instruct"
	GITEE_AI_QWEN2_5_7B_INSTRUCT           GiteeAIModelNameEnum = "Qwen2.5-7B-Instruct"
	GITEE_AI_DEEPSEEK_R1_DISTILL_QWEN_7B   GiteeAIModelNameEnum = "DeepSeek-R1-Distill-Qwen-7B"
	GITEE_AI_QWEN2_5_CODER_14B_INSTRUCT    GiteeAIModelNameEnum = "Qwen2.5-Coder-14B-Instruct"
	GITEE_AI_DEEPSEEK_R1_DISTILL_QWEN_32B  GiteeAIModelNameEnum = "DeepSeek-R1-Distill-Qwen-32B"
	GITEE_AI_QWEN2_72B_INSTRUCT            GiteeAIModelNameEnum = "Qwen2-72B-Instruct"
	GITEE_AI_CODE_RACCOON_V1               GiteeAIModelNameEnum = "code-raccoon-v1"
	GITEE_AI_QWEN2_7B_INSTRUCT             GiteeAIModelNameEnum = "Qwen2-7B-Instruct"
	GITEE_AI_DEEPSEEK_V3                   GiteeAIModelNameEnum = "DeepSeek-V3"
	GITEE_AI_QWEN2_5_14B_INSTRUCT          GiteeAIModelNameEnum = "Qwen2.5-14B-Instruct"
	GITEE_AI_DEEPSEEK_R1_DISTILL_QWEN_14B  GiteeAIModelNameEnum = "DeepSeek-R1-Distill-Qwen-14B"
)

func (GiteeAIModelNameEnum) Value

func (m GiteeAIModelNameEnum) Value() string

Value 返回模型枚举值

type IAI

type IAI interface {
	API() string                // AI API 服务地址
	Model() string              // 模型名称
	APIKey() string             // 服务商 API KEY
	IsSupportTool() bool        // 模型是否支持工具
	MetaData() *MetaData        // 模型源数据参数
	Header() http.Header        // 请求头
	History() *Messages         // 历史消息列表, 包括当前消息
	OnFail() TOnFail            // 消息接收时, 返回失败时回调函数
	OnReceive() TOnReceive      // 消息接收时, 返回成功时消息接收
	SetOnFail(fn TOnFail)       // 设置 失败回调函数
	SetOnReceive(fn TOnReceive) // 设置 消息接收回调函数
	System(content string)      // 用于预先定义模型的基础行为框架和响应风格
}

IAI 基础接口

type IGiteeAI

type IGiteeAI interface {
	IAI
	SetModel(name GiteeAIModelNameEnum)       // 设置当前模型
	ChatRole(content string, role Role)       // 带有角色的聊天, 发送消息并以普通方式全量返回
	ChatStreamRole(content string, role Role) // 带有角色的聊天, 发送消息并以流方式返回
	Chat(content string)                      // 发送消息并以普通方式全量返回
	ChatStream(content string)                // 发送消息并以流方式返回
	Options() *Options                        // 返回当前选项
}

IGiteeAI gitee ai 模型接口

func NewGiteeAI

func NewGiteeAI(options Options, isSupportTool bool) IGiteeAI

NewGiteeAI 创建一个 Gitee AI

type LogitBias

type LogitBias map[string]float32

func (*LogitBias) Add

func (m *LogitBias) Add(name string, value float32)

Add 值范围 -100 ~ 100

type Message

type Message struct {
	Role    Role   `json:"role,omitempty"`
	Content string `json:"content,omitempty"`
}

Message 消息

type Messages

type Messages []Message

Messages 消息列表

func (*Messages) Add

func (m *Messages) Add(message Message)

Add 添加历史消息

func (*Messages) Clear added in v1.0.1

func (m *Messages) Clear()

Clear 清空历史消息

type MetaData

type MetaData struct {
	Model            string         `json:"model,omitempty"`             // 模型 required
	Messages         Messages       `json:"messages,omitempty"`          // 包含当前会话所有消息的列表
	Stream           bool           `json:"stream"`                      // 是否是流式输出
	MaxTokens        int            `json:"max_tokens,omitempty"`        // 最大生成长度 default: 0
	FrequencyPenalty float32        `json:"frequency_penalty,omitempty"` // 频率惩罚 default: 0
	PresencePenalty  float32        `json:"presence_penalty,omitempty"`  // 存在惩罚 default: 0
	Stop             []string       `json:"stop,omitempty"`              // 停止词
	Temperature      float32        `json:"temperature,omitempty"`       // 温度 default: 1
	TopP             float32        `json:"top_p,omitempty"`             // Top p default: 1
	TopLogprobs      int            `json:"top_logprobs,omitempty"`      // Top Logprobs default: 0
	ResponseFormat   ResponseFormat `json:"response_format,omitempty"`   // 响应格式
	Seed             int            `json:"seed,omitempty"`              // 随机种子 default: 0
	N                int            `json:"n,omitempty"`                 // 生成数量 default: 1
	LogitBias        LogitBias      `json:"logit_bias,omitempty"`        // 对数偏差 additional properties double min: -100 max: 100 default: {"1000": 99.9, "1001": -99.9}
	User             string         `json:"user,omitempty"`              // 客户侧的用户标识 string | nullable
	Tools            Tools          `json:"tools,omitempty"`             // 模型可以调用的工具列表。目前仅支持函数作为工具 array object[]
	GuidedJSON       any            `json:"guided_json,omitempty"`       // 如果指定,输出将遵循JSON模式 anyof
	GuidedChoice     []string       `json:"guided_choice,omitempty"`     // 如果指定,输出将恰好是其中一个选项
}

MetaData 元数据参数配置

func (*MetaData) ToJSON

func (m *MetaData) ToJSON() (data []byte)

ToJSON 模型元数据配置转成 JSON

type Options

type Options struct {
	BaseURL string // 服务商地址
	API     string // 接口
	APIKey  string // 秘钥
}

Options 基础配置选项

type ResponseFormat

type ResponseFormat map[string]string

ResponseFormat 响应格式

func (*ResponseFormat) Add

func (m *ResponseFormat) Add(name, value string)

type Role

type Role string

Role 角色

type TChoice

type TChoice struct {
	FinishReason string            `json:"finish_reason"`
	Index        int               `json:"index"`
	Message      map[string]string `json:"message"`
	Logprobs     any               `json:"logprobs"`
	Delta        TDelta            `json:"delta"`
}

type TDelta

type TDelta struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type TMessage

type TMessage struct {
	Success bool
	Content []string
}

type TOnFail

type TOnFail func(err *TResponseError)

TOnFail 接收失败消息事件

type TOnReceive

type TOnReceive func(message *TResponse)

TOnReceive 接收成功消息事件

type TResponse

type TResponse struct {
	Id        string  `json:"id"`
	Object    string  `json:"object"`
	Created   int     `json:"created"`
	Model     string  `json:"model"`
	Usage     TUsage  `json:"usage"`
	Choices   Choices `json:"choices"`
	Error     string  `json:"error"`
	ErrorType string  `json:"error_type"`
}

type TResponseError

type TResponseError struct {
	Code    string
	Message string
	Type    string
	Other   TResponseErrorOther
}

type TResponseErrorOther

type TResponseErrorOther map[string]string

func (*TResponseErrorOther) Get

func (m *TResponseErrorOther) Get(name string) string

type TUsage

type TUsage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

type Tool

type Tool struct {
	Type     string    `json:"type,omitempty"`     // const: function default: function
	Function *Function `json:"function,omitempty"` // 包含函数的详细信息 required
}

Tool 模型调用的工具

func NewTool

func NewTool() *Tool

func (*Tool) AddFunc

func (m *Tool) AddFunc(name, desc string) *Function

type Tools

type Tools []*Tool

Tools 工具列表

func (*Tools) Add

func (m *Tools) Add(tool *Tool)

Jump to

Keyboard shortcuts

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