File size: 5,278 Bytes
a9df80e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from crewai import Agent, Task, Crew, Process
# from crewai_tools import CodeInterpreterTool

# Model options
llm_models = [
    "gemini/gemini-1.5-flash",
    "gemini/gemini-1.5-pro",
    "gemini/gemini-pro"
]

selected_model = llm_models[0]


def set_model(selected_model_name):
    global selected_model
    selected_model = selected_model_name


def configure_api_keys(gemini_api_key):
    if not gemini_api_key:
        raise ValueError("Gemini API key is required")
    os.environ['GEMINI_API_KEY'] = gemini_api_key


def run_crew_game(gemini_api_key, game):
    try:
        # Configure API keys (no search tool used)
        configure_api_keys(gemini_api_key)

        # Agents
        senior_game_developer_agent = Agent(
            role='Senior Game Developer',
            goal='Design and develop engaging games',
            verbose=True,
            backstory=(
                "You are a Senior Game Developer at a leading game development studio. "
                "Your expertise lies in game design, programming in Python, and creating immersive gaming experiences using pygame. "
                "You strive to produce high-quality, innovative games that captivate players."
            ),
            allow_delegation=True,
            llm=selected_model,
        )

        qa_engineer_agent = Agent(
            role='Software Quality Control Engineer',
            goal='Create Perfect code, by analyzing the code that is given for errors',
            backstory=(
                "You are a software engineer that specializes in checking code for errors. "
                "You have an eye for detail and a knack for finding hidden bugs. "
                "You check for missing imports, variable declarations, mismatched brackets, syntax errors, "
                "security vulnerabilities, and logic errors."
            ),
            allow_delegation=False,
            verbose=True,
            # allow_code_execution=True,
            llm=selected_model,
            # tools=[CodeInterpreterTool()],
        )

        chief_qa_engineer_agent = Agent(
            role='Chief Software Quality Control Engineer',
            goal='Ensure that the code does the job that it is supposed to do',
            backstory=(
                "You feel that programmers always do only half the job, so you are super dedicated to making high quality code."
            ),
            allow_delegation=True,
            # allow_code_execution=True,
            verbose=True,
            llm=selected_model,
            # tools=[CodeInterpreterTool()],
        )

        # Tasks
        code_task = Task(
            description=f'''You will create a game using Python, these are the instructions:



                                Instructions

                                ------------

                                {game}

                            ''',
            expected_output='Your Final answer must be the full Python code, only the Python code and nothing else.',
            agent=senior_game_developer_agent,
        )

        review_task = Task(
            description=f'''You will create a game using Python, these are the instructions:



                            Instructions

                            ------------

                            {game}



                            Using the code you got, check for errors. Check for logic errors,

                            syntax errors, missing imports, variable declarations, mismatched brackets,

                            and security vulnerabilities.

                        ''',
            expected_output='Your Final answer must be the full Python code, only the Python code and nothing else.',
            agent=qa_engineer_agent,
        )

        evaluate_task = Task(
            description=f'''You are helping create a game using Python, these are the instructions:



                            Instructions

                            ------------

                            {game}



                            You will look over the code to ensure that it is complete and

                            does the job that it is supposed to do.

                         ''',
            expected_output='Your Final answer must be the full Python code, only the Python code and nothing else.',
            agent=chief_qa_engineer_agent,
            output_file='game.py'
        )

        crew = Crew(
            agents=[senior_game_developer_agent , qa_engineer_agent, chief_qa_engineer_agent],
            tasks=[code_task, review_task, evaluate_task],
            process=Process.sequential,
            verbose=True,
            max_rpm=100,
            share_crew=True,
            output_log_file=True
        )

        crew.kickoff(inputs={'game': game})
        with open("game.py", "r", encoding='utf-8') as f:
            content = f.read()
        with open("logs.txt", 'r', encoding='utf-8') as f:
            logs = f.read()
        with open("logs.txt", 'w', encoding='utf-8') as f:
            f.truncate(0)
        return content, logs

    except Exception as e:
        return f"Error: {str(e)}", str(e)