🐛 Bug: Fix the bug where the timestamp is a decimal.
Browse files- response.py +3 -3
- test/parse_markdown.py +0 -159
- test/{test_vertex copy.py → test_vertex.py} +0 -0
- test/translate_md.py +0 -50
response.py
CHANGED
|
@@ -48,7 +48,7 @@ async def check_response(response, error_log):
|
|
| 48 |
return None
|
| 49 |
|
| 50 |
async def fetch_gemini_response_stream(client, url, headers, payload, model):
|
| 51 |
-
timestamp = datetime.timestamp(datetime.now())
|
| 52 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
| 53 |
error_message = await check_response(response, "fetch_gemini_response_stream")
|
| 54 |
if error_message:
|
|
@@ -93,7 +93,7 @@ async def fetch_gemini_response_stream(client, url, headers, payload, model):
|
|
| 93 |
yield "data: [DONE]\n\r\n"
|
| 94 |
|
| 95 |
async def fetch_vertex_claude_response_stream(client, url, headers, payload, model):
|
| 96 |
-
timestamp = datetime.timestamp(datetime.now())
|
| 97 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
| 98 |
error_message = await check_response(response, "fetch_vertex_claude_response_stream")
|
| 99 |
if error_message:
|
|
@@ -156,7 +156,7 @@ async def fetch_gpt_response_stream(client, url, headers, payload, max_redirects
|
|
| 156 |
yield line.strip() + "\n\r\n"
|
| 157 |
|
| 158 |
async def fetch_claude_response_stream(client, url, headers, payload, model):
|
| 159 |
-
timestamp = datetime.timestamp(datetime.now())
|
| 160 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
| 161 |
error_message = await check_response(response, "fetch_claude_response_stream")
|
| 162 |
if error_message:
|
|
|
|
| 48 |
return None
|
| 49 |
|
| 50 |
async def fetch_gemini_response_stream(client, url, headers, payload, model):
|
| 51 |
+
timestamp = int(datetime.timestamp(datetime.now()))
|
| 52 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
| 53 |
error_message = await check_response(response, "fetch_gemini_response_stream")
|
| 54 |
if error_message:
|
|
|
|
| 93 |
yield "data: [DONE]\n\r\n"
|
| 94 |
|
| 95 |
async def fetch_vertex_claude_response_stream(client, url, headers, payload, model):
|
| 96 |
+
timestamp = int(datetime.timestamp(datetime.now()))
|
| 97 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
| 98 |
error_message = await check_response(response, "fetch_vertex_claude_response_stream")
|
| 99 |
if error_message:
|
|
|
|
| 156 |
yield line.strip() + "\n\r\n"
|
| 157 |
|
| 158 |
async def fetch_claude_response_stream(client, url, headers, payload, model):
|
| 159 |
+
timestamp = int(datetime.timestamp(datetime.now()))
|
| 160 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
| 161 |
error_message = await check_response(response, "fetch_claude_response_stream")
|
| 162 |
if error_message:
|
test/parse_markdown.py
DELETED
|
@@ -1,159 +0,0 @@
|
|
| 1 |
-
class MarkdownEntity:
|
| 2 |
-
def __init__(self, content: str, entity_type: str):
|
| 3 |
-
self.content = content
|
| 4 |
-
self.entity_type = entity_type
|
| 5 |
-
|
| 6 |
-
def __repr__(self):
|
| 7 |
-
return f'<{self.entity_type}: {self.content}>'
|
| 8 |
-
|
| 9 |
-
class Title(MarkdownEntity):
|
| 10 |
-
def __init__(self, content: str, level: int):
|
| 11 |
-
super().__init__(content, 'Title')
|
| 12 |
-
self.level = level
|
| 13 |
-
|
| 14 |
-
class CodeBlock(MarkdownEntity):
|
| 15 |
-
def __init__(self, content: str, language: str = 'python'):
|
| 16 |
-
super().__init__(content, 'CodeBlock')
|
| 17 |
-
self.language = language
|
| 18 |
-
|
| 19 |
-
class ListItem(MarkdownEntity):
|
| 20 |
-
def __init__(self, content: str):
|
| 21 |
-
super().__init__(content, 'ListItem')
|
| 22 |
-
|
| 23 |
-
class Link(MarkdownEntity):
|
| 24 |
-
def __init__(self, content: str, url: str):
|
| 25 |
-
super().__init__(content, 'Link')
|
| 26 |
-
self.url = url
|
| 27 |
-
|
| 28 |
-
class EmptyLine(MarkdownEntity):
|
| 29 |
-
def __init__(self, content: str):
|
| 30 |
-
super().__init__(content, 'EmptyLine')
|
| 31 |
-
|
| 32 |
-
class Paragraph(MarkdownEntity):
|
| 33 |
-
def __init__(self, content: str):
|
| 34 |
-
super().__init__(content, 'Paragraph')
|
| 35 |
-
|
| 36 |
-
def parse_markdown(lines, delimiter='\n\n'):
|
| 37 |
-
entities = []
|
| 38 |
-
current_code_block = []
|
| 39 |
-
in_code_block = False
|
| 40 |
-
language = None
|
| 41 |
-
|
| 42 |
-
for line in lines:
|
| 43 |
-
# line = line.strip()
|
| 44 |
-
|
| 45 |
-
if line.startswith('#'):
|
| 46 |
-
level = line.count('#')
|
| 47 |
-
title_content = line[level:].strip()
|
| 48 |
-
entities.append(Title(title_content, level))
|
| 49 |
-
|
| 50 |
-
elif line.startswith('```'):
|
| 51 |
-
if in_code_block and language:
|
| 52 |
-
entities.append(CodeBlock(''.join(current_code_block), language))
|
| 53 |
-
current_code_block = []
|
| 54 |
-
in_code_block = False
|
| 55 |
-
language = None
|
| 56 |
-
else:
|
| 57 |
-
in_code_block = True
|
| 58 |
-
language = line.lstrip('`').strip()
|
| 59 |
-
|
| 60 |
-
elif in_code_block:
|
| 61 |
-
current_code_block.append(line)
|
| 62 |
-
|
| 63 |
-
elif '[' in line and ']' in line and '(' in line and ')' in line and line.count('[') == 1:
|
| 64 |
-
start = line.index('[') + 1
|
| 65 |
-
end = line.index(']')
|
| 66 |
-
url_start = line.index('(') + 1
|
| 67 |
-
url_end = line.index(')')
|
| 68 |
-
link_text = line[start:end].strip()
|
| 69 |
-
link_url = line[url_start:url_end].strip()
|
| 70 |
-
entities.append(Link(link_text, link_url))
|
| 71 |
-
|
| 72 |
-
elif line == delimiter:
|
| 73 |
-
entities.append(EmptyLine(line))
|
| 74 |
-
|
| 75 |
-
elif line:
|
| 76 |
-
entities.append(Paragraph(line))
|
| 77 |
-
|
| 78 |
-
return entities
|
| 79 |
-
|
| 80 |
-
def convert_entities_to_text(entities):
|
| 81 |
-
result = []
|
| 82 |
-
for entity in entities:
|
| 83 |
-
if isinstance(entity, Title):
|
| 84 |
-
result.append(f"{'#' * entity.level} {entity.content}")
|
| 85 |
-
elif isinstance(entity, CodeBlock):
|
| 86 |
-
code = entity.content.lstrip('\n').rstrip('\n')
|
| 87 |
-
result.append(f"```{entity.language}\n{code}\n```")
|
| 88 |
-
elif isinstance(entity, ListItem):
|
| 89 |
-
result.append(f"- {entity.content}")
|
| 90 |
-
elif isinstance(entity, Link):
|
| 91 |
-
result.append(f"[{entity.content}]({entity.url})")
|
| 92 |
-
elif isinstance(entity, EmptyLine):
|
| 93 |
-
result.append(f"{entity.content}")
|
| 94 |
-
elif isinstance(entity, Paragraph):
|
| 95 |
-
result.append(f"{entity.content}")
|
| 96 |
-
return ''.join(result)
|
| 97 |
-
|
| 98 |
-
def save_text_to_file(text: str, file_path: str):
|
| 99 |
-
with open(file_path, 'w', encoding='utf-8') as file:
|
| 100 |
-
file.write(text)
|
| 101 |
-
|
| 102 |
-
def process_markdown_entities_and_save(entities, file_path, raw_text=None):
|
| 103 |
-
# Step 1: Convert entities to text
|
| 104 |
-
text_output = convert_entities_to_text(entities)
|
| 105 |
-
if raw_text and raw_text != text_output:
|
| 106 |
-
raise ValueError("The text output does not match the raw text input.")
|
| 107 |
-
# Step 2: Save to file
|
| 108 |
-
save_text_to_file(text_output, file_path)
|
| 109 |
-
|
| 110 |
-
def read_markdown_file(file_path):
|
| 111 |
-
with open(file_path, 'r', encoding='utf-8') as file:
|
| 112 |
-
return file.read()
|
| 113 |
-
|
| 114 |
-
def split_markdown(text, delimiter='\n\n'):
|
| 115 |
-
# 使用两个换行符作为分割标记,分割段落
|
| 116 |
-
# 创建一个新的列表来存储结果
|
| 117 |
-
paragraphs = text.split(delimiter)
|
| 118 |
-
result = []
|
| 119 |
-
|
| 120 |
-
# 遍历分割后的段落,在它们之间插入空行实体
|
| 121 |
-
for i, paragraph in enumerate(paragraphs):
|
| 122 |
-
if i > 0:
|
| 123 |
-
# 在非第一段之前插入空行实体
|
| 124 |
-
result.append(delimiter)
|
| 125 |
-
|
| 126 |
-
# 添加当前段落
|
| 127 |
-
result.append(paragraph)
|
| 128 |
-
|
| 129 |
-
return result
|
| 130 |
-
|
| 131 |
-
def get_entities_from_markdown_file(file_path, delimiter='\n\n'):
|
| 132 |
-
# 读取 Markdown 文件
|
| 133 |
-
markdown_text = read_markdown_file(file_path)
|
| 134 |
-
|
| 135 |
-
# 分割 Markdown 文档
|
| 136 |
-
paragraphs = split_markdown(markdown_text, delimiter=delimiter)
|
| 137 |
-
|
| 138 |
-
# 解析 Markdown 文档
|
| 139 |
-
return parse_markdown(paragraphs, delimiter=delimiter)
|
| 140 |
-
|
| 141 |
-
if __name__ == '__main__':
|
| 142 |
-
markdown_file_path = "README_CN.md" # 替换为你的 Markdown 文件路径
|
| 143 |
-
|
| 144 |
-
# 读取 Markdown 文件
|
| 145 |
-
delimiter = '\n'
|
| 146 |
-
markdown_text = read_markdown_file(markdown_file_path)
|
| 147 |
-
paragraphs = split_markdown(markdown_text, delimiter=delimiter)
|
| 148 |
-
parsed_entities = parse_markdown(paragraphs, delimiter=delimiter)
|
| 149 |
-
|
| 150 |
-
# # 显示解析结果
|
| 151 |
-
# result = [str(entity) for entity in parsed_entities]
|
| 152 |
-
# for idx, entity in enumerate(result):
|
| 153 |
-
# print(f"段落 {idx + 1} 解析:{entity}\n")
|
| 154 |
-
|
| 155 |
-
# 保存到文件
|
| 156 |
-
output_file_path = "output.md"
|
| 157 |
-
process_markdown_entities_and_save(parsed_entities, output_file_path, raw_text=markdown_text)
|
| 158 |
-
|
| 159 |
-
print(f"Markdown 文档已保存到 {output_file_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test/{test_vertex copy.py → test_vertex.py}
RENAMED
|
File without changes
|
test/translate_md.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from ModelMerge import chatgpt
|
| 3 |
-
from parse_markdown import get_entities_from_markdown_file, process_markdown_entities_and_save
|
| 4 |
-
|
| 5 |
-
def translate_text(text, agent):
|
| 6 |
-
result = agent.ask(text)
|
| 7 |
-
return result
|
| 8 |
-
|
| 9 |
-
def translate(input_file_path, output_file_path="output.md", language="English", api_key=None, api_url="https://api.openai.com/v1/chat/completions", engine="gpt-4o"):
|
| 10 |
-
if not api_key:
|
| 11 |
-
raise ValueError("API key is required for translation.")
|
| 12 |
-
translator_prompt = (
|
| 13 |
-
"You are a translation engine, you can only translate text and cannot interpret it, and do not explain. "
|
| 14 |
-
"Translate the text to {}, please do not explain any sentences, just translate or leave them as they are. "
|
| 15 |
-
"Retain all spaces and line breaks in the original text. "
|
| 16 |
-
"Please do not wrap the code in code blocks, I will handle it myself. "
|
| 17 |
-
"If the code has comments, you should translate the comments as well. "
|
| 18 |
-
"This is the content you need to translate: "
|
| 19 |
-
).format(language)
|
| 20 |
-
|
| 21 |
-
agent = chatgpt(
|
| 22 |
-
api_key=api_key,
|
| 23 |
-
api_url=api_url,
|
| 24 |
-
engine=engine,
|
| 25 |
-
system_prompt=translator_prompt,
|
| 26 |
-
use_plugins=False
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
# 读取 Markdown 文件
|
| 30 |
-
raw_paragraphs = get_entities_from_markdown_file(input_file_path, delimiter='\n')
|
| 31 |
-
target_paragraphs = raw_paragraphs
|
| 32 |
-
|
| 33 |
-
# 逐段翻译
|
| 34 |
-
for index, paragraph in enumerate(raw_paragraphs):
|
| 35 |
-
if paragraph.content and paragraph.content.strip() != "":
|
| 36 |
-
translated_text = translate_text(paragraph.content, agent)
|
| 37 |
-
if translated_text:
|
| 38 |
-
target_paragraphs[index].content = translated_text
|
| 39 |
-
|
| 40 |
-
# 输出翻译结果
|
| 41 |
-
process_markdown_entities_and_save(target_paragraphs, output_file_path)
|
| 42 |
-
|
| 43 |
-
if __name__ == "__main__":
|
| 44 |
-
input_file_path = "README_CN.md"
|
| 45 |
-
output_file_path = "README.md"
|
| 46 |
-
language = "English"
|
| 47 |
-
api_key = os.getenv("API")
|
| 48 |
-
api_url = os.getenv("API_URL")
|
| 49 |
-
engine = "gpt-4o"
|
| 50 |
-
translate(input_file_path, output_file_path, language, api_key, api_url, engine)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|