Skip to content

Commit bae2ad8

Browse files
authored
Merge pull request #1562 from Anindyadeep/enhance-history
Enhance history with filterable content
2 parents 58c2071 + 26c97f8 commit bae2ad8

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

dspy/clients/lm.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2+
import uuid
23
import ujson
34
import functools
45
from pathlib import Path
6+
from datetime import datetime
57

68
try:
79
import warnings
@@ -53,8 +55,14 @@ def __call__(self, prompt=None, messages=None, **kwargs):
5355
entry = dict(prompt=prompt, messages=messages, kwargs=kwargs, response=response)
5456
entry = dict(**entry, outputs=outputs, usage=dict(response["usage"]))
5557
entry = dict(**entry, cost=response.get("_hidden_params", {}).get("response_cost"))
58+
entry = dict(
59+
**entry,
60+
timestamp=datetime.now().isoformat(),
61+
uuid=str(uuid.uuid4()),
62+
model=self.model,
63+
model_type=self.model_type,
64+
)
5665
self.history.append(entry)
57-
5866
return outputs
5967

6068
def inspect_history(self, n: int = 1):
@@ -103,8 +111,11 @@ def _inspect_history(lm, n: int = 1):
103111
for item in lm.history[-n:]:
104112
messages = item["messages"] or [{"role": "user", "content": item['prompt']}]
105113
outputs = item["outputs"]
114+
timestamp = item.get("timestamp", "Unknown time")
106115

107116
print("\n\n\n")
117+
print("\x1b[34m" + f"[{timestamp}]" + "\x1b[0m" + "\n")
118+
108119
for msg in messages:
109120
print(_red(f"{msg['role'].capitalize()} message:"))
110121
print(msg['content'].strip())
@@ -117,4 +128,4 @@ def _inspect_history(lm, n: int = 1):
117128
choices_text = f" \t (and {len(outputs)-1} other completions)"
118129
print(_red(choices_text, end=""))
119130

120-
print("\n\n\n")
131+
print("\n\n\n")

0 commit comments

Comments
 (0)