From 8ee05dcafae58c515dea84168ef5fdb31ae307ea Mon Sep 17 00:00:00 2001 From: Neil Deshmukh Date: Thu, 29 Feb 2024 15:42:59 -0500 Subject: [PATCH 1/4] only init logging if logging openai usage --- dsp/modules/gpt3.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/dsp/modules/gpt3.py b/dsp/modules/gpt3.py index e19ca40f54..27f921bb1d 100644 --- a/dsp/modules/gpt3.py +++ b/dsp/modules/gpt3.py @@ -1,13 +1,6 @@ import logging from logging.handlers import RotatingFileHandler -# Configure logging -logging.basicConfig( - level=logging.INFO, - format="%(message)s", - handlers=[logging.FileHandler("openai_usage.log")], -) - import functools import json from typing import Any, Literal, Optional, cast @@ -19,6 +12,14 @@ from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on from dsp.modules.lm import LM +if dsp.settings.log_openai_usage: + # Configure logging + logging.basicConfig( + level=logging.INFO, + format="%(message)s", + handlers=[logging.FileHandler("openai_usage.log")], + ) + try: OPENAI_LEGACY = int(openai.version.__version__[0]) == 0 except Exception: From 0fe57b4bb34a7f3d59e4d48d80e37693f696416b Mon Sep 17 00:00:00 2001 From: Neil Deshmukh Date: Thu, 29 Feb 2024 22:16:28 -0500 Subject: [PATCH 2/4] update azure logger --- dsp/modules/azure_openai.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/dsp/modules/azure_openai.py b/dsp/modules/azure_openai.py index d930bec6b5..ab66524efa 100644 --- a/dsp/modules/azure_openai.py +++ b/dsp/modules/azure_openai.py @@ -1,12 +1,5 @@ import logging -# Configure logging -logging.basicConfig( - level=logging.INFO, - format="%(message)s", - handlers=[logging.FileHandler("azure_openai_usage.log")], -) - import functools import json from typing import Any, Literal, Optional, cast @@ -18,6 +11,14 @@ from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on from dsp.modules.lm import LM +if dsp.settings.log_openai_usage: + # Configure logging + logging.basicConfig( + level=logging.INFO, + format="%(message)s", + handlers=[logging.FileHandler("azure_openai_usage.log")], + ) + try: OPENAI_LEGACY = int(openai.version.__version__[0]) == 0 except Exception: From 4e3fcdc18f7d3cbf033e5d5fb2a30c9f0998b70f Mon Sep 17 00:00:00 2001 From: Neil Deshmukh Date: Thu, 29 Feb 2024 22:31:21 -0500 Subject: [PATCH 3/4] change to setting path of log files --- dsp/modules/azure_openai.py | 21 +++++++++++++-------- dsp/modules/databricks.py | 10 ++++++++-- dsp/modules/gpt3.py | 21 +++++++++++++-------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/dsp/modules/azure_openai.py b/dsp/modules/azure_openai.py index ab66524efa..ba0ff1564d 100644 --- a/dsp/modules/azure_openai.py +++ b/dsp/modules/azure_openai.py @@ -1,4 +1,4 @@ -import logging +import logging, os import functools import json @@ -11,13 +11,18 @@ from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on from dsp.modules.lm import LM -if dsp.settings.log_openai_usage: - # Configure logging - logging.basicConfig( - level=logging.INFO, - format="%(message)s", - handlers=[logging.FileHandler("azure_openai_usage.log")], - ) + +# Configure logging path +log_file_path = f'azure_openai_usage.log' +if "DSPY_USAGE_LOGGING_DIR" in os.environ: + log_file_path = os.path.join(os.environ["DSPY_USAGE_LOGGING_DIR"], log_file_path) + +# Configure logging +logging.basicConfig( + level=logging.INFO, + format="%(message)s", + handlers=[logging.FileHandler(log_file_path)], +) try: OPENAI_LEGACY = int(openai.version.__version__[0]) == 0 diff --git a/dsp/modules/databricks.py b/dsp/modules/databricks.py index 73813a3eeb..93206a5e05 100644 --- a/dsp/modules/databricks.py +++ b/dsp/modules/databricks.py @@ -1,12 +1,18 @@ -import logging +import logging, os from logging.handlers import RotatingFileHandler + +# Configure logging path +log_file_path = f'openai_usage.log' +if "DSPY_USAGE_LOGGING_DIR" in os.environ: + log_file_path = os.path.join(os.environ["DSPY_USAGE_LOGGING_DIR"], log_file_path) + # Configure logging logging.basicConfig( level=logging.INFO, format='%(message)s', handlers=[ - logging.FileHandler('openai_usage.log') + logging.FileHandler(log_file_path) ] ) diff --git a/dsp/modules/gpt3.py b/dsp/modules/gpt3.py index 27f921bb1d..f08d6155e7 100644 --- a/dsp/modules/gpt3.py +++ b/dsp/modules/gpt3.py @@ -1,4 +1,4 @@ -import logging +import logging, os from logging.handlers import RotatingFileHandler import functools @@ -12,13 +12,18 @@ from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on from dsp.modules.lm import LM -if dsp.settings.log_openai_usage: - # Configure logging - logging.basicConfig( - level=logging.INFO, - format="%(message)s", - handlers=[logging.FileHandler("openai_usage.log")], - ) + +# Configure logging path +log_file_path = f'openai_usage.log' +if "DSPY_USAGE_LOGGING_DIR" in os.environ: + log_file_path = os.path.join(os.environ["DSPY_USAGE_LOGGING_DIR"], log_file_path) + +# Configure logging +logging.basicConfig( + level=logging.INFO, + format="%(message)s", + handlers=[logging.FileHandler(log_file_path)], +) try: OPENAI_LEGACY = int(openai.version.__version__[0]) == 0 From 9dd022863ea70ce08d15cae3ae1b68299584cdbd Mon Sep 17 00:00:00 2001 From: Neil Deshmukh Date: Thu, 29 Feb 2024 22:32:36 -0500 Subject: [PATCH 4/4] move logging config --- dsp/modules/azure_openai.py | 23 +++++++++++------------ dsp/modules/gpt3.py | 25 ++++++++++++------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/dsp/modules/azure_openai.py b/dsp/modules/azure_openai.py index ba0ff1564d..6bf1898a68 100644 --- a/dsp/modules/azure_openai.py +++ b/dsp/modules/azure_openai.py @@ -1,17 +1,5 @@ import logging, os -import functools -import json -from typing import Any, Literal, Optional, cast - -import backoff -import openai - -import dsp -from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on -from dsp.modules.lm import LM - - # Configure logging path log_file_path = f'azure_openai_usage.log' if "DSPY_USAGE_LOGGING_DIR" in os.environ: @@ -24,6 +12,17 @@ handlers=[logging.FileHandler(log_file_path)], ) +import functools +import json +from typing import Any, Literal, Optional, cast + +import backoff +import openai + +import dsp +from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on +from dsp.modules.lm import LM + try: OPENAI_LEGACY = int(openai.version.__version__[0]) == 0 except Exception: diff --git a/dsp/modules/gpt3.py b/dsp/modules/gpt3.py index f08d6155e7..bc5b9b9eff 100644 --- a/dsp/modules/gpt3.py +++ b/dsp/modules/gpt3.py @@ -1,20 +1,8 @@ import logging, os from logging.handlers import RotatingFileHandler -import functools -import json -from typing import Any, Literal, Optional, cast - -import dsp -import backoff -import openai - -from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on -from dsp.modules.lm import LM - - # Configure logging path -log_file_path = f'openai_usage.log' +log_file_path = f'azure_openai_usage.log' if "DSPY_USAGE_LOGGING_DIR" in os.environ: log_file_path = os.path.join(os.environ["DSPY_USAGE_LOGGING_DIR"], log_file_path) @@ -25,6 +13,17 @@ handlers=[logging.FileHandler(log_file_path)], ) +import functools +import json +from typing import Any, Literal, Optional, cast + +import dsp +import backoff +import openai + +from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on +from dsp.modules.lm import LM + try: OPENAI_LEGACY = int(openai.version.__version__[0]) == 0 except Exception: