当前位置: 首页 > news >正文

【Langchain大语言模型开发教程】评估

 🔗 LangChain for LLM Application Development - DeepLearning.AI

学习目标

1、Example generation

2、Manual evaluation and debug

3、LLM-assisted evaluation

4、LangChain evaluation platform

1、引包、加载环境变量;

import osfrom dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env filefrom langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
from langchain.document_loaders import CSVLoader
from langchain.indexes import VectorstoreIndexCreator
from langchain.vectorstores import DocArrayInMemorySearch

2、加载数据;

file = 'OutdoorClothingCatalog_1000.csv'
loader = CSVLoader(file_path=file, encoding='utf-8')
data = loader.load()

3、创建向量数据库(内存警告⚠);

model_name = "bge-large-en-v1.5"
embeddings = HuggingFaceEmbeddings(model_name=model_name,
)db = DocArrayInMemorySearch.from_documents(data, embeddings)
retriever = db.as_retriever()

 4、初始化一个LLM并创建一个RetrievalQ链;

llm = ChatOpenAI(api_key=os.environ.get('ZHIPUAI_API_KEY'),base_url=os.environ.get('ZHIPUAI_API_URL'),model="glm-4",temperature=0.98)qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever,verbose=True,chain_type_kwargs = {"document_separator": "<<<<>>>>>"}
)

 Example generation

from langchain.evaluation.qa import QAGenerateChainexample_gen_chain = QAGenerateChain.from_llm(llm)new_examples = example_gen_chain.apply_and_parse([{"doc": t} for t in data[:5]]
)

这里我们打印一下这个生成的example,发现是一个列表长下面这个样子;


[{'qa_pairs': {'query': "What is the unique feature of the innersole in the Women's Campside Oxfords?", 'answer': 'The innersole has a vintage hunt, fish, and camping motif.'}}, {'qa_pairs': {'query': 'What is the name of the dog mat that is ruggedly constructed from recycled plastic materials, helping to keep dirt and water off the floors and plastic out of landfills?', 'answer': 'The name of the dog mat is Recycled Waterhog Dog Mat, Chevron Weave.'}}, {'qa_pairs': {'query': 'What is the name of the product described in the document that is suitable for Infant and Toddler Girls?', 'answer': "The product is called 'Infant and Toddler Girls' Coastal Chill Swimsuit, Two-Piece'."}}, {'qa_pairs': {'query': 'What is the primary material used in the construction of the Refresh Swimwear V-Neck Tankini, and what percentage of it is recycled?', 'answer': 'The primary material is nylon, with 82% of it being recycled nylon.'}}, {'qa_pairs': {'query': 'What is the material used for the EcoFlex 3L Storm Pants, according to the document?', 'answer': 'The EcoFlex 3L Storm Pants are made of 100% nylon, exclusive of trim.'}}]

所以这里我们需要进行一步提取;

for example in new_examples:examples.append(example["qa_pairs"])print(examples)qa.invoke(examples[0]["query"])

 Manual Evaluation

import langchain
langchain.debug = True #开始debug模式,查看chain中的详细步骤

 我们再次执行来查看chain中的细节;

 LLM-assisted evaluation

那我们是不是可以使用语言模型来评估呢;

langchain.debug = False #关闭debug模式from langchain.evaluation.qa import QAEvalChain

让大语言模型来为我们每个example来生成答案; 

predictions = qa.apply(examples)

我们初始化一个评估链;

eval_chain = QAEvalChain.from_llm(llm)

让大语言模型对实际答案和预测答案进行对比并给出一个评分;

graded_outputs = eval_chain.evaluate(examples, predictions)

最后,我们可以打印一下看看结果; 

for i, eg in enumerate(examples):print(f"Example {i}:")print("Question: " + predictions[i]['query'])print("Real Answer: " + predictions[i]['answer'])print("Predicted Answer: " + predictions[i]['result'])print("Predicted Grade: " + graded_outputs[i]['results'])print()

http://www.lryc.cn/news/406272.html

相关文章:

  • Python爬虫小项目实战
  • PHP Filesystem 简介
  • 源代码加密软件哪家好?五款企业级加密软件推荐
  • Redis常见的数据类型及操作方式
  • 谷粒商城实战笔记-55-商品服务-API-三级分类-修改-拖拽数据收集
  • AI绘画入门实践|Midjourney:使用 --seed 制作情侣头像与漫画
  • 笔记:Enum中FlagsAttribute特性的用法
  • QWidget如何切换ui
  • web网站组成
  • 带您详细了解安全漏洞的产生和防护
  • 【接口测试】params传参与body传参区别
  • 【docker】部署证书过期监控系统mouday/domain-admin
  • 高级java每日一道面试题-2024年7月17日
  • css中如何清除浮动
  • 【网络】tcp_socket
  • Live555源码阅读笔记:哈希表的实现
  • vue3创建vite项目
  • Maven概述
  • Easyu中datagrid点击时获取所在行的数据
  • java项目中添加SDK项目作为依赖使用(无需上传Maven)
  • 区块链和数据要素融合的价值及应用
  • 以太坊的可扩展性危机:探索执行层的瓶颈
  • 静态解析activiti文本,不入库操作流程
  • 100个python的基本语法知识【上】
  • Python从0到100(四十四):读取数据库数据
  • ZLMRTCClient配置说明与用法(含示例)
  • nginx代理服务配置,基于http协议-Linux(CentOS)
  • Photos框架 - 自定义媒体资源选择器(数据部分)
  • Spring Boot + Spring Cloud 入门
  • 怎么使用动态IP地址上网