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) {
|
||||
fmt.Println("create ai client")
|
||||
aiclient := openai.NewClient(env.GetEnvVariables().OpenaiHalToken)
|
||||
|
||||
fmt.Println("ai client created")
|
||||
|
||||
res, err := aiclient.ChatCompletions(m.Content)
|
||||
if err != nil {
|
||||
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 {
|
||||
aiResponse := res.Choices[0].Message.Content
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
+13
-24
@@ -19,14 +19,11 @@ func NewClient(token string) *Client {
|
||||
return &Client{HttpClient: httpclient}
|
||||
}
|
||||
|
||||
func (c Client) ChatCompletions(prompt string) (*CompletionResponse, error) {
|
||||
body := &ChatCompletionPayload{
|
||||
Model: "gpt-3.5-turbo",
|
||||
Messages: []CompletionPayloadMessage{
|
||||
{Role: "system", Content: "You are a fun comedian who only respond in french"},
|
||||
{Role: "user", Content: cleanPrompt(prompt)},
|
||||
},
|
||||
MaxTokens: 50,
|
||||
func (c Client) Completions(prompt string) (*CompletionResponse, error) {
|
||||
body := &CompletionPayload{
|
||||
Model: "gpt-3.5-turbo",
|
||||
Prompt: cleanPrompt(prompt),
|
||||
MaxTokens: 100,
|
||||
Temperature: 1,
|
||||
N: 1,
|
||||
}
|
||||
@@ -39,19 +36,11 @@ func (c Client) ChatCompletions(prompt string) (*CompletionResponse, error) {
|
||||
res, err := c.HttpClient.NewRequest().
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetBody(b).
|
||||
Post("https://api.openai.com/v1/chat/completions")
|
||||
Post("https://api.openai.com/v1/completions")
|
||||
if err != nil {
|
||||
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
|
||||
if err := json.Unmarshal(res.Body(), &r); err != nil {
|
||||
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, "")
|
||||
}
|
||||
|
||||
type ChatCompletionPayload struct {
|
||||
Model string `json:"model"`
|
||||
Messages []CompletionPayloadMessage `json:"messages"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
Temperature float32 `json:"temperature"`
|
||||
TopP float32 `json:"top_p"`
|
||||
N int `json:"n"`
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user