LCA-LLM/LCA_RAG/GLMqa.py

49 lines
1.5 KiB
Python
Raw Normal View History

2024-12-29 17:33:02 +08:00
# glm
from zhipuai import ZhipuAI
import re
import pandas as pd
'''
经过换算需要135000左右的token
'''
# def zhipuQA(prompt):
# client = ZhipuAI(api_key="434790cf952335f18b6347e7b6de9777.V50p55zfk8Ye4ojV") # 请填写您自己的APIKey
# response = client.chat.completions.create(
# model = "glm-4",
# messages = [
# {"role":"user","content":prompt}
# ],
# )
# content = response.choices[0].message.content
# print('\n',content)
# res = re.sub(r'\s+', '', content) #处理空格
# return res
# instruction = "你是生命周期领域富有经验和知识的专家。文件的每一行都是一个问题根据你所掌握的知识回答问题不要列出几点来回答不需要换行只需要用1句话回答问题。"
# question = []
# answers = []
# with open("/home/zhangxj/WorkFile/LCA-GPT/QA/filters/question.txt","r",encoding="utf-8") as file:
# for line in file.readlines():
# question.append(line.strip())
# for ques in question:
# message = (instruction+ques)
# ans = zhipuQA(message)
# answers.append(ans)
# data = {"ans":answers}
# df = pd.DataFrame(data)
# df.to_csv("/home/zhangxj/WorkFile/LCA-GPT/QA/eval/GLM.csv",index=False)
df = pd.read_excel("/home/zhangxj/WorkFile/LCA-GPT/QA/eval/GLMoutput.xlsx")
answers = df['content'].values.tolist()
with open("/home/zhangxj/WorkFile/LCA-GPT/QA/eval/GLMpred.txt","w",encoding="utf-8") as file:
for ans in answers:
line = re.sub(r'\s+', '', ans)
file.write(line+"\n")