|
| 1 | +package chat |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = Describe("Run chat", func() { |
| 16 | + It("streams a single chat response", func() { |
| 17 | + var capturedModel string |
| 18 | + var capturedAuth string |
| 19 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | + if r.URL.Path == "/v1/models" { |
| 21 | + w.Header().Set("Content-Type", "application/json") |
| 22 | + fmt.Fprint(w, `{"object":"list","data":[{"id":"test-model","object":"model"}]}`) |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + Expect(r.URL.Path).To(Equal("/v1/chat/completions")) |
| 27 | + capturedAuth = r.Header.Get("Authorization") |
| 28 | + |
| 29 | + var body struct { |
| 30 | + Model string `json:"model"` |
| 31 | + Messages []struct { |
| 32 | + Role string `json:"role"` |
| 33 | + Content string `json:"content"` |
| 34 | + } `json:"messages"` |
| 35 | + } |
| 36 | + Expect(json.NewDecoder(r.Body).Decode(&body)).To(Succeed()) |
| 37 | + capturedModel = body.Model |
| 38 | + Expect(body.Messages).To(HaveLen(1)) |
| 39 | + Expect(body.Messages[0].Role).To(Equal("user")) |
| 40 | + Expect(body.Messages[0].Content).To(Equal("hello")) |
| 41 | + |
| 42 | + w.Header().Set("Content-Type", "text/event-stream") |
| 43 | + fmt.Fprint(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hi\"}}]}\n\n") |
| 44 | + fmt.Fprint(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"!\"}}]}\n\n") |
| 45 | + fmt.Fprint(w, "data: [DONE]\n\n") |
| 46 | + })) |
| 47 | + defer server.Close() |
| 48 | + |
| 49 | + var out bytes.Buffer |
| 50 | + err := Run(GinkgoT().Context(), Options{ |
| 51 | + Model: "test-model", |
| 52 | + BaseURL: server.URL + "/v1", |
| 53 | + APIKey: "secret", |
| 54 | + In: strings.NewReader("hello\n/exit\n"), |
| 55 | + Out: &out, |
| 56 | + }) |
| 57 | + |
| 58 | + Expect(err).ToNot(HaveOccurred()) |
| 59 | + Expect(capturedModel).To(Equal("test-model")) |
| 60 | + Expect(capturedAuth).To(Equal("Bearer secret")) |
| 61 | + Expect(out.String()).To(ContainSubstring("assistant: hi!")) |
| 62 | + Expect(out.String()).To(ContainSubstring("bye")) |
| 63 | + }) |
| 64 | + |
| 65 | + It("auto-selects the only available model", func() { |
| 66 | + server := chatTestServer([]string{"solo"}, nil) |
| 67 | + defer server.Close() |
| 68 | + |
| 69 | + var out bytes.Buffer |
| 70 | + err := Run(GinkgoT().Context(), Options{ |
| 71 | + BaseURL: server.URL + "/v1", |
| 72 | + In: strings.NewReader("/exit\n"), |
| 73 | + Out: &out, |
| 74 | + }) |
| 75 | + |
| 76 | + Expect(err).ToNot(HaveOccurred()) |
| 77 | + Expect(out.String()).To(ContainSubstring("LocalAI chat (solo)")) |
| 78 | + }) |
| 79 | + |
| 80 | + It("returns an actionable error when no models are installed", func() { |
| 81 | + server := chatTestServer(nil, nil) |
| 82 | + defer server.Close() |
| 83 | + |
| 84 | + err := Run(GinkgoT().Context(), Options{ |
| 85 | + BaseURL: server.URL + "/v1", |
| 86 | + In: strings.NewReader(""), |
| 87 | + }) |
| 88 | + |
| 89 | + Expect(err).To(HaveOccurred()) |
| 90 | + Expect(err.Error()).To(ContainSubstring("no chat models are installed")) |
| 91 | + Expect(err.Error()).To(ContainSubstring("local-ai models install <model>")) |
| 92 | + }) |
| 93 | + |
| 94 | + It("returns an actionable error when multiple models are available without a selection", func() { |
| 95 | + server := chatTestServer([]string{"alpha", "beta"}, nil) |
| 96 | + defer server.Close() |
| 97 | + |
| 98 | + err := Run(GinkgoT().Context(), Options{ |
| 99 | + BaseURL: server.URL + "/v1", |
| 100 | + In: strings.NewReader(""), |
| 101 | + }) |
| 102 | + |
| 103 | + Expect(err).To(HaveOccurred()) |
| 104 | + Expect(err.Error()).To(ContainSubstring("multiple models are available")) |
| 105 | + Expect(err.Error()).To(ContainSubstring("--model")) |
| 106 | + Expect(err.Error()).To(ContainSubstring("alpha")) |
| 107 | + Expect(err.Error()).To(ContainSubstring("beta")) |
| 108 | + }) |
| 109 | + |
| 110 | + It("lists and switches models inside the chat", func() { |
| 111 | + requestedModels := []string{} |
| 112 | + server := chatTestServer([]string{"alpha", "beta"}, func(model string) { |
| 113 | + requestedModels = append(requestedModels, model) |
| 114 | + }) |
| 115 | + defer server.Close() |
| 116 | + |
| 117 | + var out bytes.Buffer |
| 118 | + err := Run(GinkgoT().Context(), Options{ |
| 119 | + Model: "alpha", |
| 120 | + BaseURL: server.URL + "/v1", |
| 121 | + In: strings.NewReader("/models\n/model beta\nhello\n/exit\n"), |
| 122 | + Out: &out, |
| 123 | + }) |
| 124 | + |
| 125 | + Expect(err).ToNot(HaveOccurred()) |
| 126 | + Expect(out.String()).To(ContainSubstring("* alpha")) |
| 127 | + Expect(out.String()).To(ContainSubstring(" beta")) |
| 128 | + Expect(out.String()).To(ContainSubstring("switched to beta; conversation cleared")) |
| 129 | + Expect(requestedModels).To(Equal([]string{"beta"})) |
| 130 | + }) |
| 131 | +}) |
| 132 | + |
| 133 | +func chatTestServer(models []string, onChat func(model string)) *httptest.Server { |
| 134 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 135 | + switch r.URL.Path { |
| 136 | + case "/v1/models": |
| 137 | + w.Header().Set("Content-Type", "application/json") |
| 138 | + fmt.Fprint(w, `{"object":"list","data":[`) |
| 139 | + for i, model := range models { |
| 140 | + if i > 0 { |
| 141 | + fmt.Fprint(w, ",") |
| 142 | + } |
| 143 | + fmt.Fprintf(w, `{"id":%q,"object":"model"}`, model) |
| 144 | + } |
| 145 | + fmt.Fprint(w, `]}`) |
| 146 | + case "/v1/chat/completions": |
| 147 | + var body struct { |
| 148 | + Model string `json:"model"` |
| 149 | + } |
| 150 | + Expect(json.NewDecoder(r.Body).Decode(&body)).To(Succeed()) |
| 151 | + if onChat != nil { |
| 152 | + onChat(body.Model) |
| 153 | + } |
| 154 | + w.Header().Set("Content-Type", "text/event-stream") |
| 155 | + fmt.Fprint(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"}}]}\n\n") |
| 156 | + fmt.Fprint(w, "data: [DONE]\n\n") |
| 157 | + default: |
| 158 | + w.WriteHeader(http.StatusNotFound) |
| 159 | + } |
| 160 | + })) |
| 161 | +} |
0 commit comments