There’re multiple problem when I try to send json to my flask KGRAG llm server

Problem:

There are problems with the code chat_with_llm.py I think, that send the json in the wrong format or something, history and max_token which I’m not sure how to solve.
original code from here

qwen_server.py

This is my code for starting the flask server

import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
import json
from flask import Flask, request, jsonify

# Set CUDA device
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Model path configuration
model_path = "E:/.cache/Pretrain_models/qwen/Qwen-7B-Chat"

# Initialize tokenizer and model without manually assigning it to a device
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, load_in_8bit=True)
model.generation_config = GenerationConfig.from_pretrained(model_path)

def predict_model(data):
    """
    Generates a response based on the input data.
    """
    text = data["message"][0]["content"]
    inputs = tokenizer(text, return_tensors='pt').to(device)
    outputs = model.generate(**inputs, max_new_tokens=data["max_tokens"], top_k=data["top_k"], top_p=data["top_p"], 
                             temperature=data["temperature"], repetition_penalty=data["repetition_penalty"], 
                             num_beams=data["num_beams"])
    response = tokenizer.decode(outputs[0][len(inputs["input_ids"][0]):], skip_special_tokens=True)
    return response

app = Flask(__name__)

@app.route("/generate", methods=["POST", "GET"])
def generate():
    """
    Flask endpoint to generate model responses.
    """
    try:
        data = json.loads(request.data)
        print(data)
        res = predict_model(data)
        label = "success"
    except Exception as e:
        res = ""
        label = "error"
        print(e)
    return jsonify({"output": [res], "status": label})
   
if __name__ == '__main__':
    app.run(port=3001, debug=False, host='0.0.0.0')  # Allows external network access.

Terminal:

when I asked “感冒是什么” in the chat_with_llm.py terminal, it respond this

Expecting value: line 1 column 1 (char 0)
127.0.0.1 - - [19/May/2024 16:15:30] "GET /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': '你叫啥'}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:15:45] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': "请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。"}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:22:18] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': "请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。"}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:22:18] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': "请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。"}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:22:18] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': "请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。"}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:22:18] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': "请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。"}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:22:18] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': "请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。"}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:22:18] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': "请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。"}], 'history': []}
'max_tokens'
127.0.0.1 - - [19/May/2024 16:22:18] "POST /generate HTTP/1.1" 200 -
{'message': [{'role': 'user', 'content': "请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。"}], 'history': []}

chat_with_llm.py

Stackoverflow won’t let me send the code, it said it might be spam, idk why.
I think the problem occur at here
enter image description here

Terminal:

It’s the terminal where I enter my question

model init finished ......
USER INPUT:感冒是什么
step1: linking entity.....
step2:recall kg facts....
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
["请判定问题:感冒是什么所提及的是感冒的哪几个信息,请从['预防措施', '治疗方式', '名称', '治疗周期', '治愈概率', '疾病病因', '治疗科室', '疾病简介', '易感人群', '推荐食谱', '忌吃', '宜吃', '常用药品', '生产药品', '好评药品', '诊断检查', '症状', '并发症', '所属科室']中进行选择,并以列表形式返回。", '', set()]
MATCH p=(m:Disease)-[r*..1]-(n) where m.name = '感冒' return p
0 []
step3:generate answer...
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
<Response [200]>
request error 'history'
KGRAG_BOT OUTPUT:

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật