prise en charge des outils + weather
This commit is contained in:
1 parent
a36f1231cd
commit
0a6ffab050
14 files changed
+623
-113
No files matched your search
@@ -120,7 +120,7 @@ func (s *Service) AddChatMessage(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
messages, err := s.repoChat.GetChatMessages(userID, teamID, chatID)
|
||||
messages, err := s.repoChat.GetChatMessages(userID, teamID, chatID, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -158,7 +158,7 @@ func (s *Service) GetChatWithMessages(userID string, teamID, chatID string) (*do
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
messages, err := s.repoChat.GetChatMessages(userID, teamID, chatID)
|
||||
messages, err := s.repoChat.GetChatMessages(userID, teamID, chatID, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -207,7 +207,7 @@ func (s *Service) runQuery(
|
||||
s.mu.Unlock()
|
||||
|
||||
go func() {
|
||||
_, err := chatSession.run(ctx)
|
||||
err := chatSession.run(ctx)
|
||||
if err != nil {
|
||||
slog.Info("chat session error", "error", err)
|
||||
}
|
||||
|
||||
@@ -21,10 +21,16 @@ type ChatSession struct {
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (s *ChatSession) run(ctx context.Context) (*domain.Message, error) {
|
||||
func (s *ChatSession) run(ctx context.Context) error {
|
||||
|
||||
params := &domain.LLMParams{}
|
||||
|
||||
tools := make([]*domain.ToolDefinition, 0)
|
||||
for _, t := range allTools {
|
||||
tools = append(tools, t.Definition(ctx))
|
||||
}
|
||||
params.Tools = tools
|
||||
|
||||
s.mu.Lock()
|
||||
if len(s.subscribers) > 0 {
|
||||
params.OnChunk = func(chunk *domain.Chunk) {
|
||||
@@ -40,19 +46,66 @@ func (s *ChatSession) run(ctx context.Context) (*domain.Message, error) {
|
||||
Content: s.agent.SystemPrompt,
|
||||
}
|
||||
|
||||
// TODO : loop with tools
|
||||
msg, err := s.llmEngine.Stream(
|
||||
ctx,
|
||||
s.provider,
|
||||
s.modelID,
|
||||
params,
|
||||
append([]*domain.Message{systemPrompt}, s.messages...),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msg.ChatID = s.chatID
|
||||
msg.TeamID = s.teamID
|
||||
continue_loop := true
|
||||
|
||||
return s.repoChat.CreateChatMessage(s.userID, msg)
|
||||
// LOOP on TOOLS
|
||||
for continue_loop {
|
||||
msg := s.llmEngine.Stream(
|
||||
ctx,
|
||||
s.provider,
|
||||
s.modelID,
|
||||
params,
|
||||
append([]*domain.Message{systemPrompt}, s.messages...),
|
||||
)
|
||||
|
||||
msg.ChatID = s.chatID
|
||||
msg.TeamID = s.teamID
|
||||
|
||||
msg, err := s.repoChat.CreateChatMessage(s.userID, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.messages = append(s.messages, msg)
|
||||
|
||||
// TOOL
|
||||
if len(msg.ToolCalls) > 0 {
|
||||
continue_loop = true
|
||||
for _, tc := range msg.ToolCalls {
|
||||
|
||||
tool := GetTool(tc.Function.Name)
|
||||
output, err := tool.Execute(ctx, []byte(tc.Function.Arguments))
|
||||
var toolResponse *domain.Message
|
||||
if err != nil {
|
||||
toolResponse = &domain.Message{
|
||||
ChatID: s.chatID,
|
||||
TeamID: s.teamID,
|
||||
ToolCallID: tc.ID,
|
||||
Role: string(domain.RoleTool),
|
||||
Content: "ERROR: " + err.Error(),
|
||||
}
|
||||
} else {
|
||||
toolResponse = &domain.Message{
|
||||
ChatID: s.chatID,
|
||||
TeamID: s.teamID,
|
||||
ToolCallID: tc.ID,
|
||||
Role: string(domain.RoleTool),
|
||||
Content: string(output),
|
||||
}
|
||||
}
|
||||
toolResponse, err = s.repoChat.CreateChatMessage(s.userID, toolResponse)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.messages = append(s.messages, toolResponse)
|
||||
}
|
||||
|
||||
} else {
|
||||
continue_loop = false
|
||||
}
|
||||
// END TOOL
|
||||
|
||||
}
|
||||
// END LOOP
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
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]
|
||||
}
|
||||
Reference in new issue
Block a user