-
Notifications
You must be signed in to change notification settings - Fork 40
Persist local agent chat hotfix in agent-api template #809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -68,7 +68,7 @@ spec: | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| items: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - "http://localhost:3000" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - "http://localhost:13000" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allowMethods: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type: array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| items: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -153,10 +153,31 @@ spec: | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| - name: main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| image: ${workload.container.image} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| imagePullPolicy: ${environmentConfigs.imagePullPolicy} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| command: | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ${has(workload.container.command) ? workload.container.command : oc_omit()} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| args: | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ${has(workload.container.args) ? workload.container.args : oc_omit()} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| command: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - /bin/sh | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - -lc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cat >/tmp/amp_chat_hotfix.py <<'PY' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| p = Path('/workspace/main.py') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text = p.read_text() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| old = """# Pydantic model for handling chat input\nclass Question(BaseModel):\n prompt: str\n""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new = """# Pydantic model for handling chat input\nclass Question(BaseModel):\n prompt: str | None = None\n message: str | None = None\n session_id: str | None = None\n context: dict | None = None\n""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if old in text: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text = text.replace(old, new, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| old2 = """# Route to chat with OpenAI\n@app.post(\"/chat/\")\ndef ask_openai(question: Question):\n try:\n response = client.chat.completions.create(\n model=\"gpt-4o\",\n messages=[{\"role\": \"user\", \"content\": question.prompt}]\n )\n result = response.choices[0].message.content\n\n # Store chat history in database\n db = SessionLocal()\n db.add(QueryHistory(question=question.prompt, response=result))\n db.commit()\n\n return {\"response\": result}\n except Exception as e:\n raise HTTPException(status_code=500, detail=str(e))\n""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new2 = """# Route to chat with OpenAI\n@app.post(\"/chat/\")\ndef ask_openai(question: Question):\n try:\n user_prompt = question.message or question.prompt\n if not user_prompt:\n raise HTTPException(status_code=422, detail=\"message or prompt is required\")\n\n response = client.chat.completions.create(\n model=\"gpt-4o\",\n messages=[{\"role\": \"user\", \"content\": user_prompt}]\n )\n result = response.choices[0].message.content\n\n # Store chat history in database\n db = SessionLocal()\n db.add(QueryHistory(question=user_prompt, response=result))\n db.commit()\n\n return {\"response\": result}\n except HTTPException:\n raise\n except Exception as e:\n raise HTTPException(status_code=500, detail=str(e))\n""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if old2 in text: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| text = text.replace(old2, new2, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| p.write_text(text) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print("startup hotfix applied") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+168
to
+177
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Major:
🛡️ Proposed fix — conditional reporting+ applied = False
if old in text:
text = text.replace(old, new, 1)
+ applied = True
...
if old2 in text:
text = text.replace(old2, new2, 1)
+ applied = True
p.write_text(text)
- print("startup hotfix applied")
+ if applied:
+ print("startup hotfix applied")
+ else:
+ print("startup hotfix: no patterns matched — source may have changed")📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /layers/google.python.runtime/python/bin/python3 /tmp/amp_chat_hotfix.py | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Major: hardcoded GCP Buildpack interpreter path breaks non-GCP agent-api deployments.
Consider falling back to a PATH-resolved interpreter so the script is a no-op for non-matching containers: 🛡️ Proposed fix — use PATH-resolved python3 with fallback- /layers/google.python.runtime/python/bin/python3 /tmp/amp_chat_hotfix.py
+ PYTHON3=$(command -v python3 2>/dev/null || echo /layers/google.python.runtime/python/bin/python3)
+ "$PYTHON3" /tmp/amp_chat_hotfix.pyNote that this alone is still insufficient if 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exec /cnb/process/web | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resources: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| requests: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cpu: ${environmentConfigs.resources.requests.cpu} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: unguarded
read_text()crashes startup for any agent-api without/workspace/main.py.p.read_text()raisesFileNotFoundErrorif the target file doesn't exist. Since thisClusterComponentTypeis shared across allagent-apiworkloads — including those built withamp-dockeroramp-ballerina-buildpack(seeallowedWorkflows, lines 12–17) — any agent that doesn't happen to have/workspace/main.pywill CrashLoopBackOff becauseexec /cnb/process/webnever runs.🛡️ Proposed fix — add an existence guard at the top of the patch script
🤖 Prompt for AI Agents