From c1176ba3ba0b8da6f439c1a10fb904d6368fccd1 Mon Sep 17 00:00:00 2001 From: xucai Date: Sun, 9 Feb 2025 20:57:58 +0800 Subject: [PATCH 1/2] feature/support-lm-reasoning-field --- dspy/clients/base_lm.py | 7 +++++++ dspy/clients/lm.py | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/dspy/clients/base_lm.py b/dspy/clients/base_lm.py index 1cb97575ed..15f5d98e86 100644 --- a/dspy/clients/base_lm.py +++ b/dspy/clients/base_lm.py @@ -43,6 +43,7 @@ def _inspect_history(history, n: int = 1): for item in history[-n:]: messages = item["messages"] or [{"role": "user", "content": item["prompt"]}] outputs = item["outputs"] + reasoning_content_list = item.get("reasoning_content_list", []) timestamp = item.get("timestamp", "Unknown time") print("\n\n\n") @@ -67,6 +68,12 @@ def _inspect_history(history, n: int = 1): print(_blue(image_str.strip())) print("\n") + if reasoning_content_list: + print(_red("Reasoning From LLM:")) + for c in reasoning_content_list: + print(c.strip()) + print("\n") + print(_red("Response:")) print(_green(outputs[0].strip())) diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py index 549029d50f..6fc692c821 100644 --- a/dspy/clients/lm.py +++ b/dspy/clients/lm.py @@ -142,6 +142,11 @@ def __call__(self, prompt=None, messages=None, **kwargs): entry = dict(prompt=prompt, messages=messages, kwargs=kwargs, response=response) entry = dict(**entry, outputs=outputs, usage=dict(response["usage"])) entry = dict(**entry, cost=response.get("_hidden_params", {}).get("response_cost")) + if 'reasoning_field' in kwargs.keys(): + entry = dict(**entry, reasoning_content_list=[getattr(c.message, kwargs['reasoning_field']) + for c in response["choices"] + if hasattr(c.message, kwargs['reasoning_field'])]) + entry = dict( **entry, timestamp=datetime.now().isoformat(), From 39aef2f0166dfbf8f9c1ad8e93a570d6bf672caa Mon Sep 17 00:00:00 2001 From: xucai Date: Sun, 9 Feb 2025 21:29:50 +0800 Subject: [PATCH 2/2] feature/support-lm-reasoning-field --- dspy/clients/base_lm.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dspy/clients/base_lm.py b/dspy/clients/base_lm.py index 15f5d98e86..e2e83b244b 100644 --- a/dspy/clients/base_lm.py +++ b/dspy/clients/base_lm.py @@ -70,8 +70,7 @@ def _inspect_history(history, n: int = 1): if reasoning_content_list: print(_red("Reasoning From LLM:")) - for c in reasoning_content_list: - print(c.strip()) + print(reasoning_content_list[0].strip()) print("\n") print(_red("Response:"))