Translate comment to English
This commit is contained in:
@@ -27,15 +27,15 @@ from dotenv import load_dotenv
|
|||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
def estimate_tokens(text: str) -> int:
|
def estimate_tokens(text: str) -> int:
|
||||||
"""估算文本的token数量
|
"""Estimate the number of tokens in text
|
||||||
中文每字约1.5个token
|
Chinese characters: approximately 1.5 tokens per character
|
||||||
英文每字约0.25个token
|
English characters: approximately 0.25 tokens per character
|
||||||
"""
|
"""
|
||||||
# 使用正则表达式分别匹配中文字符和非中文字符
|
# Use regex to match Chinese and non-Chinese characters separately
|
||||||
chinese_chars = len(re.findall(r'[\u4e00-\u9fff]', text))
|
chinese_chars = len(re.findall(r'[\u4e00-\u9fff]', text))
|
||||||
non_chinese_chars = len(re.findall(r'[^\u4e00-\u9fff]', text))
|
non_chinese_chars = len(re.findall(r'[^\u4e00-\u9fff]', text))
|
||||||
|
|
||||||
# 计算估算的token数量
|
# Calculate estimated token count
|
||||||
tokens = chinese_chars * 1.5 + non_chinese_chars * 0.25
|
tokens = chinese_chars * 1.5 + non_chinese_chars * 0.25
|
||||||
|
|
||||||
return int(tokens)
|
return int(tokens)
|
||||||
@@ -241,7 +241,7 @@ class DocumentManager:
|
|||||||
class SearchMode(str, Enum):
|
class SearchMode(str, Enum):
|
||||||
naive = "naive"
|
naive = "naive"
|
||||||
local = "local"
|
local = "local"
|
||||||
global_ = "global" # 使用 global_ 因为 global 是 Python 保留关键字,但枚举值会转换为字符串 "global"
|
global_ = "global" # Using global_ because global is a Python reserved keyword, but enum value will be converted to string "global"
|
||||||
hybrid = "hybrid"
|
hybrid = "hybrid"
|
||||||
mix = "mix"
|
mix = "mix"
|
||||||
|
|
||||||
@@ -254,7 +254,7 @@ class OllamaMessage(BaseModel):
|
|||||||
class OllamaChatRequest(BaseModel):
|
class OllamaChatRequest(BaseModel):
|
||||||
model: str = LIGHTRAG_MODEL
|
model: str = LIGHTRAG_MODEL
|
||||||
messages: List[OllamaMessage]
|
messages: List[OllamaMessage]
|
||||||
stream: bool = True # 默认为流式模式
|
stream: bool = True # Default to streaming mode
|
||||||
options: Optional[Dict[str, Any]] = None
|
options: Optional[Dict[str, Any]] = None
|
||||||
|
|
||||||
class OllamaChatResponse(BaseModel):
|
class OllamaChatResponse(BaseModel):
|
||||||
@@ -490,11 +490,11 @@ def create_app(args):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# 如果响应是字符串(比如命中缓存),直接返回
|
# If response is a string (e.g. cache hit), return directly
|
||||||
if isinstance(response, str):
|
if isinstance(response, str):
|
||||||
return QueryResponse(response=response)
|
return QueryResponse(response=response)
|
||||||
|
|
||||||
# 如果是异步生成器,根据stream参数决定是否流式返回
|
# If it's an async generator, decide whether to stream based on stream parameter
|
||||||
if request.stream:
|
if request.stream:
|
||||||
result = ""
|
result = ""
|
||||||
async for chunk in response:
|
async for chunk in response:
|
||||||
@@ -511,7 +511,7 @@ def create_app(args):
|
|||||||
@app.post("/query/stream", dependencies=[Depends(optional_api_key)])
|
@app.post("/query/stream", dependencies=[Depends(optional_api_key)])
|
||||||
async def query_text_stream(request: QueryRequest):
|
async def query_text_stream(request: QueryRequest):
|
||||||
try:
|
try:
|
||||||
response = await rag.aquery( # 使用 aquery 而不是 query,并添加 await
|
response = await rag.aquery( # Use aquery instead of query, and add await
|
||||||
request.query,
|
request.query,
|
||||||
param=QueryParam(
|
param=QueryParam(
|
||||||
mode=request.mode,
|
mode=request.mode,
|
||||||
@@ -691,7 +691,7 @@ def create_app(args):
|
|||||||
|
|
||||||
for prefix, mode in mode_map.items():
|
for prefix, mode in mode_map.items():
|
||||||
if query.startswith(prefix):
|
if query.startswith(prefix):
|
||||||
# 移除前缀后,清理开头的额外空格
|
# After removing prefix an leading spaces
|
||||||
cleaned_query = query[len(prefix):].lstrip()
|
cleaned_query = query[len(prefix):].lstrip()
|
||||||
return cleaned_query, mode
|
return cleaned_query, mode
|
||||||
|
|
||||||
@@ -699,17 +699,14 @@ def create_app(args):
|
|||||||
|
|
||||||
@app.post("/api/chat")
|
@app.post("/api/chat")
|
||||||
async def chat(raw_request: Request, request: OllamaChatRequest):
|
async def chat(raw_request: Request, request: OllamaChatRequest):
|
||||||
# # 打印原始请求数据
|
|
||||||
# body = await raw_request.body()
|
|
||||||
# logging.info(f"收到 /api/chat 原始请求: {body.decode('utf-8')}")
|
|
||||||
"""Handle chat completion requests"""
|
"""Handle chat completion requests"""
|
||||||
try:
|
try:
|
||||||
# 获取所有消息内容
|
# Get all messages
|
||||||
messages = request.messages
|
messages = request.messages
|
||||||
if not messages:
|
if not messages:
|
||||||
raise HTTPException(status_code=400, detail="No messages provided")
|
raise HTTPException(status_code=400, detail="No messages provided")
|
||||||
|
|
||||||
# 获取最后一条消息作为查询
|
# Get the last message as query
|
||||||
query = messages[-1].content
|
query = messages[-1].content
|
||||||
|
|
||||||
# 解析查询模式
|
# 解析查询模式
|
||||||
@@ -723,7 +720,7 @@ def create_app(args):
|
|||||||
|
|
||||||
# 调用RAG进行查询
|
# 调用RAG进行查询
|
||||||
query_param = QueryParam(
|
query_param = QueryParam(
|
||||||
mode=mode, # 使用解析出的模式,如果没有前缀则为默认的 hybrid
|
mode=mode,
|
||||||
stream=request.stream,
|
stream=request.stream,
|
||||||
only_need_context=False
|
only_need_context=False
|
||||||
)
|
)
|
||||||
@@ -731,7 +728,7 @@ def create_app(args):
|
|||||||
if request.stream:
|
if request.stream:
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
|
|
||||||
response = await rag.aquery( # 需要 await 来获取异步生成器
|
response = await rag.aquery( # Need await to get async generator
|
||||||
cleaned_query,
|
cleaned_query,
|
||||||
param=query_param
|
param=query_param
|
||||||
)
|
)
|
||||||
@@ -742,9 +739,9 @@ def create_app(args):
|
|||||||
last_chunk_time = None
|
last_chunk_time = None
|
||||||
total_response = ""
|
total_response = ""
|
||||||
|
|
||||||
# 确保 response 是异步生成器
|
# Ensure response is an async generator
|
||||||
if isinstance(response, str):
|
if isinstance(response, str):
|
||||||
# 如果是字符串,分两次发送
|
# If it's a string, send in two parts
|
||||||
first_chunk_time = time.time_ns()
|
first_chunk_time = time.time_ns()
|
||||||
last_chunk_time = first_chunk_time
|
last_chunk_time = first_chunk_time
|
||||||
total_response = response
|
total_response = response
|
||||||
|
@@ -1,12 +1,12 @@
|
|||||||
"""
|
"""
|
||||||
LightRAG Ollama 兼容接口测试脚本
|
LightRAG Ollama Compatibility Interface Test Script
|
||||||
|
|
||||||
这个脚本测试 LightRAG 的 Ollama 兼容接口,包括:
|
This script tests the LightRAG's Ollama compatibility interface, including:
|
||||||
1. 基本功能测试(流式和非流式响应)
|
1. Basic functionality tests (streaming and non-streaming responses)
|
||||||
2. 查询模式测试(local、global、naive、hybrid)
|
2. Query mode tests (local, global, naive, hybrid)
|
||||||
3. 错误处理测试(包括流式和非流式场景)
|
3. Error handling tests (including streaming and non-streaming scenarios)
|
||||||
|
|
||||||
所有响应都使用 JSON Lines 格式,符合 Ollama API 规范。
|
All responses use the JSON Lines format, complying with the Ollama API specification.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -24,20 +24,10 @@ class OutputControl:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def set_verbose(cls, verbose: bool) -> None:
|
def set_verbose(cls, verbose: bool) -> None:
|
||||||
"""设置输出详细程度
|
|
||||||
|
|
||||||
Args:
|
|
||||||
verbose: True 为详细模式,False 为静默模式
|
|
||||||
"""
|
|
||||||
cls._verbose = verbose
|
cls._verbose = verbose
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_verbose(cls) -> bool:
|
def is_verbose(cls) -> bool:
|
||||||
"""获取当前输出模式
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
当前是否为详细模式
|
|
||||||
"""
|
|
||||||
return cls._verbose
|
return cls._verbose
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -50,7 +40,6 @@ class TestResult:
|
|||||||
timestamp: str = ""
|
timestamp: str = ""
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
"""初始化后设置时间戳"""
|
|
||||||
if not self.timestamp:
|
if not self.timestamp:
|
||||||
self.timestamp = datetime.now().isoformat()
|
self.timestamp = datetime.now().isoformat()
|
||||||
|
|
||||||
@@ -61,7 +50,6 @@ class TestStats:
|
|||||||
self.start_time = datetime.now()
|
self.start_time = datetime.now()
|
||||||
|
|
||||||
def add_result(self, result: TestResult):
|
def add_result(self, result: TestResult):
|
||||||
"""添加测试结果"""
|
|
||||||
self.results.append(result)
|
self.results.append(result)
|
||||||
|
|
||||||
def export_results(self, path: str = "test_results.json"):
|
def export_results(self, path: str = "test_results.json"):
|
||||||
@@ -87,7 +75,6 @@ class TestStats:
|
|||||||
print(f"\n测试结果已保存到: {path}")
|
print(f"\n测试结果已保存到: {path}")
|
||||||
|
|
||||||
def print_summary(self):
|
def print_summary(self):
|
||||||
"""打印测试统计摘要"""
|
|
||||||
total = len(self.results)
|
total = len(self.results)
|
||||||
passed = sum(1 for r in self.results if r.success)
|
passed = sum(1 for r in self.results if r.success)
|
||||||
failed = total - passed
|
failed = total - passed
|
||||||
@@ -132,7 +119,7 @@ def make_request(url: str, data: Dict[str, Any], stream: bool = False) -> reques
|
|||||||
stream: 是否使用流式响应
|
stream: 是否使用流式响应
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
requests.Response 对象
|
requests.Response: 对象
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
requests.exceptions.RequestException: 请求失败且重试次数用完
|
requests.exceptions.RequestException: 请求失败且重试次数用完
|
||||||
|
Reference in New Issue
Block a user