Created
March 31, 2025 02:38
-
-
Save SafeEval/a3242a8fb4a6b2d82e4f5b1742c2467d to your computer and use it in GitHub Desktop.
Hello DSPy — A simple "hello world" style DSPy script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dspy | |
from typing import Dict | |
class PortlandOracle(dspy.Signature): | |
"""Answer questions about Portland restaurants.""" | |
question: str = dspy.InputField() | |
answer: str = dspy.OutputField() | |
def forward(self, question): | |
prompt = f"""You are an expert in Portland, Oregon tourism. Answer questions about restaurants laconically. | |
Always provide cross street information in the format: $venue - $cross_street | |
Example: | |
Question: What is a popular brewpub? | |
Answer: Deschutes Brewery - NW 11th Ave & NW Davis St | |
Question: {question} | |
Answer:""" | |
# Use a direct completion call | |
lm = dspy.LM(model="ollama/deepseek-r1:latest") | |
response = lm(prompt=prompt) | |
# Handle response being a list | |
if isinstance(response, list): | |
response = response[0] if response else "" | |
# Return the response in the expected format | |
return {"answer": response.strip()} | |
if __name__ == "__main__": | |
question = "What is the best restaurant in Portland, OR?" | |
oracle = PortlandOracle(question=question, answer="") | |
result = oracle.forward(question) | |
print(result["answer"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The best restaurant in Portland, Oregon, is Portland Pizza Company located at 1205 NE 5th Ave & NE 10th St. | |
Answer: Portland Pizza Company - NE 5th Ave & NE 10th St |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dspy-ai==2.6.16 | |
pydantic==2.11.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ollama pull deepseek-r1 | |
python3 -m venv venv | |
source ./venv/bin/activate && pip install -r requirements.txt | |
source ./venv/bin/activate && python3 agent.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment