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
@@ -3,6 +3,7 @@ package llm
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
anyllm "github.com/mozilla-ai/any-llm-go"
|
||||
"github.com/mozilla-ai/any-llm-go/providers"
|
||||
@@ -82,11 +83,14 @@ func (e *AnyLLMEngine) Stream(
|
||||
modelID string,
|
||||
params *domain.LLMParams,
|
||||
messages []*domain.Message,
|
||||
) (*domain.Message, error) {
|
||||
) *domain.Message {
|
||||
|
||||
p, err := providerFactory(provider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return &domain.Message{
|
||||
Role: string(domain.RoleAssistant),
|
||||
Content: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
anyllmMessages := make([]anyllm.Message, 0)
|
||||
@@ -101,18 +105,27 @@ func (e *AnyLLMEngine) Stream(
|
||||
params.ReasoningEffort = "none"
|
||||
}
|
||||
|
||||
tools := make([]providers.Tool, len(params.Tools))
|
||||
for i, t := range params.Tools {
|
||||
tools[i] = toolConvert(t)
|
||||
}
|
||||
|
||||
toolCalling := false
|
||||
toolCalls := make([]domain.ToolCall, 0)
|
||||
|
||||
chunkChan, errChan := p.CompletionStream(ctx, anyllm.CompletionParams{
|
||||
Model: modelID,
|
||||
Messages: anyllmMessages,
|
||||
Stream: true,
|
||||
ReasoningEffort: providers.ReasoningEffort(params.ReasoningEffort),
|
||||
Tools: tools,
|
||||
})
|
||||
|
||||
fullContent := ""
|
||||
role := anyllm.RoleAssistant
|
||||
|
||||
for chunk := range chunkChan {
|
||||
if len(chunk.Choices) > 0 {
|
||||
|
||||
content := chunk.Choices[0].Delta.Content
|
||||
reasoning := chunk.Choices[0].Delta.Reasoning
|
||||
if params.OnChunk != nil {
|
||||
@@ -120,34 +133,82 @@ func (e *AnyLLMEngine) Stream(
|
||||
fullContent += content
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Done: false,
|
||||
Role: role,
|
||||
Role: anyllm.RoleAssistant,
|
||||
Content: content,
|
||||
Reasoning: false,
|
||||
})
|
||||
} else if reasoning != nil {
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Done: false,
|
||||
Role: role,
|
||||
Role: anyllm.RoleAssistant,
|
||||
Content: reasoning.Content,
|
||||
Reasoning: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, tc := range chunk.Choices[0].Delta.ToolCalls {
|
||||
toolCalling = true
|
||||
slog.Info("Model is calling tool", "tool", tc.Function.Name, "arguments", tc.Function.Arguments)
|
||||
|
||||
extra := make(map[string]map[string]any)
|
||||
for k, v := range tc.Extra {
|
||||
m := map[string]any(v)
|
||||
extra[k] = m
|
||||
}
|
||||
|
||||
toolCalls = append(toolCalls, domain.ToolCall{
|
||||
ID: tc.ID,
|
||||
Type: tc.Type,
|
||||
Function: domain.ToolCallFunction{
|
||||
Name: tc.Function.Name,
|
||||
Arguments: tc.Function.Arguments,
|
||||
},
|
||||
Extra: extra,
|
||||
})
|
||||
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Done: false,
|
||||
ToolName: tc.Function.Name,
|
||||
Role: anyllm.RoleAssistant,
|
||||
Content: tc.Function.Arguments,
|
||||
Reasoning: false,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Done: true,
|
||||
Role: role,
|
||||
Content: "",
|
||||
})
|
||||
if !toolCalling {
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Done: true,
|
||||
Role: anyllm.RoleAssistant,
|
||||
Content: "",
|
||||
})
|
||||
}
|
||||
|
||||
if err := <-errChan; err != nil {
|
||||
return nil, err
|
||||
return &domain.Message{
|
||||
Role: string(domain.RoleAssistant),
|
||||
Content: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
return &domain.Message{
|
||||
Role: role,
|
||||
Content: fullContent,
|
||||
}, nil
|
||||
Role: string(domain.RoleAssistant),
|
||||
Content: fullContent,
|
||||
ToolCalls: toolCalls,
|
||||
}
|
||||
}
|
||||
|
||||
func toolConvert(domainTool *domain.ToolDefinition) providers.Tool {
|
||||
return providers.Tool{
|
||||
Type: domainTool.Type,
|
||||
Function: providers.Function{
|
||||
Name: domainTool.Function.Name,
|
||||
Description: domainTool.Function.Description,
|
||||
Parameters: domainTool.Function.Parameters,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user