|
|
from functools import cache |
|
|
import requests |
|
|
import os |
|
|
from typing import Any |
|
|
from pgsoft.pgdate.date_utils import beijing |
|
|
import json |
|
|
|
|
|
|
|
|
def post_to_pgai_helper(command: str, game: str) -> Any: |
|
|
myobj = { |
|
|
"token": os.getenv("pgai_code"), |
|
|
"command": command, |
|
|
} |
|
|
|
|
|
header = {"accept": "application/json", "Content-Type": "application/json"} |
|
|
url_base = f"https://pgtrain.playgo.ai/games/{game}/nlp" |
|
|
url_read = f"{url_base}/r" |
|
|
|
|
|
try: |
|
|
res = requests.post(url_read, headers=header, json=myobj) |
|
|
if res.status_code == 200: |
|
|
print(res.text) |
|
|
return res.text |
|
|
else: |
|
|
print(res.text) |
|
|
return None |
|
|
except Exception as e: |
|
|
print(e) |
|
|
return None |
|
|
|
|
|
|
|
|
from_cache = True |
|
|
|
|
|
|
|
|
|
|
|
def call_pgai(nlp_command, game): |
|
|
calling_start = beijing() |
|
|
print(f"[{calling_start}] calling ai starts") |
|
|
try: |
|
|
res = post_to_pgai_helper(nlp_command, game) |
|
|
except Exception as e: |
|
|
return f"{type(e)}: {str(e)}." |
|
|
|
|
|
calling_end = beijing() |
|
|
timecost = calling_end.timestamp() - calling_start.timestamp() |
|
|
print(f"[{calling_end}] calling ai ends, costs {timecost:.2f}s") |
|
|
global from_cache |
|
|
from_cache = False |
|
|
if res is None: |
|
|
return res |
|
|
outp = json.loads(res) |
|
|
return outp |
|
|
|
|
|
|