-
-
Notifications
You must be signed in to change notification settings - Fork 16
feat: Support function call requests/responses #56
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
Merged
samuelint
merged 10 commits into
samuelint:feat-Support-function-call-requests/responses-(from-fork)
from
caffeinism:support-tool-response
Jul 11, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cddbc73
feat: Support function call requests/responses
caffeinism ef3f35a
fix: Allow finish_reason=tool_call in streaming mode
caffeinism 6ecb6e1
test: Add chat completion chunk tests
caffeinism d104dd0
fix: Change to only include role=assistant in the first chunk, the sa…
caffeinism 1cfb88b
test: Add chat completion function call functional test
caffeinism 219e233
fix: Change to respond only to the finish_reason of the last stream
caffeinism 77320d5
test: Remove duplicate code
caffeinism 58e9d69
test: Add arguments validation
caffeinism 0abf216
fix: Check attribute for Any type chunk
caffeinism 4673dc1
test: Add stream validation
caffeinism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 8 additions & 12 deletions
20
langchain_openai_api_bridge/chat_completion/chat_completion_object_factory.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 21 additions & 34 deletions
55
langchain_openai_api_bridge/chat_completion/langchain_invoke_adapter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,56 +1,43 @@ | ||
| import time | ||
| from langchain_core.messages import BaseMessage | ||
| from langchain_openai.chat_models.base import _convert_message_to_dict | ||
| from openai.types.chat.chat_completion import ChatCompletion, Choice, ChatCompletionMessage | ||
|
|
||
| from langchain_openai_api_bridge.chat_completion.chat_completion_object_factory import ( | ||
| ChatCompletionObjectFactory, | ||
| ) | ||
| from langchain_openai_api_bridge.chat_completion.content_adapter import ( | ||
| to_string_content, | ||
| ) | ||
| from langchain_openai_api_bridge.core.role_adapter import to_openai_role | ||
| from langchain_openai_api_bridge.core.types.openai import ( | ||
| OpenAIChatCompletionChoice, | ||
| OpenAIChatCompletionObject, | ||
| OpenAIChatMessage, | ||
| ) | ||
| from langchain_core.messages import AIMessage | ||
| from langchain_core.runnables.utils import Output | ||
|
|
||
|
|
||
| class LangchainInvokeAdapter: | ||
| def __init__(self, llm_model: str, system_fingerprint: str = ""): | ||
| self.llm_model = llm_model | ||
| self.system_fingerprint = system_fingerprint | ||
|
|
||
| def to_chat_completion_object(self, invoke_result) -> OpenAIChatCompletionObject: | ||
| message = self.__create_openai_chat_message(invoke_result) | ||
| id = self.__get_id(invoke_result) | ||
| def to_chat_completion_object(self, invoke_result: Output) -> ChatCompletion: | ||
samuelint marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| invoke_message = invoke_result if isinstance(invoke_result, BaseMessage) else invoke_result["messages"][-1] | ||
| message = self.__create_openai_chat_message(invoke_message) | ||
| id = self.__get_id(invoke_message) | ||
|
|
||
| return ChatCompletionObjectFactory.create( | ||
| id=id, | ||
| model=self.llm_model, | ||
| created=int(time.time()), | ||
| object="chat.completion", | ||
| system_fingerprint=self.system_fingerprint, | ||
| choices=[ | ||
| OpenAIChatCompletionChoice( | ||
| Choice( | ||
| index=0, | ||
| message=message, | ||
| finish_reason="stop", | ||
| finish_reason="tool_calls" if "tool_calls" in message else "stop", | ||
| ) | ||
| ], | ||
| ] | ||
| ) | ||
|
|
||
| def __get_id(self, invoke_result): | ||
| if isinstance(invoke_result, AIMessage): | ||
| return invoke_result.id | ||
| def __create_openai_chat_message(self, message: BaseMessage) -> ChatCompletionMessage: | ||
| message = _convert_message_to_dict(message) | ||
| message["role"] = "assistant" | ||
| return message | ||
|
|
||
| last_message = invoke_result["messages"][-1] | ||
| return last_message.id | ||
|
|
||
| def __create_openai_chat_message(self, invoke_result) -> OpenAIChatMessage: | ||
| if isinstance(invoke_result, AIMessage): | ||
| return OpenAIChatMessage( | ||
| role=to_openai_role(invoke_result.type), | ||
| content=to_string_content(content=invoke_result.content), | ||
| ) | ||
|
|
||
| last_message = invoke_result["messages"][-1] | ||
| return OpenAIChatMessage( | ||
| role=to_openai_role(last_message.type), | ||
| content=to_string_content(content=last_message.content), | ||
| ) | ||
| def __get_id(self, message: BaseMessage): | ||
| return message.id or "" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,7 @@ | ||
| from .message import OpenAIChatMessage | ||
| from .chat_completion import ( | ||
| OpenAIChatCompletionRequest, | ||
| OpenAIChatCompletionUsage, | ||
| OpenAIChatCompletionChoice, | ||
| OpenAIChatCompletionObject, | ||
| OpenAIChatCompletionChunkChoice, | ||
| OpenAIChatCompletionChunkObject, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "OpenAIChatMessage", | ||
| "OpenAIChatCompletionRequest", | ||
| "OpenAIChatCompletionUsage", | ||
| "OpenAIChatCompletionChoice", | ||
| "OpenAIChatCompletionObject", | ||
| "OpenAIChatCompletionChunkChoice", | ||
| "OpenAIChatCompletionChunkObject", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.