大模型计算首字吐出时间

代码计算的时候稍微有一点偏差其它人有没有更好的解决办法

#coding:utf-8
import openpyxl
import requests
from loguru import logger
import json
import time
#读取excel文件的路径
filepath = "F:/chery_gpt_problem/query.xlsx"
#请求的url地址
url = "https://www.cherygpt.com/console/api/installed-apps/8f2030de-48e7-417c-a9b4-6c1f146bdaac/chat-messages"
#请求的消息头
headers={}
headers["Authorization"] = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiYTE3MzNjMjMtODAzZC00MDkyLWE4YjItZjQxN2MwNDc4YzUwIiwiZXhwIjoxNzI2MDM3OTc2LCJpc3MiOiJTRUxGX0hPU1RFRCIsInN1YiI6IkNvbnNvbGUgQVBJIFBhc3Nwb3J0In0.S93EcKy8CmNguwqGg2d6VWvNZN8jV7mC6e77eqB0S3g"
headers["Content-Type"] = "application/json"
write_workbook = openpyxl.Workbook()
sheet_write = None
#写入excel文件的路径
result_file_path = "F:/chery_gpt_problem/result.xlsx"
def read_excel():
    #打开excel文件
    workbook = openpyxl.load_workbook(filepath)

    names = workbook.sheetnames #获取表格里面的工作薄
    print(names)
    for i in range(0,len(names)):
        sheet_object = workbook[names[i]]
        print(sheet_object.max_row)
        for j in range(2,sheet_object.max_row+1):
            cell_value = str(sheet_object.cell(j, 1).value)
            print(cell_value)
            message_send(cell_value,j)
def message_send(cell_value,j):
    json_body = {}
    json_body["response_mode"] = "streaming"
    json_body["query"] = cell_value
    json_body["inputs"] = {}
    contentStr = ""
    stream_time = 0  #计算是否是首开的字母
    time_diff = 0    #时间差的值
    start_time = time.time()  # 开始发送请求时间
    responseResult = requests.post(url,headers=headers, json=json_body,stream=True)
    for line in responseResult.iter_lines():
        if line and len(line) > 15:
            try:
                data = line.decode('utf-8')
                print(data)
                responseResultJson = json.loads(str(data).replace("data:",""))
                event = responseResultJson["event"]
                if event == "message":
                    if (stream_time == 0):
                        end_time = time.time()  # 结束发送请求时间
                        time_diff = (end_time - start_time) * 1000
                        print("时间差值:" + str(time_diff))
                    contentStr = contentStr + responseResultJson["answer"]
                    stream_time = 1
                    logger.info(contentStr)
                    write_excel_data(cell_value, j, contentStr, time_diff)
                    return
                # if event == "message_end":
                #     # contentStr = contentStr + responseResultJson["answer"]
                #     logger.info(contentStr)
                #     write_excel_data(cell_value,j,contentStr,time_diff)
                #     return
            except json.JSONDecodeError:
                print("无效的json数据:",data)

def write_excel_ini():
    global sheet_write
    sheet_write = write_workbook.create_sheet("Sheet1", 0)
    sheet_write.cell(row=1, column=1).value = '发送query'  # 发送的query问题
    sheet_write.cell(row=1, column=2).value = '大模型返回第一个数据块时间'  # 返回第一个数据块时间
    sheet_write.cell(row=1, column=3).value = '大模型返回第一个数据块结果'  # 返回结果
    write_workbook.save(result_file_path)
def write_excel_data(cell_value,j,contentStr,time_diff):
    sheet_write.cell(row=j, column=1).value = cell_value  # 发送给大模型的内容
    sheet_write.cell(row=j, column=2).value = str(time_diff) # 上行的msgId
    sheet_write.cell(row=j, column=3).value = contentStr
    write_workbook.save(result_file_path)
if __name__ == '__main__':
    write_excel_ini()
    read_excel()

你可能感兴趣的:(python)