lopera47 commited on
Commit
73a7230
·
verified ·
1 Parent(s): 848648c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -49
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
- # Define tools # Description matters a lot, unless youre using @tool as a decorator and a python function (pulls docstring as tool description)
62
- search = DuckDuckGoSearchRun()
63
- tools = [
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 agent # Langchain automatically creates a system prompt from this initialization depending on its parameters
71
- self.agent = initialize_agent(
72
- tools, # List of tools with names + descriptions
73
- llm, # The LLM to generate actions/thoughts
74
- agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, # Agent behavior type, in this case set to ReAct-style agent (great choice but depends)
75
- verbose=True # Prints out each reasoning step (for debugging)
76
  )
77
- # Function to be called by the workflow
78
  def __call__(self, question: str) -> str:
79
- print(f"Agent received question (first 50 chars): {question[:50]}...")
80
- # Do not return intermidiate steps or thoughts
81
- # response = self.agent.run(question)
82
- response = self.agent.invoke({"input": question})
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
  """