Spaces:
Sleeping
Sleeping
Update app/backend/runpod_client.py
Browse filesnow runpod can handle both synch and asynch requests
- app/backend/runpod_client.py +64 -21
app/backend/runpod_client.py
CHANGED
|
@@ -25,30 +25,73 @@ def format_messages_as_prompt(messages):
|
|
| 25 |
return "\n\n".join(parts)
|
| 26 |
|
| 27 |
|
| 28 |
-
def run_prompt(prompt: str) -> str:
|
| 29 |
"""Submit a prompt to the RunPod endpoint and get back a response string."""
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
response = requests.post(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
else:
|
| 51 |
-
raise RuntimeError("
|
| 52 |
|
| 53 |
|
| 54 |
def clean_and_parse_json(raw_text: str):
|
|
|
|
| 25 |
return "\n\n".join(parts)
|
| 26 |
|
| 27 |
|
| 28 |
+
def run_prompt(prompt: str, max_tokens: int = 512, context_length: int = 8192) -> str:
|
| 29 |
"""Submit a prompt to the RunPod endpoint and get back a response string."""
|
| 30 |
+
|
| 31 |
+
payload = {
|
| 32 |
+
"input": {
|
| 33 |
+
"prompt": prompt,
|
| 34 |
+
"max_tokens": max_tokens,
|
| 35 |
+
"options": {
|
| 36 |
+
"num_ctx": context_length,
|
| 37 |
+
"num_predict": max_tokens
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
|
| 42 |
+
url = f"{ENDPOINT}/run"
|
| 43 |
+
response = requests.post(url, headers=HEADERS, json=payload, timeout=60)
|
| 44 |
+
|
| 45 |
+
if response.status_code != 200:
|
| 46 |
+
raise RuntimeError(f"HTTP {response.status_code}: {response.text}")
|
| 47 |
+
|
| 48 |
+
result = response.json()
|
| 49 |
+
print(f"Initial response: {result}")
|
| 50 |
+
|
| 51 |
+
# Check if it's a direct sync response
|
| 52 |
+
if "success" in result:
|
| 53 |
+
if result.get("success"):
|
| 54 |
+
return result["response"]
|
| 55 |
+
else:
|
| 56 |
+
raise RuntimeError(f"Handler error: {result.get('error', 'Unknown error')}")
|
| 57 |
+
|
| 58 |
+
# Handle async job response
|
| 59 |
+
elif "id" in result:
|
| 60 |
+
job_id = result["id"]
|
| 61 |
+
print(f"[RunPod] Job started: {job_id}")
|
| 62 |
+
|
| 63 |
+
# Poll for completion
|
| 64 |
+
while True:
|
| 65 |
+
status_url = f"{ENDPOINT}/status/{job_id}"
|
| 66 |
+
status_response = requests.get(status_url, headers=HEADERS)
|
| 67 |
+
|
| 68 |
+
if status_response.status_code != 200:
|
| 69 |
+
raise RuntimeError(f"Status check failed: {status_response.status_code}")
|
| 70 |
+
|
| 71 |
+
status_result = status_response.json()
|
| 72 |
+
status = status_result.get("status")
|
| 73 |
+
print(f"[RunPod] Status: {status}")
|
| 74 |
+
|
| 75 |
+
if status == "COMPLETED":
|
| 76 |
+
output = status_result.get("output", {})
|
| 77 |
+
if output.get("success"):
|
| 78 |
+
return output["response"]
|
| 79 |
+
else:
|
| 80 |
+
raise RuntimeError(f"Job failed: {output.get('error', 'Unknown error')}")
|
| 81 |
+
|
| 82 |
+
elif status == "FAILED":
|
| 83 |
+
error_msg = status_result.get("error", "Job failed without specific error")
|
| 84 |
+
raise RuntimeError(f"RunPod job failed: {error_msg}")
|
| 85 |
+
|
| 86 |
+
elif status in ["IN_QUEUE", "IN_PROGRESS"]:
|
| 87 |
+
print(f"[RunPod] Waiting... Status: {status}")
|
| 88 |
+
time.sleep(3) # Wait 3 seconds before polling again
|
| 89 |
+
|
| 90 |
+
else:
|
| 91 |
+
raise RuntimeError(f"Unknown job status: {status}")
|
| 92 |
+
|
| 93 |
else:
|
| 94 |
+
raise RuntimeError(f"Unexpected response format: {result}")
|
| 95 |
|
| 96 |
|
| 97 |
def clean_and_parse_json(raw_text: str):
|