From 83de42d109d01cb012187290e5eea5bc1a7d4b96 Mon Sep 17 00:00:00 2001 From: Erik Oosterop Date: Sat, 6 Jul 2024 21:26:06 +0000 Subject: [PATCH 1/2] Add actual costs of OpenAI API after call. --- CHANGELOG.md | 3 +++ src/crewcal/llm_extract.py | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2444e97..68c8e60 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Consider downgrading Python requirement (at least to 3.10, but I dont know how low it can gowhere) +### Added +- Show actual cost - as calculated by langchain - after call. Note: does not seem to work for gpt-4o + ## [0.8.3] ### Fixed diff --git a/src/crewcal/llm_extract.py b/src/crewcal/llm_extract.py index e707531..bd1f425 100755 --- a/src/crewcal/llm_extract.py +++ b/src/crewcal/llm_extract.py @@ -5,6 +5,7 @@ About 0.75 US cents per call in OpenAI API costs. """ + import json import logging import os @@ -85,6 +86,7 @@ def extract(self, to_file: str = "") -> None: full_sched_doc = self.read_schedule_pdf(self.schedule_path) if full_sched_doc: + # TODO: Can use gpt-4o instead; but on paper 10x as expensive model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0) prompt = ChatPromptTemplate.from_messages( [("system", template_flight_schedule), ("human", "{input}")] @@ -106,10 +108,17 @@ def extract(self, to_file: str = "") -> None: ) logging.warning( - "WARNING - This script costs 0.75 US cents per call in OpenAI API costs." + "WARNING - This script costs ~0.75 US cents per call in OpenAI API costs." ) - self.extracted_schedule = extraction_chain.invoke({"input": full_sched_doc}) + from langchain.callbacks import get_openai_callback + + with get_openai_callback() as cb: + self.extracted_schedule = extraction_chain.invoke( + {"input": full_sched_doc} + ) + + logging.warning("Actual OpenAI API cost in USD:" + str(cb.total_cost)) if to_file: self.write_json(to_file) From 00b27c522d878bcaae705bc16293ae922daf002b Mon Sep 17 00:00:00 2001 From: Erik Oosterop Date: Mon, 15 Jul 2024 22:15:08 +0000 Subject: [PATCH 2/2] Check for openai api key; show costs. --- CHANGELOG.md | 7 ++++++- pyproject.toml | 2 +- src/crewcal/llm_extract.py | 12 ++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68c8e60..e3a06db 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Consider downgrading Python requirement (at least to 3.10, but I dont know how low it can gowhere) +## [0.8.4] + ### Added -- Show actual cost - as calculated by langchain - after call. Note: does not seem to work for gpt-4o +- Show actual cost - as calculated by langchain - after call. Note: does not seem to work for gpt-4o. + +### Changed +- Catch error and show warning in log when command is run without OPENAI_API_KEY being set. ## [0.8.3] diff --git a/pyproject.toml b/pyproject.toml index 391ef8e..a769224 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ disallow_incomplete_defs = true [tool.poetry] name = "crewcal" -version = "0.8.3" +version = "0.8.4" description = "Convert an airline crew schedule pdf into iCalendar format." authors = ["Erik Oosterop "] license = "MIT" diff --git a/src/crewcal/llm_extract.py b/src/crewcal/llm_extract.py index bd1f425..f48f201 100755 --- a/src/crewcal/llm_extract.py +++ b/src/crewcal/llm_extract.py @@ -23,7 +23,10 @@ from crewcal.schedule import Schedule _ = load_dotenv(find_dotenv()) -openai.api_key = os.environ["OPENAI_API_KEY"] +try: + openai.api_key = os.environ["OPENAI_API_KEY"] +except KeyError: + logging.warning("WARNING - Environment variable OPENAI_API_KEY should be set.") class OpenAISchedule: @@ -34,6 +37,7 @@ class OpenAISchedule: schedule_path: str extracted_schedule: str = "" + llm_model_name: str = "gpt-3.5-turbo" def __init__( self, schedule_path: str, to_json_file: str = "", to_icalendar_file: str = "" @@ -86,8 +90,8 @@ def extract(self, to_file: str = "") -> None: full_sched_doc = self.read_schedule_pdf(self.schedule_path) if full_sched_doc: - # TODO: Can use gpt-4o instead; but on paper 10x as expensive - model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0) + # TODO: Can use gpt-4o instead; but app. 10x as expensive + model = ChatOpenAI(model_name=self.llm_model_name, temperature=0) prompt = ChatPromptTemplate.from_messages( [("system", template_flight_schedule), ("human", "{input}")] ) @@ -108,7 +112,7 @@ def extract(self, to_file: str = "") -> None: ) logging.warning( - "WARNING - This script costs ~0.75 US cents per call in OpenAI API costs." + "WARNING - This script costs ~0.75 US cents per call in OpenAI API costs (GPT-3.5)." ) from langchain.callbacks import get_openai_callback