mirror of
https://github.com/mudler/LocalAI.git
synced 2026-02-06 20:54:37 -05:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2adddef5fe | ||
|
|
d89c7b731a |
@@ -53,12 +53,12 @@ type MCPErrorEvent struct {
|
|||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MCPStreamEndpoint is the SSE streaming endpoint for MCP chat completions
|
// MCPEndpoint is the endpoint for MCP chat completions. Supports SSE mode, but it is not compatible with the OpenAI apis.
|
||||||
// @Summary Stream MCP chat completions with reasoning, tool calls, and results
|
// @Summary Stream MCP chat completions with reasoning, tool calls, and results
|
||||||
// @Param request body schema.OpenAIRequest true "query params"
|
// @Param request body schema.OpenAIRequest true "query params"
|
||||||
// @Success 200 {object} schema.OpenAIResponse "Response"
|
// @Success 200 {object} schema.OpenAIResponse "Response"
|
||||||
// @Router /v1/mcp/chat/completions [post]
|
// @Router /v1/mcp/chat/completions [post]
|
||||||
func MCPStreamEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator *templates.Evaluator, appConfig *config.ApplicationConfig) echo.HandlerFunc {
|
func MCPEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator *templates.Evaluator, appConfig *config.ApplicationConfig) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
ctx := c.Request().Context()
|
ctx := c.Request().Context()
|
||||||
created := int(time.Now().Unix())
|
created := int(time.Now().Unix())
|
||||||
|
|||||||
@@ -1,148 +0,0 @@
|
|||||||
package openai
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/mudler/LocalAI/core/config"
|
|
||||||
mcpTools "github.com/mudler/LocalAI/core/http/endpoints/mcp"
|
|
||||||
"github.com/mudler/LocalAI/core/http/middleware"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/mudler/LocalAI/core/schema"
|
|
||||||
"github.com/mudler/LocalAI/core/templates"
|
|
||||||
"github.com/mudler/LocalAI/pkg/model"
|
|
||||||
"github.com/mudler/cogito"
|
|
||||||
"github.com/mudler/xlog"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MCPCompletionEndpoint is the OpenAI Completion API endpoint https://platform.openai.com/docs/api-reference/completions
|
|
||||||
// @Summary Generate completions for a given prompt and model.
|
|
||||||
// @Param request body schema.OpenAIRequest true "query params"
|
|
||||||
// @Success 200 {object} schema.OpenAIResponse "Response"
|
|
||||||
// @Router /mcp/v1/completions [post]
|
|
||||||
func MCPCompletionEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, evaluator *templates.Evaluator, appConfig *config.ApplicationConfig) echo.HandlerFunc {
|
|
||||||
// We do not support streaming mode (Yet?)
|
|
||||||
return func(c echo.Context) error {
|
|
||||||
created := int(time.Now().Unix())
|
|
||||||
|
|
||||||
ctx := c.Request().Context()
|
|
||||||
|
|
||||||
// Handle Correlation
|
|
||||||
id := c.Request().Header.Get("X-Correlation-ID")
|
|
||||||
if id == "" {
|
|
||||||
id = uuid.New().String()
|
|
||||||
}
|
|
||||||
|
|
||||||
input, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_LOCALAI_REQUEST).(*schema.OpenAIRequest)
|
|
||||||
if !ok || input.Model == "" {
|
|
||||||
return echo.ErrBadRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
config, ok := c.Get(middleware.CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.ModelConfig)
|
|
||||||
if !ok || config == nil {
|
|
||||||
return echo.ErrBadRequest
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.MCP.Servers == "" && config.MCP.Stdio == "" {
|
|
||||||
return fmt.Errorf("no MCP servers configured")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get MCP config from model config
|
|
||||||
remote, stdio, err := config.MCP.MCPConfigFromYAML()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get MCP config: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we have tools in cache, or we have to have an initial connection
|
|
||||||
sessions, err := mcpTools.SessionsFromMCPConfig(config.Name, remote, stdio)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get MCP sessions: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(sessions) == 0 {
|
|
||||||
return fmt.Errorf("no working MCP servers found")
|
|
||||||
}
|
|
||||||
|
|
||||||
fragment := cogito.NewEmptyFragment()
|
|
||||||
|
|
||||||
for _, message := range input.Messages {
|
|
||||||
fragment = fragment.AddMessage(message.Role, message.StringContent)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, port, err := net.SplitHostPort(appConfig.APIAddress)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
apiKey := ""
|
|
||||||
if appConfig.ApiKeys != nil {
|
|
||||||
apiKey = appConfig.ApiKeys[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
ctxWithCancellation, cancel := context.WithCancel(ctx)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// TODO: instead of connecting to the API, we should just wire this internally
|
|
||||||
// and act like completion.go.
|
|
||||||
// We can do this as cogito expects an interface and we can create one that
|
|
||||||
// we satisfy to just call internally ComputeChoices
|
|
||||||
defaultLLM := cogito.NewOpenAILLM(config.Name, apiKey, "http://127.0.0.1:"+port)
|
|
||||||
|
|
||||||
// Build cogito options using the consolidated method
|
|
||||||
cogitoOpts := config.BuildCogitoOptions()
|
|
||||||
|
|
||||||
cogitoOpts = append(
|
|
||||||
cogitoOpts,
|
|
||||||
cogito.WithContext(ctxWithCancellation),
|
|
||||||
cogito.WithMCPs(sessions...),
|
|
||||||
cogito.WithStatusCallback(func(s string) {
|
|
||||||
xlog.Debug("[model agent] Status", "model", config.Name, "status", s)
|
|
||||||
}),
|
|
||||||
cogito.WithReasoningCallback(func(s string) {
|
|
||||||
xlog.Debug("[model agent] Reasoning", "model", config.Name, "reasoning", s)
|
|
||||||
}),
|
|
||||||
cogito.WithToolCallBack(func(t *cogito.ToolChoice, state *cogito.SessionState) cogito.ToolCallDecision {
|
|
||||||
xlog.Debug("[model agent] Tool call", "model", config.Name, "tool", t.Name, "reasoning", t.Reasoning, "arguments", t.Arguments)
|
|
||||||
return cogito.ToolCallDecision{
|
|
||||||
Approved: true,
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
cogito.WithToolCallResultCallback(func(t cogito.ToolStatus) {
|
|
||||||
xlog.Debug("[model agent] Tool call result", "model", config.Name, "tool", t.Name, "result", t.Result, "tool_arguments", t.ToolArguments)
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
f, err := cogito.ExecuteTools(
|
|
||||||
defaultLLM, fragment,
|
|
||||||
cogitoOpts...,
|
|
||||||
)
|
|
||||||
if err != nil && !errors.Is(err, cogito.ErrNoToolSelected) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err = defaultLLM.Ask(ctx, f)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp := &schema.OpenAIResponse{
|
|
||||||
ID: id,
|
|
||||||
Created: created,
|
|
||||||
Model: input.Model, // we have to return what the user sent here, due to OpenAI spec.
|
|
||||||
Choices: []schema.Choice{{Message: &schema.Message{Role: "assistant", Content: &f.LastMessage().Content}}},
|
|
||||||
Object: "text_completion",
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonResult, _ := json.Marshal(resp)
|
|
||||||
xlog.Debug("Response", "response", string(jsonResult))
|
|
||||||
|
|
||||||
// Return the prediction in the response body
|
|
||||||
return c.JSON(200, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -137,9 +137,10 @@ func RegisterLocalAIRoutes(router *echo.Echo,
|
|||||||
requestExtractor.BuildFilteredFirstAvailableDefaultModel(config.BuildUsecaseFilterFn(config.FLAG_TOKENIZE)),
|
requestExtractor.BuildFilteredFirstAvailableDefaultModel(config.BuildUsecaseFilterFn(config.FLAG_TOKENIZE)),
|
||||||
requestExtractor.SetModelAndConfig(func() schema.LocalAIRequest { return new(schema.TokenizeRequest) }))
|
requestExtractor.SetModelAndConfig(func() schema.LocalAIRequest { return new(schema.TokenizeRequest) }))
|
||||||
|
|
||||||
// MCP Stream endpoint
|
// MCP endpoint - supports both streaming and non-streaming modes
|
||||||
|
// Note: streaming mode is NOT compatible with the OpenAI apis. We have a set which streams more states.
|
||||||
if evaluator != nil {
|
if evaluator != nil {
|
||||||
mcpStreamHandler := localai.MCPStreamEndpoint(cl, ml, evaluator, appConfig)
|
mcpStreamHandler := localai.MCPEndpoint(cl, ml, evaluator, appConfig)
|
||||||
mcpStreamMiddleware := []echo.MiddlewareFunc{
|
mcpStreamMiddleware := []echo.MiddlewareFunc{
|
||||||
requestExtractor.BuildFilteredFirstAvailableDefaultModel(config.BuildUsecaseFilterFn(config.FLAG_CHAT)),
|
requestExtractor.BuildFilteredFirstAvailableDefaultModel(config.BuildUsecaseFilterFn(config.FLAG_CHAT)),
|
||||||
requestExtractor.SetModelAndConfig(func() schema.LocalAIRequest { return new(schema.OpenAIRequest) }),
|
requestExtractor.SetModelAndConfig(func() schema.LocalAIRequest { return new(schema.OpenAIRequest) }),
|
||||||
@@ -154,6 +155,7 @@ func RegisterLocalAIRoutes(router *echo.Echo,
|
|||||||
}
|
}
|
||||||
router.POST("/v1/mcp/chat/completions", mcpStreamHandler, mcpStreamMiddleware...)
|
router.POST("/v1/mcp/chat/completions", mcpStreamHandler, mcpStreamMiddleware...)
|
||||||
router.POST("/mcp/v1/chat/completions", mcpStreamHandler, mcpStreamMiddleware...)
|
router.POST("/mcp/v1/chat/completions", mcpStreamHandler, mcpStreamMiddleware...)
|
||||||
|
router.POST("/mcp/chat/completions", mcpStreamHandler, mcpStreamMiddleware...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Agent job routes
|
// Agent job routes
|
||||||
|
|||||||
@@ -79,24 +79,6 @@ func RegisterOpenAIRoutes(app *echo.Echo,
|
|||||||
app.POST("/completions", completionHandler, completionMiddleware...)
|
app.POST("/completions", completionHandler, completionMiddleware...)
|
||||||
app.POST("/v1/engines/:model/completions", completionHandler, completionMiddleware...)
|
app.POST("/v1/engines/:model/completions", completionHandler, completionMiddleware...)
|
||||||
|
|
||||||
// MCPcompletion
|
|
||||||
mcpCompletionHandler := openai.MCPCompletionEndpoint(application.ModelConfigLoader(), application.ModelLoader(), application.TemplatesEvaluator(), application.ApplicationConfig())
|
|
||||||
mcpCompletionMiddleware := []echo.MiddlewareFunc{
|
|
||||||
traceMiddleware,
|
|
||||||
re.BuildFilteredFirstAvailableDefaultModel(config.BuildUsecaseFilterFn(config.FLAG_CHAT)),
|
|
||||||
re.SetModelAndConfig(func() schema.LocalAIRequest { return new(schema.OpenAIRequest) }),
|
|
||||||
func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
||||||
return func(c echo.Context) error {
|
|
||||||
if err := re.SetOpenAIRequest(c); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return next(c)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
app.POST("/mcp/v1/chat/completions", mcpCompletionHandler, mcpCompletionMiddleware...)
|
|
||||||
app.POST("/mcp/chat/completions", mcpCompletionHandler, mcpCompletionMiddleware...)
|
|
||||||
|
|
||||||
// embeddings
|
// embeddings
|
||||||
embeddingHandler := openai.EmbeddingsEndpoint(application.ModelConfigLoader(), application.ModelLoader(), application.ApplicationConfig())
|
embeddingHandler := openai.EmbeddingsEndpoint(application.ModelConfigLoader(), application.ModelLoader(), application.ApplicationConfig())
|
||||||
embeddingMiddleware := []echo.MiddlewareFunc{
|
embeddingMiddleware := []echo.MiddlewareFunc{
|
||||||
|
|||||||
@@ -954,7 +954,7 @@ func RegisterUIAPIRoutes(app *echo.Echo, cl *config.ModelConfigLoader, ml *model
|
|||||||
if !appConfig.EnableTracing {
|
if !appConfig.EnableTracing {
|
||||||
return c.JSON(503, map[string]any{
|
return c.JSON(503, map[string]any{
|
||||||
"error": "Tracing disabled",
|
"error": "Tracing disabled",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
traces := middleware.GetTraces()
|
traces := middleware.GetTraces()
|
||||||
return c.JSON(200, map[string]interface{}{
|
return c.JSON(200, map[string]interface{}{
|
||||||
|
|||||||
Reference in New Issue
Block a user