微信扫码
添加专属顾问
我要投稿
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+中大型企业
2025-07-15
xAI 新的虚拟角色 Ani 的系统提示词
2025-07-08
【Augment】 Augment技巧之 Rewrite Prompt(重写提示) 有神奇的魔法
2025-07-07
每周帮你节省20小时的10个高效DeepSeek提示词
2025-07-06
告别"AI味儿"!5个提示词帮你润色文章!
2025-06-29
提升AI编程效果的13个Prompt技巧
2025-06-28
很多人用不好AI,写不好提示词,打造不出自己的提示词系统,是因为不明白这一点!
2025-06-26
程序员的提示工程实战手册
2025-06-23
被 AI 气到崩溃?手把手教你写出 “有效指令”
2025-05-25
2025-05-09
2025-06-02
2025-04-30
2025-05-12
2025-05-18
2025-06-18
2025-05-07
2025-04-20
2025-04-27
2025-06-17
2025-04-21
2025-03-31
2025-03-29
2025-03-17
2025-02-06
2025-01-10
2024-12-25