有关超出AI令牌数量限制的解决办法-冲刺记录

今天我完善了团队作业中的课堂内容分析模块,顺利运行了kimiapi还有出现的问题就是先前说的内容太多超出ai令牌的问题,我使用的方法是分段传给ai分析,然后把返回的结果拼接后转回,包括简单总结还有详细总结方面,但是对于思维导图的功能如果内容过度会导致浏览器渲染失败,内存不够的问题,此问题还待解决。
原代码

点击查看代码
async def generate_summary(text: str):
    response = await client.chat.completions.create(
        model=AI_CONFIG["model"],
        messages=[
            {"role": "system", "content": "请对以下内容进行总结:"},
            {"role": "user", "content": text}
        ],
        stream=True,
        max_tokens = 2000
    )

    async for chunk in response:
        if chunk.choices[0].delta.content is not None:
            yield chunk.choices[0].delta.content
这里没有对传递过来的text文件的内容大小进行限制当text文件过大时会导致令牌超出限制。 修改后的代码
点击查看代码
async def generate_summary(text: str):
    try:
        # 尝试自动获取模型对应的编码方式
        encoding = tiktoken.encoding_for_model(AI_CONFIG["model"])
    except KeyError:
        # 若自动获取失败,使用通用编码方式
        print(f"无法自动映射模型 {AI_CONFIG['model']} 到分词器,使用 cl100k_base 作为备用。")
        encoding = tiktoken.get_encoding("cl100k_base")
    max_tokens = 8000  # 预留一些令牌给系统消息等
    system_message = "请对以下内容进行总结:"
    # 计算系统消息的令牌数
    system_message_tokens = len(encoding.encode(system_message))
    available_tokens = max_tokens - system_message_tokens

    text_chunks = []
    current_chunk = ""
    current_chunk_tokens = 0

    for line in text.split('\n'):
        line_tokens = len(encoding.encode(line))
        if current_chunk_tokens + line_tokens > available_tokens:
            text_chunks.append(current_chunk)
            current_chunk = ""
            current_chunk_tokens = 0
        current_chunk += line + '\n'
        current_chunk_tokens += line_tokens

    if current_chunk:
        text_chunks.append(current_chunk)

    all_summaries = []
    for chunk in text_chunks:
        try:
            response = await client.chat.completions.create(
                model=AI_CONFIG["model"],
                messages=[
                    {"role": "system", "content": system_message},
                    {"role": "user", "content": chunk}
                ],
                stream=True,
                max_tokens = 2000
            )
            chunk_summary = ""
            async for chunk in response:
                if chunk.choices[0].delta.content is not None:
                    chunk_content = chunk.choices[0].delta.content
                    chunk_summary += chunk_content
                    yield chunk_content
            all_summaries.append(chunk_summary)
        except Exception as e:
            print(f"生成摘要时出错: {e}")
            # 可以选择继续处理其他块,或者抛出异常
            continue

    # 合并所有摘要
    final_summary = " ".join(all_summaries)
    # 流式返回合并后的摘要
    for char in final_summary:
        yield char
经过每8000一次分割的text内容,可以被ai正确识别,并且在最后流式返回合并后的摘要。
posted @ 2025-05-14 23:37  吴少奇  阅读(35)  评论(0)    收藏  举报