example: 增加了示例 API
Browse files- examples/free-api.py +48 -0
examples/free-api.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""最小版 api1 主接口调用示例。"""
|
| 3 |
+
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
import httpx
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
BASE_URL = "https://api1.pdf2zh-next.com/chatproxy"
|
| 12 |
+
TIMEOUT_SECONDS = 30.0
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def build_translate_prompt(text: str, lang_out: str = "zh") -> str:
|
| 16 |
+
"""复刻上游的翻译 prompt。"""
|
| 17 |
+
return (
|
| 18 |
+
r"You are a professional,authentic machine translation engine."
|
| 19 |
+
"\n\n"
|
| 20 |
+
+ r";; Treat next line as plain text input and translate it into "
|
| 21 |
+
+ lang_out
|
| 22 |
+
+ r", output translation ONLY. If translation is unnecessary "
|
| 23 |
+
+ r"(e.g. proper nouns, codes, {{1}}, etc. ), return the original text. "
|
| 24 |
+
+ r"NO explanations. NO notes. Input:"
|
| 25 |
+
+ "\n\n"
|
| 26 |
+
+ text
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def main() -> None:
|
| 31 |
+
"""直接调用主接口。"""
|
| 32 |
+
payload = {
|
| 33 |
+
"text": build_translate_prompt("Hello world"),
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
with httpx.Client(timeout=TIMEOUT_SECONDS) as client:
|
| 37 |
+
response = client.post(BASE_URL, json=payload)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
body = response.json()
|
| 40 |
+
|
| 41 |
+
print("=== request ===")
|
| 42 |
+
print(json.dumps(payload, ensure_ascii=False, indent=2))
|
| 43 |
+
print("\n=== response ===")
|
| 44 |
+
print(json.dumps(body, ensure_ascii=False, indent=2))
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
main()
|