zbotta
agent with structured outputs
2fce62e
import os
from typing import TypedDict, List, Dict, Any, Optional
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage, AIMessage
from langgraph.graph.message import add_messages
from langgraph.graph import START, StateGraph
from langgraph.prebuilt import ToolNode, tools_condition
import sys
from pathlib import Path
from pydantic import BaseModel
import json
class OneResponse(BaseModel):
question_id: str
answer: str
root_path = Path(__file__).parent.parent.parent
print(f"Root path for imports: {root_path}")
sys.path.insert(0, str(Path(__file__).parent.parent))
from agent.tools.search_tool import search_tool
# --- zBottaAgent Definition ---
class zBottaAgent:
def __init__(self):
print("zBottaAgent initialized.")
chat = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0.7)
tools = [search_tool]
response_schema = OneResponse.model_json_schema()
self.chat_with_tools = chat.bind(
tools=tools,
response_mime_type="application/json",
response_schema=response_schema,
)
def __call__(self, question: str) -> str:
print(f"Agent received question (first 50 chars): {question[:50]}...")
messages = [
SystemMessage(content="You are a general AI assistant. I will ask you a question. " \
"Report your thoughts, and finish your answer. " \
"Your answer SHOULD BE: a number OR as few words as possible OR a comma separated list of numbers "
"and/or strings. If you are asked for a number, don't use comma to write your number neither use " \
"units such as $ or percent sign unless specified otherwise. If you are asked for a string, " \
"don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text " \
"unless specified otherwise. If you are asked for a comma separated list, apply the above rules " \
"depending of whether the element to be put in the list is a number or a string."),
HumanMessage(content=question)
]
response = self.chat_with_tools.invoke(messages)
print(f"Agent raw response: {response}")
answer = json.loads(response.content)
final_answer = answer["answer"]
print(f"Final answer: {final_answer}")
return final_answer
if __name__ == "__main__":
os.environ["GOOGLE_API_KEY"] = "AIzaSyA28oI_EEL3Io4I1OGF_Yj890ioKcCKWlo" # Set your Google API key here for testing
agent = zBottaAgent()
test_question = "What is the capital of France?"
print(f"Testing agent with question: {test_question}")
answer = agent(test_question)