-
Notifications
You must be signed in to change notification settings - Fork 0
/
appBuilder.go
129 lines (105 loc) · 3.29 KB
/
appBuilder.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/joho/godotenv"
)
type Content struct {
Type string `json:"type"`
Text string `json:"text"`
}
type Response struct {
Content []Content `json:"content"`
}
const (
apiUrl = "https://api.anthropic.com/v1/messages"
model = "claude-3-opus-20240229"
maxTokens = 4096
)
type AppBuilder struct {
AppName string
Description string
UseFixture bool
}
func (a *AppBuilder) GetFileContent() (string, error) {
var responseText string
if a.UseFixture {
body, err := os.ReadFile("fixtures/" + a.AppName + ".txt")
if err != nil {
fmt.Println(err)
return "", err
}
return cleanResponse(string(body)), nil
}
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
return "", err
}
apiKey := os.Getenv("API_KEY")
if apiKey == "" {
fmt.Println("API_KEY not set in .env file")
return "", err
}
prompt := fmt.Sprintf(`You are a devops wizard who has been asked to help devise Terraform code to deploy a new application to AWS. You should use Terraform best practices and the latest version of Terraform. You should make recommendations based on your
knowledge of Terraform and the best practices for deploying applications. For example, if the desired application is described as a Rails application you should know to use a combination of ECS, Fargate, and RDS to deploy the application. You are helping application
developers who are not well versed in devops so it is up to you to be the expert in the room. It is also crucial that you respond only with Terraform code and not with advice or explanations. Your answer will be written directly into files and the project will fail if you reply with anything other than valid
Terraform code.
The name of the application is: %s
The description of the application is as follows:\n\n%s`, a.AppName, a.Description)
data := map[string]interface{}{
"model": model,
"max_tokens": maxTokens,
"messages": []map[string]string{
{
"role": "user",
"content": prompt,
},
},
}
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Printf("Error marshalling data: %s\n\ndata was:\n%s", err, data)
return "", err
}
req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("Error creating request: %s\n", err)
return "", err
}
req.Header.Set("x-api-key", apiKey)
req.Header.Set("anthropic-version", "2023-06-01")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error sending request: %s\n", err)
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body: %s\n", err)
return "", err
}
var response Response
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Printf("Error unmarshalling response: %s\n", err)
fmt.Printf("Response was:\n%s\n", body)
return "", err
}
responseText = response.Content[0].Text
return cleanResponse(responseText), nil
}
func cleanResponse(response string) string {
response = strings.ReplaceAll(response, "```hcl\n", "")
response = strings.ReplaceAll(response, "```", "")
response = strings.TrimSpace(response)
return response
}