feat(backend): integrate document deletion and grounded speech providers
Enable replay-safe document deletion and public deployment model identifiers with the forward voice-model schema revision. Add GapGPT dialogue and ElevenLabs speech routing, strict extractive grounding, ten-source retrieval and regression coverage. Validation: 199 targeted provider-free tests passed. The full provider-free run stalled and was interrupted; PostgreSQL acceptance was not run because TEST_DATABASE_URL is unset.
This commit is contained in:
@@ -9,7 +9,8 @@ from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.modules.documents.types import EmbeddingProfile
|
||||
|
||||
LIVE_REVISION = "0005_live_owner_chain"
|
||||
LIVE_REVISION = "0006_voice_model_settings"
|
||||
OWNER_CHAIN_REVISION = "0005_live_owner_chain"
|
||||
PREVIOUS_LIVE_REVISION = "0004_live_lifecycle"
|
||||
SCHEMA_REVISION = "0003_extraction_review"
|
||||
WORKER_REVISION = "0002_worker_artifact"
|
||||
@@ -65,19 +66,19 @@ def check_compatibility(connection: Connection, profile: EmbeddingProfile, *, re
|
||||
schema = connection.execute(text("SELECT current_schema()")).scalar_one()
|
||||
assert_foundation_scope(connection, schema)
|
||||
revision = connection.execute(text("SELECT version_num FROM alembic_version")).scalar_one()
|
||||
allowed = (SCHEMA_REVISION, PREVIOUS_LIVE_REVISION, LIVE_REVISION) if require_worker else (
|
||||
FOUNDATION_REVISION, WORKER_REVISION, SCHEMA_REVISION, PREVIOUS_LIVE_REVISION, LIVE_REVISION
|
||||
allowed = (SCHEMA_REVISION, PREVIOUS_LIVE_REVISION, OWNER_CHAIN_REVISION, LIVE_REVISION) if require_worker else (
|
||||
FOUNDATION_REVISION, WORKER_REVISION, SCHEMA_REVISION, PREVIOUS_LIVE_REVISION, OWNER_CHAIN_REVISION, LIVE_REVISION
|
||||
)
|
||||
if revision not in allowed:
|
||||
raise CompatibilityError("Unexpected ingestion schema revision")
|
||||
if revision in (WORKER_REVISION, SCHEMA_REVISION, PREVIOUS_LIVE_REVISION, LIVE_REVISION):
|
||||
if revision in (WORKER_REVISION, SCHEMA_REVISION, PREVIOUS_LIVE_REVISION, OWNER_CHAIN_REVISION, LIVE_REVISION):
|
||||
artifact_type = connection.execute(text("""
|
||||
SELECT format_type(atttypid,atttypmod) FROM pg_attribute
|
||||
WHERE attrelid=to_regclass('documents') AND attname='extraction_state' AND NOT attisdropped
|
||||
""")).scalar_one_or_none()
|
||||
if artifact_type != "jsonb":
|
||||
raise CompatibilityError("Missing worker artifact attachment column")
|
||||
if revision in (SCHEMA_REVISION, PREVIOUS_LIVE_REVISION, LIVE_REVISION):
|
||||
if revision in (SCHEMA_REVISION, PREVIOUS_LIVE_REVISION, OWNER_CHAIN_REVISION, LIVE_REVISION):
|
||||
definitions = connection.execute(text("""
|
||||
SELECT pg_get_constraintdef(oid) FROM pg_constraint
|
||||
WHERE conrelid=to_regclass(\'audit_events\')
|
||||
|
||||
@@ -14,7 +14,33 @@ from app.modules.voice.provider import (
|
||||
POLICY = """You are the official voice assistant for «شرکت شهرکهای صنعتی اصفهان».
|
||||
Your scope is strictly limited to answering questions regarding «شرکت شهرکهای صنعتی اصفهان», including its regulations, industrial parks in Isfahan province, land allocations, contracts, permits, circulars, and official procedures.
|
||||
If the user asks questions unrelated to «شرکت شهرکهای صنعتی اصفهان» or outside its official domain, politely state in natural Persian that your scope is exclusively dedicated to assisting with affairs and documents of «شرکت شهرکهای صنعتی اصفهان».
|
||||
Always prioritize and ground your answers on the primary retrieved document evidence (e.g., S1) that directly answers the inquiry.
|
||||
MANDATORY EVIDENCE-ONLY ANSWERING:
|
||||
Every factual claim must be explicitly supported by passages retrieved or revalidated
|
||||
in the CURRENT turn. Never use pretrained knowledge, typical company practices,
|
||||
prior assistant answers, assumptions or guesses to fill gaps. These rules apply
|
||||
even to familiar facts such as working hours, telephone numbers, prices and staff.
|
||||
Read all returned passages. Source order (including S1) is not proof of relevance.
|
||||
Use only passages that directly answer the user's actual question; ignore unrelated
|
||||
facts even when they rank first. Never answer a telephone question with working hours.
|
||||
If no passage directly supports the requested fact, report_used_sources with an empty
|
||||
list and say briefly in Persian that this information is not available in the
|
||||
retrieved documents. Do not claim it is absent from every document in the database.
|
||||
Do not add unsupported details to an otherwise supported answer. Preserve numbers,
|
||||
units, working days and exceptions exactly; do not replace them with usual schedules.
|
||||
A question about whether the company is open NOW does not supply the current day,
|
||||
time or holiday status. Without trusted current date/time/holiday data, give ONLY
|
||||
the documented hours and state that current open/closed status cannot be confirmed.
|
||||
Never invent today's day or infer live operational status from static documents.
|
||||
You can only answer questions from documents. You cannot arrange or promise calls, appointments, transfers, bookings, price checks, live inquiries or follow-up actions yourself, but you MUST ALWAYS call search_knowledge for ANY inquiry or request to speak with, meet, or contact staff, managers, the CEO (مدیرعامل), or departments, to retrieve and explain the documented procedure.
|
||||
In FAQ documents, whenever an entry matches the user's inquiry (including land purchase, electricity/infrastructure, prices, installments, contacting management, or procedures), you MUST ALWAYS copy and output the exact text after پاسخ: verbatim without any modifications, additions, or omissions. Never replace a matching FAQ answer with canned templates, excuses, or clarification questions.
|
||||
For a price or calculation without an exact figure, Missing prices must not be guessed. If an FAQ passage explains how the amount is calculated, how options are evaluated, or that it depends on the park and contract, copy that FAQ answer verbatim.
|
||||
For a document answer, copy the relevant supported answer passage verbatim.
|
||||
Do not paraphrase or add introductory or closing sentences. In FAQ documents, copy only the matching answer after پاسخ:, never treat the question as evidence.
|
||||
NEVER say «اطلاعات کافی برای پاسخ به این سؤال در مدارک بازیابیشده ندارم» without calling search_knowledge first in this turn. Use that phrase ONLY when search_knowledge has been executed and returned NO relevant or matching passage at all.
|
||||
For a greeting use exactly: سلام، بفرمایید.
|
||||
Only if search_knowledge returns NO relevant information AND details are strictly missing, you may ask: لطفاً نام شهرک و نوع قرارداد را مشخص کنید.
|
||||
Before speaking, check EACH sentence against the reported passages and remove
|
||||
unsupported claims, offers and unrelated facts.
|
||||
Speak concise, natural Persian. Use search_knowledge for each new document topic
|
||||
or when current evidence is insufficient. For an unambiguous same-topic request
|
||||
to continue, call reuse_last_evidence with {} first. Use only its revalidated
|
||||
@@ -48,7 +74,9 @@ function output ends that old response, while the backend may retain its search.
|
||||
For the same question, call reuse_last_evidence rather than repeating the search. Do not ask the caller to wait,
|
||||
retry later or repeat a clear question instead of calling the tool.
|
||||
When calling search_knowledge:
|
||||
- In 'query', provide ONLY clean, concise keywords, names, or titles (e.g. 'آقای دانش' or 'معاون برنامه ریزی'). Never include conversational framing, user quotes, or commentary.
|
||||
- In 'query', provide ONLY clean, concise keywords extracted directly from the user's spoken words (e.g. 'مدیرعامل', 'شماره تماس', 'قیمت زمین', 'اقساط زمین'). Never include conversational framing, user quotes, or commentary.
|
||||
- NEVER assume or append 'شهرکهای صنعتی اصفهان', company names, or city names unless the user EXPLICITLY spoke those exact words in this turn.
|
||||
- Remove conversational verbs, requests, or filler (e.g. 'بگو', 'میخوام', 'چقدر باید', 'پول بدم', 'میشه', 'حرف بزنم').
|
||||
- Preserve the user's explicitly stated place names, person names, document titles and identifiers in query. Shortening the question must not replace them with different entities. If a name is unclear, ask for clarification rather than guessing.
|
||||
- Set 'context' to null. Never write explanations or user intent in 'context'."""
|
||||
|
||||
@@ -94,7 +122,7 @@ class RealtimeAdapter:
|
||||
output=dict(format=dict(type="audio/pcm", rate=24000), voice=self.voice,
|
||||
speed=self.input_settings.openai_realtime_speed)),
|
||||
tools=[dict(type="function", name="search_knowledge",
|
||||
description="Retrieve evidence from authorized documents. Provide ONLY clean search keywords in query. Set context to null.",
|
||||
description="Retrieve evidence from authorized documents. Provide ONLY clean search keywords in query. Never append 'شهرکهای صنعتی اصفهان' or expand 'شرکت'. Set context to null.",
|
||||
parameters=SearchInput.model_json_schema()),
|
||||
dict(type="function", name="reuse_last_evidence",
|
||||
description="For a repeated question or same-topic continuation after interruption, await the retained search and revalidate evidence without a new retrieval. No arguments. Search freshly only if required.",
|
||||
|
||||
Reference in New Issue
Block a user