Spaces:
Sleeping
Sleeping
first agent defintion
Browse files
app.py
CHANGED
|
@@ -3,21 +3,56 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from langchain.agents import initialize_agent, Tool
|
| 7 |
+
from langchain.agents.agent_types import AgentType
|
| 8 |
+
from langchain.chat_models import ChatOpenAI
|
| 9 |
+
from langchain.tools import DuckDuckGoSearchRun
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
+
# class BasicAgent:
|
| 16 |
+
# def __init__(self):
|
| 17 |
+
# print("BasicAgent initialized.")
|
| 18 |
+
# def __call__(self, question: str) -> str:
|
| 19 |
+
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 20 |
+
# fixed_answer = "This is a default answer."
|
| 21 |
+
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 22 |
+
# return fixed_answer
|
| 23 |
+
|
| 24 |
# --- Basic Agent Definition ---
|
| 25 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 26 |
+
class BasicAgent: # Some times Inheritance is needed
|
| 27 |
def __init__(self):
|
| 28 |
+
print("BasicAgent with LangChain initialized.")
|
| 29 |
+
|
| 30 |
+
# Create the LLM # Temprature set to 0 because we need exact match
|
| 31 |
+
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo") # ChatML understands roles (user, assistant, system)
|
| 32 |
+
|
| 33 |
+
# Define tools # Description matters a lot, unless youre using @tool as a decorator and a python function (pulls docstring as tool description)
|
| 34 |
+
search = DuckDuckGoSearchRun()
|
| 35 |
+
tools = [
|
| 36 |
+
Tool(name="Search",
|
| 37 |
+
func=search.run,
|
| 38 |
+
description="Useful for answering questions about current events, facts, or general knowledge by querying the web."),
|
| 39 |
+
# Can add custom Gaia tools here later
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
# Initialize agent # Langchain automatically creates a system prompt from this initialization depending on its parameters
|
| 43 |
+
self.agent = initialize_agent(
|
| 44 |
+
tools, # List of tools with names + descriptions
|
| 45 |
+
llm, # The LLM to generate actions/thoughts
|
| 46 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, # Agent behavior type, in this case set to ReAct-style agent (great choice but depends)
|
| 47 |
+
verbose=True # Prints out each reasoning step (for debugging)
|
| 48 |
+
)
|
| 49 |
+
# Function to be called by the workflow
|
| 50 |
def __call__(self, question: str) -> str:
|
| 51 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 52 |
+
# Do not return intermidiate steps or thoughts
|
| 53 |
+
response = self.agent.run(question)
|
| 54 |
+
print(f"Agent response: {response}")
|
| 55 |
+
return response
|
| 56 |
|
| 57 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 58 |
"""
|