🔬
Frameworks

Use Hanzo AI with DSPy

Use DSPy to program (not prompt) Hanzo AI models. DSPy's systematic approach to LM prompting works with any OpenAI-compatible provider.

Base URL: https://api.hanzo.ai/v1

API Key: Get yours at hanzo.ai/signup · Fully OpenAI-compatible · 390+ models available

🔬

Created by Stanford NLP / Omar Khattab

License: MIT · View source on GitHub →

Hanzo AI is OpenAI-compatible, so existing DSPy code works with zero refactoring. We deeply appreciate the Stanford NLP / Omar Khattab team for building and maintaining this open-source project.

DSPy + Hanzo setup

python
pip install dspy

import dspy

lm = dspy.LM(
    model="openai/zen4-pro",
    api_key="your-hanzo-api-key",
    api_base="https://api.hanzo.ai/v1",
)
dspy.configure(lm=lm)

Signature and module

python
class QA(dspy.Signature):
    """Answer questions with short factual answers."""
    question: str = dspy.InputField()
    answer: str = dspy.OutputField(desc="often 1-3 sentences")

qa = dspy.Predict(QA)
response = qa(question="What is the Hanzo AI LLM gateway?")
print(response.answer)

Chain of thought

python
class CoTQA(dspy.Signature):
    """Answer with step-by-step reasoning."""
    question: str = dspy.InputField()
    reasoning: str = dspy.OutputField(desc="think step by step")
    answer: str = dspy.OutputField()

cot = dspy.ChainOfThought(CoTQA)
result = cot(question="Why is AI important for developers?")

Optimization with MIPROv2

python
from dspy.teleprompt import MIPROv2

teleprompter = MIPROv2(metric=my_metric, auto="medium")
optimized = teleprompter.compile(program, trainset=train_data)

Ready to get started?

Create a free account and get your API key. 100K API calls/month free forever.