generated from ks6088ts/template-go
-
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.
Merge pull request #2 from ks6088ts-labs/feature/issue-1_aoai
add aoai command
- Loading branch information
Showing
15 changed files
with
263 additions
and
41 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
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 |
---|---|---|
@@ -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 |
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
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
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
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
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
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
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 @@ | ||
/* | ||
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 | ||
} |
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,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") | ||
} |
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
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
Oops, something went wrong.