Use FastGPT app opener for greetings

This commit is contained in:
Xin Wang
2026-05-29 15:34:33 +08:00
parent ce3c384b00
commit 15c98f21fe
9 changed files with 30 additions and 50 deletions

View File

@@ -170,7 +170,6 @@ class FastGPTLLMService(LLMService):
base_url: str,
chat_id: str | None = None,
app_id: str | None = None,
send_system_prompt: bool = False,
greeting_prompt: str | None = None,
timeout: float = 60.0,
settings: FastGPTLLMSettings | None = None,
@@ -183,7 +182,6 @@ class FastGPTLLMService(LLMService):
self._chat_id = chat_id or f"voice_{uuid.uuid4().hex[:16]}"
self._app_id = (app_id or "").strip()
self._send_system_prompt = send_system_prompt
self._greeting_prompt = (greeting_prompt or "你好").strip() or "你好"
self._client = AsyncChatClient(
api_key=api_key,
@@ -246,6 +244,8 @@ class FastGPTLLMService(LLMService):
return _first_nonempty_text(
chat_config.get("welcomeText"),
app_payload.get("welcomeText"),
app_payload.get("opener"),
app_payload.get("intro"),
)
async def fetch_welcome_text(self) -> str | None:
@@ -261,7 +261,7 @@ class FastGPTLLMService(LLMService):
response.raise_for_status()
text = self._welcome_text_from_init_payload(response.json())
if text:
logger.info(f"FastGPT welcomeText loaded for appId={self._app_id}")
logger.info(f"FastGPT app opener loaded for appId={self._app_id}")
return text or None
except FastGPTError as exc:
logger.warning(f"FastGPT chat init failed: {exc}")
@@ -279,26 +279,15 @@ class FastGPTLLMService(LLMService):
def _build_fastgpt_messages(self, context: LLMContext) -> list[dict[str, str]]:
raw_messages = context.get_messages()
messages: list[dict[str, str]] = []
if self._send_system_prompt:
for message in raw_messages:
if not isinstance(message, dict) or message.get("role") != "system":
continue
text = _message_text(message)
if text:
messages.append({"role": "system", "content": text})
for message in reversed(raw_messages):
if not isinstance(message, dict) or message.get("role") != "user":
continue
text = _message_text(message)
if text:
messages.append({"role": "user", "content": text})
return messages
return [{"role": "user", "content": text}]
messages.append({"role": "user", "content": self._greeting_prompt})
return messages
return [{"role": "user", "content": self._greeting_prompt}]
async def _process_context(self, context: LLMContext) -> None:
messages = self._build_fastgpt_messages(context)