-
Notifications
You must be signed in to change notification settings - Fork 188
poc(chat): improve prompt to guide LLM toward JSON Patch format #1769
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
Conversation
|
|
Updates to Preview Branch (hoshino-poc-chat-op) ↗︎
Tasks are run on every commit but only new migration files are pushed.
View logs for this Workflow Run ↗︎. |
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
CI Feedback 🧐A test triggered by this PR failed. Here is an AI-generated analysis of the failure:
|
* Replaced the previous agent-based approach with a LangGraph workflow (`runChat`) for the `'build'` mode * Refined the system prompt to consistently guide the LLM toward proper RFC 6902 JSON Patch output, wrapped in \`\`\`json fences * Improved the prompt structure to allow informative commentary outside the JSON Patch block * Added fallback mechanism for non-schema-editing messages (i.e., omit JSON Patch) * Introduced unit tests for LangGraph flow to ensure format correctness and schema alignment * Updated dependencies to include `@langchain/langgraph`
8e0fa37 to
deed8e5
Compare
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||||||
FunamaYukina
left a comment
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.
Sorry for the late review.
frontend/apps/app/lib/chat/langGraph.ts and others were very helpful in considering the implementation of LangGraph.Thank you for creating the PoC!
| } | ||
|
|
||
| const draft = async (s: ChatState): Promise<Partial<ChatState>> => { | ||
| const agent = mastra.getAgent('databaseSchemaBuildAgent') |
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.
📝
I found that I can use mastra as is! (I thought I had to go back to LangChain...)
| const check = async (s: ChatState): Promise<Partial<ChatState>> => { | ||
| const m = s.draft?.match(/```json\s+([\s\S]+?)\s*```/i) | ||
| if (!m) return { valid: false } | ||
| try { | ||
| return { valid: true, patch: JSON.parse(m[1]) } | ||
| } catch { | ||
| return { valid: false } | ||
| } | ||
| } |
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.
[Q]
Is there a plan to enhance the check function to also verify the presence of specific properties, like 'op', similar to how the tests currently do it?
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.
Good point! I believe validation is necessary from the following perspectives, increasing in complexity as you go:
- Does it conform to the structure of a valid JSON Patch(RFC 6902)?
- (If a JSON Schema or valibot schema is defined in
schema.json) Does the resultingschema.jsonafter applying the patch conform to that schema? - Does applying the patch result in any inconsistencies with the current schema state? (e.g., referencing a non-existent key)
This is probably something we’ll implement eventually, but with low technical uncertainty, it’s a bit hard to justify doing it right away.
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.
Thank you for the details! It seems important to check until we can actually apply!
Understood. Thank you very much.✨
|
The materials created in this PoC might be used later, but I'll submit them in a separate PR. Closing this one for now. |
Just PoC. no need to merge.
poc(chat): improve prompt to guide LLM toward JSON Patch format
runChat) for the'build'mode@langchain/langgraphImage
11.mov
🧭 Overview (Flow)
This change transitions from a sequential
mastra.getAgent().generate(...)-based approach to a state-driven LLM processing pipeline using LangGraph.The new process can be represented as the following DAG:
🧩 Role of Each Node
Thanks to this design, the system can retry up to 3 times if a valid JSON Patch is not returned.
🏗️ Integration with Surrounding Code
route.tsThe
/api/chatAPI endpoint callsrunChat(...)ifmode: 'build'.It passes
schemaTextandchatHistoryinto LangGraph for stateful execution.ChatbotDialog.tsxThe frontend explicitly sets
mode: 'build', which activates the LangGraph flow.mastra/agents/databaseSchemaBuildAgent.tsThe
buildPromptused in LangGraph has been enhanced with an action-oriented system prompt to ensure the LLM always returns a JSON Patch wrapped in a ```json code fence.✅ Advantages of the DAG Approach
Clear State Management: With
ChatStateannotations, each step's expected inputs and outputs are type-safe and well-defined.Self-Healing: The
check → remind → checkloop allows automatic retries when output is malformed or incomplete.Extensibility: Adding a separate graph for
askmode or inserting new branches is straightforward and modular.🧠 Summary
This commit represents a shift from "just calling the LLM" to a more robust architecture that controls, verifies, and retries LLM behavior.
By adopting LangGraph, we've built a reliable and extensible LLM integration pipeline grounded in DAG principles.
Issue
Why is this change needed?
What would you like reviewers to focus on?
Testing Verification
What was done
🤖 Generated by PR Agent at deed8e5
runChatusing LangGraph state graphDetailed Changes
3 files
Add LangGraph-based chat workflow for schema build modeSwitch to LangGraph chat pipeline for build modeEnsure chat API uses build mode for LangGraph1 files
Add unit tests for LangGraph chat pipeline1 files
Update system prompt with strict JSON Patch output instructions2 files
Add @langchain/langgraph dependencyUpdate lockfile for LangGraph and related dependencies1 files
Additional Notes