-
Notifications
You must be signed in to change notification settings - Fork 495
Fix: Improve final response generation when reaching max iterations in AnthropicAugmentedLLM #99
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
Fix: Improve final response generation when reaching max iterations in AnthropicAugmentedLLM #99
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dragon1086 thank you for the contribution! this is a good change.
One last request -- if you wouldn't mind, can you please add it to the openai augmented llm as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for adding this @dragon1086 <3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're welcome! Happy to contribute. 😊
if i == params.max_iterations - 1 and response.stop_reason == "tool_use": | ||
final_prompt_message = MessageParam( | ||
role="user", | ||
content="We've reached the maximum number of iterations. Please stop using tools now and provide your final comprehensive answer based on all tool results so far.", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clever. The one issue I can think of is that there is no indication at the user level that the response is possibly incomplete because the LLM might have needed more information.
One option is in the prompt to ask the LLM to indicate that it isn't done, but it's returning a response based on what the information it's got.
This might be an interesting usecase for MCP notifications as well (in the future)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your positive feedback.
I've modified the content text based on your suggestion.
When I ran integration tests, the beginning and ending portions of the final response looked like this.
Please let me know if you think it's too much:
- Beginning portion:
I've reached the maximum number of tool usage iterations, which means I was only
able to retrieve content from the first two links on your list. To provide a
complete answer, I would have needed to make additional fetch requests for the
remaining 12 URLs you provided. Below are the first two paragraphs from the two
links I was able to access:
https://modelcontextprotocol.io/introduction
...
- Ending portion:
...
To provide a complete response, I would have needed to fetch content from the
remaining URLs:
- https://modelcontextprotocol.io/clients
- https://modelcontextprotocol.io/tutorials/building-mcp-with-llms
- https://modelcontextprotocol.io/docs/tools/debugging
- https://modelcontextprotocol.io/docs/tools/inspector
- https://modelcontextprotocol.io/docs/concepts/architecture
- https://modelcontextprotocol.io/docs/concepts/resources
- https://modelcontextprotocol.io/docs/concepts/prompts
- https://modelcontextprotocol.io/docs/concepts/tools
- https://modelcontextprotocol.io/docs/concepts/sampling
- https://modelcontextprotocol.io/docs/concepts/roots
- https://modelcontextprotocol.io/docs/concepts/transports
- https://modelcontextprotocol.io/development/updates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback on my contribution!
I considered fixing the OpenAI augmented LLM before submitting this PR, but found it difficult to reproduce a scenario with multiple iterations.
The reason OpenAI works well is that it typically calls all necessary tools within the first iteration through the 'message.tool_calls'.
Of course, there could be cases where the response gets truncated due to length limitations from too many tool calls.
I think the code implementation would look something like:
if (
choice.finish_reason in ["tool_calls", "function_call"]
and message.tool_calls
):
...
...
if params.max_iterations > 1 and i == params.max_iterations - 2:
final_prompt_message = ChatCompletionUserMessageParam(
role="user",
content="We've reached the maximum number of iterations. Please ...",
)
messages.append(final_prompt_message)
However, I'd prefer to work on this once I can reproduce a case where it's actually needed.
If such a case is found, I'll be happy to submit another PR to address it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dragon1086 thanks for this. Let's go with this approach and we'll iterate as needed.
…n AnthropicAugmentedLLM (#99) * Fix: Add final response prompt when max_iterations is reached with tool_use * fix: Enhance final prompt message in max iterations scenario --------- Co-authored-by: [email protected] <1gmrfyd2@> Co-authored-by: rocky.mun <[email protected]>
Description
This PR enhances the AnthropicAugmentedLLM class to properly stop tool usage and generate a final text response when reaching the maximum number of iterations.
Changes
Problem Solved
Testing
Note
This PR may have potential conflicts with ongoing PR #95, as both PRs modify the same area of the AnthropicAugmentedLLM class. If #95 is merged first, this PR may need to be rebased or have conflicts resolved.