Skip to content

Commit

Permalink
Merge pull request #2 from ks6088ts-labs/feature/issue-1_aoai
Browse files Browse the repository at this point in the history
add aoai command
  • Loading branch information
ks6088ts authored Sep 11, 2024
2 parents 8cf132e + 1b606b0 commit af5177c
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ features, by not reporting duplicate issues. Providing the following information
- **Suggest a Fix** - if you can't fix the bug yourself, perhaps you can point to what might be
causing the problem (line of code or commit)

You can file new issues by providing the above information at the corresponding repository's issues link: https://github.com/ks6088ts/template-go/issues/new.
You can file new issues by providing the above information at the corresponding repository's issues link: https://github.com/ks6088ts-labs/sandbox-go/issues/new.

### <a name="submit-pr"></a> Submitting a Pull Request (PR)

Before you submit your Pull Request (PR) consider the following guidelines:

- Search the repository https://github.com/ks6088ts/template-go/pulls for an open or closed PR that relates to your submission. You don't want to duplicate effort.
- Search the repository https://github.com/ks6088ts-labs/sandbox-go/pulls for an open or closed PR that relates to your submission. You don't want to duplicate effort.

- Make your changes in a new git fork
- Commit your changes using a descriptive commit message
Expand Down
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
blank_issues_enabled: false
contact_links:
- name: Support Request
url: https://github.com/ks6088ts/template-go/issues
url: https://github.com/ks6088ts-labs/sandbox-go/issues
about: Create an issue for support request or question relating to this repository
4 changes: 2 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ What kind of change does this Pull Request introduce?
- Get the code

```
git clone git@github.com:ks6088ts/template-go.git
cd template-go
git clone git@github.com:ks6088ts-labs/sandbox-go.git
cd sandbox-go
```

- Test the code
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ jobs:
GIT_REVISION=${{ github.sha }}
GIT_TAG=${{steps.set_version.outputs.no-dash}}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/template-go:${{steps.set_version.outputs.no-dash}}
${{ secrets.DOCKERHUB_USERNAME }}/template-go:latest
${{ secrets.DOCKERHUB_USERNAME }}/sandbox-go:${{steps.set_version.outputs.no-dash}}
${{ secrets.DOCKERHUB_USERNAME }}/sandbox-go:latest
2 changes: 1 addition & 1 deletion .github/workflows/ghcr-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
tags:
- "v*"
env:
IMAGE_NAME: template-go
IMAGE_NAME: sandbox-go
jobs:
ghcr:
runs-on: ubuntu-latest
Expand Down
10 changes: 5 additions & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ builds:
- darwin
goarch:
- amd64
- '386'
- "386"
- arm
- arm64
ignore:
- goos: darwin
goarch: '386'
goarch: "386"
flags:
- -trimpath
ldflags:
- '-s -w -X github.com/ks6088ts/template-go/internal.Version={{.Version}} -X github.com/ks6088ts/template-go/internal.Revision={{.Commit}}'
binary: '{{ .ProjectName }}_v{{ .Version }}'
- "-s -w -X github.com/ks6088ts-labs/sandbox-go/internal.Version={{.Version}} -X github.com/ks6088ts-labs/sandbox-go/internal.Revision={{.Commit}}"
binary: "{{ .ProjectName }}_v{{ .Version }}"

archives:
- format: tar.gz
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
# use zip for windows archives
format_overrides:
- goos: windows
Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
GOBUILD ?= GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go build
LDFLAGS ?= '-s -w \
-X "github.com/ks6088ts/template-go/internal.Revision=$(GIT_REVISION)" \
-X "github.com/ks6088ts/template-go/internal.Version=$(GIT_TAG)" \
-X "github.com/ks6088ts-labs/sandbox-go/internal.Revision=$(GIT_REVISION)" \
-X "github.com/ks6088ts-labs/sandbox-go/internal.Version=$(GIT_TAG)" \
'

# Docker
DOCKER_REPO_NAME ?= ks6088ts
DOCKER_IMAGE_NAME ?= template-go
DOCKER_IMAGE_NAME ?= sandbox-go
DOCKER_COMMAND ?= /app

# Tools
Expand All @@ -23,7 +23,7 @@ TRIVY_VERSION ?= 0.49.1

# Misc
OUTPUT_DIR ?= dist
OUTPUT ?= $(OUTPUT_DIR)/template-go
OUTPUT ?= $(OUTPUT_DIR)/sandbox-go

.PHONY: help
help:
Expand Down Expand Up @@ -54,6 +54,7 @@ test: ## run tests

.PHONY: build
build: ## build applications
go mod tidy
mkdir -p $(OUTPUT_DIR)
$(GOBUILD) -ldflags=$(LDFLAGS) -trimpath -o $(OUTPUT) .

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[![test](https://github.com/ks6088ts/template-go/actions/workflows/test.yaml/badge.svg?branch=main)](https://github.com/ks6088ts/template-go/actions/workflows/test.yaml?query=branch%3Amain)
[![release](https://github.com/ks6088ts/template-go/actions/workflows/release.yaml/badge.svg)](https://github.com/ks6088ts/template-go/actions/workflows/release.yaml)
[![Go Report Card](https://goreportcard.com/badge/github.com/ks6088ts/template-go)](https://goreportcard.com/report/github.com/ks6088ts/template-go)
[![Go Reference](https://pkg.go.dev/badge/github.com/ks6088ts/template-go.svg)](https://pkg.go.dev/github.com/ks6088ts/template-go)
[![test](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/test.yaml/badge.svg?branch=main)](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/test.yaml?query=branch%3Amain)
[![release](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/release.yaml/badge.svg)](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/release.yaml)
[![Go Report Card](https://goreportcard.com/badge/github.com/ks6088ts-labs/sandbox-go)](https://goreportcard.com/report/github.com/ks6088ts-labs/sandbox-go)
[![Go Reference](https://pkg.go.dev/badge/github.com/ks6088ts-labs/sandbox-go.svg)](https://pkg.go.dev/github.com/ks6088ts-labs/sandbox-go)

[![docker](https://github.com/ks6088ts/template-go/actions/workflows/docker.yaml/badge.svg?branch=main)](https://github.com/ks6088ts/template-go/actions/workflows/docker.yaml?query=branch%3Amain)
[![docker-release](https://github.com/ks6088ts/template-go/actions/workflows/docker-release.yaml/badge.svg)](https://github.com/ks6088ts/template-go/actions/workflows/docker-release.yaml)
[![ghcr-release](https://github.com/ks6088ts/template-go/actions/workflows/ghcr-release.yaml/badge.svg)](https://github.com/ks6088ts/template-go/actions/workflows/ghcr-release.yaml)
[![docker](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/docker.yaml/badge.svg?branch=main)](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/docker.yaml?query=branch%3Amain)
[![docker-release](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/docker-release.yaml/badge.svg)](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/docker-release.yaml)
[![ghcr-release](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/ghcr-release.yaml/badge.svg)](https://github.com/ks6088ts-labs/sandbox-go/actions/workflows/ghcr-release.yaml)

# template-go
# sandbox-go

A GitHub template repository for Go

Expand Down
43 changes: 43 additions & 0 deletions cmd/aoai/aoai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright © 2024 ks6088ts
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package aoai

import (
"fmt"

"github.com/spf13/cobra"
)

// aoaiCmd represents the aoai command
var aoaiCmd = &cobra.Command{
Use: "aoai",
Short: "A command for Azure OpenAI Service",
Long: `This is a command for Azure OpenAI Service.
ref. https://learn.microsoft.com/azure/ai-services/openai/overview`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("aoai called")
},
}

func GetCommand() *cobra.Command {
return aoaiCmd
}
129 changes: 129 additions & 0 deletions cmd/aoai/chatCompletion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copyright © 2024 ks6088ts
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package aoai

import (
"context"
"fmt"
"log"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/spf13/cobra"
)

// chatCompletionCmd represents the chatCompletion command
var chatCompletionCmd = &cobra.Command{
Use: "chatCompletion",
Short: "A command for Azure OpenAI Service Chat Completion",
Long: `ref. https://learn.microsoft.com/azure/ai-services/openai/chatgpt-quickstart?tabs=command-line%2Cpython-new&pivots=programming-language-go`,
Run: func(cmd *cobra.Command, args []string) {
// get the values of the flags
modelDeploymentID, err := cmd.Flags().GetString("modelDeploymentID")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
azureOpenAIEndpoint, err := cmd.Flags().GetString("azureOpenAIEndpoint")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
azureOpenAIKey, err := cmd.Flags().GetString("azureOpenAIKey")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
message, err := cmd.Flags().GetString("message")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}

keyCredential := azcore.NewKeyCredential(azureOpenAIKey)

client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

if err != nil {
log.Printf("ERROR: %s", err)
return
}

messages := []azopenai.ChatRequestMessageClassification{
&azopenai.ChatRequestSystemMessage{Content: to.Ptr("You are a helpful assistant.")},
&azopenai.ChatRequestUserMessage{Content: azopenai.NewChatRequestUserMessageContent(message)},
}

gotReply := false

resp, err := client.GetChatCompletions(context.TODO(), azopenai.ChatCompletionsOptions{
Messages: messages,
DeploymentName: &modelDeploymentID,
}, nil)

if err != nil {
log.Printf("ERROR: %s", err)
return
}

for _, choice := range resp.Choices {
gotReply = true

if choice.ContentFilterResults != nil {
fmt.Fprintf(os.Stderr, "Content filter results\n")

if choice.ContentFilterResults.Error != nil {
fmt.Fprintf(os.Stderr, " Error:%v\n", choice.ContentFilterResults.Error)
}

fmt.Fprintf(os.Stderr, " Hate: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Hate.Severity, *choice.ContentFilterResults.Hate.Filtered)
fmt.Fprintf(os.Stderr, " SelfHarm: sev: %v, filtered: %v\n", *choice.ContentFilterResults.SelfHarm.Severity, *choice.ContentFilterResults.SelfHarm.Filtered)
fmt.Fprintf(os.Stderr, " Sexual: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Sexual.Severity, *choice.ContentFilterResults.Sexual.Filtered)
fmt.Fprintf(os.Stderr, " Violence: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Violence.Severity, *choice.ContentFilterResults.Violence.Filtered)
}

if choice.Message != nil && choice.Message.Content != nil {
fmt.Fprintf(os.Stderr, "Content[%d]: %s\n", *choice.Index, *choice.Message.Content)
}

if choice.FinishReason != nil {
// this choice's conversation is complete.
fmt.Fprintf(os.Stderr, "Finish reason[%d]: %s\n", *choice.Index, *choice.FinishReason)
}
}

if gotReply {
fmt.Fprintf(os.Stderr, "Received chat completions reply\n")
}
},
}

func init() {
aoaiCmd.AddCommand(chatCompletionCmd)

chatCompletionCmd.Flags().StringP("modelDeploymentID", "d", "gpt-4o", "Model Deployment ID")
chatCompletionCmd.Flags().StringP("azureOpenAIEndpoint", "e", "", "Azure OpenAI Endpoint")
chatCompletionCmd.Flags().StringP("azureOpenAIKey", "k", "", "Azure OpenAI Key")
chatCompletionCmd.Flags().StringP("message", "m", "Hello, how are you?", "Message")
}
13 changes: 8 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"fmt"
"os"

"github.com/ks6088ts/template-go/cmd/sandbox"
"github.com/ks6088ts-labs/sandbox-go/cmd/aoai"

"github.com/ks6088ts-labs/sandbox-go/cmd/sandbox"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -34,7 +36,7 @@ var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "template-go",
Use: "sandbox-go",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Expand Down Expand Up @@ -63,7 +65,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.template-go.yaml)")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.sandbox-go.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand All @@ -83,10 +85,10 @@ func initConfig() {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".template-go" (without extension).
// Search config in home directory with name ".sandbox-go" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(".template-go")
viper.SetConfigName(".sandbox-go")
}

viper.AutomaticEnv() // read in environment variables that match
Expand All @@ -100,5 +102,6 @@ func initConfig() {
// registerSubCommands registers sub commands
func registerSubCommands() {
rootCmd.AddCommand(sandbox.GetCommand())
rootCmd.AddCommand(aoai.GetCommand())
// Add other sub commands here
}
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package cmd
import (
"fmt"

"github.com/ks6088ts/template-go/internal"
"github.com/ks6088ts-labs/sandbox-go/internal"
"github.com/spf13/cobra"
)

Expand Down
Loading

0 comments on commit af5177c

Please sign in to comment.