-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAPIGatewayTerraformAgent.py
More file actions
77 lines (54 loc) · 3.48 KB
/
APIGatewayTerraformAgent.py
File metadata and controls
77 lines (54 loc) · 3.48 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
from CodeBaseModels import CodeFile, CodeReview
from StructuredAgent import StructuredAgent
class APIGatewayTerraformAgent:
def __init__(self, model):
writer_system_message_template = """
You are an expert at writing Terraform. You will be given an API definition that includes the endpoints, request and response parameters, and data storage requirements.
Review the API definition and generate a corresponding Terraform script that will create an API Gateway on AWS.
Ensure the endpoints, request and response parameters, and data storage requirements are correctly represented based on the API definition.
Follow these rules:
1. There will be existing lambda function for each endpoint, you do not need to write terraform for the lambda functions
2. Connect each endpoint to the corresponding lambda function, The lambda function's name will be the same as the endpoint's name
3. Each endpoint will use the same authorizer lambda called "endpoint_authorizer", which already exists
4. Do not add a provider block to the terraform script, it will be supplied by another existing script
5. Use the OpenAPI Specification to define the API Gateway
Given the following information, you will create or improve a Terraform script that will create an API Gateway on AWS:
API Endpoints:
{endpoints}
Current Script:
{code}
Code Review:
{review}
"""
reviewer_system_message_template = """
You are an expert at reviewing Terraform scripts that are used to create API Gateways on AWS.
You will review the Terraform script that has been written to create an API Gateway and provide feedback on how it can be improved or corrected.
You will provide a review of the script and a score from 1 to 10, with 10 being the best.
The Terraform should follow these rules:
1. There will be existing lambda function for each endpoint, you do not need to write terraform for the lambda functions
2. Connect each endpoint to the corresponding lambda function, The lambda function's name will be the same as the endpoint's name
3. Each endpoint will use the same authorizer lambda called "endpoint_authorizer", which already exists
4. Do not add a provider block to the terraform script, it will be supplied by another existing script
5. Use the OpenAPI Specification to define the API Gateway
Given the following information, you will review the Terraform script and give feedback on how it can be improved or corrected.
API Endpoints:
{endpoints}
Current Terraform Script:
{script}
"""
self._writer_agent = StructuredAgent(model, writer_system_message_template, CodeFile)
self._reviewer_agent = StructuredAgent(model, reviewer_system_message_template, CodeReview)
def write_terraform(self, endpoints, min_quality_score: int = 8, max_review_iterations: int = 3) -> CodeFile:
review = ""
code = ""
review_score = 0
review_count = 0
while (review_score < min_quality_score and review_count < max_review_iterations):
terraform_script: CodeFile = self._writer_agent.reply("Create or improve the Terraform Script", {"endpoints": endpoints, "code": code, "review": review})
code = terraform_script.RAW_CODE
if terraform_script:
terraform_review: CodeReview = self._reviewer_agent.reply("Review the Terraform Script", {"endpoints": endpoints, "script": code})
review = terraform_review.REVIEW
review_score = terraform_review.SCORE
review_count += 1
return terraform_script