Skip to content

Commit

Permalink
openai[minor]: implement langchain-openai package (#15503)
Browse files Browse the repository at this point in the history
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
efriis committed Jan 5, 2024
1 parent a7d023a commit ebc75c5
Show file tree
Hide file tree
Showing 64 changed files with 5,999 additions and 389 deletions.
1 change: 1 addition & 0 deletions .github/workflows/_integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
make integration_tests
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ jobs:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
MISTRAL_API_KEY: ${{ secrets.MISTRAL_API_KEY }}
TOGETHER_API_KEY: ${{ secrets.TOGETHER_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: make integration_tests
working-directory: ${{ inputs.working-directory }}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
from typing import Any, Callable, Dict, List, Union

from langchain_core._api.deprecation import deprecated
from langchain_core.outputs import ChatResult
from langchain_core.pydantic_v1 import BaseModel, Field, root_validator
from langchain_core.utils import get_from_dict_or_env
Expand All @@ -16,6 +17,9 @@
logger = logging.getLogger(__name__)


@deprecated(
since="0.1.0", removal="0.2.0", alternative="langchain_openai.AzureChatOpenAI"
)
class AzureChatOpenAI(ChatOpenAI):
"""`Azure OpenAI` Chat Completion API.
Expand Down
2 changes: 2 additions & 0 deletions libs/community/langchain_community/chat_models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Union,
)

from langchain_core._api.deprecation import deprecated
from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
Expand Down Expand Up @@ -143,6 +144,7 @@ def _convert_delta_to_message_chunk(
return default_class(content=content)


@deprecated(since="0.1.0", removal="0.2.0", alternative="langchain_openai.ChatOpenAI")
class ChatOpenAI(BaseChatModel):
"""`OpenAI` Chat large language models API.
Expand Down
4 changes: 4 additions & 0 deletions libs/community/langchain_community/embeddings/azure_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import warnings
from typing import Callable, Dict, Optional, Union

from langchain_core._api.deprecation import deprecated
from langchain_core.pydantic_v1 import Field, root_validator
from langchain_core.utils import get_from_dict_or_env

from langchain_community.embeddings.openai import OpenAIEmbeddings
from langchain_community.utils.openai import is_openai_v1


@deprecated(
since="0.1.0", removal="0.2.0", alternative="langchain_openai.AzureOpenAIEmbeddings"
)
class AzureOpenAIEmbeddings(OpenAIEmbeddings):
"""`Azure OpenAI` Embeddings API."""

Expand Down
6 changes: 6 additions & 0 deletions libs/community/langchain_community/embeddings/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

import numpy as np
from langchain_core._api.deprecation import deprecated
from langchain_core.embeddings import Embeddings
from langchain_core.pydantic_v1 import BaseModel, Extra, Field, root_validator
from langchain_core.utils import get_from_dict_or_env, get_pydantic_field_names
Expand Down Expand Up @@ -137,6 +138,11 @@ async def _async_embed_with_retry(**kwargs: Any) -> Any:
return await _async_embed_with_retry(**kwargs)


@deprecated(
since="0.1.0",
removal="0.2.0",
alternative="langchain_openai.OpenAIEmbeddings",
)
class OpenAIEmbeddings(BaseModel, Embeddings):
"""OpenAI embedding models.
Expand Down
4 changes: 4 additions & 0 deletions libs/community/langchain_community/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Union,
)

from langchain_core._api.deprecation import deprecated
from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
Expand Down Expand Up @@ -724,6 +725,7 @@ def max_tokens_for_prompt(self, prompt: str) -> int:
return self.max_context_size - num_tokens


@deprecated(since="0.1.0", removal="0.2.0", alternative="langchain_openai.OpenAI")
class OpenAI(BaseOpenAI):
"""OpenAI large language models.
Expand All @@ -750,6 +752,7 @@ def _invocation_params(self) -> Dict[str, Any]:
return {**{"model": self.model_name}, **super()._invocation_params}


@deprecated(since="0.1.0", removal="0.2.0", alternative="langchain_openai.AzureOpenAI")
class AzureOpenAI(BaseOpenAI):
"""Azure-specific OpenAI large language models.
Expand Down Expand Up @@ -953,6 +956,7 @@ def lc_attributes(self) -> Dict[str, Any]:
}


@deprecated(since="0.1.0", removal="0.2.0", alternative="langchain_openai.ChatOpenAI")
class OpenAIChat(BaseLLM):
"""OpenAI Chat large language models.
Expand Down
66 changes: 15 additions & 51 deletions libs/community/langchain_community/utils/openai_functions.py
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",
]

0 comments on commit ebc75c5

Please sign in to comment.