123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- package openai
- import (
- "bytes"
- "encoding/json"
- "io"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/ollama/ollama/api"
- "github.com/stretchr/testify/assert"
- )
- func TestMiddlewareRequests(t *testing.T) {
- type testCase struct {
- Name string
- Method string
- Path string
- Handler func() gin.HandlerFunc
- Setup func(t *testing.T, req *http.Request)
- Expected func(t *testing.T, req *http.Request)
- }
- var capturedRequest *http.Request
- captureRequestMiddleware := func() gin.HandlerFunc {
- return func(c *gin.Context) {
- bodyBytes, _ := io.ReadAll(c.Request.Body)
- c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
- capturedRequest = c.Request
- c.Next()
- }
- }
- testCases := []testCase{
- {
- Name: "chat handler",
- Method: http.MethodPost,
- Path: "/api/chat",
- Handler: ChatMiddleware,
- Setup: func(t *testing.T, req *http.Request) {
- body := ChatCompletionRequest{
- Model: "test-model",
- Messages: []Message{{Role: "user", Content: "Hello"}},
- }
- bodyBytes, _ := json.Marshal(body)
- req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
- req.Header.Set("Content-Type", "application/json")
- },
- Expected: func(t *testing.T, req *http.Request) {
- var chatReq api.ChatRequest
- if err := json.NewDecoder(req.Body).Decode(&chatReq); err != nil {
- t.Fatal(err)
- }
- if chatReq.Messages[0].Role != "user" {
- t.Fatalf("expected 'user', got %s", chatReq.Messages[0].Role)
- }
- if chatReq.Messages[0].Content != "Hello" {
- t.Fatalf("expected 'Hello', got %s", chatReq.Messages[0].Content)
- }
- },
- },
- {
- Name: "completions handler",
- Method: http.MethodPost,
- Path: "/api/generate",
- Handler: CompletionsMiddleware,
- Setup: func(t *testing.T, req *http.Request) {
- temp := float32(0.8)
- body := CompletionRequest{
- Model: "test-model",
- Prompt: "Hello",
- Temperature: &temp,
- Stop: []string{"\n", "stop"},
- }
- bodyBytes, _ := json.Marshal(body)
- req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
- req.Header.Set("Content-Type", "application/json")
- },
- Expected: func(t *testing.T, req *http.Request) {
- var genReq api.GenerateRequest
- if err := json.NewDecoder(req.Body).Decode(&genReq); err != nil {
- t.Fatal(err)
- }
- if genReq.Prompt != "Hello" {
- t.Fatalf("expected 'Hello', got %s", genReq.Prompt)
- }
- if genReq.Options["temperature"] != 1.6 {
- t.Fatalf("expected 1.6, got %f", genReq.Options["temperature"])
- }
- stopTokens, ok := genReq.Options["stop"].([]any)
- if !ok {
- t.Fatalf("expected stop tokens to be a list")
- }
- if stopTokens[0] != "\n" || stopTokens[1] != "stop" {
- t.Fatalf("expected ['\\n', 'stop'], got %v", stopTokens)
- }
- },
- },
- }
- gin.SetMode(gin.TestMode)
- router := gin.New()
- endpoint := func(c *gin.Context) {
- c.Status(http.StatusOK)
- }
- for _, tc := range testCases {
- t.Run(tc.Name, func(t *testing.T) {
- router = gin.New()
- router.Use(captureRequestMiddleware())
- router.Use(tc.Handler())
- router.Handle(tc.Method, tc.Path, endpoint)
- req, _ := http.NewRequest(tc.Method, tc.Path, nil)
- if tc.Setup != nil {
- tc.Setup(t, req)
- }
- resp := httptest.NewRecorder()
- router.ServeHTTP(resp, req)
- tc.Expected(t, capturedRequest)
- })
- }
- }
- func TestMiddlewareResponses(t *testing.T) {
- type testCase struct {
- Name string
- Method string
- Path string
- TestPath string
- Handler func() gin.HandlerFunc
- Endpoint func(c *gin.Context)
- Setup func(t *testing.T, req *http.Request)
- Expected func(t *testing.T, resp *httptest.ResponseRecorder)
- }
- testCases := []testCase{
- {
- Name: "completions handler error forwarding",
- Method: http.MethodPost,
- Path: "/api/generate",
- TestPath: "/api/generate",
- Handler: CompletionsMiddleware,
- Endpoint: func(c *gin.Context) {
- c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
- },
- Setup: func(t *testing.T, req *http.Request) {
- body := CompletionRequest{
- Model: "test-model",
- Prompt: "Hello",
- }
- bodyBytes, _ := json.Marshal(body)
- req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
- req.Header.Set("Content-Type", "application/json")
- },
- Expected: func(t *testing.T, resp *httptest.ResponseRecorder) {
- if resp.Code != http.StatusBadRequest {
- t.Fatalf("expected 400, got %d", resp.Code)
- }
- if !strings.Contains(resp.Body.String(), `"invalid request"`) {
- t.Fatalf("error was not forwarded")
- }
- },
- },
- {
- Name: "list handler",
- Method: http.MethodGet,
- Path: "/api/tags",
- TestPath: "/api/tags",
- Handler: ListMiddleware,
- Endpoint: func(c *gin.Context) {
- c.JSON(http.StatusOK, api.ListResponse{
- Models: []api.ListModelResponse{
- {
- Name: "Test Model",
- },
- },
- })
- },
- Expected: func(t *testing.T, resp *httptest.ResponseRecorder) {
- assert.Equal(t, http.StatusOK, resp.Code)
- var listResp ListCompletion
- if err := json.NewDecoder(resp.Body).Decode(&listResp); err != nil {
- t.Fatal(err)
- }
- if listResp.Object != "list" {
- t.Fatalf("expected list, got %s", listResp.Object)
- }
- if len(listResp.Data) != 1 {
- t.Fatalf("expected 1, got %d", len(listResp.Data))
- }
- if listResp.Data[0].Id != "Test Model" {
- t.Fatalf("expected Test Model, got %s", listResp.Data[0].Id)
- }
- },
- },
- {
- Name: "retrieve model",
- Method: http.MethodGet,
- Path: "/api/show/:model",
- TestPath: "/api/show/test-model",
- Handler: RetrieveMiddleware,
- Endpoint: func(c *gin.Context) {
- c.JSON(http.StatusOK, api.ShowResponse{
- ModifiedAt: time.Date(2024, 6, 17, 13, 45, 0, 0, time.UTC),
- })
- },
- Expected: func(t *testing.T, resp *httptest.ResponseRecorder) {
- var retrieveResp Model
- if err := json.NewDecoder(resp.Body).Decode(&retrieveResp); err != nil {
- t.Fatal(err)
- }
- if retrieveResp.Object != "model" {
- t.Fatalf("Expected object to be model, got %s", retrieveResp.Object)
- }
- if retrieveResp.Id != "test-model" {
- t.Fatalf("Expected id to be test-model, got %s", retrieveResp.Id)
- }
- },
- },
- }
- gin.SetMode(gin.TestMode)
- router := gin.New()
- for _, tc := range testCases {
- t.Run(tc.Name, func(t *testing.T) {
- router = gin.New()
- router.Use(tc.Handler())
- router.Handle(tc.Method, tc.Path, tc.Endpoint)
- req, _ := http.NewRequest(tc.Method, tc.TestPath, nil)
- if tc.Setup != nil {
- tc.Setup(t, req)
- }
- resp := httptest.NewRecorder()
- router.ServeHTTP(resp, req)
- tc.Expected(t, resp)
- })
- }
- }
|