29 """
30 openai.api_key = os.environ["OPENAI_API_KEY"]
31 context_len = len(input_text.split())
32 logging.info(f"Generating image with {context_len} tokens in instructions")33 dimensions = config["image_size"]
34 response = openai.Image.create(prompt=input_text, n=1, size=dimensions)
35 image_url = response["data"][0]["url"]
29 logging.warning("Text contents not located with Newspaper3k")
30 parsed_data = manual_scraping(url)
31 content_len = len(str(parsed_data["text"]).split())
32 logging.info(f"Extracted {content_len} tokens from URL") 33 return parsed_data
34
35
22 completion. The function returns the generated completion as a string.
23 """
24 openai.api_key = os.environ["OPENAI_API_KEY"]
25 logging.info(f"Requesting OpenAI for {command}")26 model = config["completions_engine"]
27 template_prompt, max_t, temp = (
28 config["prompts"][command]["prompt"],
43 logging.info("Completion status: Success")
44 logging.info(f"Usage: {usage['total_tokens']} tokens")
45 logging.info(f"Prompt: {usage['prompt_tokens']} tokens")
46 logging.info(f"Completion: {usage['completion_tokens']} tokens")47 return top_completion.text
42 usage = completions["usage"]
43 logging.info("Completion status: Success")
44 logging.info(f"Usage: {usage['total_tokens']} tokens")
45 logging.info(f"Prompt: {usage['prompt_tokens']} tokens")46 logging.info(f"Completion: {usage['completion_tokens']} tokens")
47 return top_completion.text
Formatting the message manually before passing it to a logging
call does unnecessary work if logging is disabled. Consider using the logging
module's built-in formatting features to avoid that.
If your logging level is set to "WARNING"
, but the logging call is:
logging.debug("Data: %s", data)
In the above example, there will be no logging as the level debug
is below warning
. The conversion of the data
object to a string will be skipped by the logging module resulting in improved performance.
logging.debug(f"Data: {data}")
In the code shown above, the conversion of the data
object to a string will happen regardless of whether the message is logged or not (which is based on the log level).
import logging
import inspect
logging.basicConfig(level=logging.INFO)
logging.debug(f"Entered function {inspect.stack[0][3]}"))
import logging
import inspect
logging.basicConfig(level=logging.INFO)
logging.debug("Entered function %s", inspect.stack[0][3])