Ajout de SEARXNG pour les recherches web
This commit is contained in:
1 parent
87eeb41364
commit
345129c7bf
30 files changed
+919
-166
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
"trankilou.fr/lassistanoque/backend/internal/service/tool"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -17,6 +18,7 @@ type Service struct {
|
||||
repoChat domain.ChatRepository
|
||||
llmEngine domain.LLMEngine
|
||||
runningSessions map[string]*ChatSession
|
||||
toolService *tool.Service
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
@@ -26,6 +28,7 @@ func NewService(
|
||||
repoProvider domain.ProviderRepository,
|
||||
repoChat domain.ChatRepository,
|
||||
llmEngine domain.LLMEngine,
|
||||
toolService *tool.Service,
|
||||
) *Service {
|
||||
return &Service{
|
||||
repoUser: repoUser,
|
||||
@@ -33,6 +36,7 @@ func NewService(
|
||||
repoProvider: repoProvider,
|
||||
repoChat: repoChat,
|
||||
llmEngine: llmEngine,
|
||||
toolService: toolService,
|
||||
runningSessions: make(map[string]*ChatSession),
|
||||
}
|
||||
}
|
||||
@@ -194,6 +198,7 @@ func (s *Service) runQuery(
|
||||
modelID: modelID,
|
||||
messages: messages,
|
||||
llmEngine: s.llmEngine,
|
||||
toolService: s.toolService,
|
||||
subscribers: make([]domain.StreamCallback, 0),
|
||||
repoChat: s.repoChat,
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package chat
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"log/slog"
|
||||
"sync"
|
||||
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
"trankilou.fr/lassistanoque/backend/internal/service/tool"
|
||||
)
|
||||
|
||||
//go:embed system_prompt.md
|
||||
@@ -20,6 +22,7 @@ type ChatSession struct {
|
||||
modelID string
|
||||
messages []*domain.Message
|
||||
llmEngine domain.LLMEngine
|
||||
toolService *tool.Service
|
||||
repoChat domain.ChatRepository
|
||||
subscribers []domain.StreamCallback
|
||||
mu sync.Mutex
|
||||
@@ -30,7 +33,7 @@ func (s *ChatSession) run(ctx context.Context) error {
|
||||
params := &domain.LLMParams{}
|
||||
|
||||
tools := make([]*domain.ToolDefinition, 0)
|
||||
for _, t := range allTools {
|
||||
for _, t := range s.toolService.GetAllToolImpl() {
|
||||
tools = append(tools, t.Definition(ctx))
|
||||
}
|
||||
params.Tools = tools
|
||||
@@ -76,8 +79,21 @@ func (s *ChatSession) run(ctx context.Context) error {
|
||||
continue_loop = true
|
||||
for _, tc := range msg.ToolCalls {
|
||||
|
||||
tool := GetTool(tc.Function.Name)
|
||||
output, err := tool.Execute(ctx, []byte(tc.Function.Arguments))
|
||||
toolImpl := s.toolService.GetToolImpl(tc.Function.Name)
|
||||
slog.Info("Execute tool", "name", tc.Function.Name, "impl", toolImpl)
|
||||
toolRecord, err := s.toolService.GetToolByName(s.userID, s.teamID, toolImpl.Name())
|
||||
if err != nil {
|
||||
slog.Info("Execute tool", "error", err)
|
||||
toolResponse := &domain.Message{
|
||||
ChatID: s.chatID,
|
||||
TeamID: s.teamID,
|
||||
ToolCallID: tc.ID,
|
||||
Role: string(domain.RoleTool),
|
||||
Content: "ERROR: " + err.Error(),
|
||||
}
|
||||
s.repoChat.CreateChatMessage(s.userID, toolResponse)
|
||||
}
|
||||
output, err := toolImpl.Execute(ctx, []byte(tc.Function.Arguments), toolRecord.Configuration)
|
||||
var toolResponse *domain.Message
|
||||
if err != nil {
|
||||
toolResponse = &domain.Message{
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package chat
|
||||
|
||||
import "trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
|
||||
var allTools = make(map[string]domain.Tool)
|
||||
|
||||
func RegisterTool(tool domain.Tool) error {
|
||||
allTools[tool.Name()] = tool
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetTool(name string) domain.Tool {
|
||||
return allTools[name]
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package tool
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo domain.ToolRepository
|
||||
repoUser domain.UserRepository
|
||||
allTools map[string]domain.ITool
|
||||
}
|
||||
|
||||
func NewService(
|
||||
repo domain.ToolRepository,
|
||||
repoUser domain.UserRepository,
|
||||
) *Service {
|
||||
return &Service{
|
||||
repo: repo,
|
||||
repoUser: repoUser,
|
||||
allTools: make(map[string]domain.ITool),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) ListTools(userID string, teamID string) ([]*domain.Tool, error) {
|
||||
|
||||
for _, tool := range s.allTools {
|
||||
_, err := s.repo.GetToolByName(userID, teamID, tool.Name())
|
||||
slog.Info("ListTools", "err1", err)
|
||||
if err != nil {
|
||||
_, err = s.repo.CreateTool(userID, &domain.Tool{
|
||||
TeamID: teamID,
|
||||
Name: tool.Name(),
|
||||
Enabled: true,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Info("ListTools", "err2", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s.repo.ListTools(userID, teamID)
|
||||
}
|
||||
|
||||
func (s *Service) GetTool(userID string, teamID string, id string) (*domain.Tool, error) {
|
||||
return s.repo.GetToolByID(userID, teamID, id)
|
||||
}
|
||||
|
||||
func (s *Service) GetToolByName(userID string, teamID string, name string) (*domain.Tool, error) {
|
||||
return s.repo.GetToolByName(userID, teamID, name)
|
||||
}
|
||||
|
||||
func (s *Service) CreateTool(userID string, Tool *domain.Tool) (*domain.Tool, error) {
|
||||
|
||||
if _, err := s.repoUser.FindUserTeam(userID, Tool.TeamID); err != nil {
|
||||
return nil, fmt.Errorf("error finding user in team: %s", err)
|
||||
}
|
||||
|
||||
return s.repo.CreateTool(userID, Tool)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateTool(userID string, Tool *domain.Tool) (*domain.Tool, error) {
|
||||
if _, err := s.repoUser.FindUserTeam(userID, Tool.TeamID); err != nil {
|
||||
return nil, fmt.Errorf("error finding user in team: %s", err)
|
||||
}
|
||||
|
||||
if _, err := s.repo.GetToolByID(userID, Tool.TeamID, Tool.ID); err != nil {
|
||||
return nil, fmt.Errorf("error finding Tool: %s", err)
|
||||
}
|
||||
|
||||
return s.repo.UpdateTool(userID, Tool)
|
||||
}
|
||||
|
||||
func (s *Service) DeleteTool(userID string, teamID string, id string) error {
|
||||
if _, err := s.repoUser.FindUserTeam(userID, teamID); err != nil {
|
||||
return fmt.Errorf("error finding user in team: %s", err)
|
||||
}
|
||||
|
||||
if _, err := s.repo.GetToolByID(userID, teamID, id); err != nil {
|
||||
return fmt.Errorf("error finding Tool: %s", err)
|
||||
}
|
||||
|
||||
return s.repo.DeleteTool(userID, teamID, id)
|
||||
|
||||
}
|
||||
|
||||
func (s *Service) RegisterToolImpl(tool domain.ITool) error {
|
||||
s.allTools[tool.Name()] = tool
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) GetAllToolImpl() map[string]domain.ITool {
|
||||
return s.allTools
|
||||
}
|
||||
|
||||
func (s *Service) GetToolImpl(name string) domain.ITool {
|
||||
return s.allTools[name]
|
||||
}
|
||||
Reference in new issue
Block a user