f
This commit is contained in:
@@ -10,26 +10,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
fmt.Printf("\nmessage received 1: [%s]", m.Message.Content)
|
fmt.Printf("\nmessage received: [%s] [%s]", m.ID, m.Message.Content)
|
||||||
|
|
||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if containUser(m.Mentions, s.State.User.ID) {
|
if containUser(m.Mentions, s.State.User.ID) {
|
||||||
fmt.Printf("\nmessage received 2: [%s]", m.Message.Content)
|
|
||||||
|
|
||||||
aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken)
|
aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken)
|
||||||
|
|
||||||
fmt.Printf("\nAfter openai client: %+v", aiclient)
|
|
||||||
|
|
||||||
res, err := aiclient.Completions(m.Content)
|
res, err := aiclient.Completions(m.Content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("failed to query open ai with the following prompt [%s]. Error: %s", m.Content, err.Error())
|
log.Panicf("failed to query open ai with the following prompt [%s]. Error: %s", m.Content, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nopen ai responses: [%d]", len(res.Choices))
|
|
||||||
|
|
||||||
if len(res.Choices) > 0 {
|
if len(res.Choices) > 0 {
|
||||||
aiResponse := res.Choices[0].Text
|
aiResponse := res.Choices[0].Text
|
||||||
if _, err = s.ChannelMessageSend(m.ChannelID, aiResponse); err != nil {
|
if _, err = s.ChannelMessageSend(m.ChannelID, aiResponse); err != nil {
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ func main() {
|
|||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
fmt.Println("env: ", env)
|
|
||||||
|
|
||||||
discordToken := fmt.Sprintf("Bot %s", env.Token)
|
discordToken := fmt.Sprintf("Bot %s", env.Token)
|
||||||
dgclient, err := discordbot.New(discordToken)
|
dgclient, err := discordbot.New(discordToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ func NewClient(token string) *Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) Completions(prompt string) (*CompletionResponse, error) {
|
func (c Client) Completions(prompt string) (*CompletionResponse, error) {
|
||||||
fmt.Printf("\ncompletions: %s", prompt)
|
|
||||||
body := &CompletionPayload{
|
body := &CompletionPayload{
|
||||||
Model: "text-davinci-003",
|
Model: "text-davinci-003",
|
||||||
Prompt: cleanPrompt(prompt),
|
Prompt: cleanPrompt(prompt),
|
||||||
@@ -34,8 +33,6 @@ func (c Client) Completions(prompt string) (*CompletionResponse, error) {
|
|||||||
return nil, fmt.Errorf("failed to marshal the payload. %s", err.Error())
|
return nil, fmt.Errorf("failed to marshal the payload. %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nsend request to openai")
|
|
||||||
|
|
||||||
res, err := c.HttpClient.NewRequest().
|
res, err := c.HttpClient.NewRequest().
|
||||||
SetHeader("Content-Type", "application/json").
|
SetHeader("Content-Type", "application/json").
|
||||||
SetBody(b).
|
SetBody(b).
|
||||||
@@ -44,8 +41,6 @@ func (c Client) Completions(prompt string) (*CompletionResponse, error) {
|
|||||||
return nil, fmt.Errorf("failed to query openai. Error: %s", err.Error())
|
return nil, fmt.Errorf("failed to query openai. Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nreceived openai response: %+v", res.StatusCode())
|
|
||||||
|
|
||||||
var r CompletionResponse
|
var r CompletionResponse
|
||||||
if err := json.Unmarshal(res.Body(), &r); err != nil {
|
if err := json.Unmarshal(res.Body(), &r); err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse the openai response. Error: %s", err.Error())
|
return nil, fmt.Errorf("failed to parse the openai response. Error: %s", err.Error())
|
||||||
|
|||||||
Reference in New Issue
Block a user