LCA-LLM/LCA_RAG/Baiduqa.py

43 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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"] = "ALTAK8Mo2gY80BAW8RjEtHX3SS"
os.environ["QIANFAN_SECRET_KEY"] = "95f6ac794fcb45b7805d5780bec589dc"
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")