mirror of
https://github.com/aljazceru/ollama-free-model-proxy.git
synced 2025-12-17 05:04:20 +01:00
added model filtering and enchanted app compatibly
This commit is contained in:
70
main.go
70
main.go
@@ -1,18 +1,46 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
openai "github.com/sashabaranov/go-openai"
|
openai "github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var modelFilter map[string]struct{}
|
||||||
|
|
||||||
|
func loadModelFilter(path string) (map[string]struct{}, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
filter := make(map[string]struct{})
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
if line != "" {
|
||||||
|
filter[line] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return filter, nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
// Load the API key from environment variables or command-line arguments.
|
// Load the API key from environment variables or command-line arguments.
|
||||||
@@ -28,6 +56,23 @@ func main() {
|
|||||||
|
|
||||||
provider := NewOpenrouterProvider(apiKey)
|
provider := NewOpenrouterProvider(apiKey)
|
||||||
|
|
||||||
|
filter, err := loadModelFilter("models-filter")
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
slog.Info("models-filter file not found. Skipping model filtering.")
|
||||||
|
modelFilter = make(map[string]struct{})
|
||||||
|
} else {
|
||||||
|
slog.Error("Error loading models filter", "Error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
modelFilter = filter
|
||||||
|
slog.Info("Loaded models from filter:")
|
||||||
|
for model := range modelFilter {
|
||||||
|
slog.Info(" - " + model)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
r.GET("/", func(c *gin.Context) {
|
r.GET("/", func(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "Ollama is running")
|
c.String(http.StatusOK, "Ollama is running")
|
||||||
})
|
})
|
||||||
@@ -42,8 +87,27 @@ func main() {
|
|||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Respond with the list of models
|
filter := modelFilter
|
||||||
c.JSON(http.StatusOK, gin.H{"models": models})
|
// Construct a new array of model objects with extra fields
|
||||||
|
newModels := make([]map[string]interface{}, 0, len(models))
|
||||||
|
for _, m := range models {
|
||||||
|
// Если фильтр пустой, значит пропускаем проверку и берём все модели
|
||||||
|
if len(filter) > 0 {
|
||||||
|
if _, ok := filter[m.Model]; !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newModels = append(newModels, map[string]interface{}{
|
||||||
|
"name": m.Name,
|
||||||
|
"model": m.Model,
|
||||||
|
"modified_at": m.ModifiedAt,
|
||||||
|
"size": 270898672,
|
||||||
|
"digest": "9077fe9d2ae1a4a41a868836b56b8163731a8fe16621397028c2c76f838c6907",
|
||||||
|
"details": m.Details,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"models": newModels})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.POST("/api/show", func(c *gin.Context) {
|
r.POST("/api/show", func(c *gin.Context) {
|
||||||
@@ -164,5 +228,5 @@ func main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Run(":8080")
|
r.Run(":11434")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
# Ollama Proxy for OpenRouter
|
# Enchanted Proxy for OpenRouter
|
||||||
|
This repository is specifically made for use with the [Enchanted project](https://github.com/gluonfield/enchanted/tree/main).
|
||||||
|
The original author of this proxy is [marknefedov](https://github.com/marknefedov/ollama-openrouter-proxy).
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
This repository provides a proxy server that emulates [Ollama's REST API](https://github.com/ollama/ollama) but forwards requests to [OpenRouter](https://openrouter.ai/). It uses the [sashabaranov/go-openai](https://github.com/sashabaranov/go-openai) library under the hood, with minimal code changes to keep the Ollama API calls the same. This allows you to use Ollama-compatible tooling and clients, but run your requests on OpenRouter-managed models.
|
This repository provides a proxy server that emulates [Ollama's REST API](https://github.com/ollama/ollama) but forwards requests to [OpenRouter](https://openrouter.ai/). It uses the [sashabaranov/go-openai](https://github.com/sashabaranov/go-openai) library under the hood, with minimal code changes to keep the Ollama API calls the same. This allows you to use Ollama-compatible tooling and clients, but run your requests on OpenRouter-managed models.
|
||||||
Currently, it is enough for usage with [Jetbrains AI assistant](https://blog.jetbrains.com/ai/2024/11/jetbrains-ai-assistant-2024-3/#more-control-over-your-chat-experience-choose-between-gemini,-openai,-and-local-models).
|
Currently, it is enough for usage with [Jetbrains AI assistant](https://blog.jetbrains.com/ai/2024/11/jetbrains-ai-assistant-2024-3/#more-control-over-your-chat-experience-choose-between-gemini,-openai,-and-local-models).
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
- **Model Filtering**: You can provide a `models-filter` file in the same directory as the proxy. Each line in this file should contain a single model name. The proxy will only show models that match these entries. If the file doesn’t exist or is empty, no filtering is applied.
|
||||||
|
|
||||||
|
**Note**: OpenRouter model names may sometimes include a vendor prefix, for example `deepseek/deepseek-chat-v3-0324:free`. To make sure filtering works correctly, remove the vendor part when adding the name to your `models-filter` file, e.g. `deepseek-chat-v3-0324:free`.
|
||||||
|
|
||||||
- **Ollama-like API**: The server listens on `8080` and exposes endpoints similar to Ollama (e.g., `/api/chat`, `/api/tags`).
|
- **Ollama-like API**: The server listens on `8080` and exposes endpoints similar to Ollama (e.g., `/api/chat`, `/api/tags`).
|
||||||
- **Model Listing**: Fetch a list of available models from OpenRouter.
|
- **Model Listing**: Fetch a list of available models from OpenRouter.
|
||||||
- **Model Details**: Retrieve metadata about a specific model.
|
- **Model Details**: Retrieve metadata about a specific model.
|
||||||
|
|||||||
Reference in New Issue
Block a user