RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
feat: add cheapaimodel as a model provider (#21612)
Nicho Deng321dfe84ee53
diff --git a/conf/llm_factories.json b/conf/llm_factories.jsonindex cf36574a03..5d33e4c01f 100644--- a/conf/llm_factories.json+++ b/conf/llm_factories.json@@ -3495,6 +3495,14 @@"llm": [],"url": "https://router.hostinger.com/v1"},+ {+ "name": "cheapaimodel",+ "logo": "",+ "tags": "LLM,IMAGE2TEXT",+ "status": "1",+ "llm": [],+ "url": "https://api.cheapaimodel.ai/v1"+ },{"name": "StepFun","logo": "",diff --git a/conf/model.md b/conf/model.mdindex 37b5c0613c..37510a4abb 100644--- a/conf/model.md+++ b/conf/model.md@@ -176,6 +176,7 @@ The following providers are **aggregators** — they host models from multiple u| **AstraFlow** | Multi-provider aggregator || **Avian** | Multi-provider aggregator || **Baidu (Qianwen)** | Ernie + third-party models |+| **cheapaimodel** | OpenAI-compatible gateway for open-weight models (DeepSeek, GLM, Kimi) plus Grok || **CloudGPU** | GPU cloud with an OpenAI-compatible inference API for open models || **CometAPI** | Multi-provider aggregator || **DeepInfra** | Open-source model hosting |diff --git a/conf/models/cheapaimodel.json b/conf/models/cheapaimodel.jsonnew file mode 100644index 0000000000..a46d708e80--- /dev/null+++ b/conf/models/cheapaimodel.json@@ -0,0 +1,67 @@+{+ "name": "cheapaimodel",+ "url": {+ "default": "https://api.cheapaimodel.ai/v1"+ },+ "url_suffix": {+ "chat": "chat/completions",+ "models": "models"+ },+ "class": "cheapaimodel",+ "models": [+ {+ "name": "deepseek-v4-1-flash",+ "context_length": 1000000,+ "model_types": [+ "chat",+ "vision"+ ],+ "tools": {+ "support": true+ }+ },+ {+ "name": "glm-5.3",+ "context_length": 1000000,+ "model_types": [+ "chat"+ ],+ "tools": {+ "support": true+ }+ },+ {+ "name": "glm-5.3-flash",+ "context_length": 1000000,+ "model_types": [+ "chat",+ "vision"+ ],+ "tools": {+ "support": true+ }+ },+ {+ "name": "kimi-k3",+ "context_length": 1000000,+ "model_types": [+ "chat",+ "vision"+ ],+ "tools": {+ "support": true+ }+ },+ {+ "name": "grok-4.7",+ "context_length": 500000,+ "model_types": [+ "chat",+ "vision"+ ],+ "tools": {+ "support": true+ }+ }+ ]+}diff --git a/internal/entity/models/cheapaimodel.go b/internal/entity/models/cheapaimodel.gonew file mode 100644index 0000000000..24cc725dbd--- /dev/null+++ b/internal/entity/models/cheapaimodel.go@@ -0,0 +1,155 @@+//+// Copyright 2026 The InfiniFlow 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 models++import (+ "context"+ "encoding/json"+ "fmt"+ "strings"+)++// CheapaimodelModel drives cheapaimodel (https://api.cheapaimodel.ai/v1), a+// pay-per-token gateway for open-weight models (DeepSeek, GLM, Kimi) plus+// Grok. It serves OpenAI-compatible chat and model-list endpoints, so the+// embedded OpenAIAPICompatibleModel covers every call.+type CheapaimodelModel struct {+ *OpenAIAPICompatibleModel+}++// NewCheapaimodelModel creates a cheapaimodel driver.+func NewCheapaimodelModel(baseURL map[string]string, urlSuffix URLSuffix) *CheapaimodelModel {+ return &CheapaimodelModel{+ OpenAIAPICompatibleModel: NewOpenAIAPICompatibleModel(baseURL, urlSuffix),+ }+}++// Name returns the driver name.+func (m *CheapaimodelModel) Name() string {+ return "cheapaimodel"+}++// NewInstance returns a driver bound to the given base URL.+func (m *CheapaimodelModel) NewInstance(baseURL map[string]string) ModelDriver {+ return NewCheapaimodelModel(baseURL, m.baseModel.URLSuffix)+}++// ListModels lists the models visible to the API key with a bodyless GET.+// The embedded vLLM driver sends a "{}" body on GET /models, which the Google+// load balancer in front of api.cheapaimodel.ai rejects with 400, so this+// driver issues the request itself, the same way the xAI driver does.+//+// Ids in the cheapaimodel catalog (conf/models/cheapaimodel.json) take its+// model types, context length and max output, so vision models keep their+// image input and the limits reach the UI; ParseListModel only consults the+// global catalog, which has no entry for most of these ids and may hold another+// provider's figures for the rest. A context length in the response itself+// still wins. Other ids keep the types ParseListModel resolved (global catalog,+// else name inference). The provider configures only the chat endpoint, so+// every type but chat and vision is removed, and ids left with none+// (embedding, rerank, audio) are not listed.+func (m *CheapaimodelModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ListModelResponse, error) {+ if err := m.baseModel.APIConfigCheck(apiConfig); err != nil {+ return nil, err+ }++ modelsSuffix := strings.Trim(strings.TrimSpace(m.baseModel.URLSuffix.Models), "/")+ if modelsSuffix == "" {+ return nil, fmt.Errorf("cheapaimodel: models URL suffix is not configured")+ }+ url, err := m.baseModel.EndpointURL(apiConfig, modelsSuffix)+ if err != nil {+ return nil, err+ }++ body, err := m.baseModel.doGetRequest(ctx, url, apiConfig, nonStreamCallTimeout)+ if err != nil {+ return nil, err+ }++ var modelList ModelList+ if err = json.Unmarshal(body, &modelList); err != nil {+ return nil, fmt.Errorf("failed to parse response: %w", err)+ }+ if modelList.Models == nil {+ return nil, fmt.Errorf("invalid models list format")+ }++ var provider *Provider+ pm := GetProviderManager()+ if pm != nil {+ provider = pm.FindProvider("cheapaimodel")+ }++ remoteContext := make(map[string]*int, len(modelList.Models))+ for _, item := range modelList.Models {+ if item.ContextLength != nil && *item.ContextLength > 0 {+ remoteContext[strings.TrimSpace(item.ID)] = item.ContextLength+ }+ }++ models := ParseListModel(modelList)+ filtered := make([]ListModelResponse, 0, len(models))+ for _, model := range models {+ if pm != nil && provider != nil {+ if known := pm.FindModel(provider, model.Name); known != nil {+ applyCatalogModel(&model, known)+ }+ }+ if contextLength, ok := remoteContext[model.Name]; ok {+ model.ContextLength = contextLength+ }+ model.ModelTypes = chatModelTypes(model.ModelTypes)+ if len(model.ModelTypes) == 0 {+ continue+ }+ filtered = append(filtered, model)+ }+ return filtered, nil+}++// applyCatalogModel overrides a listed model with the fields its provider+// catalog entry sets.+func applyCatalogModel(model *ListModelResponse, known *Model) {+ if len(known.ModelTypes) > 0 {+ model.ModelTypes = known.ModelTypes+ }+ if known.ContextLength != nil {+ model.ContextLength = known.ContextLength+ }+ if known.MaxOutput != nil {+ model.MaxOutput = known.MaxOutput+ }+}++// chatModelTypes keeps the types served by the chat endpoint, in a new slice.+func chatModelTypes(types []string) []string {+ kept := make([]string, 0, len(types))+ for _, t := range types {+ if t == modelTypeChat || t == modelTypeVision {+ kept = append(kept, t)+ }+ }+ return kept+}++// CheckConnection verifies the API key with a model-list call; GET /models+// requires the key on cheapaimodel.+func (m *CheapaimodelModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {+ _, err := m.ListModels(ctx, apiConfig)+ return err+}diff --git a/internal/entity/models/cheapaimodel_test.go b/internal/entity/models/cheapaimodel_test.gonew file mode 100644index 0000000000..84ee3ec1bf--- /dev/null+++ b/internal/entity/models/cheapaimodel_test.go@@ -0,0 +1,267 @@+//+// Copyright 2026 The InfiniFlow 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 models++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "ragflow/internal/common"+ "strings"+ "testing"+)++// newCheapaimodelForTest creates a CheapaimodelModel instance for testing.+func newCheapaimodelForTest(baseURL string) *CheapaimodelModel {+ return NewCheapaimodelModel(+ map[string]string{"default": baseURL},+ URLSuffix{Chat: "chat/completions", Models: "models"},+ )+}++// TestCheapaimodelName tests the driver name.+func TestCheapaimodelName(t *testing.T) {+ if got := newCheapaimodelForTest("http://unused").Name(); got != "cheapaimodel" {+ t.Errorf("Name()=%q", got)+ }+}++// TestCheapaimodelFactory tests creating the driver from factory.+func TestCheapaimodelFactory(t *testing.T) {+ driver, err := NewModelFactory().CreateModelDriver("cheapaimodel", map[string]string{"default": "http://unused"}, URLSuffix{})+ if err != nil {+ t.Fatalf("CreateModelDriver: %v", err)+ }+ if _, ok := driver.(*CheapaimodelModel); !ok {+ t.Fatalf("driver type=%T, want *CheapaimodelModel", driver)+ }+ if _, ok := driver.NewInstance(map[string]string{"default": "http://other"}).(*CheapaimodelModel); !ok {+ t.Fatal("NewInstance did not return *CheapaimodelModel")+ }+}++// The shipped config must resolve to the cheapaimodel driver.+// CreateModelDriver dispatches on strings.ToLower(name), so the+// "cheapaimodel" provider name has to match the "cheapaimodel" case;+// anything else silently falls through to DummyModel.+func TestCheapaimodelProviderConfig(t *testing.T) {+ dir, restore := setupProviderTestDir(t, "cheapaimodel.json")+ defer restore()++ if err := InitProviderManager(dir); err != nil {+ t.Fatalf("InitProviderManager: %v", err)+ }++ provider := GetProviderManager().FindProvider("cheapaimodel")+ if provider == nil {+ t.Fatal("cheapaimodel provider not found")+ }+ if _, ok := provider.ModelDriver.(*CheapaimodelModel); !ok {+ t.Fatalf("ModelDriver=%T, want *models.CheapaimodelModel", provider.ModelDriver)+ }+ if got := provider.URL["default"]; got != "https://api.cheapaimodel.ai/v1" {+ t.Errorf("default URL=%q", got)+ }+ if provider.URLSuffix.Chat != "chat/completions" || provider.URLSuffix.Models != "models" {+ t.Errorf("URLSuffix=%+v", provider.URLSuffix)+ }+}++// TestCheapaimodelChatSendsAPIKey tests chat completion authentication and+// that the model id reaches the API unchanged.+func TestCheapaimodelChatSendsAPIKey(t *testing.T) {+ withSSRFBypass(t)+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path != "/chat/completions" {+ t.Errorf("path=%s", r.URL.Path)+ }+ if got := r.Header.Get("Authorization"); got != "Bearer sk-gw-test" {+ t.Errorf("Authorization=%q", got)+ }+ var body struct {+ Model string `json:"model"`+ }+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil {+ t.Errorf("decode request: %v", err)+ }+ if body.Model != "deepseek-v4-1-flash" {+ t.Errorf("model=%q", body.Model)+ }+ _ = json.NewEncoder(w).Encode(map[string]any{+ "id": "chat-cheapaimodel",+ "choices": []map[string]any{{"message": map[string]any{"content": "pong"}}},+ "usage": map[string]any{"prompt_tokens": 3, "completion_tokens": 5, "total_tokens": 8},+ })+ }))+ defer srv.Close()++ usage := &common.ModelUsage{}+ resp, err := newCheapaimodelForTest(srv.URL).ChatWithMessages(+ t.Context(),+ "deepseek-v4-1-flash",+ []Message{{Role: "user", Content: "ping"}},+ &APIConfig{ApiKey: &testAPIKey},+ nil,+ usage,+ )+ if err != nil {+ t.Fatalf("ChatWithMessages: %v", err)+ }+ if *resp.Answer != "pong" {+ t.Errorf("Answer=%q", *resp.Answer)+ }+ assertModelUsage(t, usage, 3, 5, 8)+}++// TestCheapaimodelListModelsAndCheckConnection tests model listing and+// connectivity check. The cheapaimodel model list requires the API key.+func TestCheapaimodelListModelsAndCheckConnection(t *testing.T) {+ withSSRFBypass(t)+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.Method != http.MethodGet || r.URL.Path != "/models" {+ t.Errorf("%s %s", r.Method, r.URL.Path)+ }+ if got := r.Header.Get("Authorization"); got != "Bearer sk-gw-test" {+ t.Errorf("Authorization=%q", got)+ }+ // The load balancer in front of the API rejects a GET that carries a body.+ if r.ContentLength != 0 || len(r.TransferEncoding) > 0 {+ t.Errorf("GET /models carried a body (ContentLength=%d, TransferEncoding=%v)", r.ContentLength, r.TransferEncoding)+ }+ _ = json.NewEncoder(w).Encode(map[string]any{+ "data": []map[string]any{+ {"id": "deepseek-v4-1-flash"},+ {"id": "glm-5.3"},+ },+ })+ }))+ defer srv.Close()++ model := newCheapaimodelForTest(srv.URL)+ list, err := model.ListModels(t.Context(), &APIConfig{ApiKey: &testAPIKey})+ if err != nil {+ t.Fatalf("ListModels: %v", err)+ }+ if joinModelNames(list, ",") != "deepseek-v4-1-flash,glm-5.3" {+ t.Errorf("models=%v", list)+ }+ if err := model.CheckConnection(t.Context(), &APIConfig{ApiKey: &testAPIKey}); err != nil {+ t.Fatalf("CheckConnection: %v", err)+ }+}++// Known cheapaimodel models must report their catalogued types, so the vision+// models are not downgraded to chat-only by name inference. Unknown ids keep+// the name-based fallback, and ids the chat endpoint cannot serve (embedding,+// rerank) are not listed.+func TestCheapaimodelListModelsUsesCatalogTypes(t *testing.T) {+ withSSRFBypass(t)+ dir, restore := setupProviderTestDir(t, "cheapaimodel.json")+ defer restore()+ if err := InitProviderManager(dir); err != nil {+ t.Fatalf("InitProviderManager: %v", err)+ }++ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ _ = json.NewEncoder(w).Encode(map[string]any{+ "data": []map[string]any{+ {"id": "deepseek-v4-1-flash"},+ {"id": "glm-5.3"},+ {"id": "glm-5.3-flash"},+ {"id": "kimi-k3"},+ {"id": "grok-4.7"},+ {"id": "some-new-vl-model"},+ {"id": "text-embedding-v2"},+ {"id": "bge-reranker-v2"},+ },+ })+ }))+ defer srv.Close()++ list, err := newCheapaimodelForTest(srv.URL).ListModels(t.Context(), &APIConfig{ApiKey: &testAPIKey})+ if err != nil {+ t.Fatalf("ListModels: %v", err)+ }+ want := map[string]string{+ "deepseek-v4-1-flash": "chat,vision",+ "glm-5.3": "chat",+ "glm-5.3-flash": "chat,vision",+ "kimi-k3": "chat,vision",+ "grok-4.7": "chat,vision",+ "some-new-vl-model": "chat,vision",+ }+ if len(list) != len(want) {+ t.Fatalf("models=%v", list)+ }+ for _, model := range list {+ if got := strings.Join(model.ModelTypes, ","); got != want[model.Name] {+ t.Errorf("%s types=%q, want %q", model.Name, got, want[model.Name])+ }+ }+}++// The endpoint returns ids only, and most cheapaimodel ids have no global+// catalog entry, so the context length must come from the provider catalog.+// A context length the response does carry wins over the catalog.+func TestCheapaimodelListModelsUsesCatalogContextLength(t *testing.T) {+ withSSRFBypass(t)+ dir, restore := setupProviderTestDir(t, "cheapaimodel.json")+ defer restore()+ if err := InitProviderManager(dir); err != nil {+ t.Fatalf("InitProviderManager: %v", err)+ }++ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ _ = json.NewEncoder(w).Encode(map[string]any{+ "data": []map[string]any{+ {"id": "glm-5.3"},+ {"id": "grok-4.7", "context_length": 131072},+ {"id": "some-new-model"},+ },+ })+ }))+ defer srv.Close()++ list, err := newCheapaimodelForTest(srv.URL).ListModels(t.Context(), &APIConfig{ApiKey: &testAPIKey})+ if err != nil {+ t.Fatalf("ListModels: %v", err)+ }+ catalog := GetProviderManager().FindProvider("cheapaimodel")+ glm := GetProviderManager().FindModel(catalog, "glm-5.3")+ if glm == nil || glm.ContextLength == nil {+ t.Fatal("glm-5.3 has no catalogued context length")+ }+ want := map[string]*int{"glm-5.3": glm.ContextLength, "grok-4.7": intPtr(131072), "some-new-model": nil}+ if len(list) != len(want) {+ t.Fatalf("models=%v", list)+ }+ for _, model := range list {+ got, exp := model.ContextLength, want[model.Name]+ if (got == nil) != (exp == nil) || (got != nil && *got != *exp) {+ t.Errorf("%s context_length=%v, want %v", model.Name, deref(got), deref(exp))+ }+ }+}++func intPtr(v int) *int { return &v }++func deref(p *int) any {+ if p == nil {+ return nil+ }+ return *p+}diff --git a/internal/entity/models/factory.go b/internal/entity/models/factory.goindex a34f1bcc36..5ece573e90 100644--- a/internal/entity/models/factory.go+++ b/internal/entity/models/factory.go@@ -207,6 +207,8 @@ func (f *ModelFactory) CreateModelDriver(providerName string, baseURL map[stringreturn NewCloudGPUModel(baseURL, urlSuffix), nilcase "obscurify.ai":return NewObscurifyModel(baseURL, urlSuffix), nil+ case "cheapaimodel":+ return NewCheapaimodelModel(baseURL, urlSuffix), nilcase "new api":return NewNewAPIModel(baseURL, urlSuffix), nilcase "hostinger router":diff --git a/web/src/assets/svg/llm/cheapaimodel.svg b/web/src/assets/svg/llm/cheapaimodel.svgnew file mode 100644index 0000000000..c48a4dd10a--- /dev/null+++ b/web/src/assets/svg/llm/cheapaimodel.svg@@ -0,0 +1,4 @@+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="cheapaimodel">+ <rect width="64" height="64" rx="14" fill="#121417"/>+ <path transform="translate(14.4 10) scale(1.4667)" fill="#fb6b12" d="M0 29L6 27V12Q6 9 3 10Q0 11 0 14ZM9 26L15 24V3Q15 0 12 1Q9 2 9 5ZM18 23L24 21V6Q24 3 21 4Q18 5 18 8Z"/>+</svg>diff --git a/web/src/components/svg-icon.tsx b/web/src/components/svg-icon.tsxindex 83667908c8..105dcee343 100644--- a/web/src/components/svg-icon.tsx+++ b/web/src/components/svg-icon.tsx@@ -126,6 +126,7 @@ const svgIcons = [LLMFactory.CloudGPU,LLMFactory.Obscurify,LLMFactory.HostingerRouter,+ LLMFactory.Cheapaimodel,];export const LlmIcon = ({diff --git a/web/src/constants/llm.ts b/web/src/constants/llm.tsindex d150ef07fb..673e4dcf61 100644--- a/web/src/constants/llm.ts+++ b/web/src/constants/llm.ts@@ -120,6 +120,7 @@ export enum LLMFactory {WorldRouter = 'WorldRouter',CloudGPU = 'CloudGPU',Obscurify = 'Obscurify.ai',+ Cheapaimodel = 'cheapaimodel',SystemOne = 'SystemOne',HostingerRouter = 'Hostinger Router',}@@ -225,6 +226,7 @@ export const IconMap = {[LLMFactory.CloudGPU]: 'cloudgpu',[LLMFactory.Obscurify]: 'obscurify',[LLMFactory.HostingerRouter]: 'hostinger-router',+ [LLMFactory.Cheapaimodel]: 'cheapaimodel',};/**@@ -278,6 +280,8 @@ export const APIMapUrl = {[LLMFactory.CloudGPU]: 'https://cloudgpu.app/api/console',[LLMFactory.Obscurify]: 'https://obscurify.ai/account',[LLMFactory.HostingerRouter]: 'https://hpanel.hostinger.com/ai-router',+ [LLMFactory.Cheapaimodel]:+ 'https://console.cheapaimodel.ai/keys?utm_source=ragflow',[LLMFactory.MWS]:'https://mws.ru/docs/cloud-platform/gpt/general/inference-text.html',[LLMFactory.Anthropic]: 'https://console.anthropic.com/settings/keys',