Skip to content
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

feat(1/n): api: unify agents for handling server & client tools #1178

Merged
merged 17 commits into from
Feb 21, 2025

Conversation

yanxi0830
Copy link
Contributor

@yanxi0830 yanxi0830 commented Feb 20, 2025

Problem

Our current Agent framework has discrepancies in definition on how we handle server side and client side tools.

  1. Server Tools: a single Turn is returned including ToolExecutionStep in agenst
  2. Client Tools: create_agent_turn is called in loop with client agent lib yielding the agent chunk
    https://github.com/meta-llama/llama-stack-client-python/blob/ad6ffc63df658674f275267b1befc2b7046dbf33/src/llama_stack_client/lib/agents/agent.py#L186-L211

This makes it inconsistent to work with server & client tools. It also complicates the logs to telemetry to get information about agents turn / history for observability.

Principle

The same turn_id should be used to represent the steps required to complete a user message including client tools.

Solution

  1. AgentTurnResponseEventType.turn_awaiting_input status to indicate that the current turn is not completed, and awaiting tool input
  2. continue_agent_turn endpoint to update agent turn with client's tool response.

What does this PR do?

  • Skeleton API as example

Test Plan

[Describe the tests you ran to verify your changes with result summaries. Provide clear instructions so the plan can be easily re-executed.]

  • Just API update, no functionality change
llama stack run + client-sdk test
image

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 20, 2025
@yanxi0830 yanxi0830 changed the title [RFC] unify agents for handling server & client tools feat(1/n): unify agents for handling server & client tools Feb 20, 2025
@yanxi0830 yanxi0830 changed the title feat(1/n): unify agents for handling server & client tools feat(1/n): api: unify agents for handling server & client tools Feb 20, 2025
new_messages: List[
Union[
UserMessage,
ToolResponseMessage,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove UserMessage

@yanxi0830 yanxi0830 added this to the v0.1.4 milestone Feb 21, 2025
Copy link
Contributor

@ehhuang ehhuang left a comment

Choose a reason for hiding this comment

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

LGTM

yanxi0830 added a commit that referenced this pull request Feb 21, 2025
# What does this PR do?
- #1178
- #1187
- #1194

**client changes**
- meta-llama/llama-stack-client-python#157
- meta-llama/llama-stack-client-python#158

## Test Plan
```
LLAMA_STACK_BASE_URL=http://localhost:8321 pytest -v tests/client-sdk/agents/test_agents.py --inference-model meta-llama/Llama-3.1-8B-Instruct
```

```
LLAMA_STACK_CONFIG=fireworks pytest -v tests/client-sdk/agents/test_agents.py --inference-model meta-llama/Llama-3.1-8B-Instruct
```

<img width="1080" alt="image"
src="https://github.com/user-attachments/assets/6c48e062-5d00-4fed-98de-ecca627e5195"
/>

**llama-stack-apps**
```
python -m examples.agents.react_agent localhost 8321
```

- Test with script:
https://gist.github.com/yanxi0830/f2e407527f468998a700cd29fd271b15

**Output Before**: we have 2 `turn_id` with 2 turns
- https://gist.github.com/yanxi0830/9fbd7a80fcddc784a28c59d4a9c1d943

**Output After**: we have 1 `turn_id`, the final turn have all 3 steps
- https://gist.github.com/yanxi0830/17754d56d08ccbeaec419b693137500c

<img width="622" alt="image"
src="https://github.com/user-attachments/assets/ed7f42ca-41c8-4ee9-b6fa-2299901cafb4"
/>

**Telemetry**
<img width="1389" alt="image"
src="https://github.com/user-attachments/assets/031bf6ab-959c-46a7-aa85-70a511cba296"
/>


[//]: # (## Documentation)
# What does this PR do?
- 1/n: #1178

This is a backward compat change. Default `allow_resume_turn=False` to
work with client v0.1.3.

**Expected Behaviour**

1. User sends out `agent.create_turn`. When user receives a custom
client_tool_call. The last chunk should be
```python
chunk = AgentTurnResponseStreamChunk(
        event=AgentTurnResponseEvent(
        payload=AgentTurnResponseTurnAwaitingInputPayload(
            turn=turn,
       )
    )
)
```

There should be a `tool_execution` step. 

2. When user sees turn_pending as the event_type or if there's tool call
in the response. They will execute the client tools, and submits back
the tool response. E.g.

```python
client.continue_turn(
    [ToolResponseMessage(...)]
)
```

The agent will then send back & record `tool_execution` step finish on
server. and continue with the execution.


[//]: # (If resolving an issue, uncomment and update the line below)
[//]: # (Closes #[issue-number])

> NOTE: This PR handles (1). Next PR handle (2). 

## Test Plan

- Testing w/ Client SDK 0.1.3
```
LLAMA_STACK_BASE_URL=http://localhost:8321 pytest -v tests/client-sdk/agents/test_agents.py --inference-model meta-llama/Llama-3.1-8B-Instruct
```
<img width="1075" alt="image"
src="https://github.com/user-attachments/assets/c94e9cc6-a326-405c-a2ef-925dbc3ae58e"
/>



[//]: # (## Documentation)
@yanxi0830 yanxi0830 merged commit 0fe0717 into main Feb 21, 2025
4 checks passed
@yanxi0830 yanxi0830 deleted the agents-unify-tools branch February 21, 2025 19:48
yanxi0830 added a commit to meta-llama/llama-stack-client-python that referenced this pull request Feb 21, 2025
…anxi0830/dev) (#157)

# What does this PR do?
- Adapt to meta-llama/llama-stack#1178

## Test Plan
- test in following PR

[//]: # (## Documentation)
[//]: # (- [ ] Added a Changelog entry if the change is significant)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Meta Open Source bot.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants