From ae14ce4add9c073943a1105fd653fd7e2f4d6d25 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 18 Sep 2023 21:13:42 -0700 Subject: [PATCH] LLM_OPENAI_SHOW_RESPONSES=1 debug trick, closes #286 --- docs/contributing.md | 10 ++++++++++ llm/default_plugins/openai_models.py | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/docs/contributing.md b/docs/contributing.md index a1047a34..6a140893 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -18,6 +18,16 @@ To run the tests: pytest +## Debugging tricks + +The default OpenAI plugin has a debugging mechanism for showing the exact responses that came back from the OpenAI API. + +Set the `LLM_OPENAI_SHOW_RESPONSES` environment variable like this: +```bash +LLM_OPENAI_SHOW_RESPONSES=1 llm -m chatgpt 'three word slogan for an an otter-run bakery' +``` +This will output the response (including streaming responses) to standard error, as shown in [issues 286](https://github.com/simonw/llm/issues/286). + ## Documentation Documentation for this project uses [MyST](https://myst-parser.readthedocs.io/) - it is written in Markdown and rendered using Sphinx. diff --git a/llm/default_plugins/openai_models.py b/llm/default_plugins/openai_models.py index e76e8533..90cf50ee 100644 --- a/llm/default_plugins/openai_models.py +++ b/llm/default_plugins/openai_models.py @@ -4,6 +4,7 @@ import click import datetime import openai +import os try: from pydantic import field_validator, Field # type: ignore @@ -15,6 +16,15 @@ import json import yaml +if os.environ.get("LLM_OPENAI_SHOW_RESPONSES"): + + def log_response(response, *args, **kwargs): + click.echo(response.text, err=True) + return response + + openai.requestssession = requests.Session() + openai.requestssession.hooks["response"].append(log_response) + @hookimpl def register_models(register):