sahil-datascience commited on
Commit
ef6992c
·
1 Parent(s): 6bc9639
Files changed (1) hide show
  1. app.py +9 -19
app.py CHANGED
@@ -9,7 +9,7 @@ from smolagents import (CodeAgent, InferenceClientModel,
9
  PythonInterpreterTool, VisitWebpageTool)
10
  from smolagents.agents import PromptTemplates
11
  import yaml
12
- from inference import InferenceClient
13
 
14
  # (Keep Constants as is)
15
  # --- Constants ---
@@ -19,27 +19,12 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
  with open("prompts.yaml", "r", encoding="utf-8") as f:
20
  prompts = yaml.safe_load(f)
21
 
22
- class InferenceClientModelWithSystemPrompt(InferenceClientModel):
23
- def __init__(self, model: str, system_prompt: str = ""):
24
- self.client = InferenceClient(model)
25
- self.system_prompt = system_prompt
26
- self.name = model
27
-
28
- def run(self, messages, **kwargs):
29
- # Prepend the system prompt
30
- if isinstance(messages, str):
31
- messages = [{"role": "user", "content": messages}]
32
- if messages[0]["role"] != "system":
33
- messages = [{"role": "system", "content": self.system_prompt}] + messages
34
- return self.client.chat(messages, **kwargs)
35
-
36
  # --- Basic Agent Definition ---
37
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
38
  class BasicAgent:
39
  def __init__(self):
40
- self.model = InferenceClientModelWithSystemPrompt(
41
- model="meta-llama/Llama-3-8B-Instruct",
42
- system_prompt=prompts["system"]
43
  )
44
  search_tool = DuckDuckGoSearchTool()
45
  wiki_tool = WikipediaSearchTool()
@@ -51,11 +36,16 @@ class BasicAgent:
51
  model=self.model,
52
  tools=[search_tool, wiki_tool, python_tool, visit_tool],
53
  )
 
54
  print("BasicAgent initialized.")
55
 
56
  def __call__(self, question: str) -> str:
57
  print(f"Agent received question (first 50 chars): {question[:50]}...")
58
- answer = self.agent.run(question)
 
 
 
 
59
  print(f"Agent returning answer: {answer}")
60
  # Remove "FINAL ANSWER:" if present
61
  if isinstance(answer, str) and "FINAL ANSWER:" in answer:
 
9
  PythonInterpreterTool, VisitWebpageTool)
10
  from smolagents.agents import PromptTemplates
11
  import yaml
12
+
13
 
14
  # (Keep Constants as is)
15
  # --- Constants ---
 
19
  with open("prompts.yaml", "r", encoding="utf-8") as f:
20
  prompts = yaml.safe_load(f)
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # --- Basic Agent Definition ---
23
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
24
  class BasicAgent:
25
  def __init__(self):
26
+ self.model = InferenceClientModel(
27
+ model="meta-llama/Llama-3-8B-Instruct"
 
28
  )
29
  search_tool = DuckDuckGoSearchTool()
30
  wiki_tool = WikipediaSearchTool()
 
36
  model=self.model,
37
  tools=[search_tool, wiki_tool, python_tool, visit_tool],
38
  )
39
+ self.system_prompt = prompts["system"]
40
  print("BasicAgent initialized.")
41
 
42
  def __call__(self, question: str) -> str:
43
  print(f"Agent received question (first 50 chars): {question[:50]}...")
44
+ messages = [
45
+ {"role": "system", "content": self.system_prompt},
46
+ {"role": "user", "content": question},
47
+ ]
48
+ answer = self.agent.run(messages)
49
  print(f"Agent returning answer: {answer}")
50
  # Remove "FINAL ANSWER:" if present
51
  if isinstance(answer, str) and "FINAL ANSWER:" in answer: