Created
September 2, 2025 01:35
-
-
Save prrao87/d96b4a0880507d5581c095ac0c2e3cff to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| """ | |
| Code to run a simple DSPy pipeline for extracting structured outputs | |
| from text. The example shows how to extract a resume from the given text. | |
| """ | |
| import os | |
| import dspy | |
| from dotenv import load_dotenv | |
| from pydantic import BaseModel | |
| load_dotenv() | |
| OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY") | |
| # Using OpenRouter. Switch to another LLM provider as needed | |
| lm = dspy.LM( | |
| model="openrouter/google/gemini-2.0-flash-001", | |
| api_base="https://openrouter.ai/api/v1", | |
| api_key=OPENROUTER_API_KEY, | |
| cache=False, | |
| temperature=0.5 | |
| ) | |
| dspy.configure(lm=lm) | |
| class Resume(BaseModel): | |
| first_name: str | |
| last_name: str | |
| languages: list[str] | |
| frameworks: list[str] | |
| class ExtractResume(dspy.Signature): | |
| """ | |
| Extract the resume from the text. | |
| """ | |
| text: str = dspy.InputField() | |
| resume: Resume = dspy.OutputField() | |
| if __name__ == "__main__": | |
| extractor = dspy.Predict(ExtractResume) | |
| text = """ | |
| Samuel Colvin is the creator of Pydantic. His main languages | |
| are Python and Rust. | |
| """ | |
| resume = extractor(text=text) | |
| print(resume) | |
| print(dspy.inspect_history(n=1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment