mirror of
https://github.com/mudler/LocalAI.git
synced 2026-06-11 10:19:33 -04:00
Add an opt-in `local-ai chat` command for testing chat models directly from the terminal without manually sending curl requests. The command connects to a running LocalAI server, lists available models through the existing OpenAI-compatible API, streams chat completions, and supports interactive commands such as `/models`, `/model`, `/clear`, and `/exit`. Keep `local-ai run` focused on the server lifecycle so the web UI, API clients, and multiple chat terminals can coexist against the same server. Document the new command and terminal workflow in the README and CLI docs. Tests: - go test -count=1 ./core/cli/chat - go test -count=1 ./core/cli Assisted-by: Codex:GPT-5 Signed-off-by: Ching Kao <0980124jim@gmail.com>
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Chat session", func() {
|
|
It("keeps model switching and message history out of the terminal adapter", func() {
|
|
client := &fakeChatClient{
|
|
models: []string{"alpha", "beta"},
|
|
answer: "pong",
|
|
}
|
|
|
|
session, err := newChatSession(context.Background(), client, "alpha")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(session.CurrentModel()).To(Equal("alpha"))
|
|
|
|
Expect(session.SwitchModel("beta")).To(Succeed())
|
|
Expect(session.CurrentModel()).To(Equal("beta"))
|
|
Expect(session.Send(context.Background(), "ping", io.Discard)).To(Succeed())
|
|
|
|
Expect(client.requests).To(HaveLen(1))
|
|
Expect(client.requests[0].model).To(Equal("beta"))
|
|
Expect(client.requests[0].messages).To(HaveLen(1))
|
|
Expect(client.requests[0].messages[0].Content).To(Equal("ping"))
|
|
})
|
|
})
|
|
|
|
type fakeChatClient struct {
|
|
models []string
|
|
answer string
|
|
requests []fakeChatRequest
|
|
}
|
|
|
|
type fakeChatRequest struct {
|
|
model string
|
|
messages []chatMessage
|
|
}
|
|
|
|
func (c *fakeChatClient) ListModels(context.Context) ([]string, error) {
|
|
return c.models, nil
|
|
}
|
|
|
|
func (c *fakeChatClient) StreamChat(_ context.Context, model string, messages []chatMessage, out io.Writer) (string, error) {
|
|
copied := make([]chatMessage, len(messages))
|
|
copy(copied, messages)
|
|
c.requests = append(c.requests, fakeChatRequest{model: model, messages: copied})
|
|
if _, err := io.WriteString(out, c.answer); err != nil {
|
|
return "", err
|
|
}
|
|
return c.answer, nil
|
|
}
|