Merge branch 'master' of github.com:hwchase17/langchain

This commit is contained in:
Harrison Chase 2023-06-17 11:16:19 -07:00
commit 7a48d9ee82
12 changed files with 30 additions and 44 deletions

View File

@ -13,8 +13,13 @@ MODEL_COST_PER_1K_TOKENS = {
"gpt-4-32k-0314": 0.06, "gpt-4-32k-0314": 0.06,
"gpt-4-32k-completion": 0.12, "gpt-4-32k-completion": 0.12,
"gpt-4-32k-0314-completion": 0.12, "gpt-4-32k-0314-completion": 0.12,
"gpt-4-0613": 0.06,
"gpt-4-32k-0613": 0.12,
"gpt-3.5-turbo": 0.002, "gpt-3.5-turbo": 0.002,
"gpt-3.5-turbo-0301": 0.002, "gpt-3.5-turbo-0301": 0.002,
"gpt-3.5-turbo-16k": 0.004,
"gpt-3.5-turbo-0613": 0.002,
"gpt-3.5-turbo-16k-0613": 0.004,
"text-ada-001": 0.0004, "text-ada-001": 0.0004,
"ada": 0.0004, "ada": 0.0004,
"text-babbage-001": 0.0005, "text-babbage-001": 0.0005,

View File

@ -1,7 +1,5 @@
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from pydantic import Extra
from langchain.callbacks.manager import ( from langchain.callbacks.manager import (
AsyncCallbackManagerForLLMRun, AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun, CallbackManagerForLLMRun,
@ -34,11 +32,6 @@ class ChatAnthropic(BaseChatModel, _AnthropicCommon):
model = ChatAnthropic(model="<model_name>", anthropic_api_key="my-api-key") model = ChatAnthropic(model="<model_name>", anthropic_api_key="my-api-key")
""" """
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
@property @property
def _llm_type(self) -> str: def _llm_type(self) -> str:
"""Return type of chat model.""" """Return type of chat model."""

View File

@ -5,7 +5,7 @@ from abc import ABC, abstractmethod
from functools import partial from functools import partial
from typing import Any, Dict, List, Mapping, Optional, Sequence from typing import Any, Dict, List, Mapping, Optional, Sequence
from pydantic import Extra, Field, root_validator from pydantic import Field, root_validator
import langchain import langchain
from langchain.base_language import BaseLanguageModel from langchain.base_language import BaseLanguageModel
@ -56,7 +56,6 @@ class BaseChatModel(BaseLanguageModel, ABC):
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True arbitrary_types_allowed = True
def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) -> dict: def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) -> dict:

View File

@ -15,7 +15,7 @@ from typing import (
Union, Union,
) )
from pydantic import Extra, Field, root_validator from pydantic import Field, root_validator
from tenacity import ( from tenacity import (
before_sleep_log, before_sleep_log,
retry, retry,
@ -182,7 +182,6 @@ class ChatOpenAI(BaseChatModel):
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
extra = Extra.ignore
allow_population_by_field_name = True allow_population_by_field_name = True
@root_validator(pre=True) @root_validator(pre=True)

View File

@ -51,7 +51,10 @@ class WebBaseLoader(BaseLoader):
"""kwargs for requests""" """kwargs for requests"""
def __init__( def __init__(
self, web_path: Union[str, List[str]], header_template: Optional[dict] = None self,
web_path: Union[str, List[str]],
header_template: Optional[dict] = None,
verify: Optional[bool] = True,
): ):
"""Initialize with webpage path.""" """Initialize with webpage path."""
@ -71,6 +74,9 @@ class WebBaseLoader(BaseLoader):
"bs4 package not found, please install it with " "`pip install bs4`" "bs4 package not found, please install it with " "`pip install bs4`"
) )
# Choose to verify
self.verify = verify
headers = header_template or default_header_template headers = header_template or default_header_template
if not headers.get("User-Agent"): if not headers.get("User-Agent"):
try: try:
@ -98,7 +104,7 @@ class WebBaseLoader(BaseLoader):
for i in range(retries): for i in range(retries):
try: try:
async with session.get( async with session.get(
url, headers=self.session.headers url, headers=self.session.headers, verify=self.verify
) as response: ) as response:
return await response.text() return await response.text()
except aiohttp.ClientConnectionError as e: except aiohttp.ClientConnectionError as e:
@ -173,7 +179,7 @@ class WebBaseLoader(BaseLoader):
self._check_parser(parser) self._check_parser(parser)
html_doc = self.session.get(url, **self.requests_kwargs) html_doc = self.session.get(url, verify=self.verify, **self.requests_kwargs)
html_doc.encoding = html_doc.apparent_encoding html_doc.encoding = html_doc.apparent_encoding
return BeautifulSoup(html_doc.text, parser) return BeautifulSoup(html_doc.text, parser)

View File

@ -3,7 +3,7 @@ import re
import warnings import warnings
from typing import Any, Callable, Dict, Generator, List, Mapping, Optional, Tuple, Union from typing import Any, Callable, Dict, Generator, List, Mapping, Optional, Tuple, Union
from pydantic import BaseModel, Extra, root_validator from pydantic import BaseModel, root_validator
from langchain.callbacks.manager import ( from langchain.callbacks.manager import (
AsyncCallbackManagerForLLMRun, AsyncCallbackManagerForLLMRun,
@ -140,11 +140,6 @@ class Anthropic(LLM, _AnthropicCommon):
) )
return values return values
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
@property @property
def _llm_type(self) -> str: def _llm_type(self) -> str:
"""Return type of llm.""" """Return type of llm."""

View File

@ -7,7 +7,7 @@ from pathlib import Path
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union
import yaml import yaml
from pydantic import Extra, Field, root_validator, validator from pydantic import Field, root_validator, validator
import langchain import langchain
from langchain.base_language import BaseLanguageModel from langchain.base_language import BaseLanguageModel
@ -85,7 +85,6 @@ class BaseLLM(BaseLanguageModel, ABC):
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True arbitrary_types_allowed = True
@root_validator() @root_validator()

View File

@ -20,7 +20,7 @@ from typing import (
Union, Union,
) )
from pydantic import Extra, Field, root_validator from pydantic import Field, root_validator
from tenacity import ( from tenacity import (
before_sleep_log, before_sleep_log,
retry, retry,
@ -187,7 +187,6 @@ class BaseOpenAI(BaseLLM):
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
extra = Extra.ignore
allow_population_by_field_name = True allow_population_by_field_name = True
@root_validator(pre=True) @root_validator(pre=True)
@ -686,11 +685,6 @@ class OpenAIChat(BaseLLM):
disallowed_special: Union[Literal["all"], Collection[str]] = "all" disallowed_special: Union[Literal["all"], Collection[str]] = "all"
"""Set of special tokens that are not allowed。""" """Set of special tokens that are not allowed。"""
class Config:
"""Configuration for this pydantic object."""
extra = Extra.ignore
@root_validator(pre=True) @root_validator(pre=True)
def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]:
"""Build extra kwargs from additional params that were passed in.""" """Build extra kwargs from additional params that were passed in."""

View File

@ -1,7 +1,7 @@
from abc import ABC from abc import ABC
from typing import Any, Dict, List, Literal, TypedDict, Union, cast from typing import Any, Dict, List, Literal, TypedDict, Union, cast
from pydantic import BaseModel, Field from pydantic import BaseModel, PrivateAttr
class BaseSerialized(TypedDict): class BaseSerialized(TypedDict):
@ -55,11 +55,14 @@ class Serializable(BaseModel, ABC):
""" """
return {} return {}
lc_kwargs: Dict[str, Any] = Field(default_factory=dict, exclude=True, repr=False) class Config:
extra = "ignore"
_lc_kwargs = PrivateAttr(default_factory=dict)
def __init__(self, **kwargs: Any) -> None: def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs) super().__init__(**kwargs)
self.lc_kwargs = kwargs self._lc_kwargs = kwargs
def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]: def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]:
if not self.lc_serializable: if not self.lc_serializable:
@ -69,8 +72,8 @@ class Serializable(BaseModel, ABC):
# Get latest values for kwargs if there is an attribute with same name # Get latest values for kwargs if there is an attribute with same name
lc_kwargs = { lc_kwargs = {
k: getattr(self, k, v) k: getattr(self, k, v)
for k, v in self.lc_kwargs.items() for k, v in self._lc_kwargs.items()
if not self.__exclude_fields__.get(k, False) # type: ignore if not (self.__exclude_fields__ or {}).get(k, False) # type: ignore
} }
# Merge the lc_secrets and lc_attributes from every class in the MRO # Merge the lc_secrets and lc_attributes from every class in the MRO

View File

@ -7,7 +7,7 @@ from pathlib import Path
from typing import Any, Callable, Dict, List, Mapping, Optional, Set, Union from typing import Any, Callable, Dict, List, Mapping, Optional, Set, Union
import yaml import yaml
from pydantic import Extra, Field, root_validator from pydantic import Field, root_validator
from langchain.formatting import formatter from langchain.formatting import formatter
from langchain.load.serializable import Serializable from langchain.load.serializable import Serializable
@ -119,7 +119,6 @@ class BasePromptTemplate(Serializable, ABC):
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True arbitrary_types_allowed = True
@abstractmethod @abstractmethod

View File

@ -5,7 +5,7 @@ from pathlib import Path
from string import Formatter from string import Formatter
from typing import Any, Dict, List, Union from typing import Any, Dict, List, Union
from pydantic import Extra, root_validator from pydantic import root_validator
from langchain.prompts.base import ( from langchain.prompts.base import (
DEFAULT_FORMATTER_MAPPING, DEFAULT_FORMATTER_MAPPING,
@ -48,11 +48,6 @@ class PromptTemplate(StringPromptTemplate):
"""Return the prompt type key.""" """Return the prompt type key."""
return "prompt" return "prompt"
class Config:
"""Configuration for this pydantic object."""
extra = Extra.forbid
def format(self, **kwargs: Any) -> str: def format(self, **kwargs: Any) -> str:
"""Format the prompt with the inputs. """Format the prompt with the inputs.

View File

@ -16,7 +16,7 @@ from typing import (
) )
from uuid import UUID from uuid import UUID
from pydantic import BaseModel, Extra, Field, root_validator from pydantic import BaseModel, Field, root_validator
from langchain.load.serializable import Serializable from langchain.load.serializable import Serializable
@ -229,7 +229,6 @@ class BaseMemory(Serializable, ABC):
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""
extra = Extra.forbid
arbitrary_types_allowed = True arbitrary_types_allowed = True
@property @property