feat: introduce WeKnora Lite edition with lightweight configuration and deployment
- Added a new `.env.lite.example` file for the Lite version, providing a minimal configuration template. - Updated `.env.example` to remove deprecated variables and include new Docreader settings. - Enhanced Docker configurations to support the Lite version, including a new Dockerfile for the Docreader service. - Introduced a Makefile target for building and running the Lite version, along with packaging capabilities. - Created GitHub workflows for building and releasing Lite binaries, including Homebrew formula support. - Implemented a new service file for managing the Lite version as a system service. This update enables a streamlined, single-binary deployment of WeKnora, reducing external dependencies and simplifying setup.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package vlm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
|
||||
ollamaapi "github.com/ollama/ollama/api"
|
||||
)
|
||||
|
||||
// OllamaVLM implements VLM via the local Ollama service.
|
||||
type OllamaVLM struct {
|
||||
modelName string
|
||||
modelID string
|
||||
ollamaService *ollama.OllamaService
|
||||
}
|
||||
|
||||
// NewOllamaVLM creates an Ollama-backed VLM instance.
|
||||
func NewOllamaVLM(config *Config, ollamaService *ollama.OllamaService) (*OllamaVLM, error) {
|
||||
if ollamaService == nil {
|
||||
return nil, fmt.Errorf("ollama service is required for local VLM model")
|
||||
}
|
||||
return &OllamaVLM{
|
||||
modelName: config.ModelName,
|
||||
modelID: config.ModelID,
|
||||
ollamaService: ollamaService,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Predict sends an image with a text prompt to the Ollama vision model.
|
||||
func (v *OllamaVLM) Predict(ctx context.Context, imgBytes []byte, prompt string) (string, error) {
|
||||
streamFlag := false
|
||||
chatReq := &ollamaapi.ChatRequest{
|
||||
Model: v.modelName,
|
||||
Messages: []ollamaapi.Message{
|
||||
{
|
||||
Role: "user",
|
||||
Content: prompt,
|
||||
Images: []ollamaapi.ImageData{imgBytes},
|
||||
},
|
||||
},
|
||||
Stream: &streamFlag,
|
||||
Options: map[string]interface{}{"temperature": 0.1},
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[VLM] Calling Ollama API, model=%s, imageSize=%d", v.modelName, len(imgBytes))
|
||||
|
||||
var result string
|
||||
err := v.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error {
|
||||
result = resp.Message.Content
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Ollama VLM request: %w", err)
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[VLM] Ollama response received, len=%d", len(result))
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (v *OllamaVLM) GetModelName() string { return v.modelName }
|
||||
func (v *OllamaVLM) GetModelID() string { return v.modelID }
|
||||
@@ -0,0 +1,101 @@
|
||||
package vlm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTimeout = 30 * time.Second
|
||||
defaultMaxToks = 5000
|
||||
defaultTemp = float32(0.1)
|
||||
)
|
||||
|
||||
// RemoteAPIVLM implements VLM via an OpenAI-compatible chat completions API.
|
||||
type RemoteAPIVLM struct {
|
||||
modelName string
|
||||
modelID string
|
||||
client *openai.Client
|
||||
baseURL string
|
||||
}
|
||||
|
||||
// NewRemoteAPIVLM creates a remote-API backed VLM instance.
|
||||
func NewRemoteAPIVLM(config *Config) (*RemoteAPIVLM, error) {
|
||||
apiCfg := openai.DefaultConfig(config.APIKey)
|
||||
if config.BaseURL != "" {
|
||||
apiCfg.BaseURL = config.BaseURL
|
||||
}
|
||||
apiCfg.HTTPClient = &http.Client{Timeout: defaultTimeout}
|
||||
|
||||
return &RemoteAPIVLM{
|
||||
modelName: config.ModelName,
|
||||
modelID: config.ModelID,
|
||||
client: openai.NewClientWithConfig(apiCfg),
|
||||
baseURL: config.BaseURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Predict sends an image with a text prompt to the OpenAI-compatible API.
|
||||
func (v *RemoteAPIVLM) Predict(ctx context.Context, imgBytes []byte, prompt string) (string, error) {
|
||||
mimeType := detectImageMIME(imgBytes)
|
||||
b64 := base64.StdEncoding.EncodeToString(imgBytes)
|
||||
dataURI := fmt.Sprintf("data:%s;base64,%s", mimeType, b64)
|
||||
|
||||
req := openai.ChatCompletionRequest{
|
||||
Model: v.modelName,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
MultiContent: []openai.ChatMessagePart{
|
||||
{
|
||||
Type: openai.ChatMessagePartTypeImageURL,
|
||||
ImageURL: &openai.ChatMessageImageURL{
|
||||
URL: dataURI,
|
||||
Detail: openai.ImageURLDetailAuto,
|
||||
},
|
||||
},
|
||||
{
|
||||
Type: openai.ChatMessagePartTypeText,
|
||||
Text: prompt,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
MaxTokens: defaultMaxToks,
|
||||
Temperature: defaultTemp,
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[VLM] Calling OpenAI-compatible API, model=%s, baseURL=%s, imageSize=%d",
|
||||
v.modelName, v.baseURL, len(imgBytes))
|
||||
|
||||
resp, err := v.client.CreateChatCompletion(ctx, req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("OpenAI VLM request: %w", err)
|
||||
}
|
||||
if len(resp.Choices) == 0 {
|
||||
return "", fmt.Errorf("OpenAI VLM returned no choices")
|
||||
}
|
||||
|
||||
content := resp.Choices[0].Message.Content
|
||||
logger.Infof(ctx, "[VLM] OpenAI response received, len=%d", len(content))
|
||||
return content, nil
|
||||
}
|
||||
|
||||
func (v *RemoteAPIVLM) GetModelName() string { return v.modelName }
|
||||
func (v *RemoteAPIVLM) GetModelID() string { return v.modelID }
|
||||
|
||||
// detectImageMIME returns the MIME type for the given image bytes.
|
||||
func detectImageMIME(data []byte) string {
|
||||
ct := http.DetectContentType(data)
|
||||
if strings.HasPrefix(ct, "image/") {
|
||||
return ct
|
||||
}
|
||||
return "image/png"
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package vlm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/models/utils/ollama"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// VLM defines the interface for Vision Language Model operations.
|
||||
type VLM interface {
|
||||
// Predict sends an image with a text prompt to the VLM and returns the generated text.
|
||||
Predict(ctx context.Context, imgBytes []byte, prompt string) (string, error)
|
||||
|
||||
GetModelName() string
|
||||
GetModelID() string
|
||||
}
|
||||
|
||||
// Config holds the configuration needed to create a VLM instance.
|
||||
type Config struct {
|
||||
Source types.ModelSource
|
||||
BaseURL string
|
||||
ModelName string
|
||||
APIKey string
|
||||
ModelID string
|
||||
InterfaceType string // "ollama" or "openai" (default)
|
||||
}
|
||||
|
||||
// NewVLM creates a VLM instance based on the provided configuration.
|
||||
func NewVLM(config *Config, ollamaService *ollama.OllamaService) (VLM, error) {
|
||||
ifType := strings.ToLower(config.InterfaceType)
|
||||
|
||||
if ifType == "ollama" || config.Source == types.ModelSourceLocal {
|
||||
return NewOllamaVLM(config, ollamaService)
|
||||
}
|
||||
return NewRemoteAPIVLM(config)
|
||||
}
|
||||
|
||||
// NewVLMFromLegacyConfig creates a VLM from a legacy VLMConfig (inline BaseURL/APIKey/ModelName).
|
||||
func NewVLMFromLegacyConfig(vlmCfg types.VLMConfig, ollamaService *ollama.OllamaService) (VLM, error) {
|
||||
if !vlmCfg.IsEnabled() {
|
||||
return nil, fmt.Errorf("VLM config is not enabled")
|
||||
}
|
||||
|
||||
ifType := vlmCfg.InterfaceType
|
||||
if ifType == "" {
|
||||
ifType = "openai"
|
||||
}
|
||||
|
||||
source := types.ModelSourceRemote
|
||||
if strings.EqualFold(ifType, "ollama") {
|
||||
source = types.ModelSourceLocal
|
||||
}
|
||||
|
||||
return NewVLM(&Config{
|
||||
Source: source,
|
||||
BaseURL: vlmCfg.BaseURL,
|
||||
ModelName: vlmCfg.ModelName,
|
||||
APIKey: vlmCfg.APIKey,
|
||||
InterfaceType: ifType,
|
||||
}, ollamaService)
|
||||
}
|
||||
Reference in New Issue
Block a user