Files
fission-console/LLM/llm_prompt_round6.py
T

75 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
import json
import re
import urllib.error
import urllib.request
ASK_API = "https://fission.kube5s.ru/console/api/ai/ask"
CHECK_API = "https://fission.kube5s.ru/console/api/ai/check"
HEADERS = {"X-Test-Sub": "livetest@test.local", "Content-Type": "application/json"}
PROMPT = """Ты генерируешь код одной функции для Fission.
Требования к ответу:
- Сначала коротко нормализуй задачу.
- Потом выдай один fenced code block.
- Потом выдай ровно заголовок LLM что делает и 2-5 коротких пунктов.
- Не используй bold, Explanation, английский перевод заголовка, дополнительные заголовки или fenced block вокруг объяснения.
- Не выдумывай лишние SDK, фреймворки или wrapper-код.
- Код должен быть синтаксически корректным для выбранного языка и соответствовать runtime.
Языковые правила:
- Node.js: CommonJS без ESM export-обвязки.
- Python: обычный entrypoint.
- PHP: обычный PHP-код.
- Ruby: корректный def/end.
"""
CASES = [
("nodejs", "Сделай Fission Node.js handler: name из JSON body, иначе World, ответ 200 с greeting."),
("python", "Сделай Fission Python handler: name из context, иначе World, ответ greeting."),
("php", "Сделай Fission PHP handler: name из context, иначе World, ответ 200 JSON greeting."),
("ruby", "Сделай Fission Ruby handler: name из context, иначе World, ответ greeting JSON."),
]
def post(url: str, payload: dict) -> dict:
body = json.dumps(payload).encode()
req = urllib.request.Request(url, data=body, headers=HEADERS)
try:
with urllib.request.urlopen(req, timeout=90) as resp:
return json.loads(resp.read())
except urllib.error.HTTPError as exc:
return {"http_error": exc.code, "body": exc.read().decode(errors="replace")}
def extract_code(answer: str) -> str:
match = re.search(r"```(?:[a-zA-Z0-9_+-]+)?\n(.*?)\n```", answer, re.S)
return match.group(1).strip() if match else ""
for lang, question in CASES:
ask_resp = post(ASK_API, {"question": PROMPT + "\n\nЗадача:\n" + question})
print(f"=== {lang} ===")
print("QUESTION:")
print(question)
if ask_resp.get("http_error"):
print(f"HTTP {ask_resp['http_error']}")
print(ask_resp.get("body", ""))
print("\n" + "=" * 72 + "\n")
continue
answer = ask_resp.get("answer", ask_resp.get("error", "???"))
code = extract_code(answer)
lint_resp = post(CHECK_API, {"language": lang, "code": code}) if code else {"ok": False, "result": "no code extracted"}
print("HAS EXACT HEADING:", bool(re.search(r"(^|\n)LLM что делает\s*$", answer, re.M)))
print("ANSWER:")
print(answer)
print("\nEXTRACTED CODE:")
print(code)
print("\nLINT:")
print(json.dumps(lint_resp, ensure_ascii=False, indent=2)[:2000])
print("\n" + "=" * 72 + "\n")