2023-03-03 16:37:52 +00:00
|
|
|
package gpt
|
2023-03-03 16:37:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
import "github.com/andrewstuart/openai"
|
|
|
|
|
2023-03-03 17:14:06 +00:00
|
|
|
var session openai.ChatSession
|
2023-03-03 16:37:34 +00:00
|
|
|
var client *openai.Client
|
|
|
|
|
|
|
|
func (p *GPTPlugin) getClient() (*openai.Client, error) {
|
|
|
|
token := p.c.Get("gpt.token", "")
|
|
|
|
if token == "" {
|
|
|
|
return nil, fmt.Errorf("no GPT token given")
|
|
|
|
}
|
2023-03-03 17:14:06 +00:00
|
|
|
return openai.NewClient(token)
|
2023-03-03 16:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GPTPlugin) chatGPT(request string) (string, error) {
|
2023-03-03 17:14:06 +00:00
|
|
|
if client == nil {
|
2023-03-03 16:37:34 +00:00
|
|
|
if err := p.setDefaultPrompt(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return session.Complete(context.Background(), request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GPTPlugin) setDefaultPrompt() error {
|
|
|
|
return p.setPrompt(p.c.Get("gpt.prompt", ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GPTPlugin) setPrompt(prompt string) error {
|
|
|
|
client, err := p.getClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-03 17:14:06 +00:00
|
|
|
session = client.NewChatSession(prompt)
|
2023-03-03 16:37:34 +00:00
|
|
|
return nil
|
|
|
|
}
|