core[minor], ...: add tool calls message (#18947)

core[minor], langchain[patch], openai[minor], anthropic[minor], fireworks[minor], groq[minor], mistralai[minor]

```python
class ToolCall(TypedDict):
    name: str
    args: Dict[str, Any]
    id: Optional[str]

class InvalidToolCall(TypedDict):
    name: Optional[str]
    args: Optional[str]
    id: Optional[str]
    error: Optional[str]

class ToolCallChunk(TypedDict):
    name: Optional[str]
    args: Optional[str]
    id: Optional[str]
    index: Optional[int]


class AIMessage(BaseMessage):
    ...
    tool_calls: List[ToolCall] = []
    invalid_tool_calls: List[InvalidToolCall] = []
    ...


class AIMessageChunk(AIMessage, BaseMessageChunk):
    ...
    tool_call_chunks: Optional[List[ToolCallChunk]] = None
    ...
```
Important considerations:
- Parsing logic occurs within different providers;
- ~Changing output type is a breaking change for anyone doing explicit
type checking;~
- ~Langsmith rendering will need to be updated:
https://github.com/langchain-ai/langchainplus/pull/3561~
- ~Langserve will need to be updated~
- Adding chunks:
- ~AIMessage + ToolCallsMessage = ToolCallsMessage if either has
non-null .tool_calls.~
- Tool call chunks are appended, merging when having equal values of
`index`.
  - additional_kwargs accumulate the normal way.
- During streaming:
- ~Messages can change types (e.g., from AIMessageChunk to
AIToolCallsMessageChunk)~
- Output parsers parse additional_kwargs (during .invoke they read off
tool calls).

Packages outside of `partners/`:
- https://github.com/langchain-ai/langchain-cohere/pull/7
- https://github.com/langchain-ai/langchain-google/pull/123/files

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
Bagatur
2024-04-09 18:41:42 -05:00
committed by GitHub
parent 00552918ac
commit 9514bc4d67
31 changed files with 2347 additions and 389 deletions

View File

@@ -3,7 +3,13 @@
import json
from typing import Any
from langchain_core.messages import AIMessageChunk, HumanMessage
from langchain_core.messages import (
AIMessage,
AIMessageChunk,
HumanMessage,
ToolCall,
ToolCallChunk,
)
from langchain_core.pydantic_v1 import BaseModel
from langchain_mistralai.chat_models import ChatMistralAI
@@ -151,6 +157,22 @@ def test_streaming_structured_output() -> None:
chunk_num += 1
def test_tool_call() -> None:
llm = ChatMistralAI(model="mistral-large", temperature=0)
class Person(BaseModel):
name: str
age: int
tool_llm = llm.bind_tools([Person])
result = tool_llm.invoke("Erick, 27 years old")
assert isinstance(result, AIMessage)
assert result.tool_calls == [
ToolCall(name="Person", args={"name": "Erick", "age": 27}, id=None)
]
def test_streaming_tool_call() -> None:
llm = ChatMistralAI(model="mistral-large", temperature=0)
@@ -178,6 +200,13 @@ def test_streaming_tool_call() -> None:
"age": 27,
}
assert isinstance(chunk, AIMessageChunk)
assert chunk.tool_call_chunks == [
ToolCallChunk(
name="Person", args='{"name": "Erick", "age": 27}', id=None, index=None
)
]
# where it doesn't call the tool
strm = tool_llm.stream("What is 2+2?")
acc: Any = None