Compare commits

..

37 Commits

Author SHA1 Message Date
657416780d Lead anthropic function call XML with newline 2024-03-17 18:26:32 +00:00
c143d863cb tui: support for message retry/continue
Better handling of persistence, and we now ensure the response we
persist is trimmed of whitespace, particularly important when a response
is cancelled mid-stream
2024-03-17 18:18:45 +00:00
3aff5514e4 Fix double reply callback on tool calls 2024-03-17 01:07:52 +00:00
5acdbb5675 tui: handle text wrapping ourselves, add ctrl+w wrap toggle
Gets rid of those pesky trailing characters
2024-03-17 00:43:07 +00:00
c53e952acc tui: open input/messages for editing in $EDITOR 2024-03-17 00:11:27 +00:00
3d8d3b61b3 tui: add ability to select a message 2024-03-16 05:49:04 +00:00
4fb059c850 tui: conversation rendering tweaks, remove input character limit 2024-03-16 00:37:08 +00:00
e9fde37201 tui: fixed response cancelling 2024-03-15 06:47:07 +00:00
6242ea17d8 tui: ctrl+r to retry previous message 2024-03-14 17:56:03 +00:00
2ca94e1ffb tui: fixed footer styling 2024-03-14 17:55:31 +00:00
2b0d474660 tui: removed scrollbar 2024-03-14 17:55:21 +00:00
fdf8033aff tui: minor fixed and cleanup 2024-03-14 06:39:25 +00:00
cf46088762 tui: update lodos 2024-03-14 06:01:42 +00:00
c4b78aa0c6 tui: add response waiting spinner 2024-03-14 06:01:42 +00:00
377a4f1dfa tui: add LLM response error handling
+ various other small tweaks
2024-03-14 06:01:42 +00:00
000a2ec6f2 tui: add a "scroll bar" and error view 2024-03-14 06:01:42 +00:00
387dd7534c tui: generate titles for conversations 2024-03-14 06:01:42 +00:00
c14541577e tui: persist new conversations as well 2024-03-14 06:01:42 +00:00
213e36f652 tui: add reply persistence 2024-03-14 06:01:42 +00:00
9e02277ee7 tui: improve footer rendering
Made it easier to add segmemts later, better handling of padding
2024-03-14 06:01:42 +00:00
a96eac91b3 tui: slight function order change 2024-03-14 06:01:42 +00:00
ccf2353a0b tui: cache highlighted messages
Syntax highlighting is fairly expensive, and this means we no longer
need to do syntax highlighting on the entire conversaion each time a new
message chunk is received
2024-03-14 06:01:42 +00:00
51e6f6ebf6 tui: adjust message header styling 2024-03-14 06:01:42 +00:00
6cb8d03c5b tui: style tweaks 2024-03-14 06:01:42 +00:00
50ad7d9ec6 tui: add contentStyle, applied to overall viewport content 2024-03-14 06:01:42 +00:00
5e26ee3373 tui: update TODO 2024-03-14 06:01:42 +00:00
8bc2523c17 tui: fix conversation loading 2024-03-14 06:01:42 +00:00
a06ac694c6 tui: use EnabledTools from lmcli.Context 2024-03-14 06:01:42 +00:00
00eb57820f tui: styling tweak 2024-03-14 06:01:42 +00:00
d1f10d2cfc tui: add header with title 2024-03-14 06:01:42 +00:00
1bd6baa837 tui: handle multi part responses 2024-03-14 06:01:42 +00:00
8613719b58 tui: scroll content view with output
clean up msgResponseChunk handling
2024-03-14 06:01:42 +00:00
51de2b7079 tui: ability to cancel request in flight 2024-03-14 06:01:42 +00:00
fe5baf58e3 tui: add focus switching between input/messages view 2024-03-14 06:01:42 +00:00
0ebfd39297 tui: removed confirm before send, dynamic footer
footer now rendered based on model data, instead of being set to a fixed
string
2024-03-14 06:01:42 +00:00
780c34a7ef tui: use ctx chroma highlighter 2024-03-14 06:01:42 +00:00
6bf2f1bb43 Add initial TUI 2024-03-14 06:01:42 +00:00
4 changed files with 19 additions and 62 deletions

View File

@ -118,18 +118,11 @@ func HandleConversationReply(ctx *lmcli.Context, c *model.Conversation, persist
func FormatForExternalPrompt(messages []model.Message, system bool) string { func FormatForExternalPrompt(messages []model.Message, system bool) string {
sb := strings.Builder{} sb := strings.Builder{}
for _, message := range messages { for _, message := range messages {
if message.Content == "" { if message.Role != model.MessageRoleUser && (message.Role != model.MessageRoleSystem || !system) {
continue continue
} }
switch message.Role { sb.WriteString(fmt.Sprintf("<%s>\n", message.Role.FriendlyRole()))
case model.MessageRoleAssistant, model.MessageRoleToolCall: sb.WriteString(fmt.Sprintf("\"\"\"\n%s\n\"\"\"\n\n", message.Content))
sb.WriteString("Assistant:\n\n")
case model.MessageRoleUser:
sb.WriteString("User:\n\n")
default:
continue
}
sb.WriteString(fmt.Sprintf("%s", lipgloss.NewStyle().PaddingLeft(1).Render(message.Content)))
} }
return sb.String() return sb.String()
} }
@ -140,32 +133,13 @@ func GenerateTitle(ctx *lmcli.Context, c *model.Conversation) (string, error) {
return "", err return "", err
} }
const prompt = `Above is an excerpt from a conversation between a user and AI assistant. Please reply with a short title (no more than 8 words) that reflects the topic of the conversation, read from the user's perspective. const header = "Generate a concise 4-5 word title for the conversation below."
prompt := fmt.Sprintf("%s\n\n---\n\n%s", header, FormatForExternalPrompt(messages, false))
Example conversation:
"""
User:
Hello!
Assistant:
Hello! How may I assist you?
"""
Example response:
"""
Title: A brief introduction
"""
`
conversation := FormatForExternalPrompt(messages, false)
generateRequest := []model.Message{ generateRequest := []model.Message{
{ {
Role: model.MessageRoleUser, Role: model.MessageRoleUser,
Content: fmt.Sprintf("\"\"\"\n%s\n\"\"\"\n\n%s", conversation, prompt), Content: prompt,
}, },
} }
@ -184,15 +158,12 @@ Title: A brief introduction
return "", err return "", err
} }
response = strings.TrimPrefix(response, "Title: ")
response = strings.Trim(response, "\"")
return response, nil return response, nil
} }
// ShowWaitAnimation prints an animated ellipses to stdout until something is // ShowWaitAnimation prints an animated ellipses to stdout until something is
// received on the signal channel. An empty string sent to the channel to // received on the signal channel. An empty string sent to the channel to
// notify the caller that the animation has completed (carriage returned). // noftify the caller that the animation has completed (carriage returned).
func ShowWaitAnimation(signal chan any) { func ShowWaitAnimation(signal chan any) {
// Save the current cursor position // Save the current cursor position
fmt.Print("\033[s") fmt.Print("\033[s")

View File

@ -68,7 +68,7 @@ func buildRequest(params model.RequestParameters, messages []model.Message) Requ
startIdx := 0 startIdx := 0
if len(messages) > 0 && messages[0].Role == model.MessageRoleSystem { if len(messages) > 0 && messages[0].Role == model.MessageRoleSystem {
requestBody.System = messages[0].Content requestBody.System = messages[0].Content
requestBody.Messages = requestBody.Messages[1:] requestBody.Messages = requestBody.Messages[:len(messages)-1]
startIdx = 1 startIdx = 1
} }

View File

@ -9,10 +9,9 @@ import (
"git.mlow.ca/mlow/lmcli/pkg/lmcli/model" "git.mlow.ca/mlow/lmcli/pkg/lmcli/model"
) )
const TOOL_PREAMBLE = `You have access to the following tools when replying. const TOOL_PREAMBLE = `In this environment you have access to a set of tools which may assist you in fulfilling user requests.
You may call them like this: You may call them like this:
<function_calls> <function_calls>
<invoke> <invoke>
<tool_name>$TOOL_NAME</tool_name> <tool_name>$TOOL_NAME</tool_name>
@ -25,14 +24,6 @@ You may call them like this:
Here are the tools available:` Here are the tools available:`
const TOOL_PREAMBLE_FOOTER = `Recognize the utility of these tools in a broad range of different applications, and the power they give you to solve a wide range of different problems. However, ensure that the tools are used judiciously and only when clearly relevant to the user's request. Specifically:
1. Only use a tool if the user has explicitly requested or provided information that warrants its use. Do not make assumptions about files or data existing without the user mentioning them.
2. If there is ambiguity about whether using a tool is appropriate, ask a clarifying question to the user before proceeding. Confirm your understanding of their request and intent.
3. Prioritize providing direct responses and explanations based on your own knowledge and understanding. Use tools to supplement and enhance your responses when clearly applicable, but not as a default action.`
type XMLTools struct { type XMLTools struct {
XMLName struct{} `xml:"tools"` XMLName struct{} `xml:"tools"`
ToolDescriptions []XMLToolDescription `xml:"tool_description"` ToolDescriptions []XMLToolDescription `xml:"tool_description"`
@ -160,7 +151,7 @@ func buildToolsSystemPrompt(tools []model.Tool) string {
if err != nil { if err != nil {
panic("Could not serialize []model.Tool to XMLTools") panic("Could not serialize []model.Tool to XMLTools")
} }
return TOOL_PREAMBLE + "\n\n" + xmlToolsString + "\n\n" + TOOL_PREAMBLE_FOOTER return TOOL_PREAMBLE + "\n" + xmlToolsString + "\n"
} }
func (x XMLTools) XMLString() (string, error) { func (x XMLTools) XMLString() (string, error) {

View File

@ -244,7 +244,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if last < 0 { if last < 0 {
panic("Unexpected empty messages handling msgResponseEnd") panic("Unexpected empty messages handling msgResponseEnd")
} }
m.setMessageContents(last, strings.TrimSpace(string(msg))) m.setMessageContents(last, strings.TrimSpace(m.messages[last].Content))
m.updateContent() m.updateContent()
m.status = "Press ctrl+s to send" m.status = "Press ctrl+s to send"
case msgResponseError: case msgResponseError:
@ -786,22 +786,17 @@ func (m *model) conversationView() string {
// write message heading with space for content // write message heading with space for content
user := style.Render(icon + friendly) user := style.Render(icon + friendly)
var prefix string var saved string
var suffix string
faint := lipgloss.NewStyle().Faint(true)
if m.focus == focusMessages {
if i == m.selectedMessage {
prefix = "> "
}
suffix += faint.Render(fmt.Sprintf(" (%d/%d)", i+1, msgCnt))
}
if message.ID == 0 { if message.ID == 0 {
suffix += faint.Render(" (not saved)") saved = lipgloss.NewStyle().Faint(true).Render(" (not saved)")
} }
header := lipgloss.NewStyle().PaddingLeft(1).Render(prefix + user + suffix) var selectedPrefix string
if m.focus == focusMessages && i == m.selectedMessage {
selectedPrefix = "> "
}
header := lipgloss.NewStyle().PaddingLeft(1).Render(selectedPrefix + user + saved)
sb.WriteString(header) sb.WriteString(header)
lineCnt += lipgloss.Height(header) lineCnt += lipgloss.Height(header)