|
| 1 | +/* |
| 2 | +Copyright 2025 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package openai |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + |
| 23 | + "github.com/dapr/components-contrib/conversation" |
| 24 | +) |
| 25 | + |
| 26 | +func TestOpenaiLangchainMetadata(t *testing.T) { |
| 27 | + t.Run("json marshaling with endpoint", func(t *testing.T) { |
| 28 | + metadata := OpenAILangchainMetadata{ |
| 29 | + LangchainMetadata: conversation.LangchainMetadata{ |
| 30 | + Key: "test-key", |
| 31 | + Model: "gpt-4", |
| 32 | + CacheTTL: "10m", |
| 33 | + Endpoint: "https://custom-endpoint.openai.azure.com/", |
| 34 | + }, |
| 35 | + APIType: "azure", |
| 36 | + APIVersion: "2025-01-01-preview", |
| 37 | + } |
| 38 | + |
| 39 | + bytes, err := json.Marshal(metadata) |
| 40 | + require.NoError(t, err) |
| 41 | + |
| 42 | + var unmarshaled OpenAILangchainMetadata |
| 43 | + err = json.Unmarshal(bytes, &unmarshaled) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + assert.Equal(t, metadata.Key, unmarshaled.Key) |
| 47 | + assert.Equal(t, metadata.Model, unmarshaled.Model) |
| 48 | + assert.Equal(t, metadata.CacheTTL, unmarshaled.CacheTTL) |
| 49 | + assert.Equal(t, metadata.Endpoint, unmarshaled.Endpoint) |
| 50 | + assert.Equal(t, metadata.APIType, unmarshaled.APIType) |
| 51 | + assert.Equal(t, metadata.APIVersion, unmarshaled.APIVersion) |
| 52 | + }) |
| 53 | + |
| 54 | + t.Run("json unmarshaling with endpoint", func(t *testing.T) { |
| 55 | + jsonStr := `{"key": "test-key", "model": "gpt-4", "endpoint": "https://custom-endpoint.openai.azure.com/", "apiType": "azure", "apiVersion": "2025-01-01-preview"}` |
| 56 | + |
| 57 | + var metadata OpenAILangchainMetadata |
| 58 | + err := json.Unmarshal([]byte(jsonStr), &metadata) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + assert.Equal(t, "test-key", metadata.Key) |
| 62 | + assert.Equal(t, "gpt-4", metadata.Model) |
| 63 | + assert.Equal(t, "https://custom-endpoint.openai.azure.com/", metadata.Endpoint) |
| 64 | + assert.Equal(t, "azure", metadata.APIType) |
| 65 | + assert.Equal(t, "2025-01-01-preview", metadata.APIVersion) |
| 66 | + }) |
| 67 | +} |
0 commit comments