Remove OpenAI

This commit is contained in:
Atridad Lahiji 2024-01-10 00:35:23 -07:00
parent d5fbcb1b2b
commit c240c0b101
No known key found for this signature in database
5 changed files with 0 additions and 81 deletions

View file

@ -1,6 +1,5 @@
# Tokens
DISCORD_TOKEN=""
OPENAI_API_KEY=""
REPLICATE_API_TOKEN=""
# Comma separated
COOLDOWN_ALLOW_LIST=""

View file

@ -11,7 +11,6 @@ A discord bot written in Go.
- Copy .env.example and rename to .env
- Create a Discord Bot with all gateway permissions enabled
- Generate a token for this discord bot and paste it in the .env for DISCORD_TOKEN
- Generate and provide an OpenAPI token and paste it in the .env for OPENAI_API_KEY
- Generate and provide an Replicate token and paste it in the .env for REPLICATE_API_TOKEN
- Run `go run main.go` to run locally

1
go.mod
View file

@ -14,6 +14,5 @@ require (
github.com/gorilla/websocket v1.5.1 // indirect
github.com/joho/godotenv v1.5.1
github.com/replicate/replicate-go v0.14.2
github.com/sashabaranov/go-openai v1.17.11
golang.org/x/time v0.5.0 // indirect
)

2
go.sum
View file

@ -14,8 +14,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/replicate/replicate-go v0.14.2 h1:XgK+REvYrWs7qDeyugxHA93h31qBhEFk/3p1/p2w3W8=
github.com/replicate/replicate-go v0.14.2/go.mod h1:otIrl1vDmyjNhTzmVmp/mQU3Wt1+3387gFNEsAZq0ig=
github.com/sashabaranov/go-openai v1.17.11 h1:XVr00J8JymJVx8Hjbh/5mG0V4PQHRarBU3v7k2x6MR0=
github.com/sashabaranov/go-openai v1.17.11/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

View file

@ -1,76 +0,0 @@
package lib
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"github.com/joho/godotenv"
"github.com/sashabaranov/go-openai"
)
var client *openai.Client
func init() {
godotenv.Load(".env")
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
fmt.Println("OPENAI_API_KEY environment variable not set")
os.Exit(1)
}
client = openai.NewClient(apiKey)
}
func OpenAITextGeneration(prompt string) (string, error) {
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT4TurboPreview,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return "", err
}
return resp.Choices[0].Message.Content, nil
}
func OpenAIImageGeneration(prompt string) (imageFile *bytes.Buffer, err error) {
resp, err := client.CreateImage(context.Background(), openai.ImageRequest{
Prompt: prompt,
Model: "dall-e-3",
Size: "1024x1024",
})
if err != nil {
return nil, errors.New("there was an error generating the image based on this prompt... this usually happens when the generated image violates safety requirements")
}
imageRes, err := http.Get(resp.Data[0].URL)
if err != nil {
return nil, err
}
defer imageRes.Body.Close()
imageBytes, err := io.ReadAll(imageRes.Body)
if err != nil {
return nil, err
}
return bytes.NewBuffer(imageBytes), nil
}