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

Add support for API key configuration in LiteLLM #2178

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
5 changes: 4 additions & 1 deletion mem0/llms/litellm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from typing import Dict, List, Optional

try:
@@ -16,7 +17,8 @@ def __init__(self, config: Optional[BaseLlmConfig] = None):

if not self.config.model:
self.config.model = "gpt-4o-mini"

if not self.config.api_key:
self.config.api_key = os.environ["OPENAI_API_KEY"]
Comment on lines +20 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling when OPENAI_API_KEY environment variable is not set. This will cause an uncaught KeyError exception when the API key is not configured.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if not self.config.api_key:
self.config.api_key = os.environ["OPENAI_API_KEY"]
if not self.config.api_key:
self.config.api_key = os.environ.get('OPENAI_API_KEY')
if not self.config.api_key:
raise ValueError('OPENAI_API_KEY environment variable is not set')

Comment on lines +20 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing error handling when OPENAI_API_KEY environment variable is not set. This will cause an uncaught KeyError exception at runtime.

📝 Committable Code Suggestion

‼️ Ensure you review the code suggestion before committing it to the branch. Make sure it replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if not self.config.api_key:
self.config.api_key = os.environ["OPENAI_API_KEY"]
if not self.config.api_key:
self.config.api_key = os.environ.get("OPENAI_API_KEY", None)
if not self.config.api_key:
raise ValueError("OPENAI_API_KEY environment variable is not set")

def _parse_response(self, response, tools):
"""
Process the response based on whether tools are used or not.
@@ -75,6 +77,7 @@ def generate_response(
"temperature": self.config.temperature,
"max_tokens": self.config.max_tokens,
"top_p": self.config.top_p,
"api_key": self.config.api_key,
}
if response_format:
params["response_format"] = response_format