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

68 lines
2.1 KiB
Python
Raw Normal View History

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
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",
top_p=0.5,
2024-10-15 20:57:58 +08:00
temperature=0.4,
2024-08-29 16:37:09 +08:00
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__":
# Example file path - replace with your actual file path
2024-10-10 21:03:02 +08:00
file_path = "C:\\Users\\Administrator\\Desktop\\货物标\\output4\\招标文件111_tobidders_notice_part1.docx"
2024-08-29 16:37:09 +08:00
file_id = upload_file(file_path)
2024-10-10 21:03:02 +08:00
user_query1 = ("根据该文档中的投标人(供应商、磋商)须知前附表请你保留原有层次关系以json格式返回给我表格中的信息。")
2024-08-29 16:37:09 +08:00
user_query2 = ("请提供文件中关于资格审查的具体内容和标准。")
start_time=time.time()
# First query
print("starting qianwen-long...")
result1 = qianwen_long(file_id, user_query1)
print("First Query Result:", result1)
# # 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))