From 58fc9452227a970e2a1a6123583005b82a68899c Mon Sep 17 00:00:00 2001 From: root Date: Wed, 4 Dec 2024 13:02:59 +0100 Subject: [PATCH] first commit --- .gitignore | 1 + go.mod | 12 +++++ go.sum | 12 +++++ go.work | 3 ++ go.work.sum | 12 +++++ main.go | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 go.work create mode 100644 go.work.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53c3561 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +chat-go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..622c514 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module chat-go + +go 1.23.3 + +require github.com/openai/openai-go v0.1.0-alpha.38 + +require ( + github.com/tidwall/gjson v1.14.4 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/tidwall/sjson v1.2.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e1dd97e --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/openai/openai-go v0.1.0-alpha.38 h1:j/rL0aEIHWnWaPgA8/AXYKCI79ZoW44NTIpn7qfMEXQ= +github.com/openai/openai-go v0.1.0-alpha.38/go.mod h1:3SdE6BffOX9HPEQv8IL/fi3LYZ5TUpRYaqGQZbyk11A= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= +github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= diff --git a/go.work b/go.work new file mode 100644 index 0000000..8ac335f --- /dev/null +++ b/go.work @@ -0,0 +1,3 @@ +go 1.23.3 + +use ./ diff --git a/go.work.sum b/go.work.sum new file mode 100644 index 0000000..07607b4 --- /dev/null +++ b/go.work.sum @@ -0,0 +1,12 @@ +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.14.0/go.mod h1:l38EPgmsp71HHLq9j7De57JcKOWPyhrsW1Awm1JS6K0= +github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0/go.mod h1:9kIvujWAA58nmPmWB1m23fyWic1kYZMxD9CxaWn4Qpg= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY= +github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..2199b83 --- /dev/null +++ b/main.go @@ -0,0 +1,148 @@ +package main + +import ( + "bufio" + "context" + "fmt" + "os" + + "github.com/openai/openai-go" + "github.com/openai/openai-go/option" +) + +var ( + sys = `You are an expert golang programmer.` + + model = "Qwen/Qwen2.5-Coder-7B-Instruct" + + reader = bufio.NewReader(os.Stdin) + + history = []History{} + + client = openai.NewClient( + option.WithAPIKey("EMPTY"), // defaults to os.LookupEnv("OPENAI_API_KEY") + option.WithBaseURL("http://192.168.55.14:8001/v1/"), + ) + ctx = context.Background() +) + +type History struct { + Assistant string + User string +} + +func readKey(input chan rune) { + char, _, err := reader.ReadRune() + if err != nil { + fmt.Println("Error reading key: ", err) + } + input <- char +} + +func push(h History) int { + history = append(history, h) + return len(history) +} + +func resume(s string) string { + + resumer := "you are a golang expert; very able to resume the conversation; Limited to responding to the question without adding anything else." + question := "resume this conversation in max 100 tokens: " + s + + completion, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{ + Messages: openai.F([]openai.ChatCompletionMessageParamUnion{ + openai.SystemMessage(resumer), + openai.UserMessage(question), + }), + Model: openai.F(model), + }) + if err != nil { + fmt.Printf("Error: %v\n", err) + } + + return completion.Choices[0].Message.Content +} + +func main() { + + input := make(chan rune, 1) + + for { + + current_history := History{ + Assistant: "", + User: "", + } + + print("> ") + loop: + for { + go readKey(input) + + select { + case i := <-input: + current_history.User += string(i) + switch i { + case '\n': + break loop + } + } + } + + println() + + // stream := client.Chat.Completions.NewStreaming(ctx, openai.ChatCompletionNewParams{ + // Messages: openai.F([]openai.ChatCompletionMessageParamUnion{ + // openai.SystemMessage(sys), openai.UserMessage(user), + // }), + // Model: openai.F(model), + // }) + + var messages_hist []openai.ChatCompletionMessageParamUnion + for _, h := range history { + messages_hist = append(messages_hist, openai.UserMessage(h.User), openai.AssistantMessage(h.Assistant)) + } + + // fmt.Printf("%+v\n", messages_hist) + + msg := []openai.ChatCompletionMessageParamUnion{ + openai.SystemMessage(sys), + } + msg = append(msg, messages_hist...) + msg = append(msg, openai.UserMessage(current_history.User)) + + stream := client.Chat.Completions.NewStreaming(ctx, openai.ChatCompletionNewParams{ + Messages: openai.F(msg), + Model: openai.F(model), + }) + + // fmt.Printf("%+v\n", msg) + + fmt.Println() + + // history = append(history, openai.UserMessage(user)) + + for stream.Next() { + evt := stream.Current() + if len(evt.Choices) > 0 { + print(evt.Choices[0].Delta.Content) + current_history.Assistant += evt.Choices[0].Delta.Content + } + } + println() + + if err := stream.Err(); err != nil { + panic(err) + } + + current_history.Assistant = resume(current_history.Assistant) + + // println() + // fmt.Printf("%+v\n", current_history) + + history = append(history, current_history) + + println() + println() + } +}