Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openai[minor]: implement langchain-openai package #15503

Merged
merged 66 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
20694ac
init
efriis Jan 2, 2024
48cbc10
add openai
efriis Jan 2, 2024
57d2970
add numpy
efriis Jan 2, 2024
8c43bf3
Merge branch 'master' into erick/partner-openai
efriis Jan 2, 2024
134b092
embeddings
efriis Jan 2, 2024
e309378
add back transformers option
efriis Jan 2, 2024
edb0851
chat model
efriis Jan 2, 2024
6f5eb50
downgrade mypy again
efriis Jan 2, 2024
d4fceed
x
efriis Jan 2, 2024
24e1eac
unit tests
efriis Jan 2, 2024
ad6bce5
remove duplicate test
efriis Jan 2, 2024
e6a8c25
more duplicate test
efriis Jan 2, 2024
26c395c
conversion
efriis Jan 3, 2024
1aa1dcb
Merge branch 'master' into erick/partner-openai
efriis Jan 3, 2024
01bd344
x
efriis Jan 3, 2024
9b1778b
Merge branch 'master' into erick/partner-openai
efriis Jan 3, 2024
f11d9ec
lint
efriis Jan 3, 2024
361d6e8
x
efriis Jan 3, 2024
61edd7e
lint core
efriis Jan 3, 2024
6c22e81
x
efriis Jan 3, 2024
c70d6c0
fix import test
efriis Jan 3, 2024
7b1204f
Merge branch 'master' into erick/partner-openai
efriis Jan 3, 2024
26620d8
chatopenai start
efriis Jan 3, 2024
81b0bb6
cookbook ChatOpenAI
efriis Jan 3, 2024
25e7262
docs ChatOpenAI
efriis Jan 3, 2024
00efbf4
docs ChatOpenAI
efriis Jan 3, 2024
7f385f2
docs ChatPromptTemplate
efriis Jan 3, 2024
01ed344
docs ChatPromptTemplate
efriis Jan 3, 2024
759cf14
docs ChatOpenAI
efriis Jan 3, 2024
fcfc090
docs OpenAI
efriis Jan 3, 2024
3254233
docs OpenAI
efriis Jan 3, 2024
fe7425f
docs OpenAIEmbeddings
efriis Jan 3, 2024
d27bf40
integration tests
efriis Jan 4, 2024
8e81c79
docs in separate pr
efriis Jan 4, 2024
32e207a
rm _utils
efriis Jan 4, 2024
0f06c8a
Merge branch 'master' into erick/partner-openai
efriis Jan 4, 2024
287eec7
ser
efriis Jan 4, 2024
970034f
reorg
efriis Jan 4, 2024
caa03ab
init
efriis Jan 4, 2024
8aaf50e
import test
efriis Jan 4, 2024
778fe39
tests
efriis Jan 4, 2024
92945d2
integration tests
efriis Jan 4, 2024
c389fbc
lint
efriis Jan 4, 2024
4b8ed72
mypy
efriis Jan 4, 2024
5e49c5b
fix
efriis Jan 4, 2024
cee3f6a
no more chat unit
efriis Jan 5, 2024
0abb3e0
Merge branch 'master' into erick/partner-openai
efriis Jan 5, 2024
06b2f3c
deps
efriis Jan 5, 2024
0827b72
deps
efriis Jan 5, 2024
bf766b3
deprecations
efriis Jan 5, 2024
0f7b35f
format
efriis Jan 5, 2024
e4a64fb
debug
efriis Jan 5, 2024
4a29380
rm print
efriis Jan 5, 2024
fa73df4
deps
efriis Jan 5, 2024
fefec8f
tests
efriis Jan 5, 2024
8c8b0df
t
efriis Jan 5, 2024
b4b6f71
-
efriis Jan 5, 2024
fe97a5c
simplify
efriis Jan 5, 2024
be6a46e
oops
efriis Jan 5, 2024
6277cdc
protect
efriis Jan 5, 2024
07fd7e9
delete
efriis Jan 5, 2024
5f05ad9
x
efriis Jan 5, 2024
390bb77
x
efriis Jan 5, 2024
fb8ddc7
warn
efriis Jan 5, 2024
412d059
secrets
efriis Jan 5, 2024
1e79455
Merge branch 'master' into erick/partner-openai
efriis Jan 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

causes a decent number of deprecation warnings in unit tests, but still important to test community one before removal.

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")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to discuss. This isn't a 1:1 replacement in 0.2

class OpenAIChat(BaseLLM):
"""OpenAI Chat large language models.

Expand Down
66 changes: 15 additions & 51 deletions libs/community/langchain_community/utils/openai_functions.py
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved function calling stuff to core/utils/function_calling

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",
]