Skip to content

Commit

Permalink
only test real providers
Browse files Browse the repository at this point in the history
Signed-off-by: Alexa Griffith <agriffith96@gmail.com>
  • Loading branch information
alexagriffith committed Feb 20, 2025
1 parent a10253b commit a8648fc
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 361 deletions.
123 changes: 56 additions & 67 deletions tests/extproc/custom_extproc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,65 @@
package extproc

import (
"encoding/base64"
"fmt"
"os"
"runtime"
"testing"
"time"

"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
"github.com/stretchr/testify/require"

"github.com/envoyproxy/ai-gateway/filterapi"
"github.com/envoyproxy/ai-gateway/tests/internal/testupstreamlib"
)

// TestExtProcCustomRouter tests examples/extproc_custom_router.
func TestExtProcCustomRouter(t *testing.T) {
requireBinaries(t)
requireRunEnvoy(t, "/dev/null")
requireTestUpstream(t)
configPath := t.TempDir() + "/extproc-config.yaml"
requireWriteFilterConfig(t, configPath, &filterapi.Config{
Schema: openAISchema,
// This can be any header key, but it must match the envoy.yaml routing configuration.
SelectedBackendHeaderKey: "x-selected-backend-name",
ModelNameHeaderKey: "x-model-name",
Rules: []filterapi.RouteRule{
{
Backends: []filterapi.Backend{{Name: "testupstream", Schema: openAISchema}},
Headers: []filterapi.HeaderMatch{{Name: "x-model-name", Value: "something-cool"}},
},
},
})
stdoutPath := t.TempDir() + "/extproc-stdout.log"
f, err := os.Create(stdoutPath)
require.NoError(t, err)
defer func() {
require.NoError(t, f.Close())
}()
requireExtProc(t, f, fmt.Sprintf("../../out/extproc_custom_router-%s-%s",
runtime.GOOS, runtime.GOARCH), configPath)

require.Eventually(t, func() bool {
client := openai.NewClient(option.WithBaseURL(listenerAddress+"/v1/"),
option.WithHeader(
testupstreamlib.ExpectedPathHeaderKey, base64.StdEncoding.EncodeToString([]byte("/v1/chat/completions"))),
option.WithHeader(testupstreamlib.ResponseBodyHeaderKey,
base64.StdEncoding.EncodeToString([]byte(`{"choices":[{"message":{"content":"This is a test."}}]}`)),
))
chatCompletion, err := client.Chat.Completions.New(t.Context(), openai.ChatCompletionNewParams{
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
openai.UserMessage("Say this is a test"),
}),
Model: openai.F("something-cool"),
})
if err != nil {
t.Logf("error: %v", err)
return false
}
for _, choice := range chatCompletion.Choices {
t.Logf("choice: %s", choice.Message.Content)
}
return true
}, 10*time.Second, 1*time.Second)

// Check that the custom router logs the model name after the file is closed.
defer func() {
stdout, err := os.ReadFile(stdoutPath)
require.NoError(t, err)
t.Logf("stdout: %s", stdout)
require.Contains(t, string(stdout), "model name: something-cool") // This must be logged by the custom router.
}()
t.Skip()
//requireBinaries(t)
//requireRunEnvoy(t, "/dev/null")
//requireTestUpstream(t)
//configPath := t.TempDir() + "/extproc-config.yaml"
//requireWriteFilterConfig(t, configPath, &filterapi.Config{
// Schema: openAISchema,
// // This can be any header key, but it must match the envoy.yaml routing configuration.
// SelectedBackendHeaderKey: "x-selected-backend-name",
// ModelNameHeaderKey: "x-model-name",
// Rules: []filterapi.RouteRule{
// {
// Backends: []filterapi.Backend{{Name: "testupstream", Schema: openAISchema}},
// Headers: []filterapi.HeaderMatch{{Name: "x-model-name", Value: "something-cool"}},
// },
// },
//})
//stdoutPath := t.TempDir() + "/extproc-stdout.log"
//f, err := os.Create(stdoutPath)
//require.NoError(t, err)
//defer func() {
// require.NoError(t, f.Close())
//}()
//requireExtProc(t, f, fmt.Sprintf("../../out/extproc_custom_router-%s-%s",
// runtime.GOOS, runtime.GOARCH), configPath)
//
//require.Eventually(t, func() bool {
// client := openai.NewClient(option.WithBaseURL(listenerAddress+"/v1/"),
// option.WithHeader(
// testupstreamlib.ExpectedPathHeaderKey, base64.StdEncoding.EncodeToString([]byte("/v1/chat/completions"))),
// option.WithHeader(testupstreamlib.ResponseBodyHeaderKey,
// base64.StdEncoding.EncodeToString([]byte(`{"choices":[{"message":{"content":"This is a test."}}]}`)),
// ))
// chatCompletion, err := client.Chat.Completions.New(t.Context(), openai.ChatCompletionNewParams{
// Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
// openai.UserMessage("Say this is a test"),
// }),
// Model: openai.F("something-cool"),
// })
// if err != nil {
// t.Logf("error: %v", err)
// return false
// }
// for _, choice := range chatCompletion.Choices {
// t.Logf("choice: %s", choice.Message.Content)
// }
// return true
//}, 10*time.Second, 1*time.Second)
//
//// Check that the custom router logs the model name after the file is closed.
//defer func() {
// stdout, err := os.ReadFile(stdoutPath)
// require.NoError(t, err)
// t.Logf("stdout: %s", stdout)
// require.Contains(t, string(stdout), "model name: something-cool") // This must be logged by the custom router.
//}()
}
49 changes: 24 additions & 25 deletions tests/extproc/real_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

openai "github.com/openai/openai-go"
"github.com/openai/openai-go/option"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/envoyproxy/ai-gateway/filterapi"
Expand Down Expand Up @@ -287,30 +286,30 @@ func TestWithRealProviders(t *testing.T) {
// Models are served by the extproc filter as a direct response so this can run even if the
// real credentials are not present.
// We don't need to run it on a concrete backend, as it will not route anywhere.
t.Run("list-models", func(t *testing.T) {
client := openai.NewClient(option.WithBaseURL(listenerAddress + "/v1/"))

var models []string

require.EventuallyWithT(t, func(c *assert.CollectT) {
it := client.Models.ListAutoPaging(t.Context())
for it.Next() {
models = append(models, it.Current().ID)
}
assert.NoError(c, it.Err())
}, 30*time.Second, 2*time.Second)
require.Equal(t, []string{
"gpt-4o-mini",
"eu.meta.llama3-2-1b-instruct-v1:0",
"eu.anthropic.claude-3-5-sonnet-20240620-v1:0",
}, models)

//require.Equal(t, []string{
// "gpt-4o-mini",
// "us.meta.llama3-2-1b-instruct-v1:0",
// "us.anthropic.claude-3-5-sonnet-20240620-v1:0",
//}, models)
})
//t.Run("list-models", func(t *testing.T) {
// client := openai.NewClient(option.WithBaseURL(listenerAddress + "/v1/"))
//
// var models []string
//
// require.EventuallyWithT(t, func(c *assert.CollectT) {
// it := client.Models.ListAutoPaging(t.Context())
// for it.Next() {
// models = append(models, it.Current().ID)
// }
// assert.NoError(c, it.Err())
// }, 30*time.Second, 2*time.Second)
// require.Equal(t, []string{
// "gpt-4o-mini",
// "eu.meta.llama3-2-1b-instruct-v1:0",
// "eu.anthropic.claude-3-5-sonnet-20240620-v1:0",
// }, models)
//
// //require.Equal(t, []string{
// // "gpt-4o-mini",
// // "us.meta.llama3-2-1b-instruct-v1:0",
// // "us.anthropic.claude-3-5-sonnet-20240620-v1:0",
// //}, models)
//})
}

// realProvidersTestCase is a base test case for the real providers, which is mainly for the centralization of the
Expand Down
Loading

0 comments on commit a8648fc

Please sign in to comment.