basic llm request stream
This commit is contained in:
1 parent
f74e4d1043
commit
a92cc76a9a
66 files changed
+1496
-158
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"trankilou.fr/lassistanoque/backend/internal/domain"
|
||||
)
|
||||
|
||||
type ChatSession struct {
|
||||
userID string
|
||||
teamID string
|
||||
chatID string
|
||||
agent *domain.Agent
|
||||
provider *domain.Provider
|
||||
modelID string
|
||||
messages []*domain.Message
|
||||
llmEngine domain.LLMEngine
|
||||
repoChat domain.ChatRepository
|
||||
subscribers []domain.StreamCallback
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (s *ChatSession) run(ctx context.Context) (*domain.Message, error) {
|
||||
|
||||
params := &domain.LLMParams{}
|
||||
|
||||
s.mu.Lock()
|
||||
if len(s.subscribers) > 0 {
|
||||
params.OnChunk = func(chunk *domain.Chunk) {
|
||||
for _, cb := range s.subscribers {
|
||||
cb(chunk)
|
||||
}
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
|
||||
msg, err := s.llmEngine.Stream(
|
||||
ctx,
|
||||
s.provider,
|
||||
s.modelID,
|
||||
params,
|
||||
s.messages,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msg.ChatID = s.chatID
|
||||
msg.TeamID = s.teamID
|
||||
|
||||
return s.repoChat.CreateChatMessage(s.userID, msg)
|
||||
}
|
||||
Reference in new issue
Block a user