mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-01 05:23:47 +08:00
Refactor[Go Model Provider]: refactor baseURL and modelConfig (#15627)
### What problem does this PR solve? As Title ### Type of change - [x] Refactoring
This commit is contained in:
@@ -33,9 +33,7 @@ import (
|
||||
const replicatePollInterval = time.Second
|
||||
|
||||
type ReplicateModel struct {
|
||||
BaseURL map[string]string
|
||||
URLSuffix URLSuffix
|
||||
httpClient *http.Client
|
||||
baseModel BaseModel
|
||||
}
|
||||
|
||||
func NewReplicateModel(baseURL map[string]string, urlSuffix URLSuffix) *ReplicateModel {
|
||||
@@ -47,16 +45,18 @@ func NewReplicateModel(baseURL map[string]string, urlSuffix URLSuffix) *Replicat
|
||||
transport.ResponseHeaderTimeout = 60 * time.Second
|
||||
|
||||
return &ReplicateModel{
|
||||
BaseURL: baseURL,
|
||||
URLSuffix: urlSuffix,
|
||||
httpClient: &http.Client{
|
||||
Transport: transport,
|
||||
baseModel: BaseModel{
|
||||
BaseURL: baseURL,
|
||||
URLSuffix: urlSuffix,
|
||||
httpClient: &http.Client{
|
||||
Transport: transport,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ReplicateModel) NewInstance(baseURL map[string]string) ModelDriver {
|
||||
return NewReplicateModel(baseURL, r.URLSuffix)
|
||||
return NewReplicateModel(baseURL, r.baseModel.URLSuffix)
|
||||
}
|
||||
|
||||
func (r *ReplicateModel) Name() string {
|
||||
@@ -88,24 +88,13 @@ type replicateSSEEvent struct {
|
||||
data string
|
||||
}
|
||||
|
||||
func (r *ReplicateModel) baseURLForRegion(region string) (string, error) {
|
||||
base, ok := r.BaseURL[region]
|
||||
if !ok || base == "" {
|
||||
return "", fmt.Errorf("replicate: no base URL configured for region %q", region)
|
||||
}
|
||||
return strings.TrimSuffix(base, "/"), nil
|
||||
}
|
||||
|
||||
func (r *ReplicateModel) endpoint(apiConfig *APIConfig, suffix string) (string, error) {
|
||||
region := "default"
|
||||
if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" {
|
||||
region = *apiConfig.Region
|
||||
}
|
||||
|
||||
baseURL, err := r.baseURLForRegion(region)
|
||||
baseURL, err := r.baseModel.GetBaseURL(apiConfig)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
baseURL = strings.TrimSuffix(baseURL, "/")
|
||||
return fmt.Sprintf("%s/%s", baseURL, suffix), nil
|
||||
}
|
||||
|
||||
@@ -116,7 +105,7 @@ func replicateUsesVersionEndpoint(modelName string) bool {
|
||||
|
||||
func (r *ReplicateModel) predictionEndpoint(apiConfig *APIConfig, modelName string) (string, string, error) {
|
||||
if replicateUsesVersionEndpoint(modelName) {
|
||||
endpoint, err := r.endpoint(apiConfig, r.URLSuffix.Chat)
|
||||
endpoint, err := r.endpoint(apiConfig, r.baseModel.URLSuffix.Chat)
|
||||
return endpoint, modelName, err
|
||||
}
|
||||
|
||||
@@ -125,7 +114,7 @@ func (r *ReplicateModel) predictionEndpoint(apiConfig *APIConfig, modelName stri
|
||||
return "", "", fmt.Errorf("replicate: official model name must be owner/name")
|
||||
}
|
||||
|
||||
modelsPrefix := strings.TrimSuffix(r.URLSuffix.Models, "models")
|
||||
modelsPrefix := strings.TrimSuffix(r.baseModel.URLSuffix.Models, "models")
|
||||
if modelsPrefix == "" {
|
||||
modelsPrefix = "v1/"
|
||||
}
|
||||
@@ -248,7 +237,7 @@ func (r *ReplicateModel) createPrediction(ctx context.Context, url string, versi
|
||||
req.Header.Set("Prefer", "wait=60")
|
||||
}
|
||||
|
||||
resp, err := r.httpClient.Do(req)
|
||||
resp, err := r.baseModel.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
@@ -288,7 +277,7 @@ func (r *ReplicateModel) getPrediction(ctx context.Context, url string, apiKey s
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
|
||||
resp, err := r.httpClient.Do(req)
|
||||
resp, err := r.baseModel.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
@@ -342,8 +331,8 @@ func (r *ReplicateModel) waitForPrediction(ctx context.Context, prediction *repl
|
||||
}
|
||||
|
||||
func (r *ReplicateModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) {
|
||||
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
|
||||
return nil, fmt.Errorf("api key is required")
|
||||
if err := r.baseModel.APIConfigCheck(apiConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.TrimSpace(modelName) == "" {
|
||||
return nil, fmt.Errorf("model name is required")
|
||||
@@ -381,12 +370,13 @@ func (r *ReplicateModel) ChatWithMessages(modelName string, messages []Message,
|
||||
}
|
||||
|
||||
func (r *ReplicateModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig, sender func(*string, *string) error) error {
|
||||
if err := r.baseModel.APIConfigCheck(apiConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if sender == nil {
|
||||
return fmt.Errorf("sender is required")
|
||||
}
|
||||
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
|
||||
return fmt.Errorf("api key is required")
|
||||
}
|
||||
if strings.TrimSpace(modelName) == "" {
|
||||
return fmt.Errorf("model name is required")
|
||||
}
|
||||
@@ -437,7 +427,7 @@ func (r *ReplicateModel) readPredictionStream(url string, apiKey string, sender
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
req.Header.Set("Accept", "text/event-stream")
|
||||
|
||||
resp, err := r.httpClient.Do(req)
|
||||
resp, err := r.baseModel.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
@@ -515,11 +505,11 @@ func dispatchReplicateSSEEvent(event replicateSSEEvent, sender func(*string, *st
|
||||
}
|
||||
|
||||
func (r *ReplicateModel) ListModels(apiConfig *APIConfig) ([]string, error) {
|
||||
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
|
||||
return nil, fmt.Errorf("api key is required")
|
||||
if err := r.baseModel.APIConfigCheck(apiConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url, err := r.endpoint(apiConfig, r.URLSuffix.Models)
|
||||
url, err := r.endpoint(apiConfig, r.baseModel.URLSuffix.Models)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -534,7 +524,7 @@ func (r *ReplicateModel) ListModels(apiConfig *APIConfig) ([]string, error) {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey))
|
||||
|
||||
resp, err := r.httpClient.Do(req)
|
||||
resp, err := r.baseModel.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to send request: %w", err)
|
||||
}
|
||||
@@ -679,12 +669,13 @@ func replicateKeys(m map[string]interface{}) []string {
|
||||
// {embedding: [floats]} objects); see replicateEmbedInput and
|
||||
// replicateEmbedOutputToVectors for details.
|
||||
func (r *ReplicateModel) Embed(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([]EmbeddingData, error) {
|
||||
if err := r.baseModel.APIConfigCheck(apiConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(texts) == 0 {
|
||||
return []EmbeddingData{}, nil
|
||||
}
|
||||
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
|
||||
return nil, fmt.Errorf("api key is required")
|
||||
}
|
||||
if modelName == nil || strings.TrimSpace(*modelName) == "" {
|
||||
return nil, fmt.Errorf("model name is required")
|
||||
}
|
||||
@@ -802,12 +793,13 @@ func replicateScoresFromInterface(arr []interface{}, n int) ([]float64, error) {
|
||||
// can compare against per-model thresholds, but the RelevanceScore
|
||||
// field should not be assumed to be a probability.
|
||||
func (r *ReplicateModel) Rerank(modelName *string, query string, documents []string, apiConfig *APIConfig, rerankConfig *RerankConfig) (*RerankResponse, error) {
|
||||
if err := r.baseModel.APIConfigCheck(apiConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(documents) == 0 {
|
||||
return &RerankResponse{}, nil
|
||||
}
|
||||
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
|
||||
return nil, fmt.Errorf("api key is required")
|
||||
}
|
||||
if modelName == nil || strings.TrimSpace(*modelName) == "" {
|
||||
return nil, fmt.Errorf("model name is required")
|
||||
}
|
||||
@@ -869,7 +861,6 @@ func (r *ReplicateModel) Rerank(modelName *string, query string, documents []str
|
||||
return &RerankResponse{Data: results}, nil
|
||||
}
|
||||
|
||||
|
||||
func (r *ReplicateModel) Balance(apiConfig *APIConfig) (map[string]interface{}, error) {
|
||||
return nil, fmt.Errorf("%s, no such method", r.Name())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user