|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/binary" |
| 7 | + "io" |
| 8 | + "mime/multipart" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + . "github.com/onsi/ginkgo/v2" |
| 14 | + . "github.com/onsi/gomega" |
| 15 | + "github.com/openai/openai-go/v3" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("SpiritLM backend E2E", Label("SpiritLM"), func() { |
| 19 | + Describe("Chat completions", func() { |
| 20 | + It("returns response for spirit-lm-base-7b", func() { |
| 21 | + resp, err := client.Chat.Completions.New( |
| 22 | + context.TODO(), |
| 23 | + openai.ChatCompletionNewParams{ |
| 24 | + Model: "spirit-lm-base-7b", |
| 25 | + Messages: []openai.ChatCompletionMessageParamUnion{ |
| 26 | + openai.UserMessage("Say hello."), |
| 27 | + }, |
| 28 | + }, |
| 29 | + ) |
| 30 | + Expect(err).ToNot(HaveOccurred()) |
| 31 | + Expect(len(resp.Choices)).To(Equal(1)) |
| 32 | + Expect(resp.Choices[0].Message.Content).ToNot(BeEmpty()) |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + Describe("TTS", func() { |
| 37 | + It("returns audio for spirit-lm-base-7b", func() { |
| 38 | + body := `{"model":"spirit-lm-base-7b","input":"Hello","voice":"default"}` |
| 39 | + req, err := http.NewRequest("POST", apiURL+"/audio/speech", io.NopCloser(strings.NewReader(body))) |
| 40 | + Expect(err).ToNot(HaveOccurred()) |
| 41 | + req.Header.Set("Content-Type", "application/json") |
| 42 | + |
| 43 | + httpClient := &http.Client{Timeout: 30 * time.Second} |
| 44 | + resp, err := httpClient.Do(req) |
| 45 | + Expect(err).ToNot(HaveOccurred()) |
| 46 | + defer resp.Body.Close() |
| 47 | + Expect(resp.StatusCode).To(Equal(200)) |
| 48 | + Expect(resp.Header.Get("Content-Type")).To(HavePrefix("audio/")) |
| 49 | + data, err := io.ReadAll(resp.Body) |
| 50 | + Expect(err).ToNot(HaveOccurred()) |
| 51 | + Expect(len(data)).To(BeNumerically(">", 0)) |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + Describe("Transcription", func() { |
| 56 | + It("returns transcription for spirit-lm-base-7b", func() { |
| 57 | + var buf bytes.Buffer |
| 58 | + w := multipart.NewWriter(&buf) |
| 59 | + part, err := w.CreateFormFile("file", "audio.wav") |
| 60 | + Expect(err).ToNot(HaveOccurred()) |
| 61 | + _, _ = part.Write(minimalWAVBytes()) |
| 62 | + _ = w.WriteField("model", "spirit-lm-base-7b") |
| 63 | + Expect(w.Close()).To(Succeed()) |
| 64 | + |
| 65 | + req, err := http.NewRequest("POST", apiURL+"/audio/transcriptions", &buf) |
| 66 | + Expect(err).ToNot(HaveOccurred()) |
| 67 | + req.Header.Set("Content-Type", w.FormDataContentType()) |
| 68 | + |
| 69 | + httpClient := &http.Client{Timeout: 30 * time.Second} |
| 70 | + resp, err := httpClient.Do(req) |
| 71 | + Expect(err).ToNot(HaveOccurred()) |
| 72 | + defer resp.Body.Close() |
| 73 | + Expect(resp.StatusCode).To(Equal(200)) |
| 74 | + data, err := io.ReadAll(resp.Body) |
| 75 | + Expect(err).ToNot(HaveOccurred()) |
| 76 | + Expect(string(data)).To(ContainSubstring("mocked")) |
| 77 | + }) |
| 78 | + }) |
| 79 | +}) |
| 80 | + |
| 81 | +func minimalWAVBytes() []byte { |
| 82 | + const sampleRate = 16000 |
| 83 | + const numChannels = 1 |
| 84 | + const bitsPerSample = 16 |
| 85 | + const numSamples = 160 |
| 86 | + dataSize := numSamples * numChannels * (bitsPerSample / 8) |
| 87 | + headerLen := 44 |
| 88 | + var buf bytes.Buffer |
| 89 | + buf.Write([]byte("RIFF")) |
| 90 | + _ = binary.Write(&buf, binary.LittleEndian, uint32(headerLen-8+dataSize)) |
| 91 | + buf.Write([]byte("WAVEfmt ")) |
| 92 | + _ = binary.Write(&buf, binary.LittleEndian, uint32(16)) |
| 93 | + _ = binary.Write(&buf, binary.LittleEndian, uint16(1)) |
| 94 | + _ = binary.Write(&buf, binary.LittleEndian, uint16(numChannels)) |
| 95 | + _ = binary.Write(&buf, binary.LittleEndian, uint32(sampleRate)) |
| 96 | + _ = binary.Write(&buf, binary.LittleEndian, uint32(sampleRate*numChannels*(bitsPerSample/8))) |
| 97 | + _ = binary.Write(&buf, binary.LittleEndian, uint16(numChannels*(bitsPerSample/8))) |
| 98 | + _ = binary.Write(&buf, binary.LittleEndian, uint16(bitsPerSample)) |
| 99 | + buf.Write([]byte("data")) |
| 100 | + _ = binary.Write(&buf, binary.LittleEndian, uint32(dataSize)) |
| 101 | + buf.Write(make([]byte, dataSize)) |
| 102 | + return buf.Bytes() |
| 103 | +} |
0 commit comments