chore: 初始开源发布
iotlcm-app:小程序端 + 用户端服务。 - app/:uni-app 小程序前端(基于 unibest,MIT) - 微信 AppID → YOUR_WX_APPID,uni-app AppID → __UNI__YOURAPPID - 内网后端地址 → admin.iotlcm.example.com - 应用标题与 unibest 模板 README 重写(保留上游致谢与 MIT 声明) - server/:GoFrame 用户端服务(基于 XiuAdmin,Apache-2.0) - module xiuadmin → git.5iot.com/iotlcm-public/iotlcm-app(360 文件) - 支付回调地址 → app.iotlcm.example.com,商户名与应用名泛化 - 真实手机号 → 合法 11 位编造号(保留号段前 3 位 + 后 4 位) - 依赖号码尾部的断言同步更新 - 补 server/LICENSE(Apache-2.0) - 移除内部资产:CI 触发器 - 补根 README,LF 行尾 + .gitattributes 已知:TestSysDept_RefreshDeptAncestors 在源仓同一提交上同样失败,属既有缺陷。
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
openaiModel "github.com/cloudwego/eino-ext/components/model/openai"
|
||||
"github.com/cloudwego/eino/components/model"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
type OpenAICompatibleConfig struct {
|
||||
APIKey string
|
||||
BaseURL string
|
||||
Endpoint string
|
||||
Model string
|
||||
Timeout time.Duration
|
||||
MaxTokens int
|
||||
JSONOutput bool
|
||||
}
|
||||
|
||||
const ProviderOpenAICompatible = "openai-compatible"
|
||||
|
||||
type ChatModelConfig struct {
|
||||
Provider string
|
||||
APIKey string
|
||||
BaseURL string
|
||||
Endpoint string
|
||||
Model string
|
||||
Timeout time.Duration
|
||||
MaxTokens int
|
||||
JSONOutput bool
|
||||
}
|
||||
|
||||
func NewChatModel(ctx context.Context, config ChatModelConfig) (model.BaseChatModel, error) {
|
||||
provider := strings.ToLower(strings.TrimSpace(config.Provider))
|
||||
if provider == "" {
|
||||
provider = ProviderOpenAICompatible
|
||||
}
|
||||
switch provider {
|
||||
case ProviderOpenAICompatible, "openai":
|
||||
return NewOpenAICompatibleChatModel(ctx, OpenAICompatibleConfig{
|
||||
APIKey: config.APIKey,
|
||||
BaseURL: config.BaseURL,
|
||||
Endpoint: config.Endpoint,
|
||||
Model: config.Model,
|
||||
Timeout: config.Timeout,
|
||||
MaxTokens: config.MaxTokens,
|
||||
JSONOutput: config.JSONOutput,
|
||||
})
|
||||
default:
|
||||
return nil, gerror.Newf("暂不支持AI模型提供方:%s", provider)
|
||||
}
|
||||
}
|
||||
|
||||
func NewOpenAICompatibleChatModel(ctx context.Context, config OpenAICompatibleConfig) (model.BaseChatModel, error) {
|
||||
apiKey := strings.TrimSpace(config.APIKey)
|
||||
if apiKey == "" {
|
||||
return nil, gerror.New("AI模型API Key不能为空")
|
||||
}
|
||||
modelName := strings.TrimSpace(config.Model)
|
||||
if modelName == "" {
|
||||
return nil, gerror.New("AI模型名称不能为空")
|
||||
}
|
||||
baseURL, err := ResolveOpenAIBaseURL(config.BaseURL, config.Endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
modelConfig := &openaiModel.ChatModelConfig{
|
||||
APIKey: apiKey,
|
||||
BaseURL: baseURL,
|
||||
Model: modelName,
|
||||
Timeout: config.Timeout,
|
||||
}
|
||||
if config.MaxTokens > 0 {
|
||||
modelConfig.MaxTokens = &config.MaxTokens
|
||||
}
|
||||
if config.JSONOutput {
|
||||
modelConfig.ResponseFormat = &openaiModel.ChatCompletionResponseFormat{
|
||||
Type: openaiModel.ChatCompletionResponseFormatTypeJSONObject,
|
||||
}
|
||||
}
|
||||
|
||||
chatModel, err := openaiModel.NewChatModel(ctx, modelConfig)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "初始化Eino AI模型失败")
|
||||
}
|
||||
return chatModel, nil
|
||||
}
|
||||
|
||||
func ResolveOpenAIBaseURL(baseURL string, endpoint string) (string, error) {
|
||||
value := strings.TrimSpace(baseURL)
|
||||
if value == "" {
|
||||
value = strings.TrimSpace(endpoint)
|
||||
}
|
||||
if value == "" {
|
||||
return "", gerror.New("AI模型BaseURL不能为空")
|
||||
}
|
||||
|
||||
parsed, err := url.Parse(value)
|
||||
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
|
||||
return "", gerror.New("AI模型BaseURL格式不正确")
|
||||
}
|
||||
parsed.Path = strings.TrimSuffix(strings.TrimRight(parsed.Path, "/"), "/chat/completions")
|
||||
parsed.RawPath = ""
|
||||
parsed.RawQuery = ""
|
||||
parsed.Fragment = ""
|
||||
return strings.TrimRight(parsed.String(), "/"), nil
|
||||
}
|
||||
Reference in New Issue
Block a user