Created
May 16, 2025 08:37
-
-
Save estsauver/39a132a28beb21f92c42ea6aea43fa35 to your computer and use it in GitHub Desktop.
DSPy Gemma Weirdness
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
import dspy | |
from pydantic import BaseModel | |
model = "lm_studio/gemma-3-27b-it" | |
lm = dspy.LM(model, api_key='') | |
dspy.configure(lm=lm) | |
class Sentence(BaseModel): | |
sentence: str | |
index: str | |
class SplitSentences(dspy.Signature): | |
input: str = dspy.InputField() | |
output: list[Sentence] = dspy.OutputField() | |
math = dspy.Predict(SplitSentences) | |
print(f"model = {model}") | |
print(math(input="This is sentence one. This is sentence two. This is sentence three.")) |
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
# dspy/adapters/base.py | |
class Adapter: | |
... | |
def __call__( | |
self, | |
lm: "LM", | |
lm_kwargs: dict[str, Any], | |
signature: Type[Signature], | |
demos: list[dict[str, Any]], | |
inputs: dict[str, Any], | |
) -> list[dict[str, Any]]: | |
# Monkey patched print calls to get above logs. | |
inputs = self.format(signature, demos, inputs) | |
print(f"inputs: {inputs}") | |
outputs = lm(messages=inputs, **lm_kwargs) | |
print(f"outputs: {outputs}") | |
return self._call_post_process(outputs, signature) |
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
model = lm_studio/gemma-3-27b-it | |
inputs: [{'role': 'system', 'content': 'Your input fields are:\n1. `input` (str)\nYour output fields are:\n1. `output` (list[Sentence])\nAll interactions will be structured in the following way, with the appropriate values filled in.\n\n[[ ## input ## ]]\n{input}\n\n[[ ## output ## ]]\n{output} # note: the value you produce must adhere to the JSON schema: {"type": "array", "$defs": {"Sentence": {"type": "object", "properties": {"index": {"type": "string", "title": "Index"}, "sentence": {"type": "string", "title": "Sentence"}}, "required": ["sentence", "index"], "title": "Sentence"}}, "items": {"$ref": "#/$defs/Sentence"}}\n\n[[ ## completed ## ]]\nIn adhering to this structure, your objective is: \n Given the fields `input`, produce the fields `output`.'}, {'role': 'user', 'content': '[[ ## input ## ]]\nThis is sentence one. This is sentence two. This is sentence three.\n\nRespond with the corresponding output fields, starting with the field `[[ ## output ## ]]` (must be formatted as a valid Python list[Sentence]), and then ending with the marker for `[[ ## completed ## ]]`.'}] | |
outputs: ['```json\n[\n {\n "index": "1",\n "sentence": "This is sentence one."\n },\n {\n "index": "2",\n "sentence": "This is sentence two."\n },\n {\n "index": "3",\n "sentence": "This is sentence three."\n }\n]\n```'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment