Compare commits
4 Commits
3f765234de
...
3f7f34812f
Author | SHA1 | Date | |
---|---|---|---|
3f7f34812f | |||
98e92d1ff4 | |||
e23dc17555 | |||
e0cc97e177 |
@ -44,13 +44,13 @@ func ContinueCmd(ctx *lmcli.Context) *cobra.Command {
|
||||
fmt.Print(lastMessage.Content)
|
||||
|
||||
// Submit the LLM request, allowing it to continue the last message
|
||||
continuedOutput, err := cmdutil.FetchAndShowCompletion(ctx, messages, nil)
|
||||
continuedOutput, err := cmdutil.FetchAndShowCompletion(ctx, messages)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error fetching LLM response: %v", err)
|
||||
}
|
||||
|
||||
// Append the new response to the original message
|
||||
lastMessage.Content += strings.TrimRight(continuedOutput, "\n\t ")
|
||||
lastMessage.Content += strings.TrimRight(continuedOutput[0].Content, "\n\t ")
|
||||
|
||||
// Update the original message
|
||||
err = ctx.Store.UpdateMessage(lastMessage)
|
||||
|
@ -31,7 +31,7 @@ func PromptCmd(ctx *lmcli.Context) *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
_, err := cmdutil.FetchAndShowCompletion(ctx, messages, nil)
|
||||
_, err := cmdutil.FetchAndShowCompletion(ctx, messages)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error fetching LLM response: %v", err)
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
|
||||
// fetchAndShowCompletion prompts the LLM with the given messages and streams
|
||||
// the response to stdout. Returns all model reply messages.
|
||||
func FetchAndShowCompletion(ctx *lmcli.Context, messages []model.Message, callback func(model.Message)) (string, error) {
|
||||
func FetchAndShowCompletion(ctx *lmcli.Context, messages []model.Message) ([]model.Message, error) {
|
||||
content := make(chan string) // receives the reponse from LLM
|
||||
defer close(content)
|
||||
|
||||
@ -24,7 +24,7 @@ func FetchAndShowCompletion(ctx *lmcli.Context, messages []model.Message, callba
|
||||
|
||||
completionProvider, err := ctx.GetCompletionProvider(*ctx.Config.Defaults.Model)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
requestParams := model.RequestParameters{
|
||||
@ -34,8 +34,9 @@ func FetchAndShowCompletion(ctx *lmcli.Context, messages []model.Message, callba
|
||||
ToolBag: ctx.EnabledTools,
|
||||
}
|
||||
|
||||
var apiReplies []model.Message
|
||||
response, err := completionProvider.CreateChatCompletionStream(
|
||||
context.Background(), requestParams, messages, callback, content,
|
||||
context.Background(), requestParams, messages, &apiReplies, content,
|
||||
)
|
||||
if response != "" {
|
||||
// there was some content, so break to a new line after it
|
||||
@ -46,7 +47,8 @@ func FetchAndShowCompletion(ctx *lmcli.Context, messages []model.Message, callba
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
return response, nil
|
||||
|
||||
return apiReplies, err
|
||||
}
|
||||
|
||||
// lookupConversation either returns the conversation found by the
|
||||
@ -97,22 +99,21 @@ func HandleConversationReply(ctx *lmcli.Context, c *model.Conversation, persist
|
||||
// render a message header with no contents
|
||||
RenderMessage(ctx, (&model.Message{Role: model.MessageRoleAssistant}))
|
||||
|
||||
replyCallback := func(reply model.Message) {
|
||||
if !persist {
|
||||
return
|
||||
}
|
||||
|
||||
reply.ConversationID = c.ID
|
||||
err = ctx.Store.SaveMessage(&reply)
|
||||
if err != nil {
|
||||
lmcli.Warn("Could not save reply: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = FetchAndShowCompletion(ctx, allMessages, replyCallback)
|
||||
replies, err := FetchAndShowCompletion(ctx, allMessages)
|
||||
if err != nil {
|
||||
lmcli.Fatal("Error fetching LLM response: %v\n", err)
|
||||
}
|
||||
|
||||
if persist {
|
||||
for _, reply := range replies {
|
||||
reply.ConversationID = c.ID
|
||||
|
||||
err = ctx.Store.SaveMessage(&reply)
|
||||
if err != nil {
|
||||
lmcli.Warn("Could not save reply: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func FormatForExternalPrompt(messages []model.Message, system bool) string {
|
||||
|
@ -133,7 +133,7 @@ func (c *AnthropicClient) CreateChatCompletion(
|
||||
ctx context.Context,
|
||||
params model.RequestParameters,
|
||||
messages []model.Message,
|
||||
callback provider.ReplyCallback,
|
||||
replies *[]model.Message,
|
||||
) (string, error) {
|
||||
request := buildRequest(params, messages)
|
||||
|
||||
@ -162,9 +162,7 @@ func (c *AnthropicClient) CreateChatCompletion(
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported message type: %s", content.Type)
|
||||
}
|
||||
if callback != nil {
|
||||
callback(reply)
|
||||
}
|
||||
*replies = append(*replies, reply)
|
||||
}
|
||||
|
||||
return sb.String(), nil
|
||||
@ -174,7 +172,7 @@ func (c *AnthropicClient) CreateChatCompletionStream(
|
||||
ctx context.Context,
|
||||
params model.RequestParameters,
|
||||
messages []model.Message,
|
||||
callback provider.ReplyCallback,
|
||||
replies *[]model.Message,
|
||||
output chan<- string,
|
||||
) (string, error) {
|
||||
request := buildRequest(params, messages)
|
||||
@ -293,25 +291,23 @@ func (c *AnthropicClient) CreateChatCompletionStream(
|
||||
ToolResults: toolResults,
|
||||
}
|
||||
|
||||
if callback != nil {
|
||||
callback(toolCall)
|
||||
callback(toolReply)
|
||||
if replies != nil {
|
||||
*replies = append(append(*replies, toolCall), toolReply)
|
||||
}
|
||||
|
||||
// Recurse into CreateChatCompletionStream with the tool call replies
|
||||
// added to the original messages
|
||||
messages = append(append(messages, toolCall), toolReply)
|
||||
return c.CreateChatCompletionStream(ctx, params, messages, callback, output)
|
||||
return c.CreateChatCompletionStream(ctx, params, messages, replies, output)
|
||||
}
|
||||
}
|
||||
case "message_stop":
|
||||
// return the completed message
|
||||
if callback != nil {
|
||||
callback(model.Message{
|
||||
Role: model.MessageRoleAssistant,
|
||||
Content: sb.String(),
|
||||
})
|
||||
reply := model.Message{
|
||||
Role: model.MessageRoleAssistant,
|
||||
Content: sb.String(),
|
||||
}
|
||||
*replies = append(*replies, reply)
|
||||
return sb.String(), nil
|
||||
case "error":
|
||||
return sb.String(), fmt.Errorf("an error occurred: %s", event["error"])
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/lmcli/provider"
|
||||
"git.mlow.ca/mlow/lmcli/pkg/lmcli/tools"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
)
|
||||
@ -161,7 +160,7 @@ func (c *OpenAIClient) CreateChatCompletion(
|
||||
ctx context.Context,
|
||||
params model.RequestParameters,
|
||||
messages []model.Message,
|
||||
callback provider.ReplyCallback,
|
||||
replies *[]model.Message,
|
||||
) (string, error) {
|
||||
client := openai.NewClient(c.APIKey)
|
||||
req := createChatCompletionRequest(c, params, messages)
|
||||
@ -178,19 +177,17 @@ func (c *OpenAIClient) CreateChatCompletion(
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if callback != nil {
|
||||
for _, result := range results {
|
||||
callback(result)
|
||||
}
|
||||
if results != nil {
|
||||
*replies = append(*replies, results...)
|
||||
}
|
||||
|
||||
// Recurse into CreateChatCompletion with the tool call replies
|
||||
messages = append(messages, results...)
|
||||
return c.CreateChatCompletion(ctx, params, messages, callback)
|
||||
return c.CreateChatCompletion(ctx, params, messages, replies)
|
||||
}
|
||||
|
||||
if callback != nil {
|
||||
callback(model.Message{
|
||||
if replies != nil {
|
||||
*replies = append(*replies, model.Message{
|
||||
Role: model.MessageRoleAssistant,
|
||||
Content: choice.Message.Content,
|
||||
})
|
||||
@ -204,7 +201,7 @@ func (c *OpenAIClient) CreateChatCompletionStream(
|
||||
ctx context.Context,
|
||||
params model.RequestParameters,
|
||||
messages []model.Message,
|
||||
callbback provider.ReplyCallback,
|
||||
replies *[]model.Message,
|
||||
output chan<- string,
|
||||
) (string, error) {
|
||||
client := openai.NewClient(c.APIKey)
|
||||
@ -255,20 +252,17 @@ func (c *OpenAIClient) CreateChatCompletionStream(
|
||||
if err != nil {
|
||||
return content.String(), err
|
||||
}
|
||||
|
||||
if callbback != nil {
|
||||
for _, result := range results {
|
||||
callbback(result)
|
||||
}
|
||||
if results != nil {
|
||||
*replies = append(*replies, results...)
|
||||
}
|
||||
|
||||
// Recurse into CreateChatCompletionStream with the tool call replies
|
||||
messages = append(messages, results...)
|
||||
return c.CreateChatCompletionStream(ctx, params, messages, callbback, output)
|
||||
return c.CreateChatCompletionStream(ctx, params, messages, replies, output)
|
||||
}
|
||||
|
||||
if callbback != nil {
|
||||
callbback(model.Message{
|
||||
if replies != nil {
|
||||
*replies = append(*replies, model.Message{
|
||||
Role: model.MessageRoleAssistant,
|
||||
Content: content.String(),
|
||||
})
|
||||
|
@ -6,8 +6,6 @@ import (
|
||||
"git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
|
||||
)
|
||||
|
||||
type ReplyCallback func(model.Message)
|
||||
|
||||
type ChatCompletionClient interface {
|
||||
// CreateChatCompletion requests a response to the provided messages.
|
||||
// Replies are appended to the given replies struct, and the
|
||||
@ -16,7 +14,7 @@ type ChatCompletionClient interface {
|
||||
ctx context.Context,
|
||||
params model.RequestParameters,
|
||||
messages []model.Message,
|
||||
callback ReplyCallback,
|
||||
replies *[]model.Message,
|
||||
) (string, error)
|
||||
|
||||
// Like CreateChageCompletion, except the response is streamed via
|
||||
@ -25,7 +23,7 @@ type ChatCompletionClient interface {
|
||||
ctx context.Context,
|
||||
params model.RequestParameters,
|
||||
messages []model.Message,
|
||||
callback ReplyCallback,
|
||||
replies *[]model.Message,
|
||||
output chan<- string,
|
||||
) (string, error)
|
||||
}
|
||||
|
@ -30,16 +30,15 @@ type model struct {
|
||||
convShortname string
|
||||
|
||||
// application state
|
||||
conversation *models.Conversation
|
||||
messages []models.Message
|
||||
waitingForReply bool
|
||||
replyChan chan string
|
||||
replyCancelFunc context.CancelFunc
|
||||
err error
|
||||
conversation *models.Conversation
|
||||
messages []models.Message
|
||||
replyChan chan string
|
||||
err error
|
||||
|
||||
// ui state
|
||||
focus focusState
|
||||
status string // a general status message
|
||||
focus focusState
|
||||
isWaiting bool
|
||||
status string // a general status message
|
||||
|
||||
// ui elements
|
||||
content viewport.Model
|
||||
@ -91,11 +90,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "ctrl+c":
|
||||
if m.waitingForReply {
|
||||
m.replyCancelFunc()
|
||||
} else {
|
||||
return m, tea.Quit
|
||||
}
|
||||
return m, tea.Quit
|
||||
case "q":
|
||||
if m.focus != focusInput {
|
||||
return m, tea.Quit
|
||||
@ -140,8 +135,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
cmd = waitForChunk(m.replyChan) // wait for the next chunk
|
||||
case msgResponseEnd:
|
||||
m.replyCancelFunc = nil
|
||||
m.waitingForReply = false
|
||||
m.isWaiting = false
|
||||
m.status = "Press ctrl+s to send"
|
||||
}
|
||||
|
||||
@ -190,7 +184,7 @@ func initialModel(ctx *lmcli.Context, convShortname string) model {
|
||||
|
||||
m.updateContent()
|
||||
|
||||
m.waitingForReply = false
|
||||
m.isWaiting = false
|
||||
m.status = "Press ctrl+s to send"
|
||||
return m
|
||||
}
|
||||
@ -223,8 +217,8 @@ func (m *model) handleInputKey(msg tea.KeyMsg) tea.Cmd {
|
||||
m.updateContent()
|
||||
m.content.GotoBottom()
|
||||
|
||||
m.waitingForReply = true
|
||||
m.status = "Waiting for response, press ctrl+c to cancel..."
|
||||
m.isWaiting = true
|
||||
m.status = "Waiting for response... (Press 's' to stop)"
|
||||
return m.promptLLM()
|
||||
}
|
||||
return nil
|
||||
@ -284,12 +278,9 @@ func (m *model) promptLLM() tea.Cmd {
|
||||
ToolBag: toolBag,
|
||||
}
|
||||
|
||||
ctx, replyCancelFunc := context.WithCancel(context.Background())
|
||||
m.replyCancelFunc = replyCancelFunc
|
||||
|
||||
// TODO: supply a reply callback and handle error
|
||||
var apiReplies []models.Message
|
||||
resp, _ := completionProvider.CreateChatCompletionStream(
|
||||
ctx, requestParams, m.messages, nil, m.replyChan,
|
||||
context.Background(), requestParams, m.messages, &apiReplies, m.replyChan,
|
||||
)
|
||||
|
||||
return msgResponseEnd(resp)
|
||||
@ -320,7 +311,7 @@ func (m *model) updateContent() {
|
||||
|
||||
func (m model) inputView() string {
|
||||
var inputView string
|
||||
if m.waitingForReply {
|
||||
if m.isWaiting {
|
||||
inputView = inputStyle.Faint(true).Render(m.input.View())
|
||||
} else {
|
||||
inputView = inputStyle.Render(m.input.View())
|
||||
|
Loading…
Reference in New Issue
Block a user