Skip to content

Commit

Permalink
LiteLLM: Add support for api_key and base_url (mem0ai#1746)
Browse files Browse the repository at this point in the history
  • Loading branch information
AshDevFr committed Jan 24, 2025
1 parent 2039439 commit 492dca3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mem0/configs/llms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(
openai_base_url: Optional[str] = None,
site_url: Optional[str] = None,
app_name: Optional[str] = None,
# LiteLLM specific
litellm_base_url: Optional[str] = None,
# Ollama specific
ollama_base_url: Optional[str] = None,
# AzureOpenAI specific
Expand Down Expand Up @@ -63,6 +65,8 @@ def __init__(
:type site_url: Optional[str], optional
:param app_name: Openrouter app name to use, defaults to None
:type app_name: Optional[str], optional
:param litellm_base_url: The base URL of the LLM, defaults to None
:type litellm_base_url: Optional[str], optional
:param ollama_base_url: The base URL of the LLM, defaults to None
:type ollama_base_url: Optional[str], optional
:param openai_base_url: Openai base URL to be use, defaults to "https://api.openai.com/v1"
Expand Down
6 changes: 6 additions & 0 deletions 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:
Expand Down Expand Up @@ -69,12 +70,17 @@ def generate_response(
if not litellm.supports_function_calling(self.config.model):
raise ValueError(f"Model '{self.config.model}' in litellm does not support function calling.")

api_key = self.config.api_key or os.getenv("LITELLM_PROXY_API_KEY")
base_url = self.config.litellm_base_url or os.getenv("LITELLM_PROXY_API_BASE")

params = {
"model": self.config.model,
"messages": messages,
"temperature": self.config.temperature,
"max_tokens": self.config.max_tokens,
"top_p": self.config.top_p,
"api_key": api_key,
"base_url": base_url,
}
if response_format:
params["response_format"] = response_format
Expand Down

0 comments on commit 492dca3

Please sign in to comment.