remove old code
This commit is contained in:
@@ -16,24 +16,15 @@ var messagesHistory = []*openai.ChatMessage{}
|
|||||||
func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
|
func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
isHal := m.Author.ID == s.State.User.ID
|
isHal := m.Author.ID == s.State.User.ID
|
||||||
|
|
||||||
fmt.Println("message received: ", m.Message.Content)
|
|
||||||
|
|
||||||
addMessageToHistory(m.Message, isHal)
|
addMessageToHistory(m.Message, isHal)
|
||||||
|
|
||||||
fmt.Println("added message to history", len(messagesHistory))
|
|
||||||
|
|
||||||
if isHal {
|
if isHal {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("check contains user")
|
|
||||||
|
|
||||||
if containUser(m.Mentions, s.State.User.ID) {
|
if containUser(m.Mentions, s.State.User.ID) {
|
||||||
fmt.Println("lets go")
|
|
||||||
aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken)
|
aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken)
|
||||||
|
|
||||||
fmt.Println("client created")
|
|
||||||
|
|
||||||
res, err := aiclient.Chat(messagesHistory)
|
res, err := aiclient.Chat(messagesHistory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendResponse(
|
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())
|
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 {
|
if len(res.Choices) > 0 {
|
||||||
aiResponse := res.Choices[0].Message.Content
|
aiResponse := res.Choices[0].Message.Content
|
||||||
sendResponse(s, m.ChannelID, aiResponse)
|
sendResponse(s, m.ChannelID, aiResponse)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
fmt.Println("hal not mentioned")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-77
@@ -3,7 +3,6 @@ package openai
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
|
|
||||||
resty "github.com/go-resty/resty/v2"
|
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())
|
return nil, fmt.Errorf("failed to query openai. Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
if res.IsError() {
|
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
|
var r ChatResponse
|
||||||
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())
|
||||||
@@ -54,41 +51,6 @@ func (c Client) Chat(messages []*ChatMessage) (*ChatResponse, error) {
|
|||||||
return &r, nil
|
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 {
|
type ChatResponse struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Choices []ChatResponseChoice `json:"choices"`
|
Choices []ChatResponseChoice `json:"choices"`
|
||||||
@@ -112,41 +74,3 @@ type ChatMessage struct {
|
|||||||
Role string `json:"role"`
|
Role string `json:"role"`
|
||||||
Content string `json:"content"`
|
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"`
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user