| 9 | |
| 10 | |
| 11 | class Captain: |
| 12 | |
| 13 | def __init__(self, mission: str): |
| 14 | self.mission = mission |
| 15 | |
| 16 | def run(self, team): |
| 17 | prompts = self.generate_prompts() |
| 18 | team = self.create_team(prompts, team) |
| 19 | return team |
| 20 | |
| 21 | def initialize_team(self, prompts, team): |
| 22 | db.add(team) |
| 23 | db.commit() |
| 24 | scout = Scout(instruction=prompts["scout"], teamId=team.id) |
| 25 | sentinel = Sentinel(instruction=prompts["sentinel"], teamId=team.id) |
| 26 | soldier = Soldier(instruction=prompts["soldier"], teamId=team.id) |
| 27 | db.add(scout) |
| 28 | db.add(sentinel) |
| 29 | db.add(soldier) |
| 30 | db.commit() |
| 31 | return team |
| 32 | |
| 33 | def generate_prompts(self): |
| 34 | system = """You are the captain of a team of scouts, sentinels, and soldiers. |
| 35 | You generate instructions for your team to follow based on a mission. |
| 36 | Scouts are responsible for gathering information from the internet. |
| 37 | Sentinels are responsible for monitoring the observations of scouts for changes. |
| 38 | Soldiers are responsible for writing reports. |
| 39 | Instruction examples: |
| 40 | Mission: When apple relseases a new product. |
| 41 | Scout: What is the new apple product? return the answer. |
| 42 | Sentinel: Was a new product released? Reply with (Yes/No) and the name of the product. |
| 43 | Soldier: Write a report about it. |
| 44 | """ |
| 45 | |
| 46 | prompt = f""" |
| 47 | Complete the instructions for the scouts, sentinels, and soldiers. One per line. |
| 48 | Mission:{self.mission} |
| 49 | """ |
| 50 | model = ChatOpenAI(model="gpt-4", temperature=0) |
| 51 | messages = [ |
| 52 | SystemMessage( |
| 53 | content=system |
| 54 | ), |
| 55 | HumanMessage(content=prompt), |
| 56 | ] |
| 57 | response = model(messages) |
| 58 | response = self.parse_response(response.content) |
| 59 | return response |
| 60 | |
| 61 | def parse_response(self, response): |
| 62 | lines = re.split(r'\n+', response.strip()) |
| 63 | # Extract the relevant information from the lines |
| 64 | prompts = {} |
| 65 | prompts["scout"] = lines[0].split(": ")[1] |
| 66 | prompts["sentinel"] = lines[1].split(": ")[1] |
| 67 | prompts["soldier"] = lines[2].split(": ")[1] |
| 68 | return prompts |