Skip to content

Commit cbb0cec

Browse files
committed
add DSPy operator CLI
1 parent d6a43f8 commit cbb0cec

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

docs/references.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,9 @@
8484

8585
- [LM Studio](https://lmstudio.ai/)
8686
- [Hugging Face CLI](https://huggingface.co/docs/huggingface_hub/guides/cli)
87+
88+
### DSPy
89+
90+
- [DSPy (Declarative Self-improving Python)](https://dspy.ai/)
91+
- [Language Models](https://dspy.ai/learn/programming/language_models/)
92+
- [Language Models / v3.0.3](https://github.com/stanfordnlp/dspy/blob/3.0.3/docs/docs/learn/programming/language_models.md)

scripts/dspy_operator.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import logging
2+
from logging import basicConfig
3+
4+
import dspy
5+
import typer
6+
from dotenv import load_dotenv
7+
8+
from template_langgraph.llms.azure_openais import Settings
9+
from template_langgraph.loggers import get_logger
10+
11+
# Initialize the Typer application
12+
app = typer.Typer(
13+
add_completion=False,
14+
help="DSPy operator CLI",
15+
)
16+
17+
# Set up logging
18+
logger = get_logger(__name__)
19+
20+
21+
def set_verbose_logging(verbose: bool):
22+
if verbose:
23+
logger.setLevel(logging.DEBUG)
24+
basicConfig(level=logging.DEBUG)
25+
26+
27+
@app.command()
28+
def run(
29+
question: str = typer.Option(
30+
"今日はどんな天気ですか?",
31+
"--question",
32+
"-q",
33+
help="Question to ask the DSPy model",
34+
),
35+
verbose: bool = typer.Option(
36+
False,
37+
"--verbose",
38+
"-v",
39+
help="Enable verbose output",
40+
),
41+
):
42+
set_verbose_logging(verbose)
43+
44+
settings = Settings()
45+
46+
lm = dspy.LM(
47+
model=f"azure/{settings.azure_openai_model_chat}",
48+
api_key=settings.azure_openai_api_key,
49+
api_base=settings.azure_openai_endpoint,
50+
api_version=settings.azure_openai_api_version,
51+
)
52+
dspy.configure(lm=lm)
53+
54+
# Define a module (ChainOfThought) and assign it a signature (return an answer, given a question).
55+
qa = dspy.ChainOfThought("question -> answer")
56+
57+
# Run with the default LM configured with `dspy.configure` above.
58+
prediction = qa(question=question)
59+
print(prediction)
60+
61+
62+
@app.command()
63+
def template(
64+
verbose: bool = typer.Option(
65+
False,
66+
"--verbose",
67+
"-v",
68+
help="Enable verbose output",
69+
),
70+
):
71+
set_verbose_logging(verbose)
72+
logger.info("Running...")
73+
74+
75+
if __name__ == "__main__":
76+
load_dotenv(
77+
override=True,
78+
verbose=True,
79+
)
80+
app()

0 commit comments

Comments
 (0)