Skip to content

Commit

Permalink
Merge pull request #80 from ryderwishart/use_metaconfig
Browse files Browse the repository at this point in the history
Use .env to specify port and base URL
  • Loading branch information
joshsny authored Jun 21, 2023
2 parents 6cc2d3f + 2b2e9bd commit 466693f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ OPENAI_API_KEY=
ANTHROPIC_API_KEY=
SERPAPI_API_KEY=
WOLFRAM_ALPHA_APPID=
DATABASE_PROVIDER= # sqlite | supabase
DATABASE_PROVIDER= # sqlite | supabase

# If using supabase
SUPABASE_URL=
SUPABASE_KEY=

# Optional server config
PORT=5000

# Optional Discord tokens
# Token for the bot that announces agent movement between rooms
# This token needs the message content intent to be enabled
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ GPTeam employs separate agents, each equipped with a memory, that interact with
## Viewing Agents

The world is a busy place! To get a view of what different agents are doing whilst the world is running, you can visit the `agents/` folder where there is a txt file for each agent containing a summary of their current state.

## Changing the world

To change the world, all you need to do is:
Expand All @@ -48,12 +49,15 @@ To change the world, all you need to do is:
3. Run the world again: `poetry run world`

## Setting up the Discord Integration

Read through the dedicated [Discord setup docs](DISCORD.md)

## Using with Anthropic Claude

Make sure you have an `ANTHROPIC_API_KEY` in your env, then you can use `poetry run world --claude` which will run the world using `claude-v1` for some calls and `claude-v1-instant` for others.

## Using with Window

Make sure you have the [Window extension](https://windowai.io/) installed, then you can use `poetry run world --window`. Some models may be slow to respond, since the prompts are very long.

## Contributing
Expand Down
10 changes: 8 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .utils.logging import init_logging
from .utils.parameters import DISCORD_ENABLED
from .web import get_server
from .utils.general import get_open_port

load_dotenv()

Expand Down Expand Up @@ -56,7 +57,8 @@ def run_world():

def run_server():
app = get_server()
run_in_new_loop(app.run_task())
port = get_open_port()
run_in_new_loop(app.run_task(port=port))


def run_in_new_loop(coro):
Expand All @@ -69,6 +71,8 @@ def run_in_new_loop(coro):


def run():
port = get_open_port()

process_discord = Process(target=discord_listener)
process_world = Process(target=run_world)
process_server = Process(target=run_server)
Expand All @@ -78,7 +82,9 @@ def run():
process_server.start()

sleep(3)
webbrowser.open("http://127.0.0.1:5000")

print(f"Opening browser on port {port}...")
webbrowser.open(f"http://127.0.0.1:{port}")

process_discord.join()
process_world.join()
Expand Down
24 changes: 24 additions & 0 deletions src/utils/general.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import hashlib
from typing import Callable, TypeVar
from uuid import UUID
import socket

T = TypeVar("T")
K = TypeVar("K")
Expand All @@ -17,3 +19,25 @@ def deduplicate_list(items: list[T], key: Callable[[T], K]) -> list[T]:

def seed_uuid(seed: str) -> str:
return str(UUID(hashlib.sha1(seed.encode()).hexdigest()[:32]))


def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0 # TODO: use base URL from env


def get_open_port():
# check ACTIVE_PORT first
port = os.getenv("ACTIVE_PORT", None)
if port is not None:
print(f"Active port found: {port}")
return int(port)

port = int(os.getenv("PORT", "5023"))
while is_port_in_use(port):
print(f"Port {port} is in use, trying next port...")
port += 1

# set ACTIVE_PORT to the port we found
os.environ["ACTIVE_PORT"] = str(port)
return port
4 changes: 3 additions & 1 deletion src/utils/windowai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import websocket
import uuid
import json
from .general import get_open_port


class MessageDict(TypedDict):
Expand Down Expand Up @@ -86,7 +87,8 @@ def _call(
)

ws = websocket.WebSocket()
ws.connect("ws://127.0.0.1:5000/windowmodel")
port = get_open_port()
ws.connect(f"ws://127.0.0.1:{port}/windowmodel")
ws.send(json.dumps(request))
message = ws.recv()
ws.close()
Expand Down

0 comments on commit 466693f

Please sign in to comment.