instructor
instructor copied to clipboard
Get raw prompt *before* making API call
I believe logging is the only way to capture raw prompts in instructor. This requires actually making the API call -- is there any way to get the raw prompt without actually sending it to the API? This would be useful for e.g. cost estimation in advance.
I'm using this workaround for now, but surely there should be a more straightforward way of doing this:
def get_raw_prompt(
messages: list[dict[str, str]],
client: Instructor,
model: str,
response_model: BaseModel,
verbose: bool = False,
):
log_stream = StringIO()
logger = logging.getLogger("instructor")
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler(log_stream)
logger.addHandler(stream_handler)
with patch("httpx.Client.send") as mock_send:
mock_send.return_value = None # prevent actual API calls
try:
bla = client.chat.completions.create(
messages=messages,
response_model=response_model,
model=model,
)
except Exception:
# there WILL be an error
# there's nothing you can do about it
pass # cope and seethe
log_contents = log_stream.getvalue()
if verbose:
print("\n---\n LOG \n---\n", log_contents, "\n---\n")
for line in log_contents.splitlines():
if "Instructor Request" in line:
if verbose:
print("\n---\n RAW PROMPT \n---\n", line, "\n---\n")
return line.split("Instructor Request: ")[1]
warnings.warn(
"No raw prompt found in logs. Maybe anthropic "
"isn't supported or something idk"
)
return ""