sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
+36
-11
@@ -8,13 +8,12 @@ answer, while making no network or model-provider request.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import codecs
|
||||
import math
|
||||
import re
|
||||
import uuid
|
||||
from collections.abc import AsyncIterator, Sequence
|
||||
from typing import Any
|
||||
|
||||
import tiktoken
|
||||
from langchain_core.callbacks import AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun
|
||||
from langchain_core.language_models.chat_models import BaseChatModel
|
||||
from langchain_core.messages import AIMessage, AIMessageChunk, BaseMessage
|
||||
@@ -24,12 +23,38 @@ from langgraph.graph import END, START, StateGraph
|
||||
|
||||
from deerflow.agents.thread_state import ThreadState
|
||||
|
||||
_TOKEN_ENCODING = tiktoken.get_encoding("cl100k_base")
|
||||
_LOCAL_TOKEN_PATTERN = re.compile(
|
||||
r"\s*(?:[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]|\w{1,4}|[^\w\s])|\s+",
|
||||
re.UNICODE,
|
||||
)
|
||||
_TARGET_CHUNKS_PER_SECOND = 10
|
||||
_MAX_TOKENS_PER_CHUNK = 20
|
||||
_MAX_TOKENS_PER_SECOND = 1_000
|
||||
|
||||
|
||||
def _split_local_tokens(text: str) -> list[str]:
|
||||
"""Split text into lossless, token-like units without loading model assets.
|
||||
|
||||
Fixed answers only need a stable unit for stream pacing. Using a model
|
||||
tokenizer here is both model-specific and unsafe for offline deployments:
|
||||
``tiktoken.get_encoding`` downloads its vocabulary when the host cache is
|
||||
empty. This local approximation treats each CJK character, short word
|
||||
fragment, or punctuation mark as one unit and attaches surrounding
|
||||
whitespace so the emitted chunks reconstruct the configured answer
|
||||
exactly. Limiting word fragments also prevents a long URL or identifier
|
||||
from bypassing the configured pace as one oversized unit.
|
||||
"""
|
||||
|
||||
if not text:
|
||||
return []
|
||||
tokens = _LOCAL_TOKEN_PATTERN.findall(text)
|
||||
if "".join(tokens) != text:
|
||||
# The expression is intentionally exhaustive, but keep a lossless
|
||||
# standard-library-only fallback for unusual Unicode input.
|
||||
return list(text)
|
||||
return tokens
|
||||
|
||||
|
||||
class FixedAnswerChatModel(BaseChatModel):
|
||||
"""A local streaming model that only replays an administrator answer."""
|
||||
|
||||
@@ -82,7 +107,7 @@ class FixedAnswerChatModel(BaseChatModel):
|
||||
if not answer:
|
||||
yield ChatGenerationChunk(message=AIMessageChunk(content="", id=self.message_id))
|
||||
return
|
||||
token_ids = _TOKEN_ENCODING.encode(answer, disallowed_special=())
|
||||
tokens = _split_local_tokens(answer)
|
||||
tokens_per_second = max(
|
||||
1,
|
||||
min(_MAX_TOKENS_PER_SECOND, self.tokens_per_second),
|
||||
@@ -94,14 +119,14 @@ class FixedAnswerChatModel(BaseChatModel):
|
||||
math.ceil(tokens_per_second / _TARGET_CHUNKS_PER_SECOND),
|
||||
),
|
||||
)
|
||||
decoder = codecs.getincrementaldecoder("utf-8")()
|
||||
token_groups = [token_ids[:1], *(token_ids[offset : offset + chunk_size] for offset in range(1, len(token_ids), chunk_size))]
|
||||
for index, chunk_token_ids in enumerate(token_groups):
|
||||
token_groups = [
|
||||
tokens[:1],
|
||||
*(tokens[offset : offset + chunk_size] for offset in range(1, len(tokens), chunk_size)),
|
||||
]
|
||||
for index, chunk_tokens in enumerate(token_groups):
|
||||
if index > 0:
|
||||
await asyncio.sleep(len(chunk_token_ids) / tokens_per_second)
|
||||
chunk_bytes = b"".join(_TOKEN_ENCODING.decode_single_token_bytes(token_id) for token_id in chunk_token_ids)
|
||||
is_last = index == len(token_groups) - 1
|
||||
content = decoder.decode(chunk_bytes, final=is_last)
|
||||
await asyncio.sleep(len(chunk_tokens) / tokens_per_second)
|
||||
content = "".join(chunk_tokens)
|
||||
chunk_kwargs = (
|
||||
{
|
||||
"fixed_answer": True,
|
||||
|
||||
Reference in new issue
Block a user