8000 Agent.run() can return RunResult object by aymeric-roucher · Pull Request #1337 · huggingface/smolagents · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Agent.run() can return RunResult object #1337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
May 20, 2025
Merged

Agent.run() can return RunResult object #1337

merged 29 commits into from
May 20, 2025

Conversation

aymeric-roucher
Copy link
Collaborator

No description provided.

@aymeric-roucher aymeric-roucher changed the title Start work on run results Agent.run() returns RunResult object May 16, 2025


# Choose which inference type to use!

available_inferences = ["hf_api", "hf_api_provider", "transformers", "ollama", "litellm", "openai"]
chosen_inference = "hf_api_provider"
available_inferences = ["inference_client", "transformers", "ollama", "litellm", "openai"]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aligns this with the new name since #1198.

elif chosen_inference == "hf_api_provider":
model = InferenceClientModel(provider="together")
if chosen_inference == "inference_client":
model = InferenceClientModel(model_id="meta-llama/Llama-3.3-70B-Instruct", provider="nebius")
Copy link
Collaborator Author
@aymeric-roucher aymeric-roucher May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify provider "nebius" since they don't error out when using tool_call="required"

"""Holds extended information about an agent run."""

result: Any
token_usage: TokenUsage | None
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be None in case the token usage cannot be obtained.

)
except Exception as e:
raise e
msg = msg.replace("<", r"\<").replace(">", r"\>") # HTML tags seem to break Gradio Chatbot
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albertvillanova escaping HTML tags fixe a hairy bug where HTML tagged messages wouldn't appear in the Gradio Chatbot. @yvrjsharma do you know why this is?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this should be fixed in Gradio if possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting this, I have tagged you both in a related issue on gradio repo.

@@ -1226,7 +1225,7 @@ class InferenceClientModel(ApiModel):

def __init__(
self,
model_id: str = "Qwen/Qwen2.5-Coder-32B-Instruct",
model_id: str = "Qwen/Qwen3-32B",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the default to the current best Qwen model!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may raise an error for some providers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, reverting this and leaving it for later.

@aymeric-roucher aymeric-roucher marked this pull request as ready for review May 19, 2025 12:06
@aymeric-roucher
Copy link
Collaborator Author

@albertvillanova could you take a look, then if the UI appears good to you I'll add tests!

Copy link
Member
@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Just some comments/questions below. And as you suggested, more tests to be added.

)
except Exception as e:
raise e
msg = msg.replace("<", r"\<").replace(">", r"\>") # HTML tags seem to break Gradio Chatbot
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this should be fixed in Gradio if possible.

Comment on lines -304 to -305
self.last_input_token_count: int | None = None
self.last_output_token_count: int | None = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new approach, but this is a breaking change: should we deprecate these parameters?

@@ -1226,7 +1225,7 @@ class InferenceClientModel(ApiModel):

def __init__(
self,
model_id: str = "Qwen/Qwen2.5-Coder-32B-Instruct",
model_id: str = "Qwen/Qwen3-32B",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may raise an error for some providers?

token_usage: TokenUsage | None
messages: list[dict]
timing: Timing
state: str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you describe the meaining of all attributes in the docstring? For example, we should describe what values can have state...

Copy link
Collaborator Author
@aymeric-roucher aymeric-roucher May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

self.model_id: str | None = model_id

@property
def last_input_token_count(self) -> int | None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albertvillanova WDYT of this implementation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you should emit a warning instead of logging one, no?

Copy link
Member
@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Just some comments.

@@ -1309,17 +1339,24 @@ def generate_stream(
for event in self.client.chat.completions.create(
**completion_kwargs, stream=True, stream_options={"include_usage": True}
):
if getattr(event, "usage", None):
print("EV:", event)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you forgot this print

Comment on lines 1320 to 1322
if getattr(event, "usage", None):
self.last_input_token_count = event.usage.prompt_tokens
self.last_output_token_count = event.usage.completion_tokens
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot these?

Comment on lines -1426 to -1428
if getattr(event, "usage", None):
self.last_input_token_count = event.usage.prompt_tokens
self.last_output_token_count = event.usage.completion_tokens
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And these?

Comment on lines -44 to +102
def get_total_token_counts(self):
return {
"input": self.total_input_token_count,
"output": self.total_output_token_count,
}
def get_total_token_counts(self) -> TokenUsage:
return TokenUsage(
input_tokens=self.total_input_token_count,
output_tokens=self.total_output_token_count,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For backward compatibility, maybe better:

  • keep get_total_token_counts as it was implemented before, but raising a deprecation warning
  • implement a new get_token_usage method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'll be more robust to not only keep the method get_total_token_counts, but also the underlying attributes self.total_input_token_count and self.total_output_token_counts, because people might be accessing these directly!

self.model_id: str | None = model_id

@property
def last_input_token_count(self) -> int | None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you should emit a warning instead of logging one, no?

Copy link
Member
@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized this.

aymeric-roucher and others added 6 commits May 20, 2025 15:03
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
Co-authored-by: Albert Villanova del Moral <8515462+albertvillanova@users.noreply.github.com>
@aymeric-roucher
Copy link
Collaborator Author

@albertvillanova about your suggestion with defining duration through post_init: I applied it to TokenUsage : for Timing however, since this object will often be defined at step start with an empty end_time and the end_time is only filled later, thus the duration as well, it made more sense to keep the property definition!

@aymeric-roucher aymeric-roucher merged commit 1d90caf into main May 20, 2025
4 checks passed
@aymeric-roucher aymeric-roucher changed the title Agent.run() returns RunResult object Agent.run() can return RunResult object May 20, 2025
daavoo added a commit to mozilla-ai/any-agent that referenced this pull request May 27, 2025
…__`.

After huggingface/smolagents#1337 we need to instrument all exported subclasses instead of the parent class.

- bump "smolagents[mcp]>=1.17.0".
daavoo added a commit to mozilla-ai/any-agent that referenced this pull request May 27, 2025
#350)

* fix(smolagents): Instrument `Model.generate` instead of `Model.__call__`.

After huggingface/smolagents#1337 we need to instrument all exported subclasses instead of the parent class.

- bump "smolagents[mcp]>=1.17.0".

* Fix uninstrument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0