langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.07k stars 15.18k forks source link

OpenAI Adapter Chunk response missing handling for tool_calls #25436

Open rhlarora84 opened 2 months ago

rhlarora84 commented 2 months ago

Checked other resources

Example Code

In the following code, there is no support for tool_calls args in langchain_community.adapters -> openai

def _convert_message_chunk(chunk: BaseMessageChunk, i: int) -> dict:
    _dict: Dict[str, Any] = {}
    if isinstance(chunk, AIMessageChunk):
        if i == 0:
            # Only shows up in the first chunk
            _dict["role"] = "assistant"
        if "function_call" in chunk.additional_kwargs:
            _dict["function_call"] = chunk.additional_kwargs["function_call"]
            # If the first chunk is a function call, the content is not empty string,
            # not missing, but None.
            if i == 0:
                _dict["content"] = None
        else:
            _dict["content"] = chunk.content
    else:
        raise ValueError(f"Got unexpected streaming chunk type: {type(chunk)}")
    # This only happens at the end of streams, and OpenAI returns as empty dict
    if _dict == {"content": ""}:
        _dict = {}
    return _dict

Error Message and Stack Trace (if applicable)

No response

Description

I am trying to create OpenAI compatible endpoints using Langchain. Langchain community project provides openai adapter but it is missing handling for tool_calls in the streaming workflow.

System Info

langchain==0.2.6 langchain-anthropic==0.1.19 langchain-aws==0.1.10 langchain-community==0.2.6 langchain-core==0.2.15 langchain-experimental==0.0.63 langchain-openai==0.1.15 langchain-text-splitters==0.2.2 openinference-instrumentation-langchain==0.1.20

VaibhavLakshmiS commented 3 weeks ago

Hi there! We are a group of 3 students from the University of Toronto and we are very interested in contributing to this issue, and implementing the tool_calls handler! Do you have any additional implementation details or specific cases we should consider for this feature? Thanks!