From 4848be82759fdc9582b0a88f0caf4cf8c857d588 Mon Sep 17 00:00:00 2001 From: Shroominic Date: Fri, 15 Dec 2023 11:20:43 +0100 Subject: [PATCH] =?UTF-8?q?=E2=8F=BA=EF=B8=8F=20migrate=20to=20langchain?= =?UTF-8?q?=5Fcore=20abstractions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/funcchain/_llms.py | 10 ++++------ src/funcchain/chain/creation.py | 10 +++++----- src/funcchain/chain/interface.py | 5 +++-- src/funcchain/chain/invoke.py | 7 ++++--- src/funcchain/chain/prompt.py | 10 +++++----- src/funcchain/exceptions.py | 4 ++-- src/funcchain/parser.py | 6 ++++-- src/funcchain/settings.py | 6 +++--- src/funcchain/streaming.py | 8 ++++---- src/funcchain/types.py | 4 ++-- src/funcchain/utils/decorators.py | 6 +++--- src/funcchain/utils/function_frame.py | 2 +- src/funcchain/utils/helpers.py | 7 +++---- src/funcchain/utils/model_defaults.py | 2 +- 14 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/funcchain/_llms.py b/src/funcchain/_llms.py index 407bc1b..4a496df 100644 --- a/src/funcchain/_llms.py +++ b/src/funcchain/_llms.py @@ -4,12 +4,10 @@ from pathlib import Path from typing import Any, Dict, Iterator, List, Optional, Union -from langchain.callbacks.manager import CallbackManagerForLLMRun -from langchain.chat_models.base import BaseChatModel -from langchain.schema import ChatResult -from langchain.schema.messages import AIMessage, AIMessageChunk, BaseMessage, ChatMessage, HumanMessage, SystemMessage -from langchain.schema.output import ChatGeneration, ChatGenerationChunk -from langchain_core.language_models import BaseLanguageModel +from langchain_core.callbacks.manager import CallbackManagerForLLMRun +from langchain_core.language_models import BaseChatModel, BaseLanguageModel +from langchain_core.messages import AIMessage, AIMessageChunk, BaseMessage, ChatMessage, HumanMessage, SystemMessage +from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult from langchain_core.pydantic_v1 import Field, root_validator from langchain_core.utils import get_pydantic_field_names from langchain_core.utils.utils import build_extra_kwargs diff --git a/src/funcchain/chain/creation.py b/src/funcchain/chain/creation.py index 57005fd..74eea55 100644 --- a/src/funcchain/chain/creation.py +++ b/src/funcchain/chain/creation.py @@ -1,11 +1,11 @@ from types import UnionType from typing import TypeVar, Union -from langchain.chat_models.base import BaseChatModel -from langchain.prompts import ChatPromptTemplate -from langchain.schema import AIMessage, BaseMessage, BaseOutputParser, HumanMessage -from langchain.schema.chat_history import BaseChatMessageHistory -from langchain.schema.runnable import RunnableSequence, RunnableSerializable, RunnableWithFallbacks +from langchain_core.chat_models.base import BaseChatModel +from langchain_core.prompts import ChatPromptTemplate +from langchain_core.schema import AIMessage, BaseMessage, BaseOutputParser, HumanMessage +from langchain_core.schema.chat_history import BaseChatMessageHistory +from langchain_core.schema.runnable import RunnableSequence, RunnableSerializable, RunnableWithFallbacks from PIL import Image from pydantic import BaseModel diff --git a/src/funcchain/chain/interface.py b/src/funcchain/chain/interface.py index 8c3dfbe..17579e6 100644 --- a/src/funcchain/chain/interface.py +++ b/src/funcchain/chain/interface.py @@ -1,8 +1,9 @@ from typing import TypeVar from langchain.memory import ChatMessageHistory -from langchain.schema import BaseMessage, BaseOutputParser -from langchain.schema.chat_history import BaseChatMessageHistory +from langchain_core.chat_history import BaseChatMessageHistory +from langchain_core.messages import BaseMessage +from langchain_core.output_parsers import BaseOutputParser from ..settings import SettingsOverride, get_settings from .invoke import ainvoke, invoke diff --git a/src/funcchain/chain/invoke.py b/src/funcchain/chain/invoke.py index fd85510..56d1220 100644 --- a/src/funcchain/chain/invoke.py +++ b/src/funcchain/chain/invoke.py @@ -1,8 +1,9 @@ from typing import TypeVar -from langchain.callbacks.base import Callbacks -from langchain.schema import BaseMessage, BaseOutputParser -from langchain.schema.chat_history import BaseChatMessageHistory +from langchain_core.callbacks.base import Callbacks +from langchain_core.chat_history import BaseChatMessageHistory +from langchain_core.messages import BaseMessage +from langchain_core.output_parsers import BaseOutputParser from ..settings import FuncchainSettings from ..utils.decorators import get_parent_frame, log_openai_callback, retry_parse diff --git a/src/funcchain/chain/prompt.py b/src/funcchain/chain/prompt.py index 63c9c9a..9f44d27 100644 --- a/src/funcchain/chain/prompt.py +++ b/src/funcchain/chain/prompt.py @@ -1,11 +1,11 @@ from string import Formatter from typing import Any, Optional, Type -from langchain.prompts import ChatPromptTemplate -from langchain.prompts.chat import BaseStringMessagePromptTemplate, MessagePromptTemplateT -from langchain.prompts.prompt import PromptTemplate -from langchain.schema import BaseMessage, HumanMessage, SystemMessage -from langchain.schema.chat_history import BaseChatMessageHistory +from langchain_core.chat_history import BaseChatMessageHistory +from langchain_core.messages import BaseMessage, HumanMessage, SystemMessage +from langchain_core.prompts import ChatPromptTemplate +from langchain_core.prompts.chat import BaseStringMessagePromptTemplate, MessagePromptTemplateT +from langchain_core.prompts.prompt import PromptTemplate from PIL import Image from pydantic import BaseModel diff --git a/src/funcchain/exceptions.py b/src/funcchain/exceptions.py index 1c4ddfe..c463839 100644 --- a/src/funcchain/exceptions.py +++ b/src/funcchain/exceptions.py @@ -1,7 +1,7 @@ from typing import Any -from langchain.schema.messages import BaseMessage -from langchain.schema.output_parser import OutputParserException +from langchain_core.exceptions import OutputParserException +from langchain_core.messages import BaseMessage class ParsingRetryException(OutputParserException): diff --git a/src/funcchain/parser.py b/src/funcchain/parser.py index 6ad55b6..89bb445 100644 --- a/src/funcchain/parser.py +++ b/src/funcchain/parser.py @@ -3,8 +3,10 @@ import re from typing import Callable, Optional, Type, TypeVar -from langchain.schema import AIMessage, ChatGeneration, Generation, OutputParserException -from langchain.schema.output_parser import BaseGenerationOutputParser, BaseOutputParser +from langchain_core.exceptions import OutputParserException +from langchain_core.messages import AIMessage +from langchain_core.output_parsers import BaseGenerationOutputParser, BaseOutputParser +from langchain_core.outputs import ChatGeneration, Generation from pydantic import BaseModel, ValidationError from .exceptions import ParsingRetryException diff --git a/src/funcchain/settings.py b/src/funcchain/settings.py index 881903a..403dcf4 100644 --- a/src/funcchain/settings.py +++ b/src/funcchain/settings.py @@ -5,9 +5,9 @@ from typing import Optional, TypedDict from langchain.cache import InMemoryCache -from langchain.chat_models.base import BaseChatModel -from langchain.globals import set_llm_cache -from langchain.schema.runnable import RunnableWithFallbacks +from langchain_core.globals import set_llm_cache +from langchain_core.language_models import BaseChatModel +from langchain_core.runnables import RunnableWithFallbacks from pydantic import Field from pydantic_settings import BaseSettings diff --git a/src/funcchain/streaming.py b/src/funcchain/streaming.py index 5e6e368..259ac31 100644 --- a/src/funcchain/streaming.py +++ b/src/funcchain/streaming.py @@ -3,13 +3,13 @@ from typing import Any, AsyncGenerator, Awaitable, Callable, Coroutine, Generator from uuid import UUID -from langchain.callbacks.base import AsyncCallbackHandler -from langchain.schema.messages import BaseMessage -from langchain.schema.output import ChatGenerationChunk, GenerationChunk, LLMResult +from langchain_core.callbacks.base import AsyncCallbackHandler +from langchain_core.messages import BaseMessage +from langchain_core.outputs import ChatGenerationChunk, GenerationChunk, LLMResult class AsyncStreamHandler(AsyncCallbackHandler): - """Async callback handler that can be used to handle callbacks from langchain.""" + """Async callback handler that can be used to handle callbacks from langchain_core.""" def __init__( self, fn: Callable[[str], Awaitable[None] | None], default_kwargs: dict diff --git a/src/funcchain/types.py b/src/funcchain/types.py index e150cae..ac94ea3 100644 --- a/src/funcchain/types.py +++ b/src/funcchain/types.py @@ -3,8 +3,8 @@ from typing import Optional from langchain.output_parsers.format_instructions import PYDANTIC_FORMAT_INSTRUCTIONS -from langchain.schema import OutputParserException -from langchain.schema.output_parser import BaseOutputParser +from langchain_core.exceptions import OutputParserException +from langchain_core.output_parsers import BaseOutputParser from pydantic import BaseModel, Field from typing_extensions import Self diff --git a/src/funcchain/utils/decorators.py b/src/funcchain/utils/decorators.py index b510106..eca8379 100644 --- a/src/funcchain/utils/decorators.py +++ b/src/funcchain/utils/decorators.py @@ -6,9 +6,9 @@ from langchain.callbacks import get_openai_callback from langchain.callbacks.openai_info import OpenAICallbackHandler -from langchain.schema import AIMessage -from langchain.schema.chat_history import BaseChatMessageHistory -from langchain.schema.output_parser import OutputParserException +from langchain_core.chat_history import BaseChatMessageHistory +from langchain_core.exceptions import OutputParserException +from langchain_core.messages import AIMessage from rich import print from ..exceptions import ParsingRetryException diff --git a/src/funcchain/utils/function_frame.py b/src/funcchain/utils/function_frame.py index 624d140..c93c2f1 100644 --- a/src/funcchain/utils/function_frame.py +++ b/src/funcchain/utils/function_frame.py @@ -2,7 +2,7 @@ from inspect import FrameInfo, currentframe, getouterframes from typing import Union -from langchain.schema import BaseOutputParser, StrOutputParser +from langchain_core.output_parsers import BaseOutputParser, StrOutputParser from ..parser import BoolOutputParser, ParserBaseModel, PydanticOutputParser diff --git a/src/funcchain/utils/helpers.py b/src/funcchain/utils/helpers.py index 586bf7d..16005e3 100644 --- a/src/funcchain/utils/helpers.py +++ b/src/funcchain/utils/helpers.py @@ -2,10 +2,9 @@ from docstring_parser import parse from langchain.chat_models import ChatOpenAI -from langchain.chat_models.base import BaseChatModel -from langchain.schema.language_model import BaseLanguageModel -from langchain.schema.messages import HumanMessage, SystemMessage -from langchain.schema.runnable import Runnable, RunnableWithFallbacks +from langchain_core.language_models import BaseChatModel, BaseLanguageModel +from langchain_core.messages import HumanMessage, SystemMessage +from langchain_core.runnables import Runnable, RunnableWithFallbacks from pydantic import BaseModel from tiktoken import encoding_for_model diff --git a/src/funcchain/utils/model_defaults.py b/src/funcchain/utils/model_defaults.py index 8ef0692..b6c4d5f 100644 --- a/src/funcchain/utils/model_defaults.py +++ b/src/funcchain/utils/model_defaults.py @@ -2,7 +2,7 @@ from typing import Any from langchain.chat_models import AzureChatOpenAI, ChatAnthropic, ChatGooglePalm, ChatOpenAI -from langchain.chat_models.base import BaseChatModel +from langchain_core.language_models import BaseChatModel from .._llms import ChatLlamaCpp from ..settings import FuncchainSettings