-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (78 loc) · 2.37 KB
/
main.go
File metadata and controls
93 lines (78 loc) · 2.37 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
package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/sashabaranov/go-openai"
)
type ChatRequest struct {
Message string `json:"message"`
}
type ChatResponse struct {
Response string `json:"response"`
}
func main() {
resumeBytes, err := ioutil.ReadFile("resume.txt")
if err != nil {
panic("Could not read resume.txt: " + err.Error())
}
resumeText := string(resumeBytes)
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
panic("OPENAI_API_KEY enviroment variable not set")
}
client := openai.NewClient(apiKey)
router := gin.Default()
// router.Use(corsMiddleware()) // uncoment this for local tests
router.POST("/chat", func(ctx *gin.Context) {
var req ChatRequest
if err := ctx.ShouldBindBodyWithJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant that answers reqruters and tech people questions about nir alon:\n\n" + resumeText,
},
{
Role: openai.ChatMessageRoleUser,
Content: req.Message,
},
}
resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: messages,
MaxTokens: 150,
Temperature: 0.2,
})
if err != nil {
fmt.Println("OpenAI error:", err)
ctx.JSON(http.StatusInternalServerError, gin.H{"response": "Error contacting OpenAI"})
return
}
if len(resp.Choices) == 0 {
fmt.Println("OpenAI response had 0 choices! Full response:", resp)
ctx.JSON(http.StatusInternalServerError, gin.H{"response": "No response from OpenAI"})
return
}
answer := resp.Choices[0].Message.Content
ctx.JSON(http.StatusOK, ChatResponse{Response: answer})
})
router.Run(":8080")
}
// func corsMiddleware() gin.HandlerFunc {
// return func(c *gin.Context) {
// c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
// c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
// c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization")
// if c.Request.Method == "OPTIONS" {
// c.AbortWithStatus(204)
// return
// }
// c.Next()
// }
// }