LCA-LLM/LCA_RAG/Baiduqa.py

43 lines
1.4 KiB
Python
Raw Normal View History

2024-12-29 17:33:02 +08:00
import os
import qianfan
from qianfan.resources.console.data import Data
import pandas as pd
import re
# 使用安全认证AK/SK鉴权通过环境变量方式初始化替换下列示例中参数安全认证Access Key替换your_iam_akSecret Key替换your_iam_sk
os.environ["QIANFAN_ACCESS_KEY"] = "ALTxxxxxx"
os.environ["QIANFAN_SECRET_KEY"] = "95fxxxxxxxx"
chat_comp = qianfan.ChatCompletion()
question = []
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())
answers = []
for ques in question:
# 指定特定模型
content = ("你是生命周期领域富有经验和知识的专家。根据你所掌握的知识只用1句话回答问题。不要列出几点来回答不需要换行"+ques)
resp = chat_comp.do(model="ERNIE-3.5-8K", messages=[
{
"role": "user",
"content": content
}])
ans = resp["body"]["result"]
line = re.sub(r'\s+', '', ans)
print(line)
answers.append(line)
data = {"ans":answers}
df = pd.DataFrame(data)
df.to_csv("/home/zhangxj/WorkFile/LCA-GPT/QA/eval/baidu.csv",index=False)
with open("/home/zhangxj/WorkFile/LCA-GPT/QA/eval/ERNIEpred.txt","w",encoding="utf-8") as file:
for ans in answers:
line = re.sub(r'\s+', '', ans)
file.write(line+"\n")