langchain_openai: Add "strict" parameter to OpenAIFunctionsAgent (#25862)

- **Description:** OpenAI recently introduced a "strict" parameter for
[structured outputs in their
API](https://openai.com/index/introducing-structured-outputs-in-the-api/).
An optional `strict` parameter has been added to
`create_openai_functions_agent()` and `create_openai_tools_agent()` so
developers can use this feature in those agents.

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
Michael Paciullo 2024-08-29 18:27:07 -04:00 committed by GitHub
parent fabd3295fa
commit e7c856c298
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,4 @@
from typing import Sequence
from typing import Optional, Sequence
from langchain_core.language_models import BaseLanguageModel
from langchain_core.prompts.chat import ChatPromptTemplate
@ -13,7 +13,10 @@ from langchain.agents.output_parsers.openai_tools import OpenAIToolsAgentOutputP
def create_openai_tools_agent(
llm: BaseLanguageModel, tools: Sequence[BaseTool], prompt: ChatPromptTemplate
llm: BaseLanguageModel,
tools: Sequence[BaseTool],
prompt: ChatPromptTemplate,
strict: Optional[bool] = None,
) -> Runnable:
"""Create an agent that uses OpenAI tools.
@ -87,7 +90,9 @@ def create_openai_tools_agent(
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")
llm_with_tools = llm.bind(tools=[convert_to_openai_tool(tool) for tool in tools])
llm_with_tools = llm.bind(
tools=[convert_to_openai_tool(tool, strict=strict) for tool in tools]
)
agent = (
RunnablePassthrough.assign(