Spaces:
Running
Running
Nrighton233j commited on
Commit ·
2736a9f
1
Parent(s): b16bd7a
fix: raise final-step token budget and ctx for longer answers
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ os.makedirs(LOG_DIR, exist_ok=True)
|
|
| 19 |
print("Loading JOY...")
|
| 20 |
llm = Llama(
|
| 21 |
model_path=MODEL_PATH,
|
| 22 |
-
n_ctx=
|
| 23 |
n_threads=2,
|
| 24 |
n_batch=128,
|
| 25 |
verbose=False
|
|
@@ -36,17 +36,14 @@ def fetch_url(url):
|
|
| 36 |
try:
|
| 37 |
r = requests.get(url, timeout=10, headers={"User-Agent": "JOY-Agent/1.0"})
|
| 38 |
soup = BeautifulSoup(r.text, "html.parser")
|
| 39 |
-
# Remove scripts and styles
|
| 40 |
for tag in soup(["script","style","nav","footer","header"]):
|
| 41 |
tag.decompose()
|
| 42 |
text = soup.get_text(separator=" ", strip=True)
|
| 43 |
-
# Trim to 800 chars to fit context
|
| 44 |
return text[:800]
|
| 45 |
except Exception as e:
|
| 46 |
return f"FETCH_ERROR: {str(e)}"
|
| 47 |
|
| 48 |
def think(command, key):
|
| 49 |
-
# Auth check
|
| 50 |
if key != SESSION_KEY:
|
| 51 |
log_denied(command)
|
| 52 |
return {"error": "denied", "reason": "invalid key"}, 403
|
|
@@ -58,16 +55,17 @@ def think(command, key):
|
|
| 58 |
|
| 59 |
steps = []
|
| 60 |
final_answer = None
|
|
|
|
| 61 |
|
| 62 |
-
for i in range(
|
|
|
|
| 63 |
response = llm.create_chat_completion(
|
| 64 |
messages=messages,
|
| 65 |
-
max_tokens=40,
|
| 66 |
temperature=0.7
|
| 67 |
)
|
| 68 |
content = response["choices"][0]["message"]["content"].strip()
|
| 69 |
|
| 70 |
-
# Check for FETCH
|
| 71 |
if "FETCH:" in content:
|
| 72 |
url = content.split("FETCH:")[1].strip().split()[0]
|
| 73 |
steps.append({"step": i+1, "action": "FETCH", "url": url})
|
|
@@ -75,7 +73,6 @@ def think(command, key):
|
|
| 75 |
messages.append({"role": "assistant", "content": content})
|
| 76 |
messages.append({"role": "user", "content": f"PAGE CONTENT: {page}\nNow answer."})
|
| 77 |
|
| 78 |
-
# Check for ANSWER
|
| 79 |
elif "ANSWER:" in content:
|
| 80 |
final_answer = content.split("ANSWER:")[1].strip()
|
| 81 |
steps.append({"step": i+1, "action": "ANSWER", "content": final_answer})
|
|
|
|
| 19 |
print("Loading JOY...")
|
| 20 |
llm = Llama(
|
| 21 |
model_path=MODEL_PATH,
|
| 22 |
+
n_ctx=1024,
|
| 23 |
n_threads=2,
|
| 24 |
n_batch=128,
|
| 25 |
verbose=False
|
|
|
|
| 36 |
try:
|
| 37 |
r = requests.get(url, timeout=10, headers={"User-Agent": "JOY-Agent/1.0"})
|
| 38 |
soup = BeautifulSoup(r.text, "html.parser")
|
|
|
|
| 39 |
for tag in soup(["script","style","nav","footer","header"]):
|
| 40 |
tag.decompose()
|
| 41 |
text = soup.get_text(separator=" ", strip=True)
|
|
|
|
| 42 |
return text[:800]
|
| 43 |
except Exception as e:
|
| 44 |
return f"FETCH_ERROR: {str(e)}"
|
| 45 |
|
| 46 |
def think(command, key):
|
|
|
|
| 47 |
if key != SESSION_KEY:
|
| 48 |
log_denied(command)
|
| 49 |
return {"error": "denied", "reason": "invalid key"}, 403
|
|
|
|
| 55 |
|
| 56 |
steps = []
|
| 57 |
final_answer = None
|
| 58 |
+
max_steps = 2
|
| 59 |
|
| 60 |
+
for i in range(max_steps):
|
| 61 |
+
is_last_step = (i == max_steps - 1)
|
| 62 |
response = llm.create_chat_completion(
|
| 63 |
messages=messages,
|
| 64 |
+
max_tokens=250 if is_last_step else 40,
|
| 65 |
temperature=0.7
|
| 66 |
)
|
| 67 |
content = response["choices"][0]["message"]["content"].strip()
|
| 68 |
|
|
|
|
| 69 |
if "FETCH:" in content:
|
| 70 |
url = content.split("FETCH:")[1].strip().split()[0]
|
| 71 |
steps.append({"step": i+1, "action": "FETCH", "url": url})
|
|
|
|
| 73 |
messages.append({"role": "assistant", "content": content})
|
| 74 |
messages.append({"role": "user", "content": f"PAGE CONTENT: {page}\nNow answer."})
|
| 75 |
|
|
|
|
| 76 |
elif "ANSWER:" in content:
|
| 77 |
final_answer = content.split("ANSWER:")[1].strip()
|
| 78 |
steps.append({"step": i+1, "action": "ANSWER", "content": final_answer})
|