mirror of
https://github.com/langchain-ai/oap-langgraph-tools-agent.git
synced 2026-07-01 20:54:00 -04:00
Allow OAP to pass API keys to the agent (#37)
This commit is contained in:
+1
-1
@@ -13,4 +13,4 @@ ANTHROPIC_API_KEY=""
|
||||
# For user level authentication
|
||||
SUPABASE_URL=""
|
||||
# Ensure this is your Supabase Service Role key
|
||||
SUPABASE_KEY=""
|
||||
SUPABASE_KEY=""
|
||||
@@ -86,3 +86,8 @@ For creation methods, it auto-injects the user's ID into the metadata. This is t
|
||||
By using custom authentication, we can call this LangGraph server directly from a frontend application, without having to worry about exposing API keys/secrets, since you only need a JWT token from Supabase to authenticate.
|
||||
|
||||
For more info, see our [LangGraph custom auth docs](https://langchain-ai.github.io/langgraph/tutorials/auth/getting_started/).
|
||||
|
||||
## Model API Keys
|
||||
This agent is configured to accept model API keys set by a user in the OAP settings page. The default OAP implementation allows users to add their own OpenAI, Anthropic, and Google API keys, and these can easily be extended in OAP.
|
||||
|
||||
If a user sets their API keys in the OAP settings page, those keys will be passed to the agent through the config in an 'apiKeys' field and used by default. Otherwise, we will automatically fall back on the environment variables set in the agent deployed on LangGraph platform.
|
||||
+21
-1
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from typing import Optional, List
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -136,6 +137,24 @@ class GraphConfigPydantic(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
def get_api_key_for_model(model_name: str, config: RunnableConfig):
|
||||
model_name = model_name.lower()
|
||||
model_to_key = {
|
||||
"openai:": "OPENAI_API_KEY",
|
||||
"anthropic:": "ANTHROPIC_API_KEY",
|
||||
"google": "GOOGLE_API_KEY"
|
||||
}
|
||||
key_name = next((key for prefix, key in model_to_key.items()
|
||||
if model_name.startswith(prefix)), None)
|
||||
if not key_name:
|
||||
return None
|
||||
api_keys = config.get("configurable", {}).get("apiKeys", {})
|
||||
if api_keys and api_keys.get(key_name) and len(api_keys[key_name]) > 0:
|
||||
return api_keys[key_name]
|
||||
# Fallback to environment variable
|
||||
return os.getenv(key_name)
|
||||
|
||||
|
||||
async def graph(config: RunnableConfig):
|
||||
cfg = GraphConfigPydantic(**config.get("configurable", {}))
|
||||
tools = []
|
||||
@@ -216,6 +235,7 @@ async def graph(config: RunnableConfig):
|
||||
cfg.model_name,
|
||||
temperature=cfg.temperature,
|
||||
max_tokens=cfg.max_tokens,
|
||||
api_key=get_api_key_for_model(cfg.model_name, config) or "No token found"
|
||||
)
|
||||
|
||||
return create_react_agent(
|
||||
@@ -223,4 +243,4 @@ async def graph(config: RunnableConfig):
|
||||
model=model,
|
||||
tools=tools,
|
||||
config_schema=GraphConfigPydantic,
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user