微信扫码
添加专属顾问
我要投稿
1. 安装
在使用DSPy Visualizer之前,需要确保已经安装了langwatch 以及 dspy 的python包。
pip install dspy-ai==2.4.12pip install langwatch
2. 编写DSPy程序
本示例将编写简单的优化器使用示例,如图-1所示,DSPy的优化流程需要准备数据集、程序主体、优化器以及衡量指标,然后在固定数据集合衡量指标的情况下,调整程序主体和算法以达到优化的目的,具体步骤如下:
1)获取数据集,本示例中通过导入方式获取训练集和测试集。
import dspyfrom get_dataset import custom_trainset as train_setfrom get_dataset import custom_testset as test_set
from dspy.datasets.gsm8k import gsm8k_metricfrom dspy.teleprompt import BootstrapFewShot
# 定义并设置大模型model_name = 'llama3'lm = dspy.OllamaLocal(model=model_name)dspy.settings.configure(lm=lm)
class CoT(dspy.Module):def __init__(self):super().__init__()self.prog = dspy.ChainOfThought("question -> answer")def forward(self, question):return self.prog(question=question)cot = CoT()
config = dict(max_bootstrapped_demos=4, max_labeled_demos=4, max_rounds=2)# Optimize! Use the `gsm8k_metric` here. In general, the metric is going to tell the optimizer how well it's doing.teleprompter = BootstrapFewShot(metric=gsm8k_metric, **config)
import langwatchlangwatch.login()langwatch.dspy.init(experiment="test", optimizer=teleprompter)optimized_cot = teleprompter.compile(cot, trainset=train_set)
由于langwatch已经提供了可视化的在线服务,因此在线方式无需启动可视化程序,之间获取API-Key即可,如下步骤所示:
2)本地运行程序,根据提示输入API-Key,并在 dashboard查看结果。
如上界面展示的为训练两步得到的可视化数据,可以看出在第一步时优化器得到了3个demo,第二步时,优化器得到了4个demo。
也可以点击具体的 Predictors、Examples、LLM Calls(图中LLM Call无信息,因为使用了LLama3本地访问,如果是OpenAI接口则有数据显示)查看详细信息。
1)下载github langwatch 仓库并 复制 langwatch/.env.example 中的文件内容到 langwatch/.env
2)运行 docker compose up --build
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-02-05
当AI能替你干活,什么能力决定你不可替代?
2026-02-03
Claude Code 创始人亲授:10 条进阶秘籍(附 12 条工作流 Prompt 清单)
2026-01-31
全网首发!一张通往“人类禁区”的船票,手把手教你把 AI 送进 Moltbook
2026-01-30
ESLint创始人揭秘:用AI角色扮演编程,效率飙升
2026-01-30
谷歌官方推出!10 个 Gem 提示词,附详细Gem自律助手创建流程
2026-01-30
Skill手搓“自动化PPT神器”,不写一行代码
2026-01-29
简单的AGENTS.md竟然完胜复杂Skills,Vercel实测
2026-01-25
Claude Code 最佳实践:50 个实用技巧
2025-11-14
2025-12-03
2025-12-26
2025-12-17
2026-01-18
2025-11-27
2025-11-09
2026-01-04
2025-11-30
2026-01-07
2026-02-05
2026-01-21
2026-01-16
2026-01-13
2026-01-05
2025-12-22
2025-12-14
2025-12-03