Spaces:
Sleeping
Sleeping
Update langchain_logic/agent_setup.py
Browse files- langchain_logic/agent_setup.py +30 -13
langchain_logic/agent_setup.py
CHANGED
|
@@ -1,29 +1,46 @@
|
|
| 1 |
-
import os
|
| 2 |
from langchain.agents import AgentExecutor, create_tool_calling_agent
|
| 3 |
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def create_agent_executor():
|
| 12 |
"""Creates the LangChain agent and executor."""
|
| 13 |
-
# Add the new tool to the list
|
| 14 |
tools = [
|
| 15 |
-
schedule_appointment,
|
| 16 |
-
search_for_appointments,
|
| 17 |
-
list_all_appointments,
|
| 18 |
-
delete_appointment_records,
|
| 19 |
update_appointment_record
|
| 20 |
]
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# You can optionally update the prompt to mention the new capability
|
| 26 |
prompt = ChatPromptTemplate.from_messages([
|
|
|
|
| 27 |
("system", """You are an advanced, helpful appointment scheduling assistant.
|
| 28 |
|
| 29 |
Your tasks are to schedule, search, update, and delete appointments using the available tools.
|
|
@@ -47,7 +64,7 @@ def create_agent_executor():
|
|
| 47 |
agent_executor = AgentExecutor(
|
| 48 |
agent=agent,
|
| 49 |
tools=tools,
|
| 50 |
-
verbose=True,
|
| 51 |
handle_parsing_errors=True
|
| 52 |
)
|
| 53 |
return agent_executor
|
|
|
|
| 1 |
+
import os # <-- Import the os module
|
| 2 |
from langchain.agents import AgentExecutor, create_tool_calling_agent
|
| 3 |
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
+
from langchain_logic.tools import (
|
| 6 |
+
schedule_appointment,
|
| 7 |
+
search_for_appointments,
|
| 8 |
+
delete_appointment_records,
|
| 9 |
+
update_appointment_record,
|
| 10 |
+
list_all_appointments
|
| 11 |
+
)
|
| 12 |
from datetime import datetime
|
| 13 |
|
| 14 |
+
# --- IMPORTANT ---
|
| 15 |
+
# This code will now work both locally (with a .env file) and on Hugging Face Spaces.
|
| 16 |
+
# On Hugging Face, os.getenv("GOOGLE_API_KEY") will read the secret you set.
|
| 17 |
+
# Locally, load_dotenv() in your main file will load it from .env for os.getenv to find.
|
| 18 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 19 |
+
|
| 20 |
+
if not google_api_key:
|
| 21 |
+
raise ValueError("GOOGLE_API_KEY not found. Please set it as a secret in Hugging Face Spaces or in a .env file for local development.")
|
| 22 |
+
# ---------------
|
| 23 |
+
|
| 24 |
|
| 25 |
def create_agent_executor():
|
| 26 |
"""Creates the LangChain agent and executor."""
|
|
|
|
| 27 |
tools = [
|
| 28 |
+
schedule_appointment,
|
| 29 |
+
search_for_appointments,
|
| 30 |
+
list_all_appointments,
|
| 31 |
+
delete_appointment_records,
|
| 32 |
update_appointment_record
|
| 33 |
]
|
|
|
|
| 34 |
|
| 35 |
+
# Explicitly pass the API key to the model
|
| 36 |
+
llm = ChatGoogleGenerativeAI(
|
| 37 |
+
model="gemini-2.5-flash-lite-preview-06-17",
|
| 38 |
+
temperature=0,
|
| 39 |
+
google_api_key=google_api_key # <-- Pass the key here
|
| 40 |
+
)
|
| 41 |
|
|
|
|
| 42 |
prompt = ChatPromptTemplate.from_messages([
|
| 43 |
+
# ... (your prompt remains the same) ...
|
| 44 |
("system", """You are an advanced, helpful appointment scheduling assistant.
|
| 45 |
|
| 46 |
Your tasks are to schedule, search, update, and delete appointments using the available tools.
|
|
|
|
| 64 |
agent_executor = AgentExecutor(
|
| 65 |
agent=agent,
|
| 66 |
tools=tools,
|
| 67 |
+
verbose=True,
|
| 68 |
handle_parsing_errors=True
|
| 69 |
)
|
| 70 |
return agent_executor
|