当前位置: 首页 > 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()

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 数据集相关类代码回顾理解 | utils.make_grid\list comprehension\np.transpose
  • C++实用指南:Lambda 表达式的妙用
  • C#中的字符串
  • vue3前端开发-小兔鲜项目-使用pinia插件完成token的本地存储
  • 推荐一款前端滑动验证码插件(Vue、uniapp)
  • C++设计模式--单例模式
  • Java人力资源招聘社会校招类型招聘系统PC端
  • Linux服务管理(四)Apache服务
  • <Python><paddle>基于python使用百度paddleocr实现车牌识别
  • Vue2从基础到实战(指令修饰符)详解
  • Golang | Leetcode Golang题解之第290题单词规律
  • 便携移动工作站,端侧 AI 大模型设备折腾笔记:ROG 幻 X 和 4090 扩展坞
  • Python常用内置库介绍
  • Redis学习[3] ——持久化
  • android(安卓)最简单明了解释版本控制之MinSdkVersion、CompileSdkVersion、TargetSdkVersion
  • Google 是如何开发 Web 框架的
  • hexo+github搭建个人博客
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • Angular4 模板式表单用法以及验证
  • Centos6.8 使用rpm安装mysql5.7
  • CSS 提示工具(Tooltip)
  • express如何解决request entity too large问题
  • Javascript基础之Array数组API
  • spark本地环境的搭建到运行第一个spark程序
  • 给github项目添加CI badge
  • 欢迎参加第二届中国游戏开发者大会
  • 记一次用 NodeJs 实现模拟登录的思路
  • 如何学习JavaEE,项目又该如何做?
  • 使用 @font-face
  • 我是如何设计 Upload 上传组件的
  • 小程序测试方案初探
  • 移动端唤起键盘时取消position:fixed定位
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • 进程与线程(三)——进程/线程间通信
  • 容器镜像
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • # 计算机视觉入门
  • #APPINVENTOR学习记录
  • #ifdef 的技巧用法
  • #数学建模# 线性规划问题的Matlab求解
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (PySpark)RDD实验实战——取最大数出现的次数
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (二)原生js案例之数码时钟计时
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (四)js前端开发中设计模式之工厂方法模式
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (转)Unity3DUnity3D在android下调试
  • (自适应手机端)行业协会机构网站模板
  • (自用)gtest单元测试