File size: 10,978 Bytes
b3509ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ac1d19
b3509ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ac1d19
b3509ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a69441c
 
ee8e0ef
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import os
import sys
import yaml
import json
from pathlib import Path

sys.path.append(str(Path('__file__').parent.parent))
from swarmai.__main__ import run_swarm

"""
Define some global parameters.
This is a simple frontent for the swarm.

The swarm has a config, the default output and entry-point.

Default swarm config (for copilot =)):
swarm:
  agents: # supported: manager, analyst, googler, crunchbase_searcher
    - type: manager
      n: 2
    - type: analyst
      n: 2
    - type: googler
      n: 2
    - type: crunchbase_searcher # scraper can only have one job in parallel
      n: 1
  timeout_min: 10
  run_dir: ./tmp/swarm
task:
  role: |
    professional venture capital agency, who has a proven track reckord of consistently funding successful startups
  global_goal: |
    A new startup just send us their pitch. Find if the startup is worth investing in. The startup is called Brainamics and it is in the space of brain computer interfaces.
    More information about them: 'https://brainamics.de', 'https://www.linkedin.com/company/thebrainamics/'
  goals:
    - Generate a comprehensive description of the startup. Describe their value proposition, the product, USP and business model of a startup.
    - Find any mentions of the startup in the news, social media, etc. Add links.
    - Find top 10 companies and startups in this field. Find out their locations, raised funding, value proposition, differentiation, etc.
    - Find top 5 investors in this field. Includ specific details in the format of 'company AAA (link) invested in company BBB (link) $XX in year YYYY'
    - Describe the market size, growth rate and trends of this field.
    - Main problems and challenges of the field. Create an extensive list of problems. What can stop the field from growing? What can stop the company from succeeding?
    - Briefly describe the technology for the non-tech audience. Include links to the main articles in the field.
    - What questions should we ask the startup to make a more informed decision? Avoid generic and obvious questions and focus on field/domain specific questions that can uncover problems with this specific startup.

"""
SWARM_CONFIG_PATH = "swarm_config.yaml"
ALLOWED_AGENTS = ["manager", "analyst", "googler", "crunchbase_searcher"]

SWARM_DEFAULT_RUN_FOLDER = (Path("__file__").parent / "tmp" / "swarm").resolve()
SWARM_DEFAULT_JSON_OUTPUT = str(SWARM_DEFAULT_RUN_FOLDER / "output.json")
SWARM_DEAFAULT_LOGS = str(SWARM_DEFAULT_RUN_FOLDER / "swarm.json")
SWARM_DEFAULT_SHARED_MEMORY = str(SWARM_DEFAULT_RUN_FOLDER / "shared_memory")

def get_swarm_config():
    """
    Load the swarm config from the default location.
    """
    with open(SWARM_CONFIG_PATH) as f:
        swarm_config = yaml.load(f, Loader=yaml.FullLoader)
    return swarm_config

def set_swarm_role(role_description):
    """
    Set the role for the swarm. It's specified in the swarm_config.yaml file under: swarm.task.role
    """
    if role_description=="":
        role_description = "professional venture capital agency, who has a proven track reckord of consistently funding successful startups"
    swarm_config = get_swarm_config()
    print(f"Setting role to: {role_description}")
    swarm_config["task"]["role"] = role_description
    with open(SWARM_CONFIG_PATH, "w") as f:
        yaml.dump(swarm_config, f)
def get_swarm_role():
    """
    Get the role for the swarm. It's specified in the swarm_config.yaml file under: swarm.task.role
    """
    swarm_config = get_swarm_config()
    return swarm_config["task"]["role"]

def set_swarm_global_goal(global_goal):
    """
    Set the global goal for the swarm. It's specified in the swarm_config.yaml file under: swarm.task.global_goal
    """
    if global_goal=="":
        global_goal = "A new startup just send us their pitch. Find if the startup is worth investing in. The startup is called Brainamics and it is in the space of brain computer interfaces."
    swarm_config = get_swarm_config()
    print(f"Setting global goal to: {global_goal}")
    swarm_config["task"]["global_goal"] = global_goal
    with open(SWARM_CONFIG_PATH, "w") as f:
        yaml.dump(swarm_config, f)

def get_swarm_global_goal():
    """
    Get the global goal for the swarm. It's specified in the swarm_config.yaml file under: swarm.task.global_goal
    """
    swarm_config = get_swarm_config()
    return swarm_config["task"]["global_goal"]

def set_swarm_goals(goals: list):
    """
    Set the goals for the swarm. It's specified in the swarm_config.yaml file under: swarm.task.goals

    Default goals:
    - Generate a comprehensive description of the startup. Describe their value proposition, the product, USP and business model of a startup.
    - Find any mentions of the startup in the news, social media, etc. Add links.
    - Find top 10 companies and startups in this field. Find out their locations, raised funding, value proposition, differentiation, etc.
    - Find top 5 investors in this field. Includ specific details in the format of 'company AAA (link) invested in company BBB (link) $XX in year YYYY'
    - Describe the market size, growth rate and trends of this field.
    - Main problems and challenges of the field. Create an extensive list of problems. What can stop the field from growing? What can stop the company from succeeding?
    - Briefly describe the technology for the non-tech audience. Include links to the main articles in the field.
    - What questions should we ask the startup to make a more informed decision? Avoid generic and obvious questions and focus on field/domain specific questions that can uncover problems with this specific startup.
    """
    try:
        if len(goals) == 0:
            raise ValueError("Goals can't be empty.")
        
        all_empty = True
        for idx, goal in enumerate(goals):
            if goal != "":
                all_empty = False
                break
            else:
                # remove empty goals
                goals.pop(idx)
        if not all_empty:
            raise ValueError("Goals can't be empty.")
    except ValueError:
        goals = [
            "Generate a comprehensive description of the startup. Describe their value proposition, the product, USP and business model of a startup.",
            "Find any mentions of the startup in the news, social media, etc. Add links.",
            "Find top 10 companies and startups in this field. Find out their locations, raised funding, value proposition, differentiation, etc.",
            "Find top 5 investors in this field. Includ specific details in the format of 'company AAA (link) invested in company BBB (link) $XX in year YYYY'",
            "Describe the market size, growth rate and trends of this field.",
            "Main problems and challenges of the field. Create an extensive list of problems. What can stop the field from growing? What can stop the company from succeeding?",
            "Briefly describe the technology for the non-tech audience. Include links to the main articles in the field.",
            "What questions should we ask the startup to make a more informed decision? Avoid generic and obvious questions and focus on field/domain specific questions that can uncover problems with this specific startup."
        ]
    swarm_config = get_swarm_config()
    print(f"Setting goals to: {goals}")
    swarm_config["task"]["goals"] = goals
    with open(SWARM_CONFIG_PATH, "w") as f:
        yaml.dump(swarm_config, f)

def get_swarm_goals():
    """
    Get the goals for the swarm. It's specified in the swarm_config.yaml file under: swarm.task.goals
    """
    swarm_config = get_swarm_config()
    return swarm_config["task"]["goals"]

def set_swarm_agents_config(agents_config: list):
    """
    Set the agents config for the swarm. It's specified in the swarm_config.yaml file under: swarm.agents
    """
    try:
        if len(agents_config) == 0:
            raise ValueError("No agents config specified.")
        for agent_config in agents_config:
            if "type" not in agent_config:
                raise ValueError(f"Agent config {agent_config} does not have a type specified.")
            if agent_config["type"] not in ALLOWED_AGENTS:
                raise ValueError(f"Agent type {agent_config['type']} is not supported. Supported agents: {ALLOWED_AGENTS}")
            if "n" not in agent_config:
                raise ValueError(f"Agent config {agent_config} does not have a number of agents specified.")
            if agent_config["n"] == '':
                raise ValueError(f"Agent config {agent_config} does not have a number of agents specified.")
            if agent_config["n"] < 0:
                raise ValueError(f"Agent config {agent_config} has a negative number of agents specified.")
            if agent_config["n"] > 100:
                raise ValueError(f"Agent config {agent_config} has a number of agents specified that is too large. Max number of agents is 10.")
    except ValueError as e:
        agents_config = [
            {"type": "manager", "n": 2},
            {"type": "analyst", "n": 2},
            {"type": "googler", "n": 2},
        ]
    swarm_config = get_swarm_config()
    print(f"Setting agents config to: {agents_config}")
    swarm_config["swarm"]["agents"] = agents_config
    with open(SWARM_CONFIG_PATH, "w") as f:
        yaml.dump(swarm_config, f)
def get_swarm_agents_config():
    """
    Get the agents config for the swarm. It's specified in the swarm_config.yaml file under: swarm.agents
    """
    swarm_config = get_swarm_config()
    return swarm_config["swarm"]["agents"]

def read_swarm_output():
    """
    Read the output of the swarm. The file can sometimes be locked by the swarm, so we need to handle this.
    """
    try:
        with open(SWARM_DEFAULT_JSON_OUTPUT) as f:
            final_out = ""
            output = json.load(f)
            for _, value in output.items():
                final_out+="========================================\n"
                final_out+="========================================\n"
                for key, value in value.items():
                    final_out+=f"**{key}**:\n{value}\n\n"
            f.close()
    except Exception:
        final_out = "Swarm is starting up (needs ~2-3 minutes for first results and ~30 sec for first logs)..."
    return final_out

def read_swarm_logs():
    """
    Read the logs of the swarm. The file can sometimes be locked by the swarm, so we need to handle this.
    """
    try:
        with open(SWARM_DEAFAULT_LOGS) as f:
            # read last 100 lines
            logs = f.readlines()[-100:]
            final_out = "\n".join(logs)
            f.close()
    except Exception:
        final_out = "Swarm is starting up..."
    return final_out

def execute_swarm(smarm_ui_status):
    _ = run_swarm() # when it finishes, it will set SWARM_IS_RUNNING to False
    raise TimeoutError("Swarm timed out. Please try again.")