You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.1 KiB
Go

package main
import (
"fmt"
"net/http"
"io"
"os"
"context"
gogpt "github.com/sashabaranov/go-gpt3"
)
var c *gogpt.Client
func index(w http.ResponseWriter, req *http.Request) {
//time.Sleep(4 * time.Second)
(w).Header().Set("Access-Control-Allow-Origin", "*")
req.Body = http.MaxBytesReader(w, req.Body, 1024 * 1024) // no sending huge files to crash the server
promptBytes, err := io.ReadAll(req.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
} else {
promptString := string(promptBytes)
fmt.Println("Println line prompt string: ", promptString)
ctx := context.Background()
req := gogpt.CompletionRequest{
Model: gogpt.GPT3Curie,
MaxTokens: 30,
Prompt: promptString,
Temperature: 0.9,
Stop: []string{"\""},
N: 1,
}
resp, err := c.CreateCompletion(ctx, req)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
response := resp.Choices[0].Text
fmt.Println("Println response: ", response)
fmt.Fprintf(w, "%s", response)
}
}
func main() {
c = gogpt.NewClient(os.Getenv("OPENAI_KEY"))
http.HandleFunc("/", index)
http.ListenAndServe(":8090", nil)
}