openai_functions.py — langchain Source File
Architecture documentation for openai_functions.py, a python file in the langchain codebase. 6 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR b8b3ce74_efe3_046c_c72d_d35e337c9841["openai_functions.py"] 9d14ea65_8b2e_6721_a947_acc89905651f["json"] b8b3ce74_efe3_046c_c72d_d35e337c9841 --> 9d14ea65_8b2e_6721_a947_acc89905651f e27da29f_a1f7_49f3_84d5_6be4cb4125c8["logging"] b8b3ce74_efe3_046c_c72d_d35e337c9841 --> e27da29f_a1f7_49f3_84d5_6be4cb4125c8 2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"] b8b3ce74_efe3_046c_c72d_d35e337c9841 --> 2bf6d401_816d_d011_3b05_a6114f55ff58 feec1ec4_6917_867b_d228_b134d0ff8099["typing"] b8b3ce74_efe3_046c_c72d_d35e337c9841 --> feec1ec4_6917_867b_d228_b134d0ff8099 59e0d3b0_0f8e_4b79_d442_e9b4821561c7["langchain_core.agents"] b8b3ce74_efe3_046c_c72d_d35e337c9841 --> 59e0d3b0_0f8e_4b79_d442_e9b4821561c7 9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"] b8b3ce74_efe3_046c_c72d_d35e337c9841 --> 9444498b_8066_55c7_b3a2_1d90c4162a32 style b8b3ce74_efe3_046c_c72d_d35e337c9841 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import json
import logging
from collections.abc import Sequence
from typing import Any
from langchain_core.agents import AgentAction, AgentActionMessageLog
from langchain_core.messages import AIMessage, BaseMessage, FunctionMessage
_logger = logging.getLogger(__name__)
def _convert_agent_action_to_messages(
agent_action: AgentAction,
observation: str,
) -> list[BaseMessage]:
"""Convert an agent action to a message.
This code is used to reconstruct the original AI message from the agent action.
Args:
agent_action: Agent action to convert.
observation: The result of the tool invocation.
Returns:
AIMessage or the previous messages plus a FunctionMessage that corresponds to
the original tool invocation
"""
if isinstance(agent_action, AgentActionMessageLog):
return [
*list(agent_action.message_log),
_create_function_message(agent_action, observation),
]
return [AIMessage(content=agent_action.log)]
def _create_function_message(
agent_action: AgentAction,
observation: Any,
) -> FunctionMessage:
"""Convert agent action and observation into a function message.
Args:
agent_action: the tool invocation request from the agent.
observation: the result of the tool invocation.
Returns:
FunctionMessage that corresponds to the original tool invocation.
Raises:
ValueError: if the observation cannot be converted to a string.
"""
if not isinstance(observation, str):
try:
content = json.dumps(observation, ensure_ascii=False)
except TypeError:
content = str(observation)
except Exception:
_logger.exception("Unexpected error converting observation to string.")
content = str(observation)
else:
content = observation
return FunctionMessage(
name=agent_action.tool,
content=content,
)
def format_to_openai_function_messages(
intermediate_steps: Sequence[tuple[AgentAction, str]],
) -> list[BaseMessage]:
"""Convert (AgentAction, tool output) tuples into FunctionMessages.
Args:
intermediate_steps: Steps the LLM has taken to date, along with observations
Returns:
list of messages to send to the LLM for the next prediction
Raises:
ValueError: if the observation cannot be converted to a string.
"""
messages = []
for agent_action, observation in intermediate_steps:
messages.extend(_convert_agent_action_to_messages(agent_action, observation))
return messages
# Backwards compatibility
format_to_openai_functions = format_to_openai_function_messages
Domain
Subdomains
Functions
Dependencies
- collections.abc
- json
- langchain_core.agents
- langchain_core.messages
- logging
- typing
Source
Frequently Asked Questions
What does openai_functions.py do?
openai_functions.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ClassicChains subdomain.
What functions are defined in openai_functions.py?
openai_functions.py defines 3 function(s): _convert_agent_action_to_messages, _create_function_message, format_to_openai_function_messages.
What does openai_functions.py depend on?
openai_functions.py imports 6 module(s): collections.abc, json, langchain_core.agents, langchain_core.messages, logging, typing.
Where is openai_functions.py in the architecture?
openai_functions.py is located at libs/langchain/langchain_classic/agents/format_scratchpad/openai_functions.py (domain: AgentOrchestration, subdomain: ClassicChains, directory: libs/langchain/langchain_classic/agents/format_scratchpad).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free