-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from langchain-ai/erick/ai-endpoints-remove-la…
…ngchain-community-dependency ai-endpoints: remove langchain-community dependency
- Loading branch information
Showing
4 changed files
with
66 additions
and
360 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import annotations | ||
|
||
from typing import ( | ||
Any, | ||
Dict, | ||
) | ||
|
||
from langchain_core.messages import ( | ||
AIMessage, | ||
BaseMessage, | ||
ChatMessage, | ||
FunctionMessage, | ||
HumanMessage, | ||
SystemMessage, | ||
ToolMessage, | ||
) | ||
|
||
|
||
def convert_message_to_dict(message: BaseMessage) -> dict: | ||
"""Convert a LangChain message to a dictionary. | ||
Args: | ||
message: The LangChain message. | ||
Returns: | ||
The dictionary. | ||
""" | ||
message_dict: Dict[str, Any] | ||
if isinstance(message, ChatMessage): | ||
message_dict = {"role": message.role, "content": message.content} | ||
elif isinstance(message, HumanMessage): | ||
message_dict = {"role": "user", "content": message.content} | ||
elif isinstance(message, AIMessage): | ||
message_dict = {"role": "assistant", "content": message.content} | ||
if "function_call" in message.additional_kwargs: | ||
message_dict["function_call"] = message.additional_kwargs["function_call"] | ||
# If function call only, content is None not empty string | ||
if message_dict["content"] == "": | ||
message_dict["content"] = None | ||
if "tool_calls" in message.additional_kwargs: | ||
message_dict["tool_calls"] = message.additional_kwargs["tool_calls"] | ||
# If tool calls only, content is None not empty string | ||
if message_dict["content"] == "": | ||
message_dict["content"] = None | ||
elif isinstance(message, SystemMessage): | ||
message_dict = {"role": "system", "content": message.content} | ||
elif isinstance(message, FunctionMessage): | ||
message_dict = { | ||
"role": "function", | ||
"content": message.content, | ||
"name": message.name, | ||
} | ||
elif isinstance(message, ToolMessage): | ||
message_dict = { | ||
"role": "tool", | ||
"content": message.content, | ||
"tool_call_id": message.tool_call_id, | ||
} | ||
else: | ||
raise TypeError(f"Got unknown type {message}") | ||
if "name" in message.additional_kwargs: | ||
message_dict["name"] = message.additional_kwargs["name"] | ||
return message_dict |
This file contains 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
Oops, something went wrong.