from langchain.chains import RetrievalQA
from langchain.evaluation import QAEvalChain
from langchain_community.document_loaders import TextLoader
from langchain_community.embeddings import OllamaEmbeddings
from langchain_community.llms.ollama import Ollama
from langchain_community.vectorstores.faiss import FAISS #使用 vectorstore 进行存储
from langchain_text_splitters import RecursiveCharacterTextSplitter
llm = Ollama(model="qwen:7b")
loader = TextLoader("/home/cmcc/server/file/测试文档.txt", encoding="utf-8")
doc = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=3000, chunk_overlap=400)
docs = text_splitter.split_documents(doc)
#向量搜索引擎
embeddings = OllamaEmbeddings()
docsearch = FAISS.from_documents(docs, embeddings)
# FAISS.save_local("Ver", "index")
#创建你的检索引擎 retriever 设置检索的数据库是那个
chain = RetrievalQA.from_chain_type(llm = llm, chain_type="stuff", retriever=docsearch.as_retriever(), input_key = "question")
question_answers = [
{"question":"小明的哥哥是谁", "answer":"小雄"},
{"question":"小刘的哥哥是谁", "answer":"小蓝"},
{"question":"小张的姐姐是谁", "answer":"小红"},
]
predictions = chain.apply(question_answers)
print(predictions)
#启动评估链
eval_chain = QAEvalChain.from_llm(llm)
#让模型自己评分,下面代码帮助 eval_chain 指导不同部分在哪里
graed_outputs = eval_chain.evaluate(question_answers,
predictions,
question_key="question",
prediction_key="result",
answer_key="answer"
)
print(graed_outputs)
# query = "小张的姐姐是谁"
# response = chain.run(query)
# print(response)