微信扫码
添加专属顾问
我要投稿
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-03-18
别再裸用 Claude Code,这 20 个隐藏命令,太爽了
2026-03-16
TRAE 技术专家推荐:6个技巧让你的 Agent 更听话
2026-03-13
OpenAI 发布 Codex 最佳实践指南:AI 编程工作流首次曝光
2026-03-07
2026 Claude Skills 全岗位合集(新增篇):6类全新岗位,18个高价值Agent Skill,告别无效加班
2026-03-05
如何成为顶级 Agentic 工程师
2026-03-05
哭了!早知道这些Claude Code快捷键和命令,我能少熬80%的夜!
2026-02-28
Google说只有5%的人真正会用AI,他们做对了什么?
2026-02-28
技能即资产:从临时Prompt到可复用Skill的AI工程范式进化
2026-01-04
2025-12-26
2026-01-13
2026-02-26
2026-01-29
2026-01-18
2026-01-30
2026-01-10
2026-01-17
2026-01-07
2026-02-28
2026-02-12
2026-02-12
2026-02-08
2026-02-05
2026-02-05
2026-01-23
2026-01-21