fix: prompt_cache_key openai chat -> openai responses (#6861)
This commit is contained in:
@@ -380,6 +380,14 @@ func ChatCompletionsRequestToResponsesRequest(req *dto.GeneralOpenAIRequest) (*d
|
|||||||
presencePenaltyRaw, _ = kitutil.Marshal(req.PresencePenalty)
|
presencePenaltyRaw, _ = kitutil.Marshal(req.PresencePenalty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var promptCacheKeyRaw json.RawMessage
|
||||||
|
if req.PromptCacheKey != "" {
|
||||||
|
promptCacheKeyRaw, err = kitutil.Marshal(req.PromptCacheKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal prompt_cache_key: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
out := &dto.OpenAIResponsesRequest{
|
out := &dto.OpenAIResponsesRequest{
|
||||||
Model: req.Model,
|
Model: req.Model,
|
||||||
Input: inputRaw,
|
Input: inputRaw,
|
||||||
@@ -396,6 +404,7 @@ func ChatCompletionsRequestToResponsesRequest(req *dto.GeneralOpenAIRequest) (*d
|
|||||||
ParallelToolCalls: parallelToolCallsRaw,
|
ParallelToolCalls: parallelToolCallsRaw,
|
||||||
Store: req.Store,
|
Store: req.Store,
|
||||||
Metadata: req.Metadata,
|
Metadata: req.Metadata,
|
||||||
|
PromptCacheKey: promptCacheKeyRaw,
|
||||||
EnableThinking: req.EnableThinking,
|
EnableThinking: req.EnableThinking,
|
||||||
ThinkingBudget: req.ThinkingBudget,
|
ThinkingBudget: req.ThinkingBudget,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,38 @@ func TestChatCompletionsRequestToResponsesRequestInstructionsAndTools(t *testing
|
|||||||
assert.Equal(t, "function_call_output", gjson.GetBytes(got.Input, "3.type").String())
|
assert.Equal(t, "function_call_output", gjson.GetBytes(got.Input, "3.type").String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChatCompletionsRequestToResponsesRequestPreservesPromptCacheKey(t *testing.T) {
|
||||||
|
t.Run("present", func(t *testing.T) {
|
||||||
|
key := "session-\"quoted\"\\path\n世界"
|
||||||
|
got, err := ChatCompletionsRequestToResponsesRequest(&dto.GeneralOpenAIRequest{
|
||||||
|
Model: "gpt-test",
|
||||||
|
Messages: []dto.Message{{Role: "user", Content: "hello"}},
|
||||||
|
PromptCacheKey: key,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
keyRaw, err := kitutil.Marshal(key)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, keyRaw, []byte(got.PromptCacheKey))
|
||||||
|
|
||||||
|
encoded, err := kitutil.Marshal(got)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, key, gjson.GetBytes(encoded, "prompt_cache_key").String())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("absent", func(t *testing.T) {
|
||||||
|
got, err := ChatCompletionsRequestToResponsesRequest(&dto.GeneralOpenAIRequest{
|
||||||
|
Model: "gpt-test",
|
||||||
|
Messages: []dto.Message{{Role: "user", Content: "hello"}},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
encoded, err := kitutil.Marshal(got)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.False(t, gjson.GetBytes(encoded, "prompt_cache_key").Exists())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestChatCompletionsRequestToResponsesRequestPreservesQwenThinkingBudget(t *testing.T) {
|
func TestChatCompletionsRequestToResponsesRequestPreservesQwenThinkingBudget(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
Reference in New Issue
Block a user