Skip to content

Commit

Permalink
Merge pull request #4 from EJOOSTEROP/feat-cost_estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
EJOOSTEROP authored Jul 15, 2024
2 parents 2c156de + 00b27c5 commit de5658f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ 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.

### Changed
- Catch error and show warning in log when command is run without OPENAI_API_KEY being set.

## [0.8.3]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "MIT"
Expand Down
21 changes: 17 additions & 4 deletions src/crewcal/llm_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
About 0.75 US cents per call in OpenAI API costs.
"""

import json
import logging
import os
Expand All @@ -22,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:
Expand All @@ -33,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 = ""
Expand Down Expand Up @@ -85,7 +90,8 @@ def extract(self, to_file: str = "") -> None:
full_sched_doc = self.read_schedule_pdf(self.schedule_path)

if full_sched_doc:
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}")]
)
Expand All @@ -106,10 +112,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 (GPT-3.5)."
)

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)
Expand Down

0 comments on commit de5658f

Please sign in to comment.