'commit'
This commit is contained in:
@@ -9,6 +9,10 @@ from Config.Config import ZHIPU_API_KEY
|
||||
# 设置日志
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 添加历史记录存储变量
|
||||
history_records = []
|
||||
MAX_HISTORY = 10
|
||||
|
||||
def create_llm_client() -> AsyncOpenAI:
|
||||
"""初始化并返回异步OpenAI客户端"""
|
||||
return AsyncOpenAI(
|
||||
@@ -120,33 +124,38 @@ async def getGgbCommand(QvqResult):
|
||||
|
||||
|
||||
async def translate_to_ggb(natural_language_cmd):
|
||||
"""将自然语言指令翻译成GeoGebra指令集(异步版本)"""
|
||||
# 精简的prompt,专注于单句指令翻译
|
||||
custom_prompt = f"""
|
||||
请将以下自然语言指令翻译为GeoGebra指令集:
|
||||
"{natural_language_cmd}"
|
||||
global history_records
|
||||
|
||||
要求:
|
||||
1. 仅返回GeoGebra指令,不包含任何解释性文字
|
||||
2. 指令必须可直接在GeoGebra中执行
|
||||
3. 保持指令简洁明确
|
||||
4. 以```geogebra 开头,以 ```结束 【注意】```geogebra 前面一定要加上 \n 换行!
|
||||
5. 用户提到的元素,比如点A,线段AB等,都视为已存在,不要重新创建
|
||||
# 构建包含历史记录的提示
|
||||
history_prompt = """
|
||||
以下是最近的对话历史,供你参考:
|
||||
"""
|
||||
|
||||
# 添加最近的历史记录(最多10条)
|
||||
for i, (user_cmd, ggb_cmd) in enumerate(reversed(history_records[-MAX_HISTORY:])):
|
||||
history_prompt += f"用户命令 {len(history_records)-i}: {user_cmd}\nGeoGebra指令 {len(history_records)-i}: {ggb_cmd}\n\n"
|
||||
|
||||
custom_prompt = f"{history_prompt}
|
||||
请为下面的要求生成GeoGebra指令集:
|
||||
\"{natural_language_cmd}\"
|
||||
【注意】:
|
||||
1. 指令必须可直接在GeoGebra中执行,需要返回:一次性完整指令集。
|
||||
2. 以```geogebra 开头,以 ```结束 【注意】```geogebra 前面一定要加上 \n 换行!
|
||||
"
|
||||
|
||||
try:
|
||||
# 创建异步OpenAI客户端
|
||||
client = create_llm_client()
|
||||
|
||||
# 使用异步流式调用
|
||||
completion = await client.chat.completions.create(
|
||||
#model="deepseek-r1-0528",
|
||||
model="Moonshot-Kimi-K2-Instruct",
|
||||
model="Moonshot-Kimi-K2-Instruct",# 使用Kimi K2-Instruct模型
|
||||
messages=[{"role": "user", "content": custom_prompt}],
|
||||
stream=True
|
||||
)
|
||||
|
||||
is_answering = False
|
||||
full_response = ""
|
||||
async for chunk in completion:
|
||||
delta = chunk.choices[0].delta
|
||||
# 打印思考过程
|
||||
@@ -158,35 +167,22 @@ async def translate_to_ggb(natural_language_cmd):
|
||||
print("\n" + "=" * 20 + "完整回复" + "=" * 20 + "\n")
|
||||
is_answering = True
|
||||
# 打印回复过程
|
||||
if delta.content:
|
||||
full_response += delta.content
|
||||
yield delta.content
|
||||
|
||||
# 保存本次对话到历史记录
|
||||
if full_response:
|
||||
history_records.append((natural_language_cmd, full_response))
|
||||
# 保持历史记录不超过最大数量
|
||||
if len(history_records) > MAX_HISTORY:
|
||||
history_records.pop(0)
|
||||
|
||||
except Exception as e:
|
||||
print(f"翻译指令错误: {str(e)}")
|
||||
yield f"错误: {str(e)}"
|
||||
|
||||
|
||||
# 示例用法
|
||||
if __name__ == '__main__':
|
||||
# file_name = "QvqResult_14.txt"
|
||||
# with open(file_name, 'r', encoding='utf-8') as file:
|
||||
# QvqResult = file.read() # 直接获取整个文件内容为字符串
|
||||
#
|
||||
# async def test():
|
||||
# async for content in getGgbCommand(QvqResult):
|
||||
# if content:
|
||||
# print(content, end="", flush=True)
|
||||
#
|
||||
#
|
||||
# asyncio.run(test())
|
||||
|
||||
|
||||
async def test_translate():
|
||||
async for content in translate_to_ggb("把A和B连接起来"):
|
||||
if content:
|
||||
print(content, end="", flush=True)
|
||||
|
||||
|
||||
# 运行测试
|
||||
asyncio.run(test_translate())
|
||||
|
||||
|
||||
|
||||
@@ -261,3 +257,14 @@ async def process_geometry_image(image_url: str):
|
||||
logger.error(f"流式处理图片时发生错误: {str(e)}")
|
||||
yield ""
|
||||
|
||||
|
||||
# 示例用法
|
||||
if __name__ == '__main__':
|
||||
async def test_translate():
|
||||
async for content in translate_to_ggb("把A和B连接起来"):
|
||||
if content:
|
||||
print(content, end="", flush=True)
|
||||
|
||||
|
||||
# 运行测试
|
||||
asyncio.run(test_translate())
|
Reference in New Issue
Block a user