11.26 函数级别限制

This commit is contained in:
zy123 2024-11-26 11:10:12 +08:00
parent 7e71a0aadd
commit 907f225049
8 changed files with 59 additions and 47 deletions

View File

@ -9,7 +9,7 @@ services:
- .env # 使用 .env 文件中的环境变量 - .env # 使用 .env 文件中的环境变量
volumes: volumes:
# - .:/flask_project # 将当前目录挂载到容器的 /flask_project 目录(可选,便于开发时实时更新代码) # - .:/flask_project # 将当前目录挂载到容器的 /flask_project 目录(可选,便于开发时实时更新代码)
- /home/Z/zbparse_output:/flask_project/flask_app/static/output # 额外的数据卷挂载 - /home/Z/zbparse_output_dev:/flask_project/flask_app/static/output # 额外的数据卷挂载
restart: unless-stopped # 容器退出时自动重启,除非明确停止 restart: unless-stopped # 容器退出时自动重启,除非明确停止
privileged: true privileged: true

View File

@ -12,7 +12,6 @@ import requests
from dashscope import Assistants, Messages, Runs, Threads from dashscope import Assistants, Messages, Runs, Threads
from llama_index.indices.managed.dashscope import DashScopeCloudRetriever from llama_index.indices.managed.dashscope import DashScopeCloudRetriever
from flask_app.general.通义千问long import qianwen_long, upload_file from flask_app.general.通义千问long import qianwen_long, upload_file
prompt = """ prompt = """
# 角色 # 角色
你是一个文档处理专家专门负责理解和操作基于特定内容的文档任务这包括解析总结搜索或生成与给定文档相关的各类信息 你是一个文档处理专家专门负责理解和操作基于特定内容的文档任务这包括解析总结搜索或生成与给定文档相关的各类信息
@ -268,7 +267,7 @@ def multi_threading(queries, knowledge_name="", file_id="", llm_type=1):
with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor: with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
future_to_query = {} future_to_query = {}
for index, query in enumerate(queries): for index, query in enumerate(queries):
time.sleep(0.7) # 每提交一个任务后等待0.5秒 # time.sleep(0.5) # 每提交一个任务后等待0.5秒目前设置了直接对qianwen-long直接限制无需sleep
future = executor.submit(llm_call, query, knowledge_name, file_id, result_queue, index, llm_type) future = executor.submit(llm_call, query, knowledge_name, file_id, result_queue, index, llm_type)
future_to_query[future] = index future_to_query[future] = index
retry_counts[index] = 0 # 初始化重试次数 retry_counts[index] = 0 # 初始化重试次数

View File

@ -5,8 +5,6 @@ import time
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from flask_app.general.doubao import doubao_model, generate_full_user_query from flask_app.general.doubao import doubao_model, generate_full_user_query
from docx import Document from docx import Document
from flask_app.general.通义千问long import upload_file, qianwen_long_text
from flask_app.general.通用功能函数 import process_string_list from flask_app.general.通用功能函数 import process_string_list

View File

@ -1,4 +1,7 @@
import json import json
from functools import wraps
from ratelimit import limits, sleep_and_retry
import random import random
import time import time
from pathlib import Path from pathlib import Path
@ -16,6 +19,19 @@ def upload_file(file_path):
file = client.files.create(file=Path(file_path), purpose="file-extract") file = client.files.create(file=Path(file_path), purpose="file-extract")
return file.id return file.id
@sleep_and_retry
@limits(calls=4, period=1) # 每秒最多调用4次
def rate_limiter():
pass # 这个函数本身不执行任何操作,只用于限流
# 创建一个共享的装饰器
def shared_rate_limit(func):
@wraps(func)
def wrapper(*args, **kwargs):
rate_limiter() # 通过共享的限流器
return func(*args, **kwargs)
return wrapper
@shared_rate_limit
def qianwen_long(file_id, user_query): def qianwen_long(file_id, user_query):
print("call qianwen-long...") print("call qianwen-long...")
""" """
@ -48,38 +64,7 @@ def qianwen_long(file_id, user_query):
# Return the response content # Return the response content
# return completion.choices[0].message.content,completion.usage # return completion.choices[0].message.content,completion.usage
return completion.choices[0].message.content return completion.choices[0].message.content
@shared_rate_limit
def qianwen_long_text(file_id, user_query):
print("call qianwen-long text...")
"""
Uses a previously uploaded file to generate a response based on a user query.
"""
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
# Generate a response based on the file ID
completion = client.chat.completions.create(
model="qwen-long",
# top_p=0.5,
temperature=0.5,
messages=[
{
'role': 'system',
'content': f'fileid://{file_id}'
},
{
'role': 'user',
'content': user_query
}
],
stream=False
)
# Return the response content
return completion.choices[0].message.content
def qianwen_long_stream(file_id, user_query): def qianwen_long_stream(file_id, user_query):
print("调用 qianwen-long stream...") print("调用 qianwen-long stream...")
""" """
@ -135,10 +120,39 @@ def qianwen_long_stream(file_id, user_query):
print("\n中断流式响应。") print("\n中断流式响应。")
except Exception as e: except Exception as e:
print(f"\n处理流式响应时出错: {e}") print(f"\n处理流式响应时出错: {e}")
print() # 换行
return full_response # 返回完整的响应内容 return full_response # 返回完整的响应内容
@shared_rate_limit
def qianwen_long_text(file_id, user_query):
print("call qianwen-long text...")
"""
Uses a previously uploaded file to generate a response based on a user query.
"""
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
# Generate a response based on the file ID
completion = client.chat.completions.create(
model="qwen-long",
# top_p=0.5,
temperature=0.5,
messages=[
{
'role': 'system',
'content': f'fileid://{file_id}'
},
{
'role': 'user',
'content': user_query
}
],
stream=False
)
# Return the response content
return completion.choices[0].message.content
if __name__ == "__main__": if __name__ == "__main__":
# Example file path - replace with your actual file path # Example file path - replace with your actual file path

View File

@ -4,7 +4,7 @@ import re
from PyPDF2 import PdfWriter, PdfReader from PyPDF2 import PdfWriter, PdfReader
from flask_app.general.通义千问long import upload_file, qianwen_long, qianwen_long_text from flask_app.general.通义千问long import upload_file, qianwen_long
from flask_app.general.通用功能函数 import process_string_list from flask_app.general.通用功能函数 import process_string_list
@ -144,7 +144,7 @@ def find_forbidden(qualification=""):
"该招标文件规定的投标人不得存在的其他情形有哪些,请以列表给我提供信息,形如[xx,xx,...]," "该招标文件规定的投标人不得存在的其他情形有哪些,请以列表给我提供信息,形如[xx,xx,...],"
"请你不要回答有关\"信誉要求\"的内容,若原文未提及,返回[]。" "请你不要回答有关\"信誉要求\"的内容,若原文未提及,返回[]。"
) )
qianwen_forbidden_str = qianwen_long_text(file_id, user_query_forbidden) qianwen_forbidden_str = qianwen_long(file_id, user_query_forbidden)
else: else:
qianwen_forbidden_str = "[]" qianwen_forbidden_str = "[]"

View File

@ -5,7 +5,7 @@ import time
import re import re
from flask_app.general.format_change import pdf2docx from flask_app.general.format_change import pdf2docx
from flask_app.general.通义千问long import upload_file, qianwen_long_text from flask_app.general.通义千问long import upload_file, qianwen_long
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from flask_app.general.table_content_extraction import extract_tables_main from flask_app.general.table_content_extraction import extract_tables_main
@ -345,7 +345,7 @@ def handle_query(file_path, user_query, output_file, result_key, keywords, trunc
file_id = upload_file(output_file) file_id = upload_file(output_file)
# qianwen_ans = qianwen_long(file_id, user_query) # qianwen_ans = qianwen_long(file_id, user_query)
qianwen_ans = qianwen_long_text(file_id, user_query) qianwen_ans = qianwen_long(file_id, user_query)
num_list = process_string_list(qianwen_ans) num_list = process_string_list(qianwen_ans)
print(result_key + "选中的序号:" + str(num_list)) print(result_key + "选中的序号:" + str(num_list))

View File

@ -3,7 +3,7 @@ import json
import re import re
import time import time
from collections import defaultdict from collections import defaultdict
from flask_app.general.通义千问long import upload_file, qianwen_long, qianwen_long_text from flask_app.general.通义千问long import upload_file, qianwen_long
def combine_technical_and_business(data, target_values): def combine_technical_and_business(data, target_values):
@ -211,7 +211,7 @@ def combine_evaluation_standards(truncate_file):
) # 应对竞争性谈判这种无评分要求的情况 ) # 应对竞争性谈判这种无评分要求的情况
# 执行查询 # 执行查询
judge_res = qianwen_long_text(file_id, user_query1) judge_res = qianwen_long(file_id, user_query1)
# 默认 judge 为 True # 默认 judge 为 True
judge = True judge = True
@ -268,7 +268,7 @@ def combine_evaluation_standards(truncate_file):
""" """
) )
# 执行第二个查询 # 执行第二个查询
evaluation_res = qianwen_long_text(file_id, user_query) #有些重复的键名只有qianwen_long_text能保留 evaluation_res = qianwen_long(file_id, user_query) #有些重复的键名只有qianwen_long_text能保留
# print(evaluation_res) # print(evaluation_res)
# 清理和处理响应 # 清理和处理响应
cleaned_evaluation_res = parse_json_with_duplicates(evaluation_res) #处理重复键名的情况 cleaned_evaluation_res = parse_json_with_duplicates(evaluation_res) #处理重复键名的情况

View File

@ -14,4 +14,5 @@ dashscope==1.19.2
PyMuPDF==1.24.1 PyMuPDF==1.24.1
openai==1.33.0 openai==1.33.0
pathlib==1.0.1 pathlib==1.0.1
alibabacloud_bailian20231229==1.7.0 alibabacloud_bailian20231229==1.7.0
ratelimit==2.2.1