-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (95 loc) · 2.81 KB
/
main.go
File metadata and controls
111 lines (95 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"log"
"context"
"net/http"
"encoding/json"
"os"
openai "github.com/sashabaranov/go-openai"
"github.com/rs/cors"
"io/ioutil"
)
type ChatRequest struct {
Action string `json:"action"`
Parameters struct {
Message string `json:"message"`
} `json:"parameters"`
}
type Character struct {
Name string `json:"name"`
Description string `json:"description"`
Purpose string `json:"purpose"`
Backstory string `json:"backstory"`
Motivations []string `json:"motivations"`
ShortTermGoals []string `json:"short_term_goals"`
LongTermGoals []string `json:"long_term_goals"`
PersonalityTraits []string `json:"personality_traits"`
}
func main() {
http.HandleFunc("/chat", chatHandler)
http.HandleFunc("/generateProfilePicture", generateProfilePictureHandler)
corsMiddleware := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000"},
AllowedMethods: []string{"POST", "OPTIONS"},
AllowedHeaders: []string{"Content-Type"},
AllowCredentials: true,
})
handler := corsMiddleware.Handler(http.DefaultServeMux)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Server is listening on port %s...", port)
log.Fatal(http.ListenAndServe(":"+port, handler))
}
func chatHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
bodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
bodyString := string(bodyBytes)
// Call ChatGPT API
chatResponse := chatGPTRequest(bodyString)
response := map[string]string{
"response": chatResponse, // Replace with the actual AI-generated response
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func chatGPTRequest(prompt string) string {
KEY := os.Getenv("OPENAI_API_KEY")
ctx := context.Background()
client := openai.NewClient(KEY)
resp, err := client.CreateChatCompletion(
ctx,
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
})
if err != nil {
log.Fatalln(err)
}
return resp.Choices[0].Message.Content
}
func generateProfilePictureHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Replace the URL with the actual URL of the image generated by DALLE
imageURL := "https://labs.openai.com/api/labs/public/generations/generation-OgEdnmwNhSsc5HOINEZgy4bq/download"
response := map[string]string{
"image_url": imageURL,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}