Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: streamline OpenAI_Chat initialization and deprecate old parameters #734

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions src/vanna/openai/openai_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,28 @@ class OpenAI_Chat(VannaBase):
def __init__(self, client=None, config=None):
VannaBase.__init__(self, config=config)

# default parameters - can be overrided using config
self.temperature = 0.7
# Ensure config is a dictionary
config = config or {}

if "temperature" in config:
self.temperature = config["temperature"]
# Default parameters - can be overridden using config
self.temperature = config.get("temperature", 0.7)

if "api_type" in config:
raise Exception(
"Passing api_type is now deprecated. Please pass an OpenAI client instead."
)

if "api_base" in config:
raise Exception(
"Passing api_base is now deprecated. Please pass an OpenAI client instead."
)

if "api_version" in config:
raise Exception(
"Passing api_version is now deprecated. Please pass an OpenAI client instead."
)
# Raise exceptions for deprecated parameters
for deprecated_param in ["api_type", "api_base", "api_version"]:
if deprecated_param in config:
raise Exception(
f"Passing {deprecated_param} is now deprecated. Please pass an OpenAI client instead."
)

if client is not None:
self.client = client
return

if config is None and client is None:
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
return

if "api_key" in config:
self.client = OpenAI(api_key=config["api_key"])
# Initialize the OpenAI client with optional overrides from config
self.client = OpenAI(
api_key=config.get("api_key"),
base_url=config.get("base_url")
)

def system_message(self, message: str) -> any:
return {"role": "system", "content": message}
Expand Down Expand Up @@ -106,10 +97,7 @@ def submit_prompt(self, prompt, **kwargs) -> str:
temperature=self.temperature,
)
else:
if num_tokens > 3500:
model = "gpt-3.5-turbo-16k"
else:
model = "gpt-3.5-turbo"
model = "gpt-4o-mini"

print(f"Using model {model} for {num_tokens} tokens (approx)")
response = self.client.chat.completions.create(
Expand Down