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

add chat tool calling #74

Merged
merged 12 commits into from
Jul 23, 2024
Merged

add chat tool calling #74

merged 12 commits into from
Jul 23, 2024

Conversation

mattf
Copy link
Collaborator

@mattf mattf commented Jul 17, 2024

from langchain_core.pydantic_v1 import Field
from langchain_core.tools import tool
from langchain_nvidia_ai_endpoints import ChatNVIDIA

@tool
def xxyyzz(
    a: int = Field(..., description="First number"),
    b: int = Field(..., description="Second number"),
) -> int:
    """xxyyzz two numbers"""
    return (a**b) % (b - a)

llm = ChatNVIDIA(model=tool_model, **mode).bind_tools(tools=[xxyyzz])

invoke -

response = llm.invoke("What is 11 xxyyzz 3?", tool_choice="required")
print(response.tool_calls)

streaming -

from functools import reduce
from operator import add

response = reduce(add, llm.stream("What is 11 xxyyzz 3?", tool_choice="required")
print(response.tool_calls)

Note: stream now returns an AIMessageChunk. The old behavior was to return a ChatMessageChunk. This new behavior is not technically API breaking because stream declares it returns a BaseMessageChunk and both AIMessageChunk & ChatMessageChunk are children of BaseMessageChunk. However, AIMessageChunk and ChatMessageChunk are siblings and not compatible. ChatMessageChunk has a role property that is not present on either BaseMessageChunk or AIMessageChunk. A user who expected role in the response will be broken by this change.

@mattf mattf requested review from dglogo and raspawar July 17, 2024 14:58
@mattf mattf self-assigned this Jul 17, 2024
@mattf mattf force-pushed the mattf/add-chat-tool-calling branch 4 times, most recently from cc7a5d2 to 5804d3d Compare July 19, 2024 14:27
@mattf mattf force-pushed the mattf/add-chat-tool-calling branch from 5804d3d to f743d78 Compare July 23, 2024 13:49
@mattf mattf merged commit e207e02 into main Jul 23, 2024
12 checks passed
@mattf mattf deleted the mattf/add-chat-tool-calling branch July 23, 2024 19:56
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.

2 participants