Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions internal/llm/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"errors"
"io"

"github.com/aavshr/panda/internal/db"

Check failure on line 8 in internal/llm/openai/openai.go

View workflow job for this annotation

GitHub Actions / lint-build-test

could not import github.com/aavshr/panda/internal/db (-: cannot compile Go 1.23 code) (typecheck)
client "github.com/sashabaranov/go-openai"
)

Expand Down Expand Up @@ -65,20 +66,28 @@
return nil
}

func (o *OpenAI) CreateChatCompletion(ctx context.Context, model, input string) (string, error) {
func (o *OpenAI) dbMessagesToClientMessage(messages []*db.Message) []client.ChatCompletionMessage {
var clientMessages []client.ChatCompletionMessage
for _, m := range messages {
m := m
clientMessages = append(clientMessages, client.ChatCompletionMessage{
Role: m.Role,
Content: m.Content,
})
}
return clientMessages
}

// TODO: fix coupling with db message
func (o *OpenAI) CreateChatCompletion(ctx context.Context, model string, messages []*db.Message) (string, error) {
if o.apiKey == "" {
return "", ErrAPIKeyNotSet
}
resp, err := o.client.CreateChatCompletion(
ctx,
client.ChatCompletionRequest{
Model: model,
Messages: []client.ChatCompletionMessage{
{
Role: client.ChatMessageRoleUser,
Content: input,
},
},
Model: model,
Messages: o.dbMessagesToClientMessage(messages),
},
)
if err != nil {
Expand All @@ -90,19 +99,15 @@
return resp.Choices[0].Message.Content, nil
}

func (o *OpenAI) CreateChatCompletionStream(ctx context.Context, model, input string) (io.ReadCloser, error) {
// TODO: coupling with db.Message
func (o *OpenAI) CreateChatCompletionStream(ctx context.Context, model string, messages []*db.Message) (io.ReadCloser, error) {
if o.apiKey == "" {
return nil, ErrAPIKeyNotSet
}
req := client.ChatCompletionRequest{
Model: model,
Messages: []client.ChatCompletionMessage{
{
Role: client.ChatMessageRoleUser,
Content: input,
},
},
Stream: true,
Model: model,
Messages: o.dbMessagesToClientMessage(messages),
Stream: true,
}
stream, err := o.client.CreateChatCompletionStream(ctx, req)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/ui/components/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"fmt"
"strings"

"github.com/aavshr/panda/internal/ui/styles"

Check failure on line 7 in internal/ui/components/chat.go

View workflow job for this annotation

GitHub Actions / lint-build-test

could not import github.com/aavshr/panda/internal/ui/styles (-: cannot compile Go 1.23 code) (typecheck)
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

// TODO: use same Message model throughout the app
type Message struct {
Content string
CreatedAt string
Expand Down
6 changes: 3 additions & 3 deletions internal/ui/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"slices"
"time"

"github.com/aavshr/panda/internal/config"

Check failure on line 11 in internal/ui/handlers.go

View workflow job for this annotation

GitHub Actions / lint-build-test

could not import github.com/aavshr/panda/internal/config (-: cannot compile Go 1.23 code) (typecheck)
"github.com/aavshr/panda/internal/db"
"github.com/aavshr/panda/internal/ui/components"
"github.com/aavshr/panda/internal/ui/styles"

Check failure on line 14 in internal/ui/handlers.go

View workflow job for this annotation

GitHub Actions / lint-build-test

could not import github.com/aavshr/panda/internal/ui/styles (-: cannot compile Go 1.23 code) (typecheck)
"github.com/aavshr/panda/internal/utils"

Check failure on line 15 in internal/ui/handlers.go

View workflow job for this annotation

GitHub Actions / lint-build-test

could not import github.com/aavshr/panda/internal/utils (-: cannot compile Go 1.23 code) (typecheck)
tea "github.com/charmbracelet/bubbletea"
"golang.design/x/clipboard"
)
Expand Down Expand Up @@ -146,10 +146,10 @@
if err := m.store.CreateMessage(userMessage); err != nil {
return m.cmdError(fmt.Errorf("store.CreateMessage: %w", err))
}
m.setMessages(append(m.messages, userMessage))
// TODO: history
messages := append(m.messages, userMessage)
m.setMessages(messages)
reader, err := m.llm.CreateChatCompletionStream(context.Background(),
m.userConfig.LLMModel, msg.Value)
m.userConfig.LLMModel, messages)
if err != nil {
return m.cmdError(fmt.Errorf("llm.CreateChatCompletionStream: %w", err))
}
Expand Down
6 changes: 4 additions & 2 deletions internal/ui/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import (
"context"
"github.com/aavshr/panda/internal/db"

Check failure on line 5 in internal/ui/llm/llm.go

View workflow job for this annotation

GitHub Actions / lint-build-test

could not import github.com/aavshr/panda/internal/db (-: cannot compile Go 1.23 code) (typecheck)
"io"
"strings"
)

// TODO: fix coupling with db message
type LLM interface {
CreateChatCompletion(context.Context, string, string) (string, error)
CreateChatCompletionStream(context.Context, string, string) (io.ReadCloser, error)
CreateChatCompletion(context.Context, string, []*db.Message) (string, error)
CreateChatCompletionStream(context.Context, string, []*db.Message) (io.ReadCloser, error)
SetAPIKey(string) error
}

Expand Down
Loading