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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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,
|
2024-10-29 20:40:14 +08:00
|
|
|
|
temperature=0.5,
|
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
|
|
|
|
|
|
2024-11-08 15:58:49 +08:00
|
|
|
|
def qianwen_long_stream(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=True
|
|
|
|
|
)
|
|
|
|
|
# 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
|
|
|
|
|
|
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-11-04 10:52:23 +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...")
|
2024-11-04 10:52:23 +08:00
|
|
|
|
result1 ,result2= qianwen_long(file_id, user_query1)
|
2024-08-29 16:37:09 +08:00
|
|
|
|
print("First Query Result:", result1)
|
2024-11-04 10:52:23 +08:00
|
|
|
|
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))
|