Spaces:
Sleeping
Sleeping
Update multiagents.py
Browse files- multiagents.py +15 -62
multiagents.py
CHANGED
|
@@ -3,13 +3,15 @@
|
|
| 3 |
import os
|
| 4 |
import dotenv
|
| 5 |
from smolagents import CodeAgent
|
| 6 |
-
from smolagents import OpenAIServerModel
|
| 7 |
from tools.fetch import fetch_webpage, search_web
|
| 8 |
from smolagents import PythonInterpreterTool
|
| 9 |
from tools.yttranscript import get_youtube_transcript, get_youtube_title_description
|
| 10 |
from tools.stt import get_text_transcript_from_audio_file
|
| 11 |
from tools.image import analyze_image
|
| 12 |
from common.mylogger import mylog
|
|
|
|
|
|
|
| 13 |
import myprompts
|
| 14 |
|
| 15 |
from groq_api import GrokApi
|
|
@@ -41,70 +43,20 @@ dotenv.load_dotenv()
|
|
| 41 |
# api_base="https://api.openai.com/v1",
|
| 42 |
# api_key=os.environ["OPENAI_API_KEY"],
|
| 43 |
# )
|
|
|
|
|
|
|
|
|
|
| 44 |
# --- Agent wrappers ---
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# You could keep this for backward compatibility if needed
|
| 52 |
-
return self.generate(prompt, **kwargs)
|
| 53 |
-
|
| 54 |
-
def generate(self, messages=None, stop_sequences=None, max_tokens=None, temperature=None, **kwargs):
|
| 55 |
-
"""
|
| 56 |
-
Generate method compatible with smolagents framework.
|
| 57 |
-
|
| 58 |
-
Args:
|
| 59 |
-
messages: Can be a string prompt or list of messages
|
| 60 |
-
stop_sequences: List of strings where generation should stop (optional)
|
| 61 |
-
max_tokens: Maximum number of tokens to generate (optional)
|
| 62 |
-
temperature: Sampling temperature (optional)
|
| 63 |
-
**kwargs: Other parameters that might be passed by the framework
|
| 64 |
-
"""
|
| 65 |
-
try:
|
| 66 |
-
# Handle different input formats that smolagents might send
|
| 67 |
-
if messages is None:
|
| 68 |
-
# If no messages provided, check if prompt is in kwargs
|
| 69 |
-
prompt = kwargs.get('prompt', '')
|
| 70 |
-
elif isinstance(messages, str):
|
| 71 |
-
# If messages is a string, use it as prompt
|
| 72 |
-
prompt = messages
|
| 73 |
-
elif isinstance(messages, list) and len(messages) > 0:
|
| 74 |
-
# If messages is a list, extract the content from the last message
|
| 75 |
-
last_message = messages[-1]
|
| 76 |
-
if isinstance(last_message, dict):
|
| 77 |
-
prompt = last_message.get('content', str(last_message))
|
| 78 |
-
else:
|
| 79 |
-
prompt = str(last_message)
|
| 80 |
-
else:
|
| 81 |
-
prompt = str(messages) if messages else ''
|
| 82 |
-
|
| 83 |
-
# Call your GrokApi with the prompt
|
| 84 |
-
response = GrokApi(
|
| 85 |
-
system_prompt="You are a helpful assistant.",
|
| 86 |
-
user_input=prompt
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
# Convert response to string and handle stop_sequences if needed
|
| 90 |
-
response_text = str(response)
|
| 91 |
-
|
| 92 |
-
# If stop_sequences are provided, truncate the response at the first occurrence
|
| 93 |
-
if stop_sequences:
|
| 94 |
-
for stop_seq in stop_sequences:
|
| 95 |
-
if stop_seq in response_text:
|
| 96 |
-
response_text = response_text.split(stop_seq)[0]
|
| 97 |
-
break
|
| 98 |
-
|
| 99 |
-
return response_text
|
| 100 |
-
|
| 101 |
-
except Exception as e:
|
| 102 |
-
print(f"Error in GrokApiAgent.generate: {e}")
|
| 103 |
-
return f"Error generating response: {e}"
|
| 104 |
|
| 105 |
# Pick your agent here (just change one line to test different ones)
|
| 106 |
# My_Agent = BasicAgent
|
| 107 |
-
My_Agent =
|
| 108 |
|
| 109 |
def check_final_answer(final_answer, agent_memory) -> bool:
|
| 110 |
"""
|
|
@@ -193,7 +145,8 @@ class MultiAgent:
|
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|
| 195 |
# Example usage
|
| 196 |
-
|
|
|
|
| 197 |
question = """
|
| 198 |
What was the actual enrollment of the Malko competition in 2023?
|
| 199 |
"""
|
|
|
|
| 3 |
import os
|
| 4 |
import dotenv
|
| 5 |
from smolagents import CodeAgent
|
| 6 |
+
#from smolagents import OpenAIServerModel
|
| 7 |
from tools.fetch import fetch_webpage, search_web
|
| 8 |
from smolagents import PythonInterpreterTool
|
| 9 |
from tools.yttranscript import get_youtube_transcript, get_youtube_title_description
|
| 10 |
from tools.stt import get_text_transcript_from_audio_file
|
| 11 |
from tools.image import analyze_image
|
| 12 |
from common.mylogger import mylog
|
| 13 |
+
from smolagents import LiteLLMModel # Import LiteLLMModel instead of OpenAIServerModel
|
| 14 |
+
|
| 15 |
import myprompts
|
| 16 |
|
| 17 |
from groq_api import GrokApi
|
|
|
|
| 43 |
# api_base="https://api.openai.com/v1",
|
| 44 |
# api_key=os.environ["OPENAI_API_KEY"],
|
| 45 |
# )
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
# --- Agent wrappers ---
|
| 50 |
+
groq_model = LiteLLMModel(
|
| 51 |
+
model_id="qwen/qwen3-32b", # or any other Groq model like groq/mixtral-8x7b-32768
|
| 52 |
+
api_key=os.environ["GROQ_API_KEY"],
|
| 53 |
+
temperature=0.1,
|
| 54 |
+
max_tokens=4000,
|
| 55 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Pick your agent here (just change one line to test different ones)
|
| 58 |
# My_Agent = BasicAgent
|
| 59 |
+
My_Agent = groq_model
|
| 60 |
|
| 61 |
def check_final_answer(final_answer, agent_memory) -> bool:
|
| 62 |
"""
|
|
|
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
# Example usage
|
| 148 |
+
|
| 149 |
+
asyncio.run(main())
|
| 150 |
question = """
|
| 151 |
What was the actual enrollment of the Malko competition in 2023?
|
| 152 |
"""
|