diff --git a/handlers/message_created.go b/handlers/message_created.go index 7decda5..3f9ce72 100644 --- a/handlers/message_created.go +++ b/handlers/message_created.go @@ -16,24 +16,15 @@ var messagesHistory = []*openai.ChatMessage{} func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) { isHal := m.Author.ID == s.State.User.ID - fmt.Println("message received: ", m.Message.Content) - addMessageToHistory(m.Message, isHal) - fmt.Println("added message to history", len(messagesHistory)) - if isHal { return } - fmt.Println("check contains user") - if containUser(m.Mentions, s.State.User.ID) { - fmt.Println("lets go") aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken) - fmt.Println("client created") - res, err := aiclient.Chat(messagesHistory) if err != nil { sendResponse( @@ -45,14 +36,10 @@ func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) { log.Panicf("failed to query open ai with the following prompt [%s]. Error: %s", m.Content, err.Error()) } - fmt.Println("Choices: ", res.Choices) - if len(res.Choices) > 0 { aiResponse := res.Choices[0].Message.Content sendResponse(s, m.ChannelID, aiResponse) } - } else { - fmt.Println("hal not mentioned") } } diff --git a/openai/client.go b/openai/client.go index 80286af..91f2af3 100644 --- a/openai/client.go +++ b/openai/client.go @@ -3,7 +3,6 @@ package openai import ( "encoding/json" "fmt" - "regexp" resty "github.com/go-resty/resty/v2" ) @@ -41,11 +40,9 @@ func (c Client) Chat(messages []*ChatMessage) (*ChatResponse, error) { return nil, fmt.Errorf("failed to query openai. Error: %s", err.Error()) } if res.IsError() { - return nil, fmt.Errorf("openai return [%d]: %s", res.StatusCode(), res.Error()) + return nil, fmt.Errorf("openai returned [%d]: %s", res.StatusCode(), res.Error()) } - fmt.Println("body response: ", string(res.Body()), res.Status()) - var r ChatResponse if err := json.Unmarshal(res.Body(), &r); err != nil { return nil, fmt.Errorf("failed to parse the openai response. Error: %s", err.Error()) @@ -54,41 +51,6 @@ func (c Client) Chat(messages []*ChatMessage) (*ChatResponse, error) { return &r, nil } -func (c Client) Completions(prompt string) (*CompletionResponse, error) { - body := &CompletionPayload{ - Model: "text-davinci-003", - Prompt: cleanPrompt(prompt), - MaxTokens: 100, - Temperature: 1, - N: 1, - } - - b, err := json.Marshal(body) - if err != nil { - return nil, fmt.Errorf("failed to marshal the payload. %s", err.Error()) - } - - res, err := c.HttpClient.NewRequest(). - SetHeader("Content-Type", "application/json"). - SetBody(b). - Post("https://api.openai.com/v1/completions") - if err != nil { - return nil, fmt.Errorf("failed to query openai. Error: %s", err.Error()) - } - - var r CompletionResponse - if err := json.Unmarshal(res.Body(), &r); err != nil { - return nil, fmt.Errorf("failed to parse the openai response. Error: %s", err.Error()) - } - - return &r, nil -} - -func cleanPrompt(p string) string { - regex := regexp.MustCompile(`<@\d+>`) - return regex.ReplaceAllString(p, "") -} - type ChatResponse struct { ID string `json:"id"` Choices []ChatResponseChoice `json:"choices"` @@ -112,41 +74,3 @@ type ChatMessage struct { Role string `json:"role"` Content string `json:"content"` } - -// ----- - -type CompletionPayload struct { - Model string `json:"model"` - Prompt string `json:"prompt"` - MaxTokens int `json:"max_tokens"` - Temperature float32 `json:"temperature"` - TopP float32 `json:"top_p"` - N int `json:"n"` -} - -type CompletionPayloadMessage struct { - Role string `json:"role"` - Content string `json:"content"` - Name string `json:"name,omitempty"` -} - -type CompletionResponse struct { - ID string `json:"id"` - Object string `json:"object"` - Created int64 `json:"created"` - Model string `json:"model"` - Usage CompletionUsage `json:"usage"` - Choices []CompletionChoice `json:"choices"` -} - -type CompletionUsage struct { - PromptTokens int `json:"prompt_tokens"` - CompletionTokens int `json:"completion_tokens"` - TotalTokens int `json:"total_tokens"` -} - -type CompletionChoice struct { - Text string `json:"text"` - FinishReason string `json:"finish_reason"` - Index int `json:"index"` -}