mirror of
https://github.com/microsoft/autogen.git
synced 2026-04-20 03:02:16 -04:00
Fix message history limiter for tool call (#3178)
* fix: message history limiter to support tool calls * add: pytest and docs for message history limiter for tool calls * Added keep_first_message for HistoryLimiter transform * Update to inbetween to between * Updated keep_first_message to non-optional, logic for history limiter * Update transforms.py * Update test_transforms to match utils introduction, add keep_first_message testing * Update test_transforms.py for pre-commit checks --------- Co-authored-by: Mark Sze <66362098+marklysze@users.noreply.github.com> Co-authored-by: Chi Wang <wang.chi@microsoft.com>
This commit is contained in:
@@ -59,7 +59,28 @@ pprint.pprint(processed_messages)
|
||||
{'content': 'very very very very very very long string', 'role': 'user'}]
|
||||
```
|
||||
|
||||
By applying the `MessageHistoryLimiter`, we can see that we were able to limit the context history to the 3 most recent messages.
|
||||
By applying the `MessageHistoryLimiter`, we can see that we were able to limit the context history to the 3 most recent messages. However, if the splitting point is between a "tool_calls" and "tool" pair, the complete pair will be included to obey the OpenAI API call constraints.
|
||||
|
||||
```python
|
||||
max_msg_transfrom = transforms.MessageHistoryLimiter(max_messages=3)
|
||||
|
||||
messages = [
|
||||
{"role": "user", "content": "hello"},
|
||||
{"role": "tool_calls", "content": "calling_tool"},
|
||||
{"role": "tool", "content": "tool_response"},
|
||||
{"role": "user", "content": "how are you"},
|
||||
{"role": "assistant", "content": [{"type": "text", "text": "are you doing?"}]},
|
||||
]
|
||||
|
||||
processed_messages = max_msg_transfrom.apply_transform(copy.deepcopy(messages))
|
||||
pprint.pprint(processed_messages)
|
||||
```
|
||||
```console
|
||||
[{'content': 'calling_tool', 'role': 'tool_calls'},
|
||||
{'content': 'tool_response', 'role': 'tool'},
|
||||
{'content': 'how are you', 'role': 'user'},
|
||||
{'content': [{'text': 'are you doing?', 'type': 'text'}], 'role': 'assistant'}]
|
||||
```
|
||||
|
||||
#### Example 2: Limiting the Number of Tokens
|
||||
|
||||
|
||||
Reference in New Issue
Block a user