File size: 5,387 Bytes
b540717 82aac67 b540717 82aac67 b540717 7fe495d b540717 7fe495d b540717 7fe495d b540717 7fe495d b540717 82aac67 9de9186 b540717 82aac67 b540717 82aac67 c2b686e b540717 c2b686e b540717 84013cf b540717 c2b686e b540717 4e108c0 8eb2049 4e108c0 8eb2049 b540717 c2b686e 7fe495d b540717 82aac67 b540717 8eb2049 84013cf b540717 c2b686e 7fe495d b540717 82aac67 b540717 8eb2049 84013cf b540717 c2b686e c442328 c2b686e 8eb2049 84013cf c2b686e 84013cf b540717 c2b686e b540717 c2b686e b540717 82aac67 0ce4b1d b540717 84013cf b540717 82aac67 b540717 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# smol_agent.py
import os
from smolagents import MultiStepAgent, CodeAgent, OpenAIServerModel, FinalAnswerTool, PythonInterpreterTool
from tools.smol_tools import *
import prompts.custom_prompts as custom_prompts
from openai import OpenAI
from models import PatchedOpenAIServerModel
from dotenv import load_dotenv
load_dotenv()
# api keys
openai_api_key = os.getenv("OPENAI_API_KEY")
openrouter_api_key = os.getenv("OPENROUTER_API_KEY")
# models
gpt_4o_model = OpenAIServerModel(
model_id="gpt-4o",
api_key=openai_api_key,
)
gpt_41_model = OpenAIServerModel(
model_id="gpt-4.1",
api_key=openai_api_key,
)
gpt_41_mini_model = OpenAIServerModel(
model_id="gpt-4.1-mini",
api_key=openai_api_key,
)
o4_mini_model = PatchedOpenAIServerModel(
model_id="o4-mini",
api_key=openai_api_key,
)
deepseek_v3_model = OpenAIServerModel(
model_id="deepseek/deepseek-chat-v3-0324:free",
api_key=openrouter_api_key,
api_base="https://openrouter.ai/api/v1/"
)
deepseek_r1_model = OpenAIServerModel(
model_id="deepseek/deepseek-r1-0528:free",
api_key=openrouter_api_key,
api_base="https://openrouter.ai/api/v1/",
)
llma_3_2_vision_model = OpenAIServerModel(
model_id="meta-llama/llama-3.2-11b-vision-instruct:free",
api_key=openrouter_api_key,
api_base="https://openrouter.ai/api/v1/",
)
# model = PatchedOpenAIServerModel(
# model_id="o3-mini",
# openai_api_key=openai_api_key,
# )
# variables for testing
VERBOSITY_LEVEL = 0
def extract_final_answer(result):
if isinstance(result, dict) and "final_answer" in result:
return str(result["final_answer"]).strip()
return ""
# class FinalAgent(CodeAgent):
# def __call__(self, question: str) -> str:
# try:
# question = custom_prompts.system_prompt + "\nTHE QUESTION:\n" + question + '\n' + custom_prompts.output_prompt
# # get the raw answer
# answer = super().__call__(question)
# # final_answer = extract_final_answer(answer)
# return answer
# except Exception as e:
# error = f"An error occurred while processing the question: {e}"
# print(error)
# return error
# tools for the expert team of agents
HACKER_TOOLS = [
WikipediaSearchTool(),
# WikiSearchTool(),
VisitWebpageTool(),
# DuckDuckGoSearchTool(),
# CalculatorTool(),
Multiply(),
Divide(),
Add(),
Subtract(),
Modulus(),
FinalAnswerTool()
]
SCOUT_TOOLS = [
SpeechToTextTool(),
FinalAnswerTool(),
AnalyzeImage(),
YouTubeTranscript(),
]
# HACKER_IMPORTS = [
# "pandas",
# "numpy",
# "bs4",
# "requests",
# "markdownify"
# ]
AUTHORIZED_IMPORTS = [
# "pytube",
# "opencv-python",
"pandas",
"numpy",
"bs4",
"requests",
"markdownify"
]
# a genius hacker that can use the power of the web to answer questions
HACKER = CodeAgent(
name="hacker",
description = custom_prompts.HACKER_DESCRIPTION,
model=gpt_41_mini_model,
tools=HACKER_TOOLS,
verbosity_level=VERBOSITY_LEVEL,
add_base_tools=True,
additional_authorized_imports=AUTHORIZED_IMPORTS,
max_steps=6
)
SCOUT = CodeAgent(
name="scout",
description = custom_prompts.SCOUT_DESCRIPTION,
model=gpt_41_mini_model,
tools=SCOUT_TOOLS,
verbosity_level=VERBOSITY_LEVEL,
add_base_tools=True,
additional_authorized_imports=AUTHORIZED_IMPORTS,
max_steps=6
)
THINKER = CodeAgent(
name="thinker",
description = custom_prompts.THINKER_DESCRIPTION,
model=o4_mini_model,
tools=[FinalAnswerTool()],
verbosity_level=VERBOSITY_LEVEL,
add_base_tools=True,
additional_authorized_imports=AUTHORIZED_IMPORTS,
max_steps=6
)
# print("Final system_prompt being used:\n", custom_prompts.commander_prompt_templates["system_prompt"])
COMMANDER = CodeAgent(
name="commander",
description = custom_prompts.COMMANDER_DESCRIPTION,
model=gpt_41_mini_model,
add_base_tools=True,
tools=[PythonInterpreterTool(), FinalAnswerTool()],
managed_agents=[HACKER, SCOUT, THINKER],
planning_interval=2,
verbosity_level=VERBOSITY_LEVEL,
# additional_authorized_imports=AUTHORIZED_IMPORTS,
prompt_templates=custom_prompts.commander_prompt_templates,
max_steps=12,
)
class TaskForce:
def __init__(self):
self.commander = COMMANDER
def __call__(self, question: str) -> str:
try:
question = "\nTHE QUESTION:\n" + question + '\n' + custom_prompts.output_prompt
# get the raw answer
answer = self.commander.run(question)
# final_answer = extract_final_answer(answer)
return answer
except Exception as e:
error = f"An error occurred while processing the question: {e}"
print(error)
return error
# def get_agent():
# # return FinalAgent(
# # name="MyAgent",
# # model=model,
# # tools=MY_TOOLS,
# # add_base_tools=True,
# # planning_interval=2,
# # verbosity_level=1,
# # )
# for testing purposes
if __name__ == "__main__":
task_force = TaskForce()
question = "What is the capital of France?"
answer = HACKER.run(question)
# answer = task_force(question)
print(f"Answer: {answer}") |