-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickCodingLLMGroq.py
157 lines (141 loc) · 9.55 KB
/
QuickCodingLLMGroq.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from groq import Groq
import subprocess
import re
import os
groq_api_key = os.environ.get('GROQ_API_KEY')
# Replace this with your actual API key
API_KEY = groq_api_key
client = Groq(api_key=API_KEY)
model = "llama3-70b-8192"
# Idea Generation Agent
"""
System: You are an agent that generates game ideas for hackathons. The ideas should be concise, engaging, and have clear potential for development into a Python game.
User: Generate an idea for a Python game project. Format the response with 'Idea: <Idea>' followed by 'Description: <Description>'. The description should provide a high-level overview of the game concept, its features, and potential implementation details.
"""
idea_generation = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are an agent that generates game ideas for hackathons. The ideas should be concise, engaging, and have clear potential for development into a Python game.",
},
{
"role": "user",
"content": "Generate an idea for a Python game project. Format the response with 'Idea: <Idea>' followed by 'Description: <Description>'. The description should provide a high-level overview of the game concept, its features, and potential implementation details.",
}
],
model=model,
)
idea = idea_generation.choices[0].message.content
print(idea)
# Planning Agent
"""
System: You are a planning agent responsible for breaking down a Python game project into smaller tasks, components, and functions based on the provided idea. Your plan should outline the required features, game mechanics, and necessary functions for the project.
User: Project Idea: <idea>
Based on the provided project idea, generate a detailed plan outlining the required features, game mechanics, and necessary functions. Format the response with 'Plan: <Plan>' followed by a bullet-pointed list of tasks or components. The plan should be structured in a way that allows multiple agents to work on different components of the project simultaneously and contribute to a single cohesive Python script.
"""
planning_agent = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a planning agent responsible for breaking down a Python game project into smaller tasks, components, and functions based on the provided idea. Your plan should outline the required features, game mechanics, and necessary functions for the project.",
},
{
"role": "user",
"content": f"Project Idea: {idea}\n\nBased on the provided project idea, generate a detailed plan outlining the required features, game mechanics, and necessary functions. Format the response with 'Plan: <Plan>' followed by a bullet-pointed list of tasks or components. The plan should be structured in a way that allows multiple agents to work on different components of the project simultaneously and contribute to a single cohesive Python script.",
}
],
model=model,
)
project_plan = planning_agent.choices[0].message.content
print(project_plan)
# Function Generation Agents
"""
System: You are an agent responsible for generating a Python function based on the provided plan and project idea. Your function should be focused on a specific task or component outlined in the plan and should integrate seamlessly with the overall project.
User: Project Idea: <idea>
Plan: <project_plan>
Based on the provided plan, generate a Python function that implements one of the tasks or components outlined in the plan. Format the response with '# <Function Name>' followed by the function code. The function should be self-contained, well-documented, and adhere to best practices for Python coding. Ensure that the function can be integrated into a single Python script along with the functions generated by other agents.
"""
agents = ["Alice", "Bob", "Charlie", "David", "Eve"]
functions = {}
for agent in agents:
function_generation = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are an agent responsible for generating a Python function based on the provided plan and project idea. Your function should be focused on a specific task or component outlined in the plan and should integrate seamlessly with the overall project.",
},
{
"role": "user",
"content": f"Project Idea: {idea}\n\nPlan: {project_plan}\n\nBased on the provided plan, generate a Python function that implements one of the tasks or components outlined in the plan. Format the response with '# <Function Name>' followed by the function code. The function should be self-contained, well-documented, and adhere to best practices for Python coding. Ensure that the function can be integrated into a single Python script along with the functions generated by other agents.",
}
],
model=model,
)
function_code = function_generation.choices[0].message.content
functions[agent] = function_code
print(f"{agent} Function:\n{function_code}")
# Integration Agent
"""
System: You are an integration agent responsible for combining individual Python functions into a cohesive script. Your task is to integrate the provided functions into a single Python file while ensuring code quality, modularity, and maintainability.
User: Project Idea: <idea>
Plan: <project_plan>
Functions: <functions>
Integrate the provided Python functions into a single cohesive script. Ensure that the script follows best practices for code structure, organization, and readability. Include necessary imports, a main function, and any additional code required to make the script executable and adhering to the project plan. Remove any unnecessary comments, formatting, or placeholders. The resulting script should be a clean and runnable Python file.
"""
integration_agent = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are an integration agent responsible for combining individual Python functions into a cohesive script. Your task is to integrate the provided functions into a single Python file while ensuring code quality, modularity, and maintainability.",
},
{
"role": "user",
"content": f"Project Idea: {idea}\n\nPlan: {project_plan}\n\nFunctions:\n\n" + "\n\n".join([f"# {agent} Function\n{func}" for agent, func in functions.items()]) + "\n\nIntegrate the provided Python functions into a single cohesive script. Ensure that the script follows best practices for code structure, organization, and readability. Include necessary imports, a main function, and any additional code required to make the script executable and adhering to the project plan. Remove any unnecessary comments, formatting, or placeholders. The resulting script should be a clean and runnable Python file.",
}
],
model=model,
)
integrated_script = integration_agent.choices[0].message.content
print(integrated_script)
# Clean up the integrated script
integrated_script = re.sub(r'```.*?```', '', integrated_script, flags=re.DOTALL) # Remove code block formatting
integrated_script = re.sub(r'#\s*[A-Za-z]+\s*Function\n', '', integrated_script) # Remove function name comments
integrated_script = re.sub(r'^\s*\n', '', integrated_script, flags=re.MULTILINE) # Remove leading blank lines
# Testing and Validation Agent
"""
System: You are a testing and validation agent. Your task is to validate the integrated Python script's functionality, performance, and adherence to the original plan and requirements.
User: Project Idea: <idea>
Plan: <project_plan>
Integrated Script: <integrated_script>
Test and validate the provided integrated Python script. Your feedback should cover aspects such as functional correctness, performance, edge cases, and adherence to the project plan and requirements. Provide clear and actionable recommendations for addressing any identified issues or areas for improvement.
"""
testing_agent = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a testing and validation agent. Your task is to validate the integrated Python script's functionality, performance, and adherence to the original plan and requirements.",
},
{
"role": "user",
"content": f"Project Idea: {idea}\n\nPlan: {project_plan}\n\nIntegrated Script:\n\n{integrated_script}\n\nTest and validate the provided integrated Python script. Your feedback should cover aspects such as functional correctness, performance, edge cases, and adherence to the project plan and requirements. Provide clear and actionable recommendations for addressing any identified issues or areas for improvement.",
}
],
model=model,
)
testing_feedback = testing_agent.choices[0].message.content
print(testing_feedback)
def run_python_code(code):
result = subprocess.run(["python", "-c", code], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return result.stdout.decode("utf-8"), result.stderr.decode("utf-8")
output, errors = run_python_code(integrated_script)
print(f"Output:\n{output}\n")
print(f"Errors:\n{errors}\n")
# Print the final project components
print(f"Project Idea:\n{idea}\n")
print(f"Project Plan:\n{project_plan}\n")
print(f"Integrated Script:\n{integrated_script}\n")
print(f"Testing and Validation Feedback:\n{testing_feedback}\n")
# Save the integrated script to a file
with open("Final_Script.py", "w", encoding="utf-8") as file:
file.write(integrated_script)
print("Integrated script saved to Final_Script.py")