Loading...
Loading...
Compare original and translation side by side
import dspy
from typing import Literalimport dspy
from typing import Literal
Using `Literal` locks the output to valid categories only — the AI can't hallucinate labels. `ChainOfThought` adds reasoning which improves accuracy over bare `Predict`.
使用`Literal`可将输出限定为有效类别——AI不会生成不存在的标签。`ChainOfThought`添加推理逻辑,相比基础的`Predict`能提升准确率。class TagContent(dspy.Signature):
"""Assign all applicable tags to the content."""
message: str = dspy.InputField(desc="The content to tag")
tags: list[Literal[tuple(CATEGORIES)]] = dspy.OutputField(desc="All applicable tags")
tagger = dspy.ChainOfThought(TagContent)class TagContent(dspy.Signature):
"""Assign all applicable tags to the content."""
message: str = dspy.InputField(desc="The content to tag")
tags: list[Literal[tuple(CATEGORIES)]] = dspy.OutputField(desc="All applicable tags")
tagger = dspy.ChainOfThought(TagContent)from dspy.evaluate import Evaluate
def sorting_metric(example, prediction, trace=None):
return prediction.category == example.category
evaluator = Evaluate(
devset=devset,
metric=sorting_metric,
num_threads=4,
display_progress=True,
display_table=5,
)
score = evaluator(sorter)from dspy.evaluate import Evaluate
def sorting_metric(example, prediction, trace=None):
return prediction.category == example.category
evaluator = Evaluate(
devset=devset,
metric=sorting_metric,
num_threads=4,
display_progress=True,
display_table=5,
)
score = evaluator(sorter)BootstrapFewShotoptimizer = dspy.BootstrapFewShot(
metric=sorting_metric,
max_bootstrapped_demos=4,
)
optimized_sorter = optimizer.compile(sorter, trainset=trainset)MIPROv2optimizer = dspy.MIPROv2(
metric=sorting_metric,
auto="medium",
)
optimized_sorter = optimizer.compile(sorter, trainset=trainset)BootstrapFewShotoptimizer = dspy.BootstrapFewShot(
metric=sorting_metric,
max_bootstrapped_demos=4,
)
optimized_sorter = optimizer.compile(sorter, trainset=trainset)MIPROv2optimizer = dspy.MIPROv2(
metric=sorting_metric,
auto="medium",
)
optimized_sorter = optimizer.compile(sorter, trainset=trainset)result = optimized_sorter(message="I was charged twice on my credit card last month")
print(f"Category: {result.category}")
print(f"Reasoning: {result.reasoning}")result = optimized_sorter(message="I was charged twice on my credit card last month")
print(f"Category: {result.category}")
print(f"Reasoning: {result.reasoning}")LiteralChainOfThoughtPredicthintclass SortWithHint(dspy.Signature):
message: str = dspy.InputField()
hint: str = dspy.InputField(desc="Optional hint for ambiguous cases")
category: Literal[tuple(CATEGORIES)] = dspy.OutputField()hintLiteralChainOfThoughtPredicthintclass SortWithHint(dspy.Signature):
message: str = dspy.InputField()
hint: str = dspy.InputField(desc="Optional hint for ambiguous cases")
category: Literal[tuple(CATEGORIES)] = dspy.OutputField()hint/ai-scoring/ai-improving-accuracy/ai-scoring/ai-improving-accuracy