Compare commits
1 Commits
6eca84dab8
...
a28a7a0054
Author | SHA1 | Date | |
---|---|---|---|
a28a7a0054 |
@ -12,7 +12,3 @@ func Fatal(format string, args ...any) {
|
|||||||
fmt.Fprintf(os.Stderr, format, args...)
|
fmt.Fprintf(os.Stderr, format, args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Warn(format string, args ...any) {
|
|
||||||
fmt.Fprintf(os.Stderr, format, args...)
|
|
||||||
}
|
|
||||||
|
@ -71,65 +71,29 @@ var newCmd = &cobra.Command{
|
|||||||
messageContents, err := InputFromEditor("# What would you like to say?\n", "message.*.md")
|
messageContents, err := InputFromEditor("# What would you like to say?\n", "message.*.md")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("Failed to get input: %v\n", err)
|
Fatal("Failed to get input: %v\n", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if messageContents == "" {
|
if messageContents == "" {
|
||||||
Fatal("No message was provided.\n")
|
Fatal("No message was provided.\n")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: set title if --title provided, otherwise defer for later(?)
|
fmt.Printf("> %s\n", messageContents)
|
||||||
conversation := Conversation{}
|
|
||||||
err = store.SaveConversation(&conversation)
|
|
||||||
if err != nil {
|
|
||||||
Fatal("Could not save new conversation: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
const system = "You are a helpful assistant."
|
|
||||||
messages := []Message{
|
messages := []Message{
|
||||||
{
|
{
|
||||||
ConversationID: conversation.ID,
|
|
||||||
Role: "system",
|
|
||||||
OriginalContent: system,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ConversationID: conversation.ID,
|
|
||||||
Role: "user",
|
Role: "user",
|
||||||
OriginalContent: messageContents,
|
OriginalContent: messageContents,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, message := range messages {
|
|
||||||
err = store.SaveMessage(&message)
|
|
||||||
if err != nil {
|
|
||||||
Warn("Could not save %s message: %v\n", message.Role, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, message := range messages {
|
|
||||||
message.RenderTTY(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
reply := Message{
|
|
||||||
ConversationID: conversation.ID,
|
|
||||||
Role: "assistant",
|
|
||||||
}
|
|
||||||
|
|
||||||
reply.RenderTTY(false)
|
|
||||||
|
|
||||||
receiver := make(chan string)
|
receiver := make(chan string)
|
||||||
response := make(chan string)
|
go HandleDelayedResponse(receiver)
|
||||||
go func() {
|
err = CreateChatCompletionStream("You are a helpful assistant.", messages, receiver)
|
||||||
response <- HandleDelayedResponse(receiver)
|
|
||||||
}()
|
|
||||||
err = CreateChatCompletionStream(messages, receiver)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("%v\n", err)
|
Fatal("%v\n", err)
|
||||||
}
|
return
|
||||||
|
|
||||||
reply.OriginalContent = <-response
|
|
||||||
|
|
||||||
err = store.SaveMessage(&reply)
|
|
||||||
if err != nil {
|
|
||||||
Fatal("Could not save reply: %v\n", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
@ -144,14 +108,10 @@ var promptCmd = &cobra.Command{
|
|||||||
message := strings.Join(args, " ")
|
message := strings.Join(args, " ")
|
||||||
if len(strings.Trim(message, " \t\n")) == 0 {
|
if len(strings.Trim(message, " \t\n")) == 0 {
|
||||||
Fatal("No message was provided.\n")
|
Fatal("No message was provided.\n")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const system = "You are a helpful assistant."
|
|
||||||
messages := []Message{
|
messages := []Message{
|
||||||
{
|
|
||||||
Role: "system",
|
|
||||||
OriginalContent: system,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
Role: "user",
|
Role: "user",
|
||||||
OriginalContent: message,
|
OriginalContent: message,
|
||||||
@ -160,9 +120,10 @@ var promptCmd = &cobra.Command{
|
|||||||
|
|
||||||
receiver := make(chan string)
|
receiver := make(chan string)
|
||||||
go HandleDelayedResponse(receiver)
|
go HandleDelayedResponse(receiver)
|
||||||
err := CreateChatCompletionStream(messages, receiver)
|
err := CreateChatCompletionStream("You are a helpful assistant.", messages, receiver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("%v\n", err)
|
Fatal("%v\n", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
@ -170,9 +131,6 @@ var promptCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewRootCmd() *cobra.Command {
|
func NewRootCmd() *cobra.Command {
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(newCmd, promptCmd)
|
||||||
newCmd,
|
|
||||||
promptCmd,
|
|
||||||
)
|
|
||||||
return rootCmd
|
return rootCmd
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ func InitializeConfig() *Config {
|
|||||||
|
|
||||||
_, err = file.Write(bytes)
|
_, err = file.Write(bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatal("Could not save default configuration: %v", err)
|
Fatal("Could not save default configuratoin: %v", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
@ -8,8 +8,14 @@ import (
|
|||||||
openai "github.com/sashabaranov/go-openai"
|
openai "github.com/sashabaranov/go-openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateChatCompletionRequest(messages []Message) *openai.ChatCompletionRequest {
|
func CreateChatCompletionRequest(system string, messages []Message) *openai.ChatCompletionRequest {
|
||||||
chatCompletionMessages := []openai.ChatCompletionMessage{}
|
chatCompletionMessages := []openai.ChatCompletionMessage{
|
||||||
|
{
|
||||||
|
Role: "system",
|
||||||
|
Content: system,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
for _, m := range messages {
|
for _, m := range messages {
|
||||||
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
chatCompletionMessages = append(chatCompletionMessages, openai.ChatCompletionMessage{
|
||||||
Role: m.Role,
|
Role: m.Role,
|
||||||
@ -27,11 +33,11 @@ func CreateChatCompletionRequest(messages []Message) *openai.ChatCompletionReque
|
|||||||
|
|
||||||
// CreateChatCompletion accepts a slice of Message and returns the response
|
// CreateChatCompletion accepts a slice of Message and returns the response
|
||||||
// of the Large Language Model.
|
// of the Large Language Model.
|
||||||
func CreateChatCompletion(messages []Message) (string, error) {
|
func CreateChatCompletion(system string, messages []Message) (string, error) {
|
||||||
client := openai.NewClient(config.OpenAI.APIKey)
|
client := openai.NewClient(config.OpenAI.APIKey)
|
||||||
resp, err := client.CreateChatCompletion(
|
resp, err := client.CreateChatCompletion(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
*CreateChatCompletionRequest(messages),
|
*CreateChatCompletionRequest(system, messages),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -41,13 +47,13 @@ func CreateChatCompletion(messages []Message) (string, error) {
|
|||||||
return resp.Choices[0].Message.Content, nil
|
return resp.Choices[0].Message.Content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateChatCompletionStream submits an streaming Chat Completion API request
|
// CreateChatCompletionStream submits an streaming Chat Completion API request
|
||||||
// and sends the received data to the output channel.
|
// and sends the received data to the output channel.
|
||||||
func CreateChatCompletionStream(messages []Message, output chan string) error {
|
func CreateChatCompletionStream(system string, messages []Message, output chan string) error {
|
||||||
client := openai.NewClient(config.OpenAI.APIKey)
|
client := openai.NewClient(config.OpenAI.APIKey)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
req := CreateChatCompletionRequest(messages)
|
req := CreateChatCompletionRequest(system, messages)
|
||||||
req.Stream = true
|
req.Stream = true
|
||||||
|
|
||||||
defer close(output)
|
defer close(output)
|
||||||
|
@ -2,7 +2,6 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,14 +33,12 @@ func ShowWaitAnimation(signal chan any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HandledDelayedResponse writes a waiting animation (abusing \r) and the
|
// HandledDelayedResponse writes a waiting animation (abusing \r) and the
|
||||||
// (possibly chunked) content received on the response channel to stdout.
|
// content received on the response channel to stdout. Blocks until the channel
|
||||||
// Blocks until the channel is closed.
|
// is closed.
|
||||||
func HandleDelayedResponse(response chan string) string {
|
func HandleDelayedResponse(response chan string) {
|
||||||
waitSignal := make(chan any)
|
waitSignal := make(chan any)
|
||||||
go ShowWaitAnimation(waitSignal)
|
go ShowWaitAnimation(waitSignal)
|
||||||
|
|
||||||
sb := strings.Builder{}
|
|
||||||
|
|
||||||
firstChunk := true
|
firstChunk := true
|
||||||
for chunk := range response {
|
for chunk := range response {
|
||||||
if firstChunk {
|
if firstChunk {
|
||||||
@ -50,27 +47,5 @@ func HandleDelayedResponse(response chan string) string {
|
|||||||
firstChunk = false
|
firstChunk = false
|
||||||
}
|
}
|
||||||
fmt.Print(chunk)
|
fmt.Print(chunk)
|
||||||
sb.WriteString(chunk)
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Message) RenderTTY(paddingDown bool) {
|
|
||||||
var friendlyRole string
|
|
||||||
switch m.Role {
|
|
||||||
case "user":
|
|
||||||
friendlyRole = "You"
|
|
||||||
case "system":
|
|
||||||
friendlyRole = "System"
|
|
||||||
case "assistant":
|
|
||||||
friendlyRole = "Assistant"
|
|
||||||
}
|
|
||||||
fmt.Printf("<%s>\n\n", friendlyRole)
|
|
||||||
if m.OriginalContent != "" {
|
|
||||||
fmt.Print(m.OriginalContent)
|
|
||||||
}
|
|
||||||
if paddingDown {
|
|
||||||
fmt.Print("\n\n")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,5 +46,5 @@ func InputFromEditor(placeholder string, pattern string) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Trim(content, "\n \t"), nil
|
return content, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user