FD900 commited on
Commit
f5eb447
·
verified ·
1 Parent(s): ad3a04e

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +143 -31
agent.py CHANGED
@@ -1,36 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
- import requests
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-alpha"
5
- HEADERS = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
 
 
 
 
 
 
 
 
 
 
6
 
7
- system_prompt = (
8
- "You are an expert AI assistant taking a benchmark test for reasoning. "
9
- "Answer only the main question directly. Do not explain. Do not show work. "
10
- "If you see images or documents mentioned, assume you have access and extract answer. "
11
- "Use search or tools if needed. Answer format must be plain with no prefix or suffix."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  )
13
 
14
- class BasicAgent:
15
- def __init__(self):
16
- print("✅ Agent initialized with Zephyr 7B.")
17
-
18
- def __call__(self, question: str) -> str:
19
- prompt = f"{system_prompt}\n\nQuestion: {question}\nAnswer:"
20
- try:
21
- response = requests.post(
22
- API_URL,
23
- headers=HEADERS,
24
- json={"inputs": prompt},
25
- timeout=40
26
- )
27
- response.raise_for_status()
28
- result = response.json()
29
- if isinstance(result, list):
30
- return result[0]["generated_text"].split("Answer:")[-1].strip()
31
- elif "generated_text" in result:
32
- return result["generated_text"].strip()
33
- else:
34
- return "ERROR: Unexpected response format"
35
- except Exception as e:
36
- return f"AGENT ERROR: {e}"
 
1
+ # import smolagents.models as sm_models
2
+
3
+ # _orig_roles = sm_models.MessageRole.roles
4
+
5
+ # @classmethod
6
+ # def _roles_with_control(cls):
7
+ # return _orig_roles() + ["control"]
8
+
9
+ # sm_models.MessageRole.roles = _roles_with_control
10
+
11
+
12
+
13
+ from smolagents import (CodeAgent,
14
+ GradioUI,
15
+ LiteLLMModel,
16
+ OpenAIServerModel,
17
+ ChatMessage,
18
+ ToolCallingAgent)
19
+ from smolagents.default_tools import (DuckDuckGoSearchTool,
20
+ VisitWebpageTool,
21
+ WikipediaSearchTool,
22
+ SpeechToTextTool,
23
+ PythonInterpreterTool)
24
+ import yaml
25
+ from tools.final_answer import FinalAnswerTool, check_reasoning, ensure_formatting
26
+ from tools.tools import (youtube_frames_to_images, use_vision_model,
27
+ read_file, download_file_from_url,
28
+ extract_text_from_image, analyze_csv_file,
29
+ analyze_excel_file, youtube_transcribe,
30
+ transcribe_audio, review_youtube_video)
31
  import os
32
+ from dotenv import load_dotenv
33
+ import time
34
+
35
+ load_dotenv()
36
+
37
+ # Load prompts from YAML file
38
+ with open("prompts.yaml", 'r') as stream:
39
+ prompt_templates = yaml.safe_load(stream)
40
+
41
+
42
+ # class ThinkingLiteLLMModel(LiteLLMModel):
43
+ # def __init__(self, *args, **kwargs):
44
+ # # ensure the Litellm client also maps "control" → "control"
45
+ # cr = kwargs.pop("custom_role_conversions", {})
46
+ # cr["control"] = "control"
47
+ # super().__init__(*args, custom_role_conversions=cr, **kwargs)
48
 
49
+ # def __call__(self, messages, **kwargs) -> ChatMessage:
50
+ # NOTE: content must be a list of {type, text} dicts
51
+ # thinking_msg = {
52
+ # "role": "control",
53
+ # "content": [{"type": "text", "text": "thinking"}]
54
+ # }
55
+ # # prepend onto whatever messages the Agent built
56
+ # return super().__call__([thinking_msg] + messages, **kwargs)
57
+
58
+ class SlowLiteLLMModel(LiteLLMModel):
59
+ def __init__(self, *args, **kwargs):
60
+ super().__init__(*args, **kwargs)
61
 
62
+ def __call__(self, messages, **kwargs) -> ChatMessage:
63
+ time.sleep(15)
64
+ # prepend onto whatever messages the Agent built
65
+ return super().__call__(messages, **kwargs)
66
+
67
+ # # search_model_name = 'granite3.3:latest'
68
+ # search_model_name = 'cogito:14b'
69
+ # # search_model_name = 'qwen2:7b'
70
+ # search_model = ThinkingLiteLLMModel(model_id=f'ollama_chat/{search_model_name}',
71
+ # flatten_messages_as_text=True)
72
+
73
+ # web_agent = CodeAgent(
74
+ # model=search_model,
75
+ # tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), FinalAnswerTool()],
76
+ # max_steps=6,
77
+ # verbosity_level=1,
78
+ # grammar=None,
79
+ # planning_interval=6,
80
+ # name="web_agent",
81
+ # description="Searches the web using the and reviews web pages to find information.",
82
+ # additional_authorized_imports=['bs4', 'requests', 'io', 'wiki'],
83
+ # prompt_templates=prompt_templates
84
+ # )
85
+
86
+ # image_model_name = 'llama3.2-vision'
87
+ # image_model = OpenAIServerModel(model_id=image_model_name,
88
+ # api_base='http://localhost:11434/v1/',
89
+ # api_key='ollama',
90
+ # flatten_messages_as_text=False)
91
+ # image_agent = ToolCallingAgent(
92
+ # model=image_model,
93
+ # tools=[FinalAnswerTool()],
94
+ # max_steps=4,
95
+ # verbosity_level=2,
96
+ # grammar=None,
97
+ # planning_interval=6,
98
+ # #additional_authorized_imports=["PIL", "requests", "io", "numpy"],
99
+ # name="image_agent",
100
+ # description="Review images and videos for answers to questions based on visual data",
101
+ # prompt_templates=prompt_templates
102
+ # )
103
+
104
+ # react_model_name = 'qwen2:7b'
105
+ # # Initialize the chat model
106
+ # react_model = OpenAIServerModel(model_id=react_model_name,
107
+ # api_base='http://localhost:11434/v1/',
108
+ # api_key='ollama',
109
+ # flatten_messages_as_text=False)
110
+
111
+ react_model_name = "gemini/gemini-2.5-flash-preview-04-17"
112
+ react_model = LiteLLMModel(model_id=react_model_name,
113
+ api_key=os.getenv("GEMINI_KEY"),
114
+ temperature=0.2
115
+ )
116
+
117
+
118
+ manager_agent = CodeAgent(
119
+ model=react_model,
120
+ tools=[FinalAnswerTool(),
121
+ DuckDuckGoSearchTool(),
122
+ VisitWebpageTool(max_output_length=500000),
123
+ WikipediaSearchTool(extract_format='HTML'),
124
+ SpeechToTextTool(),
125
+ youtube_frames_to_images,
126
+ youtube_transcribe,
127
+ use_vision_model,
128
+ read_file, download_file_from_url,
129
+ extract_text_from_image,
130
+ analyze_csv_file, analyze_excel_file,
131
+ transcribe_audio,
132
+ review_youtube_video
133
+ ],
134
+ managed_agents=[],
135
+ additional_authorized_imports=['os', 'pandas', 'numpy', 'PIL', 'tempfile', 'PIL.Image'],
136
+ max_steps=20,
137
+ verbosity_level=1,
138
+ planning_interval=6,
139
+ name="Manager",
140
+ description="The manager of the team, responsible for overseeing and guiding the team's work.",
141
+ final_answer_checks=[check_reasoning, ensure_formatting],
142
+ prompt_templates=prompt_templates
143
  )
144
 
145
+
146
+
147
+ if __name__ == "__main__":
148
+ GradioUI(manager_agent).launch()