-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
138 lines (115 loc) · 3.17 KB
/
main.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
)
const apiEndpoint = "https://api.openai.com/v1/chat/completions"
const temperature = 0.5
const aiModel = "gpt-3.5-turbo-0301"
type request struct {
Model string `json:"model"`
Messages []message `json:"messages"`
Temperature float32 `json:"temperature"`
}
type message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type response struct {
ID string `json:"id"`
Object string `json:"object"`
Created int `json:"created"`
Choices []struct {
Index int `json:"index"`
Message message `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
} `json:"usage"`
}
func main() {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
log.Fatal("Please set OPENAI_API_KEY environment variable")
}
logger := createLogger("interaction.log")
client := &http.Client{}
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("You: ")
if !scanner.Scan() {
break
}
input := scanner.Text()
escapedInput := strconv.Quote(input)
logger.Printf(input)
if strings.ToLower(input) == "exit" {
break
}
response, err := getAIResponse(apiKey, client, escapedInput)
if err != nil {
logger.Printf("Error getting AI response: %s\n", err.Error())
continue
}
if len(response.Choices) == 0 {
logger.Println("Error: empty response")
continue
}
message := response.Choices[0].Message
if message.Content != "" {
logAndPrintResponse(logger, message)
}
}
}
func createLogger(logFile string) *log.Logger {
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
return log.New(file, "", log.LstdFlags)
}
func getAIResponse(apiKey string, client *http.Client, escapedInput string) (*response, error) {
payload := request{
Model: aiModel,
Messages: []message{{Role: "user", Content: escapedInput}},
Temperature: temperature,
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("error marshalling payload: %s", err.Error())
}
req, err := http.NewRequest("POST", apiEndpoint, bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, fmt.Errorf("error creating request: %s", err.Error())
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var responseObj response
err = json.NewDecoder(resp.Body).Decode(&responseObj)
if err != nil {
return nil, fmt.Errorf("error decoding response: %s", err.Error())
}
return &responseObj, nil
}
func logAndPrintResponse(logger *log.Logger, message message) {
logger.Printf("AI: %s\n", message.Content)
log.Printf("AI: %s\n", message.Content)
}