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, temperature=0.4, 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 file_path = "C:\\Users\\Administrator\\Desktop\\货物标\\output4\\招标文件111_tobidders_notice_part1.docx" file_id = upload_file(file_path) user_query1 = ("根据该文档中的投标人(供应商、磋商)须知前附表,请你保留原有层次关系,以json格式返回给我表格中的信息。") 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))