-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
795de7a
commit 0c1b24f
Showing
4 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module github.com/conneroisu/groq-go/examples/terminal-chat | ||
|
||
go 1.23.0 | ||
|
||
require ( | ||
github.com/charmbracelet/bubbles v0.19.0 | ||
github.com/charmbracelet/bubbletea v1.1.0 | ||
github.com/charmbracelet/lipgloss v0.13.0 | ||
) | ||
|
||
require ( | ||
github.com/atotto/clipboard v0.1.4 // indirect | ||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect | ||
github.com/charmbracelet/x/ansi v0.2.3 // indirect | ||
github.com/charmbracelet/x/term v0.2.0 // indirect | ||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect | ||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/mattn/go-localereader v0.0.1 // indirect | ||
github.com/mattn/go-runewidth v0.0.16 // indirect | ||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect | ||
github.com/muesli/cancelreader v0.2.2 // indirect | ||
github.com/muesli/termenv v0.15.2 // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
golang.org/x/sync v0.8.0 // indirect | ||
golang.org/x/sys v0.24.0 // indirect | ||
golang.org/x/text v0.3.8 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= | ||
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= | ||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= | ||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= | ||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= | ||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= | ||
github.com/charmbracelet/bubbles v0.19.0 h1:gKZkKXPP6GlDk6EcfujDK19PCQqRjaJZQ7QRERx1UF0= | ||
github.com/charmbracelet/bubbles v0.19.0/go.mod h1:WILteEqZ+krG5c3ntGEMeG99nCupcuIk7V0/zOP0tOA= | ||
github.com/charmbracelet/bubbletea v1.1.0 h1:FjAl9eAL3HBCHenhz/ZPjkKdScmaS5SK69JAK2YJK9c= | ||
github.com/charmbracelet/bubbletea v1.1.0/go.mod h1:9Ogk0HrdbHolIKHdjfFpyXJmiCzGwy+FesYkZr7hYU4= | ||
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw= | ||
github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY= | ||
github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqosGDEY= | ||
github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= | ||
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0= | ||
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0= | ||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= | ||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= | ||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= | ||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= | ||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= | ||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= | ||
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= | ||
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= | ||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= | ||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= | ||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= | ||
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= | ||
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= | ||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | ||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= | ||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= | ||
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= | ||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/textarea" | ||
"github.com/charmbracelet/bubbles/viewport" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
"github.com/conneroisu/groq-go" | ||
) | ||
|
||
func main() { | ||
p := tea.NewProgram( | ||
initialModel(), | ||
tea.WithMouseAllMotion(), | ||
tea.WithAltScreen(), | ||
) | ||
if _, err := p.Run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type ( | ||
errMsg error | ||
) | ||
|
||
type model struct { | ||
groqClient *groq.Client | ||
viewport viewport.Model | ||
messages []string | ||
textarea textarea.Model | ||
senderStyle lipgloss.Style | ||
err error | ||
} | ||
|
||
const ( | ||
width = 500 | ||
height = 10 | ||
) | ||
|
||
func initialModel() model { | ||
groqClient, err := groq.NewClient( | ||
os.Getenv("GROQ_KEY"), | ||
) | ||
if err != nil { | ||
return model{ | ||
err: errMsg(err), | ||
} | ||
} | ||
ta := textarea.New() | ||
ta.Placeholder = "Send a message..." | ||
ta.Focus() | ||
ta.Prompt = "┃ " | ||
ta.CharLimit = 280 | ||
ta.SetWidth(500) | ||
ta.SetHeight(10) | ||
ta.FocusedStyle.CursorLine = lipgloss.NewStyle() | ||
ta.ShowLineNumbers = false | ||
|
||
vp := viewport.New(width, height) | ||
vp.SetContent(`Welcome to the chat room! | ||
Type a message and press Enter to send.`) | ||
|
||
ta.KeyMap.InsertNewline.SetEnabled(false) | ||
|
||
return model{ | ||
textarea: ta, | ||
messages: []string{}, | ||
viewport: vp, | ||
senderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("5")), | ||
err: nil, | ||
groqClient: groqClient, | ||
} | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return textarea.Blink | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var ( | ||
tiCmd tea.Cmd | ||
vpCmd tea.Cmd | ||
) | ||
|
||
m.textarea, tiCmd = m.textarea.Update(msg) | ||
m.viewport, vpCmd = m.viewport.Update(msg) | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
fmt.Println(m.textarea.Value()) | ||
return m, tea.Quit | ||
case tea.KeyDown: | ||
m.textarea.SetValue(m.textarea.Value() + "\n") | ||
m.viewport.LineDown(1) | ||
m.viewport.TotalLineCount() | ||
_ = os.WriteFile("chat.txt", []byte(m.textarea.Value()), 0644) | ||
case tea.KeyEnter: | ||
message := m.textarea.Value() | ||
if strings.TrimSpace(m.textarea.Value()) == "" { | ||
break | ||
} | ||
m.messages = append( | ||
m.messages, | ||
m.senderStyle.Render("You: ")+message, | ||
) | ||
m.viewport.SetContent(strings.Join(m.messages, "\n")) | ||
m.textarea.Reset() | ||
m.viewport.GotoBottom() | ||
re, err := m.groqClient.CreateChatCompletionStream(context.Background(), groq.ChatCompletionRequest{ | ||
Model: groq.Llama38B8192, | ||
Messages: []groq.ChatCompletionMessage{ | ||
{ | ||
Role: groq.ChatMessageRoleUser, | ||
Content: message, | ||
}, | ||
}, | ||
MaxTokens: 2000, | ||
}) | ||
if err != nil { | ||
m.err = errMsg(err) | ||
return m, nil | ||
} | ||
newIx := len(m.messages) - 1 | ||
currentCnt := "" | ||
for { | ||
response, err := re.Recv() | ||
if err != nil { | ||
m.err = errMsg(err) | ||
return m, nil | ||
} | ||
if response.Choices[0].FinishReason == groq.FinishReasonStop { | ||
break | ||
} | ||
currentCnt += response.Choices[0].Delta.Content | ||
m.messages[newIx] = m.senderStyle.Render("Groq: ") + currentCnt | ||
m.viewport.SetContent(strings.Join(m.messages, "\n")) | ||
} | ||
} | ||
case tea.MouseAction: | ||
val := tea.MouseAction(msg) | ||
if val == 4 { | ||
m.viewport.LineUp(1) | ||
return m, nil | ||
} else if val == 5 { | ||
m.viewport.LineDown(1) | ||
return m, nil | ||
} | ||
case errMsg: | ||
m.err = msg | ||
return m, nil | ||
} | ||
|
||
return m, tea.Batch(tiCmd, vpCmd) | ||
} | ||
|
||
func (m model) View() string { | ||
return fmt.Sprintf( | ||
"%s\n\n%s", | ||
m.viewport.View(), | ||
m.textarea.View(), | ||
) + "\n\n" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters