农业机械通信开发最怕什么?试试这套虹科CAN FD仿真方案
2026/5/15 20:45:44
本文档将手把手教你如何使用LangSmith对 LLM(如 Gemini, DeepSeek, GPT)进行自动化评估和对比。
点击这里Generate API key 就会获得一个key
如果你的账号被分配到了EU (欧洲)节点(URL 是eu.smith.langchain.com),你必须显式配置 Endpoint,否则会报403 Forbidden。
https://api.smith.langchain.comhttps://eu.api.smith.langchain.com设置环境变量:
ANGCHAIN_ENDPOINT="https://eu.api.smith.langchain.com"LANGCHAIN_API_KEY="lsvxxxxxxxxxxxxxxxxxxxx"你需要安装langsmith和langchain相关库。
pipinstalllangsmith langchain langchain-openai在项目根目录创建.env文件,填入以下内容:
# 开启 Tracing (可选,但推荐) LANGCHAIN_TRACING_V2=true # 你的 API Key LANGCHAIN_API_KEY="ls__your_api_key_here" # 如果你是 EU 账号,必须加这一行!US 账号可忽略 LANGCHAIN_ENDPOINT="https://eu.api.smith.langchain.com" # 你的模型 Key (用于调用模型) GEMINI_API_KEY="AIza..." DEEPSEEK_API_KEY="sk-..."我们将编写一个脚本,对比Gemini和DeepSeek在回答同一组问题时的表现。
compare_model.py)importosfromlangsmithimportClient,evaluatefromsrc.llm.gemini_chat_modelimportget_gemini_llmfromsrc.llm.deepseek_chat_modelimportget_deepseek_llm# ================= 1. 初始化客户端 =================client=Client()# ================= 2. 准备数据集 (Dataset) =================# 数据集名称dataset_name="AI_Interview_Questions"# 检查数据集是否存在,不存在则创建ifnotclient.has_dataset(dataset_name=dataset_name):print(f"创建新数据集:{dataset_name}")dataset=client.create_dataset(dataset_name=dataset_name,description="用于测试模型的基础问答能力")# 写入测试用例 (Inputs)# 可以在这里添加标准答案 (Outputs) 用于自动打分,这里仅做生成测试client.create_examples(inputs=[{"prompt":"什么是 RAG (Retrieval-Augmented Generation)?"},{"prompt":"用 Python 写一个快排算法。"},{"prompt":"解释量子纠缠,像我只有5岁一样。"},],dataset_id=dataset.id,)else:print(f"使用现有数据集:{dataset_name}")# ================= 3. 准备模型 (Target Functions) =================# 初始化 LangChain 模型对象gemini=get_gemini_llm()deepseek=get_deepseek_llm()# 定义包装函数# LangSmith 会把数据集里的 inputs (如 {"prompt": "..."}) 传给这个函数defpredict_gemini(inputs:dict):# 调用模型response=gemini.invoke(inputs["prompt"])# 返回结果,key 可以是 "output" 或 "answer"return{"output":response.content}defpredict_deepseek(inputs:dict):response=deepseek.invoke(inputs["prompt"])return{"output":response.content}# ================= 4. 运行评估 (Run Evaluation) =================print("开始评估 Gemini...")evaluate(predict_gemini,data=dataset_name,experiment_prefix="gemini-v1",# 实验名称前缀description="Gemini Pro 基础测试")print("开始评估 DeepSeek...")evaluate(predict_deepseek,data=dataset_name,experiment_prefix="deepseek-v1",description="DeepSeek Chat 基础测试")client.create_datasetclient.create_examplespredict_wrapper(包装函数)evaluate函数需要一个可调用的对象(函数)。inputs字典,必须返回一个字典(通常包含output)。evaluate(核心)这是 LangSmith 的魔法所在。它会:
predict函数。通过这种方式,原本凭感觉的“模型好坏”,变成了可视化、可量化的数据。