mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Casibase's upstream '// Copyright <yr> The OpenAgent Authors. All Rights Reserved.' (and legacy 'The Casibase Authors' on older files) was stripped from every derived source file and replaced with a Hanzo-only line. This re-adds it as a '// Portions Copyright ...' line beneath the Hanzo line on the 573 casibase-derived files, using each file's real upstream year/holder. Net-new Hanzo files are untouched. Nothing deleted. LICENSE + NOTICE were already correct.
139 lines
3.6 KiB
Go
139 lines
3.6 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
// Portions Copyright 2024 The OpenAgent Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package embedding
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/hanzoai/ai/i18n"
|
|
)
|
|
|
|
type MiniMaxEmbeddingProvider struct {
|
|
subType string
|
|
apiKey string
|
|
providerUrl string // providerUrl contains groupId
|
|
}
|
|
|
|
func NewMiniMaxEmbeddingProvider(typ string, subType string, apiKey string, providerUrl string) (*MiniMaxEmbeddingProvider, error) {
|
|
p := &MiniMaxEmbeddingProvider{
|
|
subType: subType,
|
|
apiKey: apiKey,
|
|
providerUrl: providerUrl,
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (p *MiniMaxEmbeddingProvider) GetPricing() string {
|
|
return `URL:
|
|
https://platform.minimaxi.com/document/Price
|
|
|
|
Embedding models:
|
|
|
|
| Models | Per 1,000,000 tokens |
|
|
|-----------|----------------------|
|
|
| embo-01 | ¥0.0005 |
|
|
`
|
|
}
|
|
|
|
func (p *MiniMaxEmbeddingProvider) calculatePrice(res *EmbeddingResult) error {
|
|
pricePerThousandTokens := 0.0005
|
|
res.Price = getPrice(res.TokenCount, pricePerThousandTokens)
|
|
res.Currency = "CNY"
|
|
return nil
|
|
}
|
|
|
|
type EmbeddingRequest struct {
|
|
Texts []string `json:"texts"`
|
|
Model string `json:"model"`
|
|
Type string `json:"type"`
|
|
}
|
|
type base_resp struct {
|
|
status_code int64
|
|
status_msg string
|
|
}
|
|
type EmbeddingResponse struct {
|
|
Vectors [][]float32 `json:"vectors"`
|
|
Total_tokens int64 `json:"total_tokens"`
|
|
Base_resp base_resp `json:"base_resp"`
|
|
}
|
|
|
|
func (p *MiniMaxEmbeddingProvider) QueryVector(text string, ctx context.Context, lang string) ([]float32, *EmbeddingResult, error) {
|
|
url := p.providerUrl
|
|
apiKey := p.apiKey
|
|
|
|
requestData := EmbeddingRequest{
|
|
Texts: []string{text},
|
|
Model: p.subType,
|
|
Type: "db",
|
|
}
|
|
|
|
requestBody, err := json.Marshal(requestData)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer "+apiKey)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return nil, nil, fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "embedding:request failed with status code %d: %s"), resp.StatusCode, string(body)))
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
var embeddingResponse EmbeddingResponse
|
|
err = json.Unmarshal(body, &embeddingResponse)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("%s", fmt.Sprintf(i18n.Translate(lang, "embedding:error unmarshaling response JSON: %v"), err))
|
|
}
|
|
|
|
if len(embeddingResponse.Vectors) == 0 {
|
|
return nil, nil, fmt.Errorf("%s", i18n.Translate(lang, "embedding:no embedding vector found in response"))
|
|
}
|
|
|
|
embeddingResult := &EmbeddingResult{
|
|
TokenCount: int(embeddingResponse.Total_tokens),
|
|
}
|
|
|
|
err = p.calculatePrice(embeddingResult)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return embeddingResponse.Vectors[0], embeddingResult, nil
|
|
}
|