Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,8 @@ from langchain_huggingface import HuggingFaceEndpoint
|
|
| 12 |
from langchain_community.chat_models import ChatHuggingFace
|
| 13 |
from langchain.llms.base import LLM
|
| 14 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# (Keep Constants as is)
|
| 17 |
# --- Constants ---
|
|
@@ -28,60 +30,24 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 28 |
|
| 29 |
# --- Basic Agent Definition ---
|
| 30 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 31 |
-
class BasicAgent: # Some times Inheritance is needed
|
| 32 |
-
def __init__(self):
|
| 33 |
-
print("BasicAgent with LangChain initialized.")
|
| 34 |
-
|
| 35 |
-
# # Create the LLM # Temprature set to 0 because we need exact match
|
| 36 |
-
# llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo") # ChatML understands roles (user, assistant, system)
|
| 37 |
|
| 38 |
-
# # Define the LLM
|
| 39 |
-
# llm = HuggingFaceHub(
|
| 40 |
-
# repo_id="tiiuae/falcon-7b-instruct",
|
| 41 |
-
# huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"],
|
| 42 |
-
# model_kwargs={"temperature": 0.5, "max_new_tokens": 512}
|
| 43 |
-
# )
|
| 44 |
-
|
| 45 |
-
# Define the LLM
|
| 46 |
-
llm = ChatHuggingFace(
|
| 47 |
-
repo_id="tiiuae/falcon-7b-instruct",
|
| 48 |
-
huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"],
|
| 49 |
-
temperature=0.5,
|
| 50 |
-
max_new_tokens=512
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
# # Define the LLM
|
| 54 |
-
# llm = HuggingFaceEndpoint(
|
| 55 |
-
# endpoint_url="https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct",
|
| 56 |
-
# huggingfacehub_api_token=os.environ["HUGGINGFACEHUB_API_TOKEN"],
|
| 57 |
-
# temperature=0.5,
|
| 58 |
-
# max_new_tokens=512
|
| 59 |
-
# )
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
Tool(name="Search",
|
| 65 |
-
func=search.run,
|
| 66 |
-
description="Useful for answering questions about current events, facts, or general knowledge by querying the web."),
|
| 67 |
-
# Can add custom Gaia tools here later
|
| 68 |
-
]
|
| 69 |
|
| 70 |
-
# Initialize
|
| 71 |
-
self.agent =
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
verbose=True # Prints out each reasoning step (for debugging)
|
| 76 |
)
|
| 77 |
-
|
| 78 |
def __call__(self, question: str) -> str:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
print(f"Agent response: {response}")
|
| 84 |
-
return response
|
| 85 |
|
| 86 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 87 |
"""
|
|
|
|
| 12 |
from langchain_community.chat_models import ChatHuggingFace
|
| 13 |
from langchain.llms.base import LLM
|
| 14 |
from huggingface_hub import InferenceClient
|
| 15 |
+
from somagents import Agent
|
| 16 |
+
from somagents.tools import WebSearchTool
|
| 17 |
|
| 18 |
# (Keep Constants as is)
|
| 19 |
# --- Constants ---
|
|
|
|
| 30 |
|
| 31 |
# --- Basic Agent Definition ---
|
| 32 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
class BasicAgent:
|
| 36 |
+
def __init__(self):
|
| 37 |
+
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Initialize the Agent with a text generation model (Falcon 7B instruct)
|
| 40 |
+
self.agent = Agent.from_model(
|
| 41 |
+
model="tiiuae/falcon-7b-instruct",
|
| 42 |
+
model_kwargs={"temperature": 0, "max_new_tokens": 512},
|
| 43 |
+
tools=[WebSearchTool()]
|
|
|
|
| 44 |
)
|
| 45 |
+
|
| 46 |
def __call__(self, question: str) -> str:
|
| 47 |
+
# Run the agent on the input question and return final answer
|
| 48 |
+
answer = self.agent.run(question)
|
| 49 |
+
print(answer)
|
| 50 |
+
return answer.strip()
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 53 |
"""
|