fix endpoint
This commit is contained in:
@@ -15,18 +15,13 @@ func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if containUser(m.Mentions, s.State.User.ID) {
|
if containUser(m.Mentions, s.State.User.ID) {
|
||||||
fmt.Println("create ai client")
|
|
||||||
aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken)
|
aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken)
|
||||||
|
|
||||||
fmt.Println("ai client created")
|
|
||||||
|
|
||||||
res, err := aiclient.ChatCompletions(m.Content)
|
res, err := aiclient.ChatCompletions(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.Println("ai client replied")
|
|
||||||
|
|
||||||
if len(res.Choices) > 0 {
|
if len(res.Choices) > 0 {
|
||||||
aiResponse := res.Choices[0].Message.Content
|
aiResponse := res.Choices[0].Message.Content
|
||||||
if _, err = s.ChannelMessageSend(m.ChannelID, aiResponse); err != nil {
|
if _, err = s.ChannelMessageSend(m.ChannelID, aiResponse); err != nil {
|
||||||
@@ -34,8 +29,6 @@ func OnMessageCreated(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("### ai did not return any response ###", res.Usage, res.Choices)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-24
@@ -19,14 +19,11 @@ func NewClient(token string) *Client {
|
|||||||
return &Client{HttpClient: httpclient}
|
return &Client{HttpClient: httpclient}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) ChatCompletions(prompt string) (*CompletionResponse, error) {
|
func (c Client) Completions(prompt string) (*CompletionResponse, error) {
|
||||||
body := &ChatCompletionPayload{
|
body := &CompletionPayload{
|
||||||
Model: "gpt-3.5-turbo",
|
Model: "gpt-3.5-turbo",
|
||||||
Messages: []CompletionPayloadMessage{
|
Prompt: cleanPrompt(prompt),
|
||||||
{Role: "system", Content: "You are a fun comedian who only respond in french"},
|
MaxTokens: 100,
|
||||||
{Role: "user", Content: cleanPrompt(prompt)},
|
|
||||||
},
|
|
||||||
MaxTokens: 50,
|
|
||||||
Temperature: 1,
|
Temperature: 1,
|
||||||
N: 1,
|
N: 1,
|
||||||
}
|
}
|
||||||
@@ -39,19 +36,11 @@ func (c Client) ChatCompletions(prompt string) (*CompletionResponse, error) {
|
|||||||
res, err := c.HttpClient.NewRequest().
|
res, err := c.HttpClient.NewRequest().
|
||||||
SetHeader("Content-Type", "application/json").
|
SetHeader("Content-Type", "application/json").
|
||||||
SetBody(b).
|
SetBody(b).
|
||||||
Post("https://api.openai.com/v1/chat/completions")
|
Post("https://api.openai.com/v1/completions")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to query openai. Error: %s", err.Error())
|
return nil, fmt.Errorf("failed to query openai. Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
var test interface{}
|
|
||||||
if err := json.Unmarshal(res.Body(), &test); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse the openai test response. Error: %s", err.Error())
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Println("inteface: ", test)
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
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())
|
||||||
@@ -65,13 +54,13 @@ func cleanPrompt(p string) string {
|
|||||||
return regex.ReplaceAllString(p, "")
|
return regex.ReplaceAllString(p, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatCompletionPayload struct {
|
type CompletionPayload struct {
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Messages []CompletionPayloadMessage `json:"messages"`
|
Prompt string `json:"prompt"`
|
||||||
MaxTokens int `json:"max_tokens"`
|
MaxTokens int `json:"max_tokens"`
|
||||||
Temperature float32 `json:"temperature"`
|
Temperature float32 `json:"temperature"`
|
||||||
TopP float32 `json:"top_p"`
|
TopP float32 `json:"top_p"`
|
||||||
N int `json:"n"`
|
N int `json:"n"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CompletionPayloadMessage struct {
|
type CompletionPayloadMessage struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user