zbparse/flask_app/general/通义千问long.py

190 lines
6.7 KiB
Python
Raw Normal View History

import json
2024-11-26 11:10:12 +08:00
from functools import wraps
from ratelimit import limits, sleep_and_retry
2024-11-08 15:44:29 +08:00
import random
2024-08-29 16:37:09 +08:00
import time
from pathlib import Path
from openai import OpenAI
import os
def upload_file(file_path):
"""
Uploads a file to DashScope and returns the file ID.
"""
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
file = client.files.create(file=Path(file_path), purpose="file-extract")
return file.id
2024-11-26 11:10:12 +08:00
@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
2024-08-29 16:37:09 +08:00
def qianwen_long(file_id, user_query):
print("call qianwen-long...")
"""
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",
2024-11-04 10:52:23 +08:00
# top_p=0.5,
temperature=0.5,
# response_format={"type":"json_object"},
messages=[
{
'role': 'system',
'content': f'fileid://{file_id}'
},
{
'role': 'user',
'content': user_query
}
],
stream=False
)
# Return the response content
2024-11-04 11:22:01 +08:00
# return completion.choices[0].message.content,completion.usage
2024-11-04 10:52:23 +08:00
return completion.choices[0].message.content
2024-11-26 11:10:12 +08:00
@shared_rate_limit
2024-11-08 15:58:49 +08:00
def qianwen_long_stream(file_id, user_query):
2024-11-20 15:44:05 +08:00
print("调用 qianwen-long stream...")
2024-11-08 15:58:49 +08:00
"""
使用之前上传的文件根据用户查询生成响应并实时显示流式输出
2024-11-08 15:58:49 +08:00
"""
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
# 生成基于文件ID的响应
2024-11-08 15:58:49 +08:00
completion = client.chat.completions.create(
model="qwen-long",
2024-11-20 15:44:05 +08:00
temperature=0.4,
2024-11-08 15:58:49 +08:00
messages=[
{
'role': 'system',
'content': f'fileid://{file_id}'
},
{
'role': 'user',
'content': user_query
}
],
stream=True # 启用流式响应
2024-11-08 15:58:49 +08:00
)
full_response = "" # 用于存储完整的响应内容
try:
for chunk in completion:
# 假设chunk是一个对象先将其转换为字典
if hasattr(chunk, 'to_dict'):
chunk_data = chunk.to_dict()
else:
# 如果没有to_dict方法尝试直接转换为JSON
chunk_data = json.loads(chunk.model_dump_json())
# 检查是否有'choices'字段
choices = chunk_data.get('choices', [])
if not choices:
continue # 如果没有choices跳过当前chunk
choice = choices[0] # 获取第一个choice
delta = choice.get('delta', {})
# 提取'content'字段
content = delta.get('content', '')
if content:
# print(content, end='', flush=True) # 实时打印内容
full_response += content
# 检查是否有结束条件
if choice.get('finish_reason'):
break
except KeyboardInterrupt:
print("\n中断流式响应。")
except Exception as e:
print(f"\n处理流式响应时出错: {e}")
return full_response # 返回完整的响应内容
2024-11-08 15:58:49 +08:00
2024-11-26 11:10:12 +08:00
@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
2024-08-29 16:37:09 +08:00
if __name__ == "__main__":
# Example file path - replace with your actual file path
file_path = r"C:\Users\Administrator\Desktop\货物标\output1\陕西省公安厅交通警察总队高速公路交通安全智能感知巡查系统项目 (1)_procurement.pdf"
2024-08-29 16:37:09 +08:00
file_id = upload_file(file_path)
#
# user_query1 = "该招标文件前附表中的项目名称是什么请以json格式返回给我"
# user_query2 = ("请提供文件中关于资格审查的具体内容和标准。")
# start_time=time.time()
# # First query
# print("starting qianwen-long...")
# result1 ,result2= qianwen_long(file_id, user_query1)
# print("First Query Result:", result1)
# print(type(result1))
# print(result2)
2024-08-29 16:37:09 +08:00
# # Second query
# print("starting qianwen-long...")
# result2 = qianwen_long(file_id, user_query2)
# print("Second Query Result:", result2)
# end_time=time.time()
# print("elapsed time:"+str(end_time-start_time))
user_query = """
请你根据该货物标中采购要求部分的内容请你给出"高速公路交通安全智能感知巡查系统软件"的技术参数或采购要求请以json格式返回结果键名为"高速公路交通安全智能感知巡查系统软件", 键值为一个列表列表中包含若干描述\"{}\"的技术参数(或采购要求)的字符串,需与原文完全一致,即若技术参数前存在序号也要保留,但你不可擅自增添或删减。以下为需要考虑的特殊情况:如果该货物没有相关采购要求或技术参数要求,键值为空列表。示例输出格式如下:
{{
"摄像机控制键盘": [
"1、支持串行 RS232/RS422 和 IP 混合控制,允许在一个控制器上使用 RS232/RS422/IP 控制单个系统中的摄像机;",
"2、支持 2 组 RS422 串口 VISCA 协议菊花链控制 2x7 台摄像机。"
]
}}
"""
response = qianwen_long_stream(file_id, user_query)
print("完整响应内容:")
print(response)