-
Notifications
You must be signed in to change notification settings - Fork 16k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
openai[minor]: implement langchain-openai package (#15503)
Todo - [x] copy over integration tests - [x] update docs with new instructions in #15513 - [x] add linear ticket to bump core -> community, community->langchain, and core->openai deps - [ ] (optional): add `pip install langchain-openai` command to each notebook using it - [x] Update docstrings to not need `openai` install - [x] Add serialization - [x] deprecate old models Contributor steps: - [x] Add secret names to manual integrations workflow in .github/workflows/_integration_test.yml - [x] Add secrets to release workflow (for pre-release testing) in .github/workflows/_release.yml Maintainer steps (Contributors should not do these): - [x] set up pypi and test pypi projects - [x] add credential secrets to Github Actions - [ ] add package to conda-forge Functional changes to existing classes: - now relies on openai client v1 (1.6.1) via concrete dep in langchain-openai package Codebase organization - some function calling stuff moved to `langchain_core.utils.function_calling` in order to be used in both community and langchain-openai
- Loading branch information
Showing
64 changed files
with
5,999 additions
and
389 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
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
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
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
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
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
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
66 changes: 15 additions & 51 deletions
66
libs/community/langchain_community/utils/openai_functions.py
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 |
---|---|---|
@@ -1,51 +1,15 @@ | ||
from typing import Literal, Optional, Type, TypedDict | ||
|
||
from langchain_core.pydantic_v1 import BaseModel | ||
from langchain_core.utils.json_schema import dereference_refs | ||
|
||
|
||
class FunctionDescription(TypedDict): | ||
"""Representation of a callable function to the OpenAI API.""" | ||
|
||
name: str | ||
"""The name of the function.""" | ||
description: str | ||
"""A description of the function.""" | ||
parameters: dict | ||
"""The parameters of the function.""" | ||
|
||
|
||
class ToolDescription(TypedDict): | ||
"""Representation of a callable function to the OpenAI API.""" | ||
|
||
type: Literal["function"] | ||
function: FunctionDescription | ||
|
||
|
||
def convert_pydantic_to_openai_function( | ||
model: Type[BaseModel], | ||
*, | ||
name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
) -> FunctionDescription: | ||
"""Converts a Pydantic model to a function description for the OpenAI API.""" | ||
schema = dereference_refs(model.schema()) | ||
schema.pop("definitions", None) | ||
return { | ||
"name": name or schema["title"], | ||
"description": description or schema["description"], | ||
"parameters": schema, | ||
} | ||
|
||
|
||
def convert_pydantic_to_openai_tool( | ||
model: Type[BaseModel], | ||
*, | ||
name: Optional[str] = None, | ||
description: Optional[str] = None, | ||
) -> ToolDescription: | ||
"""Converts a Pydantic model to a function description for the OpenAI API.""" | ||
function = convert_pydantic_to_openai_function( | ||
model, name=name, description=description | ||
) | ||
return {"type": "function", "function": function} | ||
# these stubs are just for backwards compatibility | ||
|
||
from langchain_core.utils.function_calling import ( | ||
FunctionDescription, | ||
ToolDescription, | ||
convert_pydantic_to_openai_function, | ||
convert_pydantic_to_openai_tool, | ||
) | ||
|
||
__all__ = [ | ||
"FunctionDescription", | ||
"ToolDescription", | ||
"convert_pydantic_to_openai_function", | ||
"convert_pydantic_to_openai_tool", | ||
] |
Oops, something went wrong.