basic llm request stream
This commit is contained in:
1 parent
f74e4d1043
commit
a92cc76a9a
66 files changed
+1496
-158
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
anyllm "github.com/mozilla-ai/any-llm-go"
|
||||
"github.com/mozilla-ai/any-llm-go/providers"
|
||||
"github.com/mozilla-ai/any-llm-go/providers/anthropic"
|
||||
"github.com/mozilla-ai/any-llm-go/providers/ollama"
|
||||
"github.com/mozilla-ai/any-llm-go/providers/openai"
|
||||
@@ -79,13 +80,13 @@ func (e *AnyLLMEngine) Stream(
|
||||
ctx context.Context,
|
||||
provider *domain.Provider,
|
||||
modelID string,
|
||||
params *domain.StreamParams,
|
||||
params *domain.LLMParams,
|
||||
messages []*domain.Message,
|
||||
) error {
|
||||
) (*domain.Message, error) {
|
||||
|
||||
p, err := providerFactory(provider)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
anyllmMessages := make([]anyllm.Message, 0)
|
||||
@@ -96,26 +97,57 @@ func (e *AnyLLMEngine) Stream(
|
||||
})
|
||||
}
|
||||
|
||||
if params.ReasoningEffort == "" {
|
||||
params.ReasoningEffort = "none"
|
||||
}
|
||||
|
||||
chunkChan, errChan := p.CompletionStream(ctx, anyllm.CompletionParams{
|
||||
Model: modelID,
|
||||
Messages: anyllmMessages,
|
||||
Stream: true,
|
||||
Model: modelID,
|
||||
Messages: anyllmMessages,
|
||||
Stream: true,
|
||||
ReasoningEffort: providers.ReasoningEffort(params.ReasoningEffort),
|
||||
})
|
||||
|
||||
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 {
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Content: content,
|
||||
})
|
||||
if content != "" {
|
||||
fullContent += content
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Done: false,
|
||||
Role: role,
|
||||
Content: content,
|
||||
Reasoning: false,
|
||||
})
|
||||
} else if reasoning != nil {
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Done: false,
|
||||
Role: role,
|
||||
Content: reasoning.Content,
|
||||
Reasoning: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
params.OnChunk(&domain.Chunk{
|
||||
Done: true,
|
||||
Role: role,
|
||||
Content: "",
|
||||
})
|
||||
|
||||
if err := <-errChan; err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil
|
||||
return &domain.Message{
|
||||
Role: role,
|
||||
Content: fullContent,
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user