krzsam commited on
Commit
86cbfce
·
1 Parent(s): cefe018
.gitignore CHANGED
@@ -3,3 +3,4 @@
3
  /questions.json
4
  /venv/
5
  /.idea/
 
 
3
  /questions.json
4
  /venv/
5
  /.idea/
6
+ /questions/
app.py CHANGED
@@ -1,13 +1,9 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
- from smolagents import CodeAgent, tool, InferenceClientModel, WebSearchTool, load_tool, PromptTemplates, Tool, FinalAnswerTool
7
- from dotenv import load_dotenv
8
- from my_prompt_config import PromptConfig
9
- from my_reverse_string import ReverseStringTool, ImageLoadTool
10
- from PIL import Image
11
 
12
  # (Keep Constants as is)
13
  # --- Constants ---
@@ -16,98 +12,29 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
  # testing --------------------------------------------
17
  testing_mode = True
18
  questions_to_run = [
19
- #"8e867cd7-cff9-4e6c-867a-ff5ddc2550be", # OK
20
- #"a1e91b78-d3d8-4675-bb8d-62741b4b68a6"
21
- #"2d83110e-a098-4ebb-9987-066c06fa42d0" # almost OK
22
- "cca530fc-4052-43b2-b130-b30968d8aa44"
23
- #"4fc2f1ae-8625-45b5-ab34-ad4433bc21f8"
24
- #"6f37996b-2ac7-44b0-8e68-6d28256631b4"
25
- #"9d191bce-651d-4746-be2d-7ef8ecadb9c2"
26
- #"cabe07ed-9eca-40ea-8ead-410ef5e83f91"
27
- #"3cef3a44-215e-4aed-8e3b-b1e3f08063b7"
28
- #"99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3"
29
- #"305ac316-eef6-4446-960a-92d80d542f82"
30
- #"f918266a-b3e0-4914-865d-4faa564f1aef"
31
- #"3f57289b-8c60-48be-bd80-01f8099ca449"
32
- #"1f975693-876d-457b-a649-393859e79bf3"
33
- #"840bfca7-4f7b-481a-8794-c560c340185d"
34
- #"bda648d7-d618-4883-88f4-3466eabd860e"
35
- #"cf106601-ab4f-4af9-b045-5295fe67b37d"
36
- #"a0c07678-e491-4bbc-8f0b-07405144218f"
37
- #"7bd855d8-463d-4ed5-93ca-5fe35145f733"
38
- #"5a0c1adf-205e-4841-a666-7c3ef95def9d"
39
  ]
40
  # testing --------------------------------------------
41
 
42
- # --- Basic Agent Definition ---
43
- # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
44
- class BasicAgent:
45
- #MODEL_CODER = "Qwen/Qwen2.5-Coder-32B-Instruct"
46
- MODEL_CODER = "Qwen/Qwen2.5-72B-Instruct"
47
- #MODEL_REASONING = "deepseek-ai/DeepSeek-R1"
48
- MODEL_REASONING = "Qwen/Qwen2.5-72B-Instruct"
49
-
50
- def __init__(self):
51
- load_dotenv()
52
- print("Agent initialized.")
53
- self.__create_agents__()
54
-
55
- def __create_agents__(self):
56
- web_search_agent = CodeAgent(
57
- tools=[WebSearchTool()],
58
- model=InferenceClientModel(model_id=self.MODEL_CODER),
59
- name="agent_websearch",
60
- description="Agent to browse and search and extract web content"
61
- )
62
-
63
- #reverse_agent = CodeAgent(
64
- # tools=[ReverseStringTool()],
65
- # model=InferenceClientModel(model_id=self.MODEL_CODER),
66
- # name="agent_reverse",
67
- # description="Agent to reverse strings"
68
- #)
69
-
70
- # self.image_generation_tool = load_tool("m-ric/text-to-image", trust_remote_code=True)
71
- image_generation_tool = Tool.from_space(
72
- "black-forest-labs/FLUX.1-schnell",
73
- name="image_generator",
74
- description="Generate an image from a prompt"
75
- )
76
-
77
- image_captioning_tool = Tool.from_space(
78
- "ovi054/image-to-prompt",
79
- name="image_captioning",
80
- description="Generate description of an image"
81
- )
82
-
83
- image_loading_tool = ImageLoadTool()
84
- print(f"Image load tool: {image_loading_tool}")
85
-
86
- #image_generation_agent = CodeAgent(
87
- # tools=[image_generation_tool],
88
- # model=InferenceClientModel(model_id=self.MODEL_CODER)
89
- #)
90
-
91
- # ImageLoadTool()
92
- self.reasoning_agent = CodeAgent(
93
- #tools=[image_generation_tool, image_captioning_tool, ReverseStringTool(), image_loading_tool],
94
- tools=[image_loading_tool, FinalAnswerTool()],
95
- model=InferenceClientModel(model_id=self.MODEL_REASONING),
96
- planning_interval=3, # This is where you activate planning!,
97
- prompt_templates=PromptConfig().PROMPT_TEMPLATES,
98
- managed_agents=[web_search_agent],
99
- additional_authorized_imports=["PIL","chess","my_tools","my_tools."],
100
- )
101
- print(f"Main agent initialized: {self.reasoning_agent}")
102
-
103
- def __call__(self, question: str) -> str:
104
- print(f"Agent received question (first 50 chars): {question[:50]}...")
105
-
106
- answer = self.reasoning_agent.run(question)
107
-
108
- print(f"Agent returning answer: {answer}")
109
- return answer
110
-
111
 
112
  # -----------------------------------------------------------------------------
113
  #
@@ -122,7 +49,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
122
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
123
 
124
  if profile:
125
- username= f"{profile.username}"
126
  print(f"User logged in: {username}")
127
  else:
128
  print("User not logged in.")
@@ -134,7 +61,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
134
 
135
  # 1. Instantiate Agent ( modify this part to create your agent)
136
  try:
137
- agent = BasicAgent()
138
  except Exception as e:
139
  print(f"Error instantiating agent: {e}")
140
  return f"Error initializing agent: {e}", None
@@ -184,8 +111,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
184
  if question_file_name is not None:
185
  ext = question_file_name[-4:]
186
  if ext == ".png":
187
- question_text = question_text + (f" . Use available tool to load an image associated with task id: "
188
- f"{task_id}")
189
 
190
  submitted_answer = agent(question_text)
191
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
+ from my_agent import MyAgent
6
+
 
 
 
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
 
12
  # testing --------------------------------------------
13
  testing_mode = True
14
  questions_to_run = [
15
+ #"8e867cd7-cff9-4e6c-867a-ff5ddc2550be", # good answer, wrong albums
16
+ #"a1e91b78-d3d8-4675-bb8d-62741b4b68a6",
17
+ #"2d83110e-a098-4ebb-9987-066c06fa42d0", # OK
18
+ #"cca530fc-4052-43b2-b130-b30968d8aa44", # OK
19
+ #"4fc2f1ae-8625-45b5-ab34-ad4433bc21f8",
20
+ #"6f37996b-2ac7-44b0-8e68-6d28256631b4",
21
+ #"9d191bce-651d-4746-be2d-7ef8ecadb9c2",
22
+ #"cabe07ed-9eca-40ea-8ead-410ef5e83f91",
23
+ #"3cef3a44-215e-4aed-8e3b-b1e3f08063b7",
24
+ #"99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3",
25
+ #"305ac316-eef6-4446-960a-92d80d542f82",
26
+ #"f918266a-b3e0-4914-865d-4faa564f1aef",
27
+ #"3f57289b-8c60-48be-bd80-01f8099ca449",
28
+ #"1f975693-876d-457b-a649-393859e79bf3",
29
+ #"840bfca7-4f7b-481a-8794-c560c340185d",
30
+ #"bda648d7-d618-4883-88f4-3466eabd860e",
31
+ #"cf106601-ab4f-4af9-b045-5295fe67b37d",
32
+ #"a0c07678-e491-4bbc-8f0b-07405144218f",
33
+ #"7bd855d8-463d-4ed5-93ca-5fe35145f733",
34
+ #"5a0c1adf-205e-4841-a666-7c3ef95def9d",
35
  ]
36
  # testing --------------------------------------------
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # -----------------------------------------------------------------------------
40
  #
 
49
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
50
 
51
  if profile:
52
+ username = f"{profile.username}"
53
  print(f"User logged in: {username}")
54
  else:
55
  print("User not logged in.")
 
61
 
62
  # 1. Instantiate Agent ( modify this part to create your agent)
63
  try:
64
+ agent = MyAgent()
65
  except Exception as e:
66
  print(f"Error instantiating agent: {e}")
67
  return f"Error initializing agent: {e}", None
 
111
  if question_file_name is not None:
112
  ext = question_file_name[-4:]
113
  if ext == ".png":
114
+ question_text = question_text + (f" Use one of available tools to load an image associated with "
115
+ f"task_id: '{task_id}'")
116
 
117
  submitted_answer = agent(question_text)
118
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
my_agent.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import CodeAgent, InferenceClientModel, WebSearchTool, FinalAnswerTool
2
+ #from smolagents import ApiModel
3
+ #from mistralai import Mistral
4
+ import os
5
+ # --------- my tools
6
+ from my_tool_reverse_string import ReverseStringTool
7
+ from my_tool_image_load import ImageLoadTool
8
+ from my_tool_chess_board import ChessBoard
9
+ from my_tool_fen import FENTool
10
+ from my_tool_chess_analysis import ChessAnalysisTool
11
+ from my_prompt_config import MyPromptConfig
12
+ from dotenv import load_dotenv
13
+
14
+ # https://huggingface.co/docs/transformers/model_doc/mistral?usage=Pipeline
15
+
16
+
17
+ # --- Basic Agent Definition ---
18
+ # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
19
+ class MyAgent:
20
+ #MODEL_REASONING = "Qwen/Qwen2.5-Coder-32B-Instruct"
21
+ #MODEL_REASONING = "deepseek-ai/DeepSeek-R1"
22
+ MODEL_REASONING = "Qwen/Qwen3-235B-A22B" # test more
23
+ #MODEL_REASONING = "mistralai/Mixtral-8x22B-v0.1" # not available via HuggingFace
24
+ chess_board_model_name = "my_chess_pieces_recognition.pth"
25
+ chess_board_model_dir = "/mnt/c/Users/krzsa/IdeaProjects/Agents-Course-Assignment/saved_models"
26
+
27
+ def __init__(self):
28
+ print("Agent initialized.")
29
+ load_dotenv()
30
+ self.mistral_api_key = os.environ["MISTRAL_API_KEY"]
31
+ self.__create_agents__()
32
+
33
+ def __create_agents__(self):
34
+ # --- Mistral -----------------------------------------------------------------------------------
35
+ # will need class implementation based on ApiModel
36
+ # https://github.com/huggingface/smolagents/blob/main/src/smolagents/models.py
37
+ # https://github.com/mistralai/client-python/tree/main/examples
38
+ #self.mistral_client = Mistral(api_key=self.mistral_api_key)
39
+ #self.model = ApiModel(
40
+ # model_id="mistral-large-2411",
41
+ # client=self.mistral_client
42
+ #)
43
+ # --- Mistral -----------------------------------------------------------------------------------
44
+
45
+ # --- HF Inference ------------------------------------------------------------------------------
46
+ self.model = InferenceClientModel(model_id=self.MODEL_REASONING)
47
+ # --- HF Inference ------------------------------------------------------------------------------
48
+
49
+ self.reasoning_agent = CodeAgent(
50
+ name="CourseAssistant",
51
+ description="General AI Assistant",
52
+ tools=[
53
+ ImageLoadTool(),
54
+ ReverseStringTool(),
55
+ ChessBoard(self.chess_board_model_name, self.chess_board_model_dir),
56
+ FENTool(),
57
+ ChessAnalysisTool(),
58
+ WebSearchTool(),
59
+ FinalAnswerTool(),
60
+ ],
61
+ model=self.model,
62
+ planning_interval=3, # This is where you activate planning!,
63
+ prompt_templates=MyPromptConfig.PROMPT_TEMPLATES,
64
+ managed_agents=[],
65
+ additional_authorized_imports=[
66
+ "PIL",
67
+ "chess",
68
+ "matplotlib",
69
+ "matplotlib.pyplot",
70
+ "stockfish",
71
+ "requests",
72
+ "wikipediaapi",
73
+ "bs4",
74
+ "my_tool_chess_analysis",
75
+ "my_tool_chess_board",
76
+ "my_tool_fen",
77
+ "my_tool_image_load",
78
+ "my_tool_reverse_string",
79
+ ],
80
+ )
81
+ #web_search_agent = CodeAgent(
82
+ # tools=[WebSearchTool()],
83
+ # model=InferenceClientModel(model_id=self.MODEL_CODER),
84
+ # name="agent_websearch",
85
+ # description="Agent to browse and search and extract web content"
86
+ #)
87
+
88
+ # self.image_generation_tool = load_tool("m-ric/text-to-image", trust_remote_code=True)
89
+ #image_generation_tool = Tool.from_space(
90
+ # "black-forest-labs/FLUX.1-schnell",
91
+ # name="image_generator",
92
+ # description="Generate an image from a prompt"
93
+ #)
94
+
95
+ #image_captioning_tool = Tool.from_space(
96
+ # "ovi054/image-to-prompt",
97
+ # name="image_captioning",
98
+ # description="Generate description of an image"
99
+ #)
100
+
101
+ #image_loading_tool = ImageLoadTool()
102
+ #print(f"Image load tool: {image_loading_tool}")
103
+
104
+ #image_generation_agent = CodeAgent(
105
+ # tools=[image_generation_tool],
106
+ # model=InferenceClientModel(model_id=self.MODEL_CODER)
107
+ #)
108
+
109
+ # ImageLoadTool()
110
+ #self.reasoning_agent = CodeAgent(
111
+ # #tools=[image_generation_tool, image_captioning_tool, ReverseStringTool(), image_loading_tool],
112
+ # tools=[image_loading_tool, FinalAnswerTool()],
113
+ # model=InferenceClientModel(model_id=self.MODEL_REASONING),
114
+ # planning_interval=3, # This is where you activate planning!,
115
+ # prompt_templates=PromptConfig().PROMPT_TEMPLATES,
116
+ # managed_agents=[web_search_agent],
117
+ # additional_authorized_imports=["PIL","chess","my_tools","my_tools."],
118
+ #)
119
+ print(f"Main agent initialized: {self.reasoning_agent}")
120
+
121
+ def __call__(self, question: str) -> str:
122
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
123
+
124
+ answer = self.reasoning_agent.run(question)
125
+
126
+ print(f"Agent returning answer: {answer}")
127
+ return answer
my_prompt_config.py CHANGED
@@ -1,17 +1,51 @@
1
  from smolagents import PromptTemplates, PlanningPromptTemplate, FinalAnswerPromptTemplate, ManagedAgentPromptTemplate
2
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- class PromptConfig:
5
  PROMPT_TEMPLATES = PromptTemplates(
6
  system_prompt="""
7
- You are a general AI assistant. I will ask you a question. Report your thoughts, and finish
8
- your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
9
- Describe your initial plan as a set of bullet points.
 
 
10
  Each bullet point should describe in one sentence an action which is to be taken in this step.
11
- Use the tools provided. If you are going to use a tool, describe in detail why you are going
12
- to use that particular tool and explain parameters used to invoke the tool.
13
- Analyze the question provided.
14
- Describe each step which needs to be taken to answer it.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  """,
16
  planning=PlanningPromptTemplate(
17
  initial_plan="""
@@ -30,12 +64,12 @@ class PromptConfig:
30
  post_messages="""
31
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of
32
  numbers and/or strings.
33
- If you are asked for a number, don’t use comma to write your number neither use units such as $ or percent
34
- sign unless specified otherwise.
35
- If you are asked for a string, don’t use articles, neither abbreviations (e.g. for cities), and write the digits in
36
- plain text unless specified otherwise.
37
- If you are asked for a comma separated list, apply the above rules depending of whether the element to be put
38
- in the list is a number or a string
39
  """
40
  ),
41
  )
 
1
  from smolagents import PromptTemplates, PlanningPromptTemplate, FinalAnswerPromptTemplate, ManagedAgentPromptTemplate
2
 
3
+ # Additional tools provided :
4
+ # * _my_tool_chess_analysis : analyze chess position provided in FEN notation and provide best move,
5
+ # available arguments: fen, player_color
6
+ # * _my_tool_chess_board : process an image and extract list of chess pieces, available arguments: img ,
7
+ # * _my_tool_fen : convert list of chess pieces into FEN notation, available arguments: chest_pieces ,
8
+ # * _my_tool_image_load : load an image for given task_id, available arguments: task_id ,
9
+ #
10
+ # Use <code></code> examples as here
11
+ # https://github.com/huggingface/smolagents/blob/main/src/smolagents/vision_web_browser.py
12
+ #
13
+ # * _my_tool_ reverse_string : reverse a string, available arguments: input_str ,
14
 
15
+ class MyPromptConfig:
16
  PROMPT_TEMPLATES = PromptTemplates(
17
  system_prompt="""
18
+ You are a general AI assistant.
19
+
20
+ Answer the following questions as best you can.
21
+
22
+ Describe your initial plan as a set of bullet points.
23
  Each bullet point should describe in one sentence an action which is to be taken in this step.
24
+ Do NOT provide hypothetical examples for the final answer.
25
+
26
+ Use the tools provided, and only those which are necessary to answer the question.
27
+ If you are going to use a tool, describe in detail how you are going
28
+ to invoke the tool and explain parameters used to invoke the tool.
29
+
30
+ The list of tools available:
31
+
32
+ * _my_tool_chess_analysis : analyze chess position provided as FEN notation
33
+ * _my_tool_chess_board : extract list of chess pieces from chess board image
34
+ * _my_tool_fen : convert list of chess pieces into FEN notation
35
+ * _my_tool_image_load : load image of the chess board for given task_id
36
+ * _my_tool_reverse_string : reverse provided string
37
+
38
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of
39
+ numbers and/or strings.
40
+ If you are asked for a number, don’t use comma to write your number neither use units such as $ or percent
41
+ sign unless specified otherwise.
42
+ If you are asked for a string, don’t use articles, neither abbreviations (e.g. for cities), and write the digits in
43
+ plain text unless specified otherwise.
44
+ If you are asked for a comma separated list, apply the above rules depending of whether the element to be put
45
+ in the list is a number or a string.
46
+
47
+ Report your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
48
+
49
  """,
50
  planning=PlanningPromptTemplate(
51
  initial_plan="""
 
64
  post_messages="""
65
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of
66
  numbers and/or strings.
67
+ If you are asked for a number, don’t use comma to write your number neither use units such as $ or
68
+ percent sign unless specified otherwise.
69
+ If you are asked for a string, don’t use articles, neither abbreviations (e.g. for cities),
70
+ and write the digits in plain text unless specified otherwise.
71
+ If you are asked for a comma separated list, apply the above rules depending of whether the element
72
+ to be put in the list is a number or a string
73
  """
74
  ),
75
  )
my_chess_analysis_tool.py → my_tool_chess_analysis.py RENAMED
@@ -1,5 +1,6 @@
1
  from smolagents import Tool
2
  from stockfish import Stockfish
 
3
 
4
 
5
  #AUTHORIZED_TYPES = [
@@ -19,9 +20,13 @@ from stockfish import Stockfish
19
 
20
 
21
  class ChessAnalysisTool(Tool):
22
- name = "_my_chess_analysis"
23
  description = """
24
- Analyze chess board provided in FEN notation and provide best move for given color
 
 
 
 
25
  """
26
 
27
  inputs = {
@@ -37,15 +42,28 @@ class ChessAnalysisTool(Tool):
37
 
38
  output_type = "string"
39
 
 
 
40
  def __init__(self):
 
41
  self.engine = Stockfish()
 
42
 
 
 
43
 
44
  def forward(self, fen: str, player_color: str) -> str:
45
- self.engine.set_fen_position(fen)
46
- board = self.engine.get_board_visual()
47
- print(f"{board}")
48
- top_3_moves = self.engine.get_top_moves(3)
49
- print(f"Top 3 moves:\n{top_3_moves}")
50
- best_move = self.engine.get_best_move()
51
- return best_move
 
 
 
 
 
 
 
 
1
  from smolagents import Tool
2
  from stockfish import Stockfish
3
+ import traceback
4
 
5
 
6
  #AUTHORIZED_TYPES = [
 
20
 
21
 
22
  class ChessAnalysisTool(Tool):
23
+ name = "_my_tool_chess_analysis"
24
  description = """
25
+ Analyze chess position provided as FEN notation and answer with best move for player color
26
+ To invoke the tool use code as below
27
+ <code>
28
+ best_answer = _my_tool_chess_analysis(fen=loaded_fen, player_color="dummy")
29
+ </code>
30
  """
31
 
32
  inputs = {
 
42
 
43
  output_type = "string"
44
 
45
+ is_initialized = True
46
+
47
  def __init__(self):
48
+ print(f"***KS*** Chess analysis tool initializing ...")
49
  self.engine = Stockfish()
50
+ print(f"***KS*** Chess analysis tool initialized with engine: {self.engine}")
51
 
52
+ def initialize(self):
53
+ print(f"***KS*** Chess analysis tool initialized (2) with engine: {self.engine}")
54
 
55
  def forward(self, fen: str, player_color: str) -> str:
56
+ print(f"***KS*** Chess analysis tool getting best move for player: {player_color} with position: {fen}")
57
+ best_move = ""
58
+ try:
59
+ self.engine.set_fen_position(fen)
60
+ board = self.engine.get_board_visual()
61
+ print(f"{board}")
62
+ top_3_moves = self.engine.get_top_moves(3)
63
+ print(f"Top 3 moves:\n{top_3_moves}")
64
+ best_move = self.engine.get_best_move()
65
+ except Exception as ex:
66
+ print(traceback.format_exc())
67
+ print(f"***KS*** Exception invoking ChessAnalysisTool: {ex}")
68
+
69
+ return f"FINAL ANSWER: {best_move}"
my_chess_board_tool.py → my_tool_chess_board.py RENAMED
@@ -10,9 +10,13 @@ import traceback
10
 
11
  # Based on; https://github.com/kratos606/chessboard-recogniser/tree/main
12
  class ChessBoard(Tool):
13
- name = "_my_chess_board"
14
  description = """
15
- Process an image representing a chess board and return board position as a list of chess pieces
 
 
 
 
16
  """
17
 
18
  inputs = {
 
10
 
11
  # Based on; https://github.com/kratos606/chessboard-recogniser/tree/main
12
  class ChessBoard(Tool):
13
+ name = "_my_tool_chess_board"
14
  description = """
15
+ Process an image of a chess board and return board position as a list of chess pieces
16
+ To invoke the tool use code as below
17
+ <code>
18
+ chess_pieces = _my_tool_chess_board(img=loaded_image)
19
+ </code>
20
  """
21
 
22
  inputs = {
my_fen_tool.py → my_tool_fen.py RENAMED
@@ -21,13 +21,17 @@ import textwrap
21
 
22
 
23
  class FENTool(Tool):
24
- name = "_my_fen_tool"
25
  description = """
26
- Convert a bord provided a s alist of chest pieces into FEN notation
 
 
 
 
27
  """
28
 
29
  inputs = {
30
- "_inp": {
31
  "type": "string",
32
  "description": "string with chest pieces",
33
  }
@@ -41,8 +45,8 @@ class FENTool(Tool):
41
  .replace('111111','6').replace('11111','5') \
42
  .replace('1111','4').replace('111','3').replace('11','2')
43
 
44
- def forward(self, _inp: str) -> str:
45
- arr = textwrap.wrap(_inp, 8)
46
  #print(f"Arr: \n{arr}")
47
 
48
  arr = arr[::-1]
 
21
 
22
 
23
  class FENTool(Tool):
24
+ name = "_my_tool_fen"
25
  description = """
26
+ Convert a list of chess pieces into FEN notation
27
+ To invoke the tool use code as below
28
+ <code>
29
+ loaded_fen = _my_tool_fen(chest_pieces="dummy")
30
+ </code>
31
  """
32
 
33
  inputs = {
34
+ "chest_pieces": {
35
  "type": "string",
36
  "description": "string with chest pieces",
37
  }
 
45
  .replace('111111','6').replace('11111','5') \
46
  .replace('1111','4').replace('111','3').replace('11','2')
47
 
48
+ def forward(self, chest_pieces: str) -> str:
49
+ arr = textwrap.wrap(chest_pieces, 8)
50
  #print(f"Arr: \n{arr}")
51
 
52
  arr = arr[::-1]
my_image_load.py → my_tool_image_load.py RENAMED
@@ -18,9 +18,13 @@ from io import BytesIO
18
 
19
 
20
  class ImageLoadTool(Tool):
21
- name = "_my_image_load"
22
  description = """
23
  Load image for the provided task id
 
 
 
 
24
  """
25
 
26
  inputs = {
 
18
 
19
 
20
  class ImageLoadTool(Tool):
21
+ name = "_my_tool_image_load"
22
  description = """
23
  Load image for the provided task id
24
+ To invoke the tool use code as below
25
+ <code>
26
+ loaded_image = _my_tool_image_load(task_id="dummy")
27
+ </code>
28
  """
29
 
30
  inputs = {
my_reverse_string.py → my_tool_reverse_string.py RENAMED
@@ -16,13 +16,17 @@ from smolagents import Tool
16
 
17
 
18
  class ReverseStringTool(Tool):
19
- name = "_my_reverse_string"
20
  description = """
21
- Decode a string which is provided in a reversed form.
 
 
 
 
22
  """
23
 
24
  inputs = {
25
- "_inp": {
26
  "type": "string",
27
  "description": "encoded input string",
28
  }
@@ -30,8 +34,8 @@ class ReverseStringTool(Tool):
30
 
31
  output_type = "string"
32
 
33
- def forward(self, _inp: str) -> str:
34
  _out = ""
35
- for a in _inp:
36
  _out = a + _out
37
  return _out
 
16
 
17
 
18
  class ReverseStringTool(Tool):
19
+ name = "_my_tool_reverse_string"
20
  description = """
21
+ Decode a string which is provided in a reversed form
22
+ To invoke the tool use code as below
23
+ <code>
24
+ reversed_string = _my_tool_reverse_string(input_str="dummy")
25
+ </code>
26
  """
27
 
28
  inputs = {
29
+ "input_str": {
30
  "type": "string",
31
  "description": "encoded input string",
32
  }
 
34
 
35
  output_type = "string"
36
 
37
+ def forward(self, input_str: str) -> str:
38
  _out = ""
39
+ for a in input_str:
40
  _out = a + _out
41
  return _out
requirements.txt CHANGED
@@ -7,4 +7,8 @@ matplotlib
7
  PyQt6
8
  opencv-python
9
  torch
10
- stockfish
 
 
 
 
 
7
  PyQt6
8
  opencv-python
9
  torch
10
+ stockfish
11
+ wikipedia-api
12
+ bs4
13
+ mistral-inference
14
+ mistralai
simple.py CHANGED
@@ -1,25 +1,20 @@
1
- import os
2
- import gradio as gr
3
- import requests
4
- import inspect
5
- import pandas as pd
6
  from smolagents import CodeAgent, tool, InferenceClientModel, WebSearchTool, load_tool, PromptTemplates, Tool, FinalAnswerTool
7
  from smolagents import PromptTemplates, PlanningPromptTemplate, FinalAnswerPromptTemplate, ManagedAgentPromptTemplate
8
 
9
- from dotenv import load_dotenv
10
- from my_reverse_string import ReverseStringTool
11
- from my_image_load import ImageLoadTool
12
- from my_chess_board_tool import ChessBoard
13
- from my_fen_tool import FENTool
14
- from my_chess_analysis_tool import ChessAnalysisTool
15
- from PIL import Image
16
 
17
 
18
  task_id = "cca530fc-4052-43b2-b130-b30968d8aa44"
19
 
20
  MODEL_REASONING = "Qwen/Qwen2.5-Coder-32B-Instruct"
21
- MODEL_CHESS = "jayasuryajsk/chess-reasoner-qwen"
22
- #MODEL_REASONING = "Qwen/Qwen2.5-72B-Instruct" not good
 
 
23
  #"meta-llama/Meta-Llama-3-70B-Instruct"
24
  # jayasuryajsk/chess-reasoner-qwen
25
  # https://huggingface.co/jayasuryajsk/chess-reasoner-qwen
@@ -34,16 +29,21 @@ PROMPT_TEMPLATES = PromptTemplates(
34
  Each bullet point should describe in one sentence an action which is to be taken in this step.
35
  Do NOT provide hypothetical examples for the final answer.
36
 
37
- Use the tools provided. If you are going to use a tool, describe in detail how you are going
 
38
  to use that particular tool and explain parameters used to invoke the tool.
39
 
40
  Tools provided :
41
- _my_image_load : load an image for given task_id ,
42
- _my_chess_board : process an image and extract list of chess pieces ,
43
- _my_fen_tool : convert list of chess pieces into FEN notation ,
44
- _my_chess_analysis : analyze chess position provided in FEN notation and provide best move ,
45
- _my_reverse_string : reverse a string ,
46
- final_answer : provide final answer
 
 
 
 
47
 
48
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of
49
  numbers and/or strings.
@@ -80,23 +80,15 @@ PROMPT_TEMPLATES = PromptTemplates(
80
 
81
  #question = f"Load an image for task id {task_id} and describe the chess position shown on the image. "
82
  #question = f"Load an image for task id {task_id} and display it using matplotlib "
83
- question = (f"Load an image for task id {task_id}, extyract checc pieces position into FEN notation, and provide "
84
- f"best move for black ")
 
 
 
85
 
86
  chess_board_model_name = "my_chess_pieces_recognition.pth"
87
  chess_board_model_dir = "/mnt/c/Users/krzsa/IdeaProjects/Agents-Course-Assignment/saved_models"
88
 
89
- #chess_agent = CodeAgent(
90
- # name="ChessAgent",
91
- # description="Chess Board Analysis Agent",
92
- # tools=[],
93
- # model=InferenceClientModel(model_id=MODEL_CHESS),
94
- # #planning_interval=3, # This is where you activate planning!,
95
- # #prompt_templates=PROMPT_TEMPLATES,
96
- # #managed_agents=[web_search_agent],
97
- # additional_authorized_imports=[],
98
- #)
99
-
100
  reasoning_agent = CodeAgent(
101
  name="CourseAssistant",
102
  description="General AI Assistant",
@@ -106,7 +98,8 @@ reasoning_agent = CodeAgent(
106
  ReverseStringTool(),
107
  ChessBoard(chess_board_model_name, chess_board_model_dir),
108
  FENTool(),
109
- ChessAnalysisTool()
 
110
  ],
111
  model=InferenceClientModel(model_id=MODEL_REASONING),
112
  planning_interval=3, # This is where you activate planning!,
@@ -117,6 +110,7 @@ reasoning_agent = CodeAgent(
117
  "chess",
118
  "matplotlib",
119
  "matplotlib.pyplot",
 
120
  "my_chess_board_tool",
121
  "my_fen_tool",
122
  "my_image_load",
 
 
 
 
 
 
1
  from smolagents import CodeAgent, tool, InferenceClientModel, WebSearchTool, load_tool, PromptTemplates, Tool, FinalAnswerTool
2
  from smolagents import PromptTemplates, PlanningPromptTemplate, FinalAnswerPromptTemplate, ManagedAgentPromptTemplate
3
 
4
+ from my_tool_reverse_string import ReverseStringTool
5
+ from my_tool_image_load import ImageLoadTool
6
+ from my_tool_chess_board import ChessBoard
7
+ from my_tool_fen import FENTool
8
+ from my_tool_chess_analysis import ChessAnalysisTool
 
 
9
 
10
 
11
  task_id = "cca530fc-4052-43b2-b130-b30968d8aa44"
12
 
13
  MODEL_REASONING = "Qwen/Qwen2.5-Coder-32B-Instruct"
14
+ #MODEL_REASONING = "deepseek-ai/deepseek-coder-33b-instruct" # not available
15
+ #MODEL_REASONING = "deepseek-ai/DeepSeek-Coder-V2-Instruct" # not available
16
+ #MODEL_CHESS = "jayasuryajsk/chess-reasoner-qwen"
17
+ #MODEL_REASONING = "Qwen/Qwen2.5-72B-Instruct" # not good
18
  #"meta-llama/Meta-Llama-3-70B-Instruct"
19
  # jayasuryajsk/chess-reasoner-qwen
20
  # https://huggingface.co/jayasuryajsk/chess-reasoner-qwen
 
29
  Each bullet point should describe in one sentence an action which is to be taken in this step.
30
  Do NOT provide hypothetical examples for the final answer.
31
 
32
+ Use the tools provided, and only those which are necessary to answer the question.
33
+ If you are going to use a tool, describe in detail how you are going
34
  to use that particular tool and explain parameters used to invoke the tool.
35
 
36
  Tools provided :
37
+ * _my_image_load : load an image for given task_id, available arguments: task_id ,
38
+
39
+ Use <code></code> examples as here
40
+ https://github.com/huggingface/smolagents/blob/main/src/smolagents/vision_web_browser.py
41
+
42
+ * _my_chess_board : process an image and extract list of chess pieces, available arguments: img ,
43
+ * _my_fen_tool : convert list of chess pieces into FEN notation, available arguments: chest_pieces ,
44
+ * _my_chess_analysis : analyze chess position provided in FEN notation and provide best move,
45
+ available arguments: fen, player_color
46
+ * _my_reverse_string : reverse a string, available arguments: input_str ,
47
 
48
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of
49
  numbers and/or strings.
 
80
 
81
  #question = f"Load an image for task id {task_id} and describe the chess position shown on the image. "
82
  #question = f"Load an image for task id {task_id} and display it using matplotlib "
83
+ #question = (f"Load an image for task id {task_id}, extyract checc pieces position into FEN notation, and provide "
84
+ # f"best move for black ")
85
+ question = ("How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the "
86
+ "latest "
87
+ "2022 version of english wikipedia.")
88
 
89
  chess_board_model_name = "my_chess_pieces_recognition.pth"
90
  chess_board_model_dir = "/mnt/c/Users/krzsa/IdeaProjects/Agents-Course-Assignment/saved_models"
91
 
 
 
 
 
 
 
 
 
 
 
 
92
  reasoning_agent = CodeAgent(
93
  name="CourseAssistant",
94
  description="General AI Assistant",
 
98
  ReverseStringTool(),
99
  ChessBoard(chess_board_model_name, chess_board_model_dir),
100
  FENTool(),
101
+ ChessAnalysisTool(),
102
+ WebSearchTool()
103
  ],
104
  model=InferenceClientModel(model_id=MODEL_REASONING),
105
  planning_interval=3, # This is where you activate planning!,
 
110
  "chess",
111
  "matplotlib",
112
  "matplotlib.pyplot",
113
+ "stockfish",
114
  "my_chess_board_tool",
115
  "my_fen_tool",
116
  "my_image_load",
test_tools.py CHANGED
@@ -1,8 +1,8 @@
1
- from my_reverse_string import ReverseStringTool
2
- from my_image_load import ImageLoadTool
3
- from my_chess_board_tool import ChessBoard
4
- from my_fen_tool import FENTool
5
- from my_chess_analysis_tool import ChessAnalysisTool
6
  import pytest
7
  import matplotlib.pyplot as plt
8
  import matplotlib as mp
 
1
+ from my_tool_reverse_string import ReverseStringTool
2
+ from my_tool_image_load import ImageLoadTool
3
+ from my_tool_chess_board import ChessBoard
4
+ from my_tool_fen import FENTool
5
+ from my_tool_chess_analysis import ChessAnalysisTool
6
  import pytest
7
  import matplotlib.pyplot as plt
8
  import matplotlib as mp