1
0

Add route to ask chatgpt about the state of the board

This commit is contained in:
vlad 2024-10-07 16:59:55 -07:00
parent e23cacfcbe
commit f5249e361c
3 changed files with 35 additions and 14 deletions

View File

@ -1,5 +1,5 @@
FROM archlinux FROM archlinux
RUN pacman -Sy --noconfirm xorg-server-xvfb xdotool scrot python-flask && pacman -Sc --noconfirm RUN pacman -Sy --noconfirm xorg-server-xvfb xdotool scrot python-flask python-openai && pacman -Sc --noconfirm
RUN mkdir -p /root/.local/share/ RUN mkdir -p /root/.local/share/
ADD profile /root/.local/share/IntoTheBreach ADD profile /root/.local/share/IntoTheBreach
ADD .generated/game/ /breach ADD .generated/game/ /breach

2
go
View File

@ -8,7 +8,7 @@
run-service() { run-service() {
_help-line "Start docker container with port 5000 exposed" _help-line "Start docker container with port 5000 exposed"
docker run --rm -ti -p 5000:5000 --name breach itbaas docker run --rm -ti -p 5000:5000 --name breach -e OPENAI_KEY=$(pass openai/project-key) itbaas
} }
build-image() { build-image() {

View File

@ -1,15 +1,23 @@
#!/usr/bin/env python #!/usr/bin/env python
from flask import Flask, send_file from flask import Flask, send_file, Response
import base64
import os import os
import subprocess import subprocess
from datetime import datetime from datetime import datetime
from openai import OpenAI
class GameInterface:
def click(self, x, y):
subprocess.run(['xdotool', 'mousemove', x, y, 'click', '1'])
def screenshot(self):
scrot = subprocess.run(['scrot', '-'], capture_output = True)
return scrot.stdout
app = Flask(__name__) app = Flask(__name__)
openai_client = OpenAI(api_key = os.environ['OPENAI_KEY'])
SCREENSHOT_DIR = "/tmp/screenshots" game = GameInterface()
if not os.path.exists(SCREENSHOT_DIR):
os.makedirs(SCREENSHOT_DIR)
@app.route('/') @app.route('/')
def index(): def index():
@ -17,17 +25,30 @@ def index():
@app.route('/screenshot') @app.route('/screenshot')
def screenshot(): def screenshot():
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') return Response(game.screenshot(), mimetype='image/png')
screenshot_path = os.path.join(SCREENSHOT_DIR, f'screenshot_{timestamp}.png')
subprocess.run(['scrot', screenshot_path])
return send_file(screenshot_path, mimetype='image/png')
@app.route('/click/<int:x>/<int:y>') @app.route('/click/<int:x>/<int:y>')
def click(x, y): def click(x, y):
subprocess.run(['xdotool', 'mousemove', str(x), str(y), 'click', '1']) game.click(str(x), str(y))
return f'Click performed at position ({x}, {y})' return f'Click performed at position ({x}, {y})'
@app.route('/ask-for-next-move')
def nextmove():
screenshot = game.screenshot()
screenshot64 = base64.b64encode(screenshot).decode('utf-8')
response = openai_client.chat.completions.create(
model="gpt-4o-mini",
messages=[ {
"role": "user",
"content": [
{"type": "text", "text": "we're playing Into the Breach. Describe the state of the board. Then tell me the best moves for our mechs to make."},
{"type": "image_url", "image_url": { "url": f"data:image/png;base64,{screenshot64}", }, },
],
} ]
)
return response.choices[0].message.content
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000) app.run(debug=True, host='0.0.0.0', port=5000)