Agentic_chatbox / app.py
thuyentruong's picture
Update app.py
03f3bb6 verified
"""
This is an example template to build a chatbox app using agentic AI
Given a question, this agent will:
+ Derive websearch query
+ Retrieve relevant information
+ Interprete the information and return the answer
To make the agent more capable, we can add other tools in the future, for example, CSV loading, calculations
Agentic framework used: Smolagents (HuggingFace)
LLM: gemini/gemini-2.0-flash-lite
@Contributor: nhatquynhthuyen.truong@cotiviti.com
@Org: Cotiviti AU
@Release Date: 28 Aug 2025
@Last Update: 28 Aug 2025
"""
import os
os.system("pip install -q smolagents ddgs litellm markdownify requests")
os.system("pip install -q llm_rs")
import gradio as gr
import requests
import inspect
import pandas as pd
from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, VisitWebpageTool
import time
import random
import logging
import numpy as np
from typing import List
import markdownify
from smolagents import tool
api_key = os.environ.get('Google_api_key')
llm = LiteLLMModel(model_id="gemini/gemini-2.0-flash-lite", api_key=api_key)
SYS_PROMPT = """You are an assistant for answering questions.
If you don't know the answer, it's OK to make a guess."""
# --- Basic Agent Definition ---
class BasicAgent:
def __init__(self):
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool(), VisitWebpageTool()
], model=llm)
print("BasicAgent initialized.")
def __call__(self, question: str) -> str:
print(f"Agent received question (first 50 chars): {question[:50]}...")
final_answer = self.agent.run(question)
print(f"Agent returning final answer: {final_answer}")
return final_answer
def run_and_submit_all(question_text):
"""
Fetches the question, runs the BasicAgent on it and return answer
"""
# 1. Instantiate Agent
try:
agent = BasicAgent()
except Exception as e:
print(f"Error instantiating agent: {e}")
return f"Error initializing agent: {e}", None
# 2. Run Agent
try:
agent_answer = agent(question_text)
except Exception as e:
print(f"Error running agent on question {question_text}: {e}")
return agent_answer
# --- Build Gradio Interface using Blocks ---
demo = gr.Blocks()
agentic_QA = gr.Interface(
fn=run_and_submit_all,
inputs=gr.Textbox(label="Question", type="text"),
outputs=gr.Textbox(label="Answer", type="text"),
title="Agentic questions and answer",
description="""
Model: gemini/gemini-2.0-flash-lite <br>
Agentic framework: smolagent
"""
)
with demo:
gr.TabbedInterface([agentic_QA], ["Agentic questions and answer"])
demo.launch()
if __name__ == "__main__":
print("Launching Gradio Interface for Basic Agent Evaluation...")
demo.launch(debug=True, share=False)