import json
import random
from http import HTTPStatus
from dashscope import Generation

def call_with_messages(messages):
    response = Generation.call(model="qwen-max",
                               messages=messages,
                               seed=random.randint(1, 10000),
                               temperature=0.5,
                               top_p=0.5,
                               top_k=50,
                               result_format='message')
    if response.status_code == HTTPStatus.OK:
        content = response.output['choices'][0]['message']['content']
        return content
    else:
        raise Exception(f'Request id: {response.request_id}, Status code: {response.status_code}, error code: {response.code}, error message: {response.message}')

def prepare_question_from_json(json_path, user_query):
    with open(json_path, 'r', encoding='utf-8') as file:
        json_data = json.load(file)
    question = json.dumps(json_data, ensure_ascii=False) + user_query
    return question

#专用于判断是否
def qianwen_ask(json_path, user_query):
    messages = []
    question = prepare_question_from_json(json_path, user_query)
    messages.append({'role': 'system', 'content': 'You are a helpful assistant.'})
    messages.append({'role': 'user', 'content': question})
    return call_with_messages(messages)

#通用问题
def qianwen_ask2(questions):
    messages = []
    messages.append({'role': 'system', 'content': 'You are a helpful assistant.'})
    messages.append({'role': 'user', 'content': questions})
    return call_with_messages(messages)

if __name__ == '__main__':
    json_path = 'judge_exist.json'
    prompt = "请你依据以上信息回答，是否组织踏勘现场？是否召开投标预备会？是否允许偏离？是否退还投标文件？是否允许分包? 是否需要递交投标保证金？是否有履约保证金（履约担保）？是否有招标代理服务费？请按json格式给我提供信息，键名分别为'是否组织踏勘现场','是否召开投标预备会','是否允许偏离','是否退还投标文件',是否允许分包','是否需要递交投标保证金','是否有履约保证金','是否有招标代理服务费',键值仅限于'是','否','未知',若存在未知或者矛盾信息，请回答'未知'。"
    try:
        content = qianwen_ask(json_path, prompt)
        print(content)
    except Exception as e:
        print(f"An error occurred: {e}")
