Thanks to visit codestin.com
Credit goes to github.com

Skip to content
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -2495,7 +2495,7 @@
"filename": "test/oai/test_gemini.py",
"hashed_secret": "7dfe63b6762fc69b8e486a2bafa43b8f7d23b788",
"is_verified": false,
"line_number": 51,
"line_number": 50,
"is_secret": false
}
],
Expand Down Expand Up @@ -3116,5 +3116,5 @@
}
]
},
"generated_at": "2025-02-16T23:27:38Z"
"generated_at": "2025-02-18T08:58:38Z"
}
13 changes: 8 additions & 5 deletions autogen/agents/experimental/websurfer/websurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

from typing import Any, Literal
from typing import Any, Literal, Optional

Check warning on line 5 in autogen/agents/experimental/websurfer/websurfer.py

View check run for this annotation

Codecov / codecov/patch

autogen/agents/experimental/websurfer/websurfer.py#L5

Added line #L5 was not covered by tests

from .... import ConversableAgent
from ....doc_utils import export_module
Expand All @@ -18,8 +18,9 @@

def __init__(
self,
*,
llm_config: dict[str, Any],
*args,
web_tool_llm_config: Optional[dict[str, Any]] = None,
web_tool: Literal["browser_use", "crawl4ai"] = "browser_use",
web_tool_kwargs: dict[str, Any] = None,
**kwargs: Any,
Expand All @@ -28,17 +29,19 @@

Args:
llm_config (dict[str, Any]): The LLM configuration.
web_tool_llm_config (dict[str, Any], optional): The LLM configuration for the web tool. I not provided, the llm_config will be used.
web_tool (Literal["browser_use", "crawl4ai"], optional): The web tool to use. Defaults to "browser_use".
web_tool_kwargs (dict[str, Any], optional): The keyword arguments for the web tool. Defaults to None.
"""
web_tool_kwargs = web_tool_kwargs if web_tool_kwargs else {}
web_tool_llm_config = web_tool_llm_config if web_tool_llm_config else llm_config

Check warning on line 37 in autogen/agents/experimental/websurfer/websurfer.py

View check run for this annotation

Codecov / codecov/patch

autogen/agents/experimental/websurfer/websurfer.py#L37

Added line #L37 was not covered by tests
if web_tool == "browser_use":
self.tool: Tool = BrowserUseTool(llm_config=llm_config, **web_tool_kwargs)
self.tool: Tool = BrowserUseTool(llm_config=web_tool_llm_config, **web_tool_kwargs)

Check warning on line 39 in autogen/agents/experimental/websurfer/websurfer.py

View check run for this annotation

Codecov / codecov/patch

autogen/agents/experimental/websurfer/websurfer.py#L39

Added line #L39 was not covered by tests
elif web_tool == "crawl4ai":
self.tool = Crawl4AITool(llm_config=llm_config, **web_tool_kwargs)
self.tool = Crawl4AITool(llm_config=web_tool_llm_config, **web_tool_kwargs)

Check warning on line 41 in autogen/agents/experimental/websurfer/websurfer.py

View check run for this annotation

Codecov / codecov/patch

autogen/agents/experimental/websurfer/websurfer.py#L41

Added line #L41 was not covered by tests
else:
raise ValueError(f"Unsupported {web_tool=}.")

super().__init__(*args, llm_config=llm_config, **kwargs)
super().__init__(llm_config=llm_config, **kwargs)

Check warning on line 45 in autogen/agents/experimental/websurfer/websurfer.py

View check run for this annotation

Codecov / codecov/patch

autogen/agents/experimental/websurfer/websurfer.py#L45

Added line #L45 was not covered by tests

self.register_for_llm()(self.tool)
21 changes: 20 additions & 1 deletion autogen/oai/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,20 @@
return [GeminiClient._convert_type_null_to_nullable(item) for item in schema]
return schema

@staticmethod
def _unwrap_references(function_parameters: dict[str, Any]) -> dict[str, Any]:
if "properties" not in function_parameters:
return function_parameters

Check warning on line 599 in autogen/oai/gemini.py

View check run for this annotation

Codecov / codecov/patch

autogen/oai/gemini.py#L599

Added line #L599 was not covered by tests

function_parameters_copy = copy.deepcopy(function_parameters)

Check warning on line 601 in autogen/oai/gemini.py

View check run for this annotation

Codecov / codecov/patch

autogen/oai/gemini.py#L601

Added line #L601 was not covered by tests

for property_name, property_value in function_parameters["properties"].items():
if "$defs" in property_value:
function_parameters_copy["properties"][property_name] = dict(jsonref.replace_refs(property_value))
function_parameters_copy["properties"][property_name].pop("$defs")

Check warning on line 606 in autogen/oai/gemini.py

View check run for this annotation

Codecov / codecov/patch

autogen/oai/gemini.py#L605-L606

Added lines #L605 - L606 were not covered by tests

return function_parameters_copy

Check warning on line 608 in autogen/oai/gemini.py

View check run for this annotation

Codecov / codecov/patch

autogen/oai/gemini.py#L608

Added line #L608 was not covered by tests

def _tools_to_gemini_tools(self, tools: list[dict[str, Any]]) -> list[Tool]:
"""Create Gemini tools (as typically requires Callables)"""
functions = []
Expand All @@ -601,10 +615,11 @@
tool["function"]["parameters"] = GeminiClient._convert_type_null_to_nullable(
tool["function"]["parameters"]
)
function_parameters = GeminiClient._unwrap_references(tool["function"]["parameters"])

Check warning on line 618 in autogen/oai/gemini.py

View check run for this annotation

Codecov / codecov/patch

autogen/oai/gemini.py#L618

Added line #L618 was not covered by tests
function = vaiFunctionDeclaration(
name=tool["function"]["name"],
description=tool["function"]["description"],
parameters=tool["function"]["parameters"],
parameters=function_parameters,
)
else:
function = GeminiClient._create_gemini_function_declaration(tool)
Expand Down Expand Up @@ -681,8 +696,12 @@
@staticmethod
def _create_gemini_function_parameters(function_parameter: dict[str, any]) -> dict[str, any]:
"""Convert function parameters to Gemini format, recursive"""
function_parameter = GeminiClient._unwrap_references(function_parameter)

Check warning on line 699 in autogen/oai/gemini.py

View check run for this annotation

Codecov / codecov/patch

autogen/oai/gemini.py#L699

Added line #L699 was not covered by tests

if "type" in function_parameter:
function_parameter["type"] = function_parameter["type"].upper()
# If the schema was created from pydantic BaseModel, it will "title" attribute which needs to be removed
function_parameter.pop("title", None)

Check warning on line 704 in autogen/oai/gemini.py

View check run for this annotation

Codecov / codecov/patch

autogen/oai/gemini.py#L704

Added line #L704 was not covered by tests

# Parameter properties and items
if "properties" in function_parameter:
Expand Down
65 changes: 52 additions & 13 deletions autogen/tools/experimental/deep_research/deep_research.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# SPDX-License-Identifier: Apache-2.0

import copy
from typing import Annotated, Any, Callable, Optional
from typing import Annotated, Any, Callable

Check warning on line 6 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L6

Added line #L6 was not covered by tests

from pydantic import BaseModel
from pydantic import BaseModel, Field

Check warning on line 8 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L8

Added line #L8 was not covered by tests

from ....agentchat import ConversableAgent
from ....doc_utils import export_module
Expand All @@ -16,16 +16,33 @@


class Subquestion(BaseModel):
question: Annotated[str, "The original question."]
answer: Annotated[Optional[str], "The answer to the question."] = None
question: Annotated[str, Field(description="The original question.")]

Check warning on line 19 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L19

Added line #L19 was not covered by tests

def format(self) -> str:
return f"Question: {self.question}\n"

Check warning on line 22 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L21-L22

Added lines #L21 - L22 were not covered by tests


class SubquestionAnswer(Subquestion):
answer: Annotated[str, Field(description="The answer to the question.")]

Check warning on line 26 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L25-L26

Added lines #L25 - L26 were not covered by tests

def format(self) -> str:
return f"Question: {self.question}\n{self.answer}\n"


class Task(BaseModel):
question: Annotated[str, "The original question."]
subquestions: Annotated[list[Subquestion], "The subquestions that need to be answered."]
question: Annotated[str, Field(description="The original question.")]
subquestions: Annotated[list[Subquestion], Field(description="The subquestions that need to be answered.")]

Check warning on line 34 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L33-L34

Added lines #L33 - L34 were not covered by tests

def format(self) -> str:
return f"Task: {self.question}\n\n" + "\n".join(

Check warning on line 37 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L36-L37

Added lines #L36 - L37 were not covered by tests
"Subquestion " + str(i + 1) + ":\n" + subquestion.format()
for i, subquestion in enumerate(self.subquestions)
)


class CompletedTask(BaseModel):
question: Annotated[str, Field(description="The original question.")]
subquestions: Annotated[list[SubquestionAnswer], Field(description="The subquestions and their answers")]

Check warning on line 45 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L43-L45

Added lines #L43 - L45 were not covered by tests

def format(self) -> str:
return f"Task: {self.question}\n\n" + "\n".join(
Expand Down Expand Up @@ -102,8 +119,15 @@
llm_config: Annotated[dict[str, Any], Depends(on(llm_config))],
max_web_steps: Annotated[int, Depends(on(max_web_steps))],
) -> str:
"""
Delegate a research task to the agent.
"""Delegate a research task to the agent.

Args:
task (str): The task to perform a research on.
llm_config (dict[str, Any]): The LLM configuration.
max_web_steps (int): The maximum number of web steps.

Returns:
str: The answer to the research task.
"""

@self.summarizer_agent.register_for_execution()
Expand Down Expand Up @@ -189,7 +213,7 @@
human_input_mode="NEVER",
)

generate_subquestions = DeepResearchTool.get_generate_subquestions(
generate_subquestions = DeepResearchTool._get_generate_subquestions(

Check warning on line 216 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L216

Added line #L216 was not covered by tests
llm_config=llm_config, max_web_steps=max_web_steps
)
decomposition_agent.register_for_execution()(generate_subquestions)
Expand All @@ -207,10 +231,20 @@
return split_question_and_answer_subquestions

@staticmethod
def get_generate_subquestions(
def _get_generate_subquestions(

Check warning on line 234 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L234

Added line #L234 was not covered by tests
llm_config: dict[str, Any],
max_web_steps: int,
) -> Callable[..., str]:
"""Get the generate_subquestions method.

Args:
llm_config (dict[str, Any]): The LLM configuration.
max_web_steps (int): The maximum number of web steps.

Returns:
Callable[..., str]: The generate_subquestions method.
"""

def generate_subquestions(
task: Task,
llm_config: Annotated[dict[str, Any], Depends(on(llm_config))],
Expand All @@ -219,12 +253,16 @@
if not task.subquestions:
task.subquestions = [Subquestion(question=task.question)]

subquestions_answers: list[SubquestionAnswer] = []

Check warning on line 256 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L256

Added line #L256 was not covered by tests
for subquestion in task.subquestions:
subquestion.answer = DeepResearchTool._answer_question(
answer = DeepResearchTool._answer_question(

Check warning on line 258 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L258

Added line #L258 was not covered by tests
subquestion.question, llm_config=llm_config, max_web_steps=max_web_steps
)
subquestions_answers.append(SubquestionAnswer(question=subquestion.question, answer=answer))

Check warning on line 261 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L261

Added line #L261 was not covered by tests

return f"{DeepResearchTool.SUBQUESTIONS_ANSWER_PREFIX} \n" + task.format()
completed_task = CompletedTask(question=task.question, subquestions=subquestions_answers)

Check warning on line 263 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L263

Added line #L263 was not covered by tests

return f"{DeepResearchTool.SUBQUESTIONS_ANSWER_PREFIX} \n" + completed_task.format()

Check warning on line 265 in autogen/tools/experimental/deep_research/deep_research.py

View check run for this annotation

Codecov / codecov/patch

autogen/tools/experimental/deep_research/deep_research.py#L265

Added line #L265 was not covered by tests

return generate_subquestions

Expand All @@ -245,7 +283,8 @@
return (content is not None) and content.startswith(DeepResearchTool.ANSWER_CONFIRMED_PREFIX)

websurfer_agent = WebSurferAgent(
llm_config=websurfer_config,
llm_config=llm_config,
web_tool_llm_config=websurfer_config,
name="WebSurferAgent",
system_message=(
"You are a web surfer agent responsible for gathering information from the web to provide information for answering a question\n"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ gemini = [
"google-auth",
"pillow",
"jsonschema",
"jsonref"
"jsonref>=1,<2"
]
together = ["together>=1.2"]
websurfer = ["beautifulsoup4", "markdownify", "pdfminer.six", "pathvalidate"]
Expand Down
Loading
Loading