Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance history with filterable content #1562

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions dspy/clients/lm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import uuid
import ujson
import functools
from pathlib import Path
from datetime import datetime

try:
import warnings
Expand Down Expand Up @@ -53,8 +55,14 @@ 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"))
entry = dict(
**entry,
timestamp=datetime.now().isoformat(),
uuid=str(uuid.uuid4()),
model=self.model,
model_type=self.model_type,
)
self.history.append(entry)

return outputs

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

print("\n\n\n")
print("\x1b[34m" + f"[{timestamp}]" + "\x1b[0m" + "\n")

for msg in messages:
print(_red(f"{msg['role'].capitalize()} message:"))
print(msg['content'].strip())
Expand All @@ -117,4 +128,4 @@ def _inspect_history(lm, n: int = 1):
choices_text = f" \t (and {len(outputs)-1} other completions)"
print(_red(choices_text, end=""))

print("\n\n\n")
print("\n\n\n")
Loading