Skip to content

Commit ea67e81

Browse files
authored
feat: add backward compatibility for LLMResultChunk and new test case (#240)
- Implemented a compatibility layer in LLMResultChunk to ensure backward compatibility with the old format by adding a `PromptMessages` field. - Added a new test case `TestLLMResultChunkCompatibility` to verify the JSON marshaling behavior of the updated LLMResultChunk structure.
1 parent 9da6137 commit ea67e81

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pkg/entities/model_entities/llm.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ type LLMResultChunk struct {
188188
Delta LLMResultChunkDelta `json:"delta" validate:"required"`
189189
}
190190

191+
/*
192+
This is a compatibility layer for the old LLMResultChunk format.
193+
The old one has the `PromptMessages` field, we need to ensure the new one is backward compatible.
194+
*/
195+
func (l LLMResultChunk) MarshalJSON() ([]byte, error) {
196+
type Alias LLMResultChunk
197+
type LLMResultChunk struct {
198+
Alias
199+
PromptMessages []any `json:"prompt_messages"`
200+
}
201+
return json.Marshal(LLMResultChunk{
202+
Alias: (Alias)(l),
203+
PromptMessages: []any{},
204+
})
205+
}
206+
191207
type LLMUsage struct {
192208
PromptTokens *int `json:"prompt_tokens" validate:"required"`
193209
PromptUnitPrice decimal.Decimal `json:"prompt_unit_price" validate:"required"`

pkg/entities/model_entities/llm_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
7+
"github.com/stretchr/testify/assert"
78
)
89

910
func TestFullFunctionPromptMessage(t *testing.T) {
@@ -298,3 +299,16 @@ func TestImagePromptMessage(t *testing.T) {
298299
t.Error(err)
299300
}
300301
}
302+
303+
func TestLLMResultChunkCompatibility(t *testing.T) {
304+
llmResultChunk := LLMResultChunk{
305+
Model: "gpt-3.5-turbo",
306+
}
307+
308+
result := parser.MarshalJson(llmResultChunk)
309+
assert.Contains(t, string(result), `"prompt_messages":[]`)
310+
311+
llmResultChunkPointer := &llmResultChunk
312+
result = parser.MarshalJson(llmResultChunkPointer)
313+
assert.Contains(t, string(result), `"prompt_messages":[]`)
314+
}

0 commit comments

Comments
 (0)