You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/libs/community/langchain_community/chat_models
Igor Drozdov b664dbcc36
feat(community): add support for tool_calls response (#23765)
When `model_kwargs={"tools": tools}` are passed to `ChatLiteLLM`, they
are executed, but the response is not recognized correctly

Let's add `tool_calls` to the `additional_kwargs`

Thank you for contributing to LangChain!

## ChatAnthropic

I used the following example to verify the output of llm with tools:

```python
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_anthropic import ChatAnthropic

class GetWeather(BaseModel):
    '''Get the current weather in a given location'''

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")

class GetPopulation(BaseModel):
    '''Get the current population in a given location'''

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")

llm = ChatAnthropic(model="claude-3-sonnet-20240229")
llm_with_tools = llm.bind_tools([GetWeather, GetPopulation])
ai_msg = llm_with_tools.invoke("Which city is hotter today and which is bigger: LA or NY?")
print(ai_msg.tool_calls)
```

I get the following response:

```json
[{'name': 'GetWeather', 'args': {'location': 'Los Angeles, CA'}, 'id': 'toolu_01UfDA89knrhw3vFV9X47neT'}, {'name': 'GetWeather', 'args': {'location': 'New York, NY'}, 'id': 'toolu_01NrYVRYae7m7z7tBgyPb3Gd'}, {'name': 'GetPopulation', 'args': {'location': 'Los Angeles, CA'}, 'id': 'toolu_01EPFEpDgzL6vV2dTpD9SVP5'}, {'name': 'GetPopulation', 'args': {'location': 'New York, NY'}, 'id': 'toolu_01B5J6tPJXgwwfhQX9BHP2dt'}]
```

## LiteLLM

Based on https://litellm.vercel.app/docs/completion/function_call

```python
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.utils.function_calling import convert_to_openai_tool
import litellm

class GetWeather(BaseModel):
    '''Get the current weather in a given location'''

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")

class GetPopulation(BaseModel):
    '''Get the current population in a given location'''

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")

prompt = "Which city is hotter today and which is bigger: LA or NY?"
tools = [convert_to_openai_tool(GetWeather), convert_to_openai_tool(GetPopulation)]

response = litellm.completion(model="claude-3-sonnet-20240229", messages=[{'role': 'user', 'content': prompt}], tools=tools)
print(response.choices[0].message.tool_calls)
```

```python
[ChatCompletionMessageToolCall(function=Function(arguments='{"location": "Los Angeles, CA"}', name='GetWeather'), id='toolu_01HeDWV5vP7BDFfytH5FJsja', type='function'), ChatCompletionMessageToolCall(function=Function(arguments='{"location": "New York, NY"}', name='GetWeather'), id='toolu_01EiLesUSEr3YK1DaE2jxsQv', type='function'), ChatCompletionMessageToolCall(function=Function(arguments='{"location": "Los Angeles, CA"}', name='GetPopulation'), id='toolu_01Xz26zvkBDRxEUEWm9pX6xa', type='function'), ChatCompletionMessageToolCall(function=Function(arguments='{"location": "New York, NY"}', name='GetPopulation'), id='toolu_01SDqKnsLjvUXuBsgAZdEEpp', type='function')]
```

## ChatLiteLLM

When I try the following

```python
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.utils.function_calling import convert_to_openai_tool
from langchain_community.chat_models import ChatLiteLLM

class GetWeather(BaseModel):
    '''Get the current weather in a given location'''

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")

class GetPopulation(BaseModel):
    '''Get the current population in a given location'''

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")

prompt = "Which city is hotter today and which is bigger: LA or NY?"
tools = [convert_to_openai_tool(GetWeather), convert_to_openai_tool(GetPopulation)]

llm = ChatLiteLLM(model="claude-3-sonnet-20240229", model_kwargs={"tools": tools})
ai_msg = llm.invoke(prompt)
print(ai_msg)
print(ai_msg.tool_calls)
```

```python
content="Okay, let's find out the current weather and populations for Los Angeles and New York City:" response_metadata={'token_usage': Usage(prompt_tokens=329, completion_tokens=193, total_tokens=522), 'model': 'claude-3-sonnet-20240229', 'finish_reason': 'tool_calls'} id='run-748b7a84-84f4-497e-bba1-320bd4823937-0'
[]
```

---

When I apply the changes of this PR, the output is

```json
[{'name': 'GetWeather', 'args': {'location': 'Los Angeles, CA'}, 'id': 'toolu_017D2tGjiaiakB1HadsEFZ4e'}, {'name': 'GetWeather', 'args': {'location': 'New York, NY'}, 'id': 'toolu_01WrDpJfVqLkPejWzonPCbLW'}, {'name': 'GetPopulation', 'args': {'location': 'Los Angeles, CA'}, 'id': 'toolu_016UKyYrVAV9Pz99iZGgGU7V'}, {'name': 'GetPopulation', 'args': {'location': 'New York, NY'}, 'id': 'toolu_01Sgv1imExFX1oiR1Cw88zKy'}]
```

If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17.

Co-authored-by: Igor Drozdov <idrozdov@gitlab.com>
3 months ago
..
__init__.py Add OCI Generative AI new model support (#22880) 3 months ago
anthropic.py (all): update removal in deprecation warnings from 0.2 to 0.3 (#21265) 5 months ago
anyscale.py community[patch]: Update root_validators to use pre=True or pre=False (#23731) 3 months ago
azure_openai.py infra: rm unused # noqa violations (#22049) 4 months ago
azureml_endpoint.py infra: rm unused # noqa violations (#22049) 4 months ago
baichuan.py community[patch]: Update root_validators ChatModels: ChatBaichuan, QianfanChatEndpoint, MiniMaxChat, ChatSparkLLM, ChatZhipuAI (#22853) 3 months ago
baidu_qianfan_endpoint.py community[patch]: set tool name for tongyi&qianfan llm (#22889) 3 months ago
bedrock.py community[patch]: Fix message formatting for Anthropic models on Amazon Bedrock (#20801) 5 months ago
cohere.py (all): update removal in deprecation warnings from 0.2 to 0.3 (#21265) 5 months ago
coze.py community[patch]: Update root_validators to use pre=True or pre=False (#23731) 3 months ago
dappier.py community[patch]: Update root_validators to use pre=True or pre=False (#23731) 3 months ago
databricks.py community[patch]: Update documentation string in databricks chat model (#21915) 4 months ago
deepinfra.py community[patch]: Update root_validators to use pre=True or pre=False (#23731) 3 months ago
edenai.py community[minor]: Add tools calls to `ChatEdenAI` (#22320) 4 months ago
ernie.py community[patch]: Update root_validators to use pre=True or pre=False (#23731) 3 months ago
everlyai.py community[minor]: import fix (#20995) 5 months ago
fake.py community[major], core[patch], langchain[patch], experimental[patch]: Create langchain-community (#14463) 10 months ago
fireworks.py community[patch]: Update root_validators to use pre=True or pre=False (#23731) 3 months ago
friendli.py community[minor]: Integration for `Friendli` LLM and `ChatFriendli` ChatModel. (#17913) 7 months ago
gigachat.py community[patch]: upgrade to recent version of mypy (#21616) 4 months ago
google_palm.py Refactor: use SecretStr for palm chat-model (#15100) 9 months ago
gpt_router.py community[patch]: callback before yield for _stream/_astream (#17907) 7 months ago
huggingface.py community[patch]: Update root_validators to use pre=True or pre=False (#23731) 3 months ago
human.py infra: add print rule to ruff (#16221) 8 months ago
hunyuan.py community[patch]: fix hunyuan message include chinese signature error (#22795) (#22796) 3 months ago
javelin_ai_gateway.py community[patch]: standardize init args, update for javelin sdk release. (#21980) 4 months ago
jinachat.py community[patch]: upgrade to recent version of mypy (#21616) 4 months ago
kinetica.py community[patch]: type ignore fixes (#18395) 7 months ago
konko.py community[minor]: import fix (#20995) 5 months ago
litellm.py feat(community): add support for tool_calls response (#23765) 3 months ago
litellm_router.py community[patch]: callback before yield for _stream/_astream (#17907) 7 months ago
llama_edge.py community[patch]: upgrade to recent version of mypy (#21616) 4 months ago
llamacpp.py openai, anthropic, ...: with_structured_output to pass in explicit tool choice (#23645) 3 months ago
maritalk.py community: fix lint (#23611) 3 months ago
meta.py docs: docstrings `langchain_community` update (#14889) 9 months ago
minimax.py community[patch]: Update root_validators ChatModels: ChatBaichuan, QianfanChatEndpoint, MiniMaxChat, ChatSparkLLM, ChatZhipuAI (#22853) 3 months ago
mlflow.py community[patch]: Fix ChatDatabricsk in case that streaming response doesn't have role field in delta chunk (#21897) 4 months ago
mlflow_ai_gateway.py community[patch]: chat model mypy fixes (#17061) 8 months ago
mlx.py docs: community docstring updates (#21040) 5 months ago
moonshot.py fix: MoonshotChat fails when setting the moonshot_api_key through the OS environment. (#23176) 3 months ago
oci_generative_ai.py Add OCI Generative AI new model support (#22880) 3 months ago
octoai.py community[minor]: add ChatOctoAI (#20059) 5 months ago
ollama.py Ollama vision support (#22734) 3 months ago
openai.py Standardized openai init params (#21739) 4 months ago
pai_eas_endpoint.py community[patch]: Update root_validators embeddings: llamacpp, jina, dashscope, mosaicml, huggingface_hub, Toolkits: Connery, ChatModels: PAI_EAS, (#22828) 3 months ago
perplexity.py docs: updated PPLX model (#23723) 3 months ago
premai.py community[minor]: Prem Templates (#22783) 3 months ago
promptlayer_openai.py docs: use standard openai params (#20160) 6 months ago
snowflake.py community[minor]: add `ChatSnowflakeCortex` chat model (#21490) 3 months ago
solar.py community[patch]: upgrade to recent version of mypy (#21616) 4 months ago
sparkllm.py community[patch]: Update root_validators ChatModels: ChatBaichuan, QianfanChatEndpoint, MiniMaxChat, ChatSparkLLM, ChatZhipuAI (#22853) 3 months ago
tongyi.py community[patch]: set tool name for tongyi&qianfan llm (#22889) 3 months ago
vertexai.py (all): update removal in deprecation warnings from 0.2 to 0.3 (#21265) 5 months ago
volcengine_maas.py community[patch]: add stop parameter support to volcengine maas (#19052) 6 months ago
yandex.py community[patch]: docstrings update (#20301) 6 months ago
yuan2.py community[patch]: upgrade to recent version of mypy (#21616) 4 months ago
zhipuai.py community[patch]: Invoke callback prior to yielding token (#23638) 3 months ago