Spaces:
Sleeping
Sleeping
| from smolagents import ( | |
| CodeAgent, | |
| ToolCallingAgent, | |
| DuckDuckGoSearchTool, | |
| InferenceClientModel, | |
| GoogleSearchTool, | |
| VisitWebpageTool, | |
| tool, | |
| LiteLLMModel, | |
| ) | |
| from opentelemetry.sdk.trace import TracerProvider | |
| from openinference.instrumentation.smolagents import SmolagentsInstrumentor | |
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | |
| from opentelemetry.sdk.trace.export import SimpleSpanProcessor | |
| trace_provider = TracerProvider() | |
| trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter())) | |
| SmolagentsInstrumentor().instrument(tracer_provider=trace_provider) | |
| from tools.mathTools import multiply, add, subtract, divide, modulus | |
| from tools.searchTools import wiki_search, mini_web_search, arvix_search | |
| def check_reasoning(final_answer,agent_memory): | |
| model = InferenceClientModel("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B") #OpenAIServerModel("gpt-4o", max_tokens=8096) | |
| prompt = ( | |
| f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}." | |
| "Please check that the reasoning process is correct: do it correctly answer the given task?" | |
| "Remember answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string." | |
| "First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not." | |
| ) | |
| messages = [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| { | |
| "type": "text", | |
| "text": prompt, | |
| } | |
| ], | |
| } | |
| ] | |
| output = model(messages).content | |
| print("Feedback: ", output) | |
| if "FAIL" in output: | |
| raise Exception(output) | |
| return True | |
| def build_agents(): | |
| model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct") | |
| web_agent = ToolCallingAgent( | |
| model=model, | |
| tools=[ | |
| multiply, add, subtract, divide, modulus,wiki_search, mini_web_search, arvix_search, | |
| GoogleSearchTool(provider="serper"), | |
| VisitWebpageTool(), | |
| ], | |
| #add_base_tools=True, | |
| #additional_authorized_imports=["time","pandas","json","numpy","markdownify","requests","re"], | |
| name="web_search_agent", | |
| description="Browses the web to find information", | |
| verbosity_level=10, | |
| max_steps=10, | |
| ) | |
| coding_agent = CodeAgent( | |
| model = model, | |
| tools=[multiply, add, subtract, divide, modulus,wiki_search, mini_web_search, arvix_search,], | |
| additional_authorized_imports=["time","pandas","json","numpy","markdownify","requests","re"], | |
| name="coding_agent", | |
| description="A coding agent that can write and execute code to answer questions.", | |
| verbosity_level=10, | |
| max_steps=10, | |
| #prompt="You are a coding agent expert in python. I will ask you a question.", | |
| ) | |
| agent = CodeAgent( | |
| model=InferenceClientModel("deepseek-ai/DeepSeek-R1",max_tokens=8096), #InferenceClientModel("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B") ,# deepseek-ai/DeepSeek-R1", max_tokens=8096), | |
| tools=[multiply, add, subtract, divide, modulus,wiki_search, mini_web_search, arvix_search, | |
| ], | |
| managed_agents=[web_agent,coding_agent], | |
| additional_authorized_imports=["time","pandas","json","numpy","markdownify","requests","re"], | |
| planning_interval=5, | |
| max_steps=10, | |
| final_answer_checks=[check_reasoning], | |
| add_base_tools=True, | |
| ) | |
| prompt = f"""\nYou are a general AI assistant. I will ask you a question. | |
| Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. | |
| YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. | |
| If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. | |
| If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. | |
| If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.""" | |
| #print("Before concatenation:") | |
| #print(agent.prompt_templates["system_prompt"]) | |
| agent.prompt_templates["system_prompt"] = agent.prompt_templates["system_prompt"] + prompt | |
| #print("After concatenation:") | |
| #print(agent.prompt_templates["system_prompt"]) | |
| return agent |