Update agent.py
Browse files
agent.py
CHANGED
|
@@ -12,12 +12,14 @@ from langchain_community.document_loaders import WikipediaLoader
|
|
| 12 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 13 |
from pathlib import Path
|
| 14 |
import tempfile
|
|
|
|
| 15 |
|
| 16 |
# constants
|
| 17 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
QUESTIONS_URL = f"{API_URL}/questions"
|
| 19 |
FILES_URL = f"{API_URL}/files"
|
| 20 |
SUBMIT_URL = f"{API_URL}/submit"
|
|
|
|
| 21 |
|
| 22 |
class AgentState(TypedDict):
|
| 23 |
messages: Annotated[list[AnyMessage], add_messages]
|
|
@@ -28,7 +30,7 @@ class AgentState(TypedDict):
|
|
| 28 |
def build_gemini_llm():
|
| 29 |
if not os.environ.get("GOOGLE_API_KEY"):
|
| 30 |
raise ValueError("GOOGLE_API_KEY environment variable is not set.")
|
| 31 |
-
return ChatGoogleGenerativeAI(model = "gemini-3.7-flash", temperature = 0, max_output_tokens =
|
| 32 |
|
| 33 |
@tool
|
| 34 |
def add_numbers(a: int, b: int) -> int:
|
|
@@ -228,15 +230,16 @@ tools = [
|
|
| 228 |
multiply_numbers,
|
| 229 |
divide_numbers,
|
| 230 |
extract_text_from_image,
|
| 231 |
-
wikipedia_search,
|
| 232 |
-
download_and_read_file,
|
| 233 |
search_web
|
| 234 |
]
|
| 235 |
model_with_tools = model.bind_tools(tools)
|
| 236 |
|
| 237 |
def assistant(state: AgentState):
|
|
|
|
|
|
|
|
|
|
| 238 |
return {
|
| 239 |
-
"messages":
|
| 240 |
"file_path": state["file_path"],
|
| 241 |
"task_id": state["task_id"],
|
| 242 |
"url": state["url"]
|
|
@@ -251,4 +254,4 @@ builder.add_edge(START, "assistant")
|
|
| 251 |
builder.add_conditional_edges("assistant", tools_condition)
|
| 252 |
builder.add_edge("tools", "assistant")
|
| 253 |
|
| 254 |
-
graph = builder.compile()
|
|
|
|
| 12 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 13 |
from pathlib import Path
|
| 14 |
import tempfile
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
|
| 17 |
# constants
|
| 18 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 19 |
QUESTIONS_URL = f"{API_URL}/questions"
|
| 20 |
FILES_URL = f"{API_URL}/files"
|
| 21 |
SUBMIT_URL = f"{API_URL}/submit"
|
| 22 |
+
load_dotenv()
|
| 23 |
|
| 24 |
class AgentState(TypedDict):
|
| 25 |
messages: Annotated[list[AnyMessage], add_messages]
|
|
|
|
| 30 |
def build_gemini_llm():
|
| 31 |
if not os.environ.get("GOOGLE_API_KEY"):
|
| 32 |
raise ValueError("GOOGLE_API_KEY environment variable is not set.")
|
| 33 |
+
return ChatGoogleGenerativeAI(model = "gemini-3.7-flash", temperature = 0, max_output_tokens = 1025, include_thoughts=True)
|
| 34 |
|
| 35 |
@tool
|
| 36 |
def add_numbers(a: int, b: int) -> int:
|
|
|
|
| 230 |
multiply_numbers,
|
| 231 |
divide_numbers,
|
| 232 |
extract_text_from_image,
|
|
|
|
|
|
|
| 233 |
search_web
|
| 234 |
]
|
| 235 |
model_with_tools = model.bind_tools(tools)
|
| 236 |
|
| 237 |
def assistant(state: AgentState):
|
| 238 |
+
response = model_with_tools.invoke(state["messages"])
|
| 239 |
+
reasoning_tokens = response.usage_metadata["output_token_details"]["reasoning"]
|
| 240 |
+
print("Reasoning tokens used:", reasoning_tokens)
|
| 241 |
return {
|
| 242 |
+
"messages": [response],
|
| 243 |
"file_path": state["file_path"],
|
| 244 |
"task_id": state["task_id"],
|
| 245 |
"url": state["url"]
|
|
|
|
| 254 |
builder.add_conditional_edges("assistant", tools_condition)
|
| 255 |
builder.add_edge("tools", "assistant")
|
| 256 |
|
| 257 |
+
graph = builder.compile()
|