feat(ollama): improve configuration and dashboard integration
CI / Lint (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Build (push) Has been cancelled

This commit is contained in:
2026-04-07 12:53:17 +00:00
parent 1b5cd2815e
commit 1e13b0376b
4 changed files with 197 additions and 7 deletions
+80 -6
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"gophergate/internal/config"
"gophergate/internal/models"
@@ -29,7 +30,16 @@ func (p *GeminiProvider) Name() string {
}
type GeminiRequest struct {
Contents []GeminiContent `json:"contents"`
Contents []GeminiContent `json:"contents"`
GenerationConfig *GeminiGenerationConfig `json:"generationConfig,omitempty"`
}
type GeminiGenerationConfig struct {
Temperature *float32 `json:"temperature,omitempty"`
TopP *float32 `json:"topP,omitempty"`
TopK *int `json:"topK,omitempty"`
MaxOutputTokens *int `json:"maxOutputTokens,omitempty"`
StopSequences []string `json:"stopSequences,omitempty"`
}
type GeminiContent struct {
@@ -125,11 +135,43 @@ func (p *GeminiProvider) ChatCompletion(ctx context.Context, req *models.Unified
})
}
body := GeminiRequest{
Contents: contents,
genConfig := &GeminiGenerationConfig{}
if req.Temperature != nil {
t := float32(*req.Temperature)
genConfig.Temperature = &t
}
if req.TopP != nil {
tp := float32(*req.TopP)
genConfig.TopP = &tp
}
if req.TopK != nil {
tk := int(*req.TopK)
genConfig.TopK = &tk
}
if req.MaxTokens != nil {
mt := int(*req.MaxTokens)
genConfig.MaxOutputTokens = &mt
}
if len(req.Stop) > 0 {
genConfig.StopSequences = req.Stop
}
url := fmt.Sprintf("%s/models/%s:generateContent?key=%s", p.config.BaseURL, req.Model, p.apiKey)
body := GeminiRequest{
Contents: contents,
GenerationConfig: genConfig,
}
baseURL := p.config.BaseURL
lowerModel := strings.ToLower(req.Model)
if strings.Contains(lowerModel, "preview") || strings.Contains(lowerModel, "3.1") || strings.Contains(lowerModel, "2.0") || strings.Contains(lowerModel, "thinking") {
// Use v1beta for preview and newer models
if !strings.Contains(baseURL, "v1beta") {
baseURL = strings.Replace(baseURL, "/v1", "/v1beta", 1)
}
}
url := fmt.Sprintf("%s/models/%s:generateContent?key=%s", baseURL, req.Model, p.apiKey)
fmt.Printf("[Gemini] POST %s\n", url)
resp, err := p.client.R().
SetContext(ctx).
@@ -219,12 +261,44 @@ func (p *GeminiProvider) ChatCompletionStream(ctx context.Context, req *models.U
})
}
genConfig := &GeminiGenerationConfig{}
if req.Temperature != nil {
t := float32(*req.Temperature)
genConfig.Temperature = &t
}
if req.TopP != nil {
tp := float32(*req.TopP)
genConfig.TopP = &tp
}
if req.TopK != nil {
tk := int(*req.TopK)
genConfig.TopK = &tk
}
if req.MaxTokens != nil {
mt := int(*req.MaxTokens)
genConfig.MaxOutputTokens = &mt
}
if len(req.Stop) > 0 {
genConfig.StopSequences = req.Stop
}
body := GeminiRequest{
Contents: contents,
Contents: contents,
GenerationConfig: genConfig,
}
baseURL := p.config.BaseURL
lowerModel := strings.ToLower(req.Model)
if strings.Contains(lowerModel, "preview") || strings.Contains(lowerModel, "3.1") || strings.Contains(lowerModel, "2.0") || strings.Contains(lowerModel, "thinking") {
// Use v1beta for preview and newer models
if !strings.Contains(baseURL, "v1beta") {
baseURL = strings.Replace(baseURL, "/v1", "/v1beta", 1)
}
}
// Use streamGenerateContent for streaming
url := fmt.Sprintf("%s/models/%s:streamGenerateContent?key=%s", p.config.BaseURL, req.Model, p.apiKey)
url := fmt.Sprintf("%s/models/%s:streamGenerateContent?key=%s", baseURL, req.Model, p.apiKey)
fmt.Printf("[Gemini-Stream] POST %s\n", url)
resp, err := p.client.R().
SetContext(ctx).