|
from langchain_google_genai import GoogleGenerativeAI |
|
from crewai_tools import SerperDevTool |
|
import datetime |
|
import json |
|
import os |
|
import streamlit as st |
|
from typing import Dict, List, Tuple, Union |
|
from langchain_core.agents import AgentFinish |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
|
|
search_tool = SerperDevTool(n_results=5) |
|
|
|
llm = GoogleGenerativeAI( |
|
model="gemini-pro", google_api_key=os.getenv("GOOGLE_API_KEY")) |
|
|
|
|
|
|
|
def step_callback(agent_output: Union[str, List[Tuple[Dict, str]], AgentFinish], agent_name, *args): |
|
with st.chat_message("AI"): |
|
|
|
if isinstance(agent_output, str): |
|
try: |
|
agent_output = json.loads(agent_output) |
|
except json.JSONDecodeError: |
|
pass |
|
|
|
if isinstance(agent_output, list) and all( |
|
isinstance(item, tuple) for item in agent_output |
|
): |
|
|
|
for action, description in agent_output: |
|
|
|
st.write(f"Agent Name: {agent_name}") |
|
st.write(f"Tool used: {getattr(action, 'tool', 'Unknown')}") |
|
st.write( |
|
f"Tool input: {getattr(action, 'tool_input', 'Unknown')}") |
|
st.write(f"{getattr(action, 'log', 'Unknown')}") |
|
with st.expander("Show observation"): |
|
st.markdown(f"Observation\n\n{description}") |
|
|
|
|
|
elif isinstance(agent_output, AgentFinish): |
|
st.write(f"Agent Name: {agent_name}") |
|
output = agent_output.return_values |
|
st.write(f"I finished my task:\n{output['output']}") |
|
|
|
|
|
else: |
|
st.write(type(agent_output)) |
|
st.write(agent_output) |
|
|