[GH-ISSUE #6670] [Feature]: Optionally include character context in Lorebook semantic search #3733

Closed
opened 2026-10-21 06:06:37 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @minikkeuen on GitHub (Sep 25, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6670

Originally assigned to: @SpicyMarinara on GitHub.

What problem does this solve?

Lorebook semantic search currently builds its vector query from user messages only.

This appears to be intentional: keeping long assistant/character prose out of the query prevents it from overwhelming a short and specific user message. However, in roleplay chats, assistant messages often contain the actual current scene context rather than just a response to the user.

For example:

Assistant:

Alex steps into the shade because the sunlight is hurting his eyes.

User:

"Are you okay?"

If there is a vector-only Lorebook entry about Alex's sensitivity to bright light, the relevant context exists in the conversation, but it is only present in the assistant message. The user's short reply does not provide enough semantic information to retrieve that entry.

Increasing "Query Messages" does not solve this, because it only adds more user messages to the vector query.

This makes semantic Lorebook retrieval much less useful for roleplay styles where the character/assistant writes most of the scene narration.

Proposed solution

Keep the current user-only behavior as the default, but add an optional Lorebook semantic-search setting to also consider recent assistant/character context.

Ideally, assistant context could be evaluated separately from the existing user query rather than simply concatenated with it.

For example:

  • User messages → user query
  • Assistant/character messages → character-context query
  • Score each Lorebook entry against both
  • Use the higher relevant score for retrieval

This would preserve the current design goal: long character prose would not dilute or overwhelm a short user query.

At the same time, users who enable the option could retrieve vectorized Lorebook entries based on events, characters, locations, or other context introduced in the character's recent response.

The existing user-only behavior could remain the default, so this would not change retrieval behavior for existing users.

Alternatives considered

One alternative would be to simply include both user and assistant messages in the same query.

However, this could reintroduce the problem the current user-only implementation appears designed to avoid: long assistant prose may dominate the embedding and reduce the relevance of a short, specific user query.

Another option would be to use only the immediately preceding assistant message together with the current user message. This may work well for roleplay, but separate queries seem preferable because they preserve the user's semantic signal instead of mixing the two contexts.

Additional context

In Engine 2.4.6, selectLorebookVectorQueryText() filters messages to the user role before applying the configured query depth.

This behavior is not obvious from the "Query Messages" setting, so it can look like vector retrieval is failing when the relevant information is present in the recent conversation but only in character/assistant messages.

Other roleplay-related semantic retrieval features already consider both user and assistant context. For example, Roleplay Summary retrieval builds its query from the most recent user and assistant messages.

This request is not intended to replace the existing user-focused behavior. The goal is to provide an opt-in retrieval mode for roleplay use cases where assistant-generated scene context is important.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @minikkeuen on GitHub (Sep 25, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6670 Originally assigned to: @SpicyMarinara on GitHub. ## What problem does this solve? Lorebook semantic search currently builds its vector query from user messages only. This appears to be intentional: keeping long assistant/character prose out of the query prevents it from overwhelming a short and specific user message. However, in roleplay chats, assistant messages often contain the actual current scene context rather than just a response to the user. For example: Assistant: > Alex steps into the shade because the sunlight is hurting his eyes. User: > "Are you okay?" If there is a vector-only Lorebook entry about Alex's sensitivity to bright light, the relevant context exists in the conversation, but it is only present in the assistant message. The user's short reply does not provide enough semantic information to retrieve that entry. Increasing "Query Messages" does not solve this, because it only adds more user messages to the vector query. This makes semantic Lorebook retrieval much less useful for roleplay styles where the character/assistant writes most of the scene narration. ## Proposed solution Keep the current user-only behavior as the default, but add an optional Lorebook semantic-search setting to also consider recent assistant/character context. Ideally, assistant context could be evaluated separately from the existing user query rather than simply concatenated with it. For example: - User messages → user query - Assistant/character messages → character-context query - Score each Lorebook entry against both - Use the higher relevant score for retrieval This would preserve the current design goal: long character prose would not dilute or overwhelm a short user query. At the same time, users who enable the option could retrieve vectorized Lorebook entries based on events, characters, locations, or other context introduced in the character's recent response. The existing user-only behavior could remain the default, so this would not change retrieval behavior for existing users. ## Alternatives considered One alternative would be to simply include both user and assistant messages in the same query. However, this could reintroduce the problem the current user-only implementation appears designed to avoid: long assistant prose may dominate the embedding and reduce the relevance of a short, specific user query. Another option would be to use only the immediately preceding assistant message together with the current user message. This may work well for roleplay, but separate queries seem preferable because they preserve the user's semantic signal instead of mixing the two contexts. ## Additional context In Engine 2.4.6, `selectLorebookVectorQueryText()` filters messages to the `user` role before applying the configured query depth. This behavior is not obvious from the "Query Messages" setting, so it can look like vector retrieval is failing when the relevant information is present in the recent conversation but only in character/assistant messages. Other roleplay-related semantic retrieval features already consider both user and assistant context. For example, Roleplay Summary retrieval builds its query from the most recent user and assistant messages. This request is not intended to replace the existing user-focused behavior. The goal is to provide an opt-in retrieval mode for roleplay use cases where assistant-generated scene context is important. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 06:06:37 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Sep 26, 2026):

SpicyMarinara (Codex) is implementing this as an opt-in lorebook setting. The existing user query will stay unchanged; enabled lorebooks will also score a separate character-context query in the same embedding batch and use the higher match score.

<!-- gh-comment-id:5845493754 --> @SpicyMarinara commented on GitHub (Sep 26, 2026): SpicyMarinara (Codex) is implementing this as an opt-in lorebook setting. The existing user query will stay unchanged; enabled lorebooks will also score a separate character-context query in the same embedding batch and use the higher match score.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#3733
No description provided.