Skip to content

feat(chatbot): separate answer instructions from prompt matching (#376) - #795

Open
PRAteek-singHWY wants to merge 6 commits into
OWASP:mainfrom
PRAteek-singHWY:fix/376-separate-answer-language-input
Open

PRAteek-singHWY wants to merge 6 commits into
OWASP:mainfrom
PRAteek-singHWY:fix/376-separate-answer-language-input

Conversation

@PRAteek-singHWY

@PRAteek-singHWY PRAteek-singHWY commented Mar 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

fixes #376

Separates answer instructions from the user prompt, so a language or style request ("Answer in Chinese") no longer goes into the embedding used for matching.

Problem

Language and style directives were part of the same text used for embeddings and matching, which made retrieval less accurate.

Changes

  • Frontend: a separate Instructions field in the chatbot UI, default Answer in English, max 200 chars.
  • API: /rest/v1/completion takes prompt and instructions separately.
  • Matching: embeddings come from prompt only.
  • Answering: instructions reach the LLM only, through chat_prompt.j2, on both the grounded and the fallback path.

Ported to the LiteLLM client (2026-11-05)

The OpenAI and Vertex clients this PR was first written against were removed from main, so the change is now in PromptHandler's LiteLLM create_chat_completion / query_llm and in chat_prompt.j2. Main merged in, no conflicts with main.

Instructions are user input, so:

  • the template gives them their own ANSWER_INSTRUCTIONS block, and rule 11 says they only control language, tone and format and never override rules 1 to 10
  • they are capped at 200 chars and collapsed to one line, so a newline cannot close the block and open a fake one
  • a missing, blank or non-string value falls back to Answer in English

Tests

  • application/tests/prompt_client_test.py: embeddings use the prompt only, instructions reach the completion, the fallback gets the default, normalisation, and the rendered prompt carries the block on both paths.
  • application/tests/web_main_test.py::TestMain::test_completion_passes_instructions_separately

Verification

  • Full suite merged with current main: 1102 tests OK (skipped=6). black and prettier clean.
  • Not tested with a live LLM, so whether a model obeys rule 11 is unverified.

Merge order

This conflicts with #1161 in prompt_client.py. #1161 should merge first; I will then merge main in here again and re-add instructions= on its completion calls.

@northdpole

Copy link
Copy Markdown
Collaborator

Review (with #796): Architecturally sound — separating retrieval prompt from answer instructions (#376) prevents language directives from polluting embeddings. Frontend + API + prompt client changes are coherent.

Blockers: Both PRs CONFLICTING on main; #796 is stacked on #795. Large diff (~1k lines on #796). Needs rebase of #795 first, then #796 onto updated #795, then review CI + manual chatbot smoke test.

Not merging now — good post-GSoC candidate.

…swer-language-input

# Conflicts:
#	application/frontend/src/pages/chatbot/chatbot.tsx
#	application/prompt_client/openai_prompt_client.py
#	application/prompt_client/prompt_client.py
#	application/prompt_client/vertex_prompt_client.py
#	application/web/web_main.py
The OpenAI and Vertex clients this change was written against are gone from
main, so instructions now go through chat_prompt.j2. They are user input, so
the template gives them their own block limited to language, tone and format,
and they are capped at 200 chars. Non-string values fall back to the default
instead of crashing. Matching still uses the question alone.
The UI field is one line but the API takes anything, and a newline could close
the template's block and open a fake one.
@coderabbitai

coderabbitai Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: OWASP/OpenCRE
  • Branch: main
  • File: .coderabbit.yml
  • Commit: facfd46
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2d89359d-04a5-4e2e-b00a-e9fe08a64da1
Commits

Reviewing files that changed from the base of the PR and between facfd46 and fbd035a.

Files selected for processing: 6

  • application/frontend/src/pages/chatbot/chatbot.tsx
  • application/prompt_client/prompt_client.py
  • application/prompt_client/templates/chat_prompt.j2
  • application/tests/prompt_client_test.py
  • application/tests/web_main_test.py
  • application/web/web_main.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Have a separate UI element to enter language request, to prevent requests in the prompt to bother the matching

2 participants