-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add tools calling for dspy.LM #2023
Closed
chenmoneygithub
wants to merge
4
commits into
stanfordnlp:main
from
chenmoneygithub:add-tool-calling
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from dspy.predict.predict import Predict | ||
|
||
|
||
class PredictWithTools(Predict): | ||
"""Predict with tool calling support. | ||
|
||
This class is a thin wrapper around the Predict class that explicit has tool calling support. | ||
""" | ||
def __init__(self, signature, callbacks=None, tools=None, tool_choice="auto", **configs): | ||
"""Initialize the PredictWithTools class. | ||
|
||
Args: | ||
signature (str): The signature of `PredictWithTools`. | ||
callbacks (list): The callbacks that are called before and after the Predict call. | ||
tools (list): The list of tools to use. For more details, please refer to | ||
https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools | ||
tool_choice (str): The tool choice strategy, defaults to "auto". For more details, please refer to | ||
https://platform.openai.com/docs/api-reference/chat/create#chat-create-tool_choice | ||
**configs: Additional configurations. | ||
""" | ||
super().__init__(signature, callbacks, **configs) | ||
self.tools = tools | ||
self.tool_choice = tool_choice | ||
|
||
def __call__(self, tools=None, tool_choice="auto", **kwargs): | ||
"""Call the PredictWithTools class. | ||
|
||
This method performs prediction using the configured language model, with the ability | ||
to invoke tools if specified. It can either return tool calling instructions or | ||
direct prediction results. | ||
|
||
Args: | ||
tools (list, optional): List of available tools for the LLM to choose from. | ||
If provided, overrides the `tools` set during initialization. | ||
tool_choice (str, optional): Strategy for tool selection, defaults to "auto". | ||
If provided, overrides the `tool_choice` set during initialization. | ||
**kwargs: Additional arguments passed to the underlying Predict class. | ||
|
||
Returns: | ||
dspy.Prediction: One of two types: | ||
- Tool calling scenario: Contains a single field 'tool_calls' with a list | ||
of tool invocations and their arguments. | ||
- Direct prediction: Contains fields as specified by self.signature. | ||
""" | ||
kwargs["tools"] = tools or self.tools | ||
kwargs["tool_choice"] = tool_choice or self.tool_choice | ||
|
||
return super().__call__(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
import dspy | ||
from dspy.predict.predict_with_tools import PredictWithTools | ||
|
||
|
||
@pytest.mark.skipif("OPENAI_API_KEY" not in os.environ, reason="OpenAI API key is not set") | ||
def test_basic_predict_with_tools(): | ||
tools = [ | ||
{ | ||
"type": "function", | ||
"function": { | ||
"name": "get_weather", | ||
"parameters": { | ||
"type": "object", | ||
"properties": {"location": {"type": "string"}}, | ||
}, | ||
}, | ||
} | ||
] | ||
with dspy.context(lm=dspy.LM("openai/gpt-4o-mini")): | ||
predict = PredictWithTools("question -> answer", tools=tools) | ||
outputs = predict(question="what's the weather in Paris?", tools=tools) | ||
assert "tool_calls" in outputs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thanks @chenmoneygithub ! Right now, we have a general solution that works well.
I'm not sure we should replace it with a special solution that doesn't work for many model providers.
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.
Yes that's a good point. Yuki from Mlflow team reached out asking if we can use the standard tool calling supported by litellm so that we can trace the tool calls. Which kinda makes sense because now we are mixing the tools calls into the
message.content
, with this approach it's hard to trace the tool calls.My plan is this:
I am seeing two downsides:
dspy.ReAct
, there might be some performance change (drop or increase, I am not sure), we need to be cautious.dspy.ReAct
will have 2 branches, one for LLms that support tool calling, the other for LLMs that don't support tool calling. So the code will be slightly more complex: https://github.com/stanfordnlp/dspy/blob/main/dspy/predict/react.py#L85-L96.And I am seeing 2 benefits:
Please let me know your thoughts!