Spaces:
Running
Running
Chandima Prabhath
commited on
Commit
·
dba77ac
1
Parent(s):
7e58c6f
Refactor generate_llm function for improved readability; streamline parameter assignments and enhance error handling for 400 Bad Request responses.
Browse files
polLLM.py
CHANGED
@@ -42,11 +42,11 @@ def generate_llm(
|
|
42 |
Send a chat-completion request to the LLM, with retries and backoff.
|
43 |
Reads defaults from config.yaml, but can be overridden per-call.
|
44 |
"""
|
45 |
-
model
|
46 |
system_prompt = _build_system_prompt()
|
47 |
messages = [
|
48 |
{"role": "system", "content": system_prompt},
|
49 |
-
{"role": "user",
|
50 |
]
|
51 |
|
52 |
backoff = 1
|
@@ -55,14 +55,18 @@ def generate_llm(
|
|
55 |
seed = random.randint(0, 2**31 - 1)
|
56 |
logger.debug(f"LLM call attempt={attempt}, model={model}, seed={seed}")
|
57 |
resp = client.chat.completions.create(
|
58 |
-
model
|
59 |
-
messages
|
60 |
-
seed
|
61 |
)
|
62 |
text = resp.choices[0].message.content.strip()
|
63 |
logger.debug("LLM response received")
|
64 |
return text
|
65 |
except Exception as e:
|
|
|
|
|
|
|
|
|
66 |
logger.error(f"LLM error on attempt {attempt}: {e}")
|
67 |
if attempt < 5:
|
68 |
time.sleep(backoff)
|
|
|
42 |
Send a chat-completion request to the LLM, with retries and backoff.
|
43 |
Reads defaults from config.yaml, but can be overridden per-call.
|
44 |
"""
|
45 |
+
model = _DEFAULT_MODEL
|
46 |
system_prompt = _build_system_prompt()
|
47 |
messages = [
|
48 |
{"role": "system", "content": system_prompt},
|
49 |
+
{"role": "user", "content": prompt},
|
50 |
]
|
51 |
|
52 |
backoff = 1
|
|
|
55 |
seed = random.randint(0, 2**31 - 1)
|
56 |
logger.debug(f"LLM call attempt={attempt}, model={model}, seed={seed}")
|
57 |
resp = client.chat.completions.create(
|
58 |
+
model=model,
|
59 |
+
messages=messages,
|
60 |
+
seed=seed,
|
61 |
)
|
62 |
text = resp.choices[0].message.content.strip()
|
63 |
logger.debug("LLM response received")
|
64 |
return text
|
65 |
except Exception as e:
|
66 |
+
# Check if the error is a 400 Bad Request
|
67 |
+
if hasattr(e, "status_code") and e.status_code == 400:
|
68 |
+
logger.error("LLM error 400 (Bad Request): Not retrying.")
|
69 |
+
return "Error: Bad Request (400)"
|
70 |
logger.error(f"LLM error on attempt {attempt}: {e}")
|
71 |
if attempt < 5:
|
72 |
time.sleep(backoff)
|