Skip to content

Support dspy.Tool as input field type and dspy.ToolCall as output field type #8242

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

chenmoneygithub
Copy link
Collaborator

The main goal is making dspy.Tool a valid input type with special format so that users can easily use dspy.Tool without using dspy.ReAct. dspy.ToolCall is just a thing wrapper over tool name and tool args to simplify the output field definition involving tool calls.

Sample usage:

import dspy

dspy.configure(
    lm=dspy.LM("openai/gpt-4o-mini", cache=False),
    # lm=dspy.LM("anthropic/claude-3-5-sonnet-20240620", cache=False),
)


class MySignature(dspy.Signature):
    question: str = dspy.InputField(description="The question to answer")
    tools: list[dspy.Tool] = dspy.InputField(description="The tools to use")
    answer: str = dspy.OutputField(description="The answer to the question, if no more tool calls are needed")
    tool_call: dspy.ToolCall = dspy.OutputField(description="The tool call information, including name and arguments")


def get_weather(city: str) -> str:
    """Get the weather for a city"""
    return f"The weather in {city} is sunny"


tools = [dspy.Tool(get_weather)]
tools_dict = {t.name: t for t in tools}

predict = dspy.Predict(MySignature)

result = predict(question="What is the weather in Paris?", tools=tools)
if result.tool_call:
    print(f"Executing tool: {result.tool_call.name}")
    print(f"Tool result: {tools_dict[result.tool_call.name](**result.tool_call.args)}")

dspy.inspect_history()

With sample output (not including history):

Executing tool: get_weather
Tool result: The weather in Paris is sunny

@chenmoneygithub
Copy link
Collaborator Author

@okhat @TomeHirata I am going to add some adapter change to this PR, will ping you for review after that is done.

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.

1 participant