From b3ecce0545005fab39891f92627ec12cca108108 Mon Sep 17 00:00:00 2001 From: Ankush Gola <9536492+agola11@users.noreply.github.com> Date: Mon, 8 May 2023 15:30:52 -0700 Subject: [PATCH] fix json saving, update docs to reference anthropic chat model (#4364) Fixes # (issue) https://github.com/hwchase17/langchain/issues/4085 --- .../models/llms/examples/streaming_llm.ipynb | 64 ++++++++----------- langchain/chains/base.py | 4 +- langchain/chat_models/base.py | 4 +- langchain/llms/base.py | 4 +- langchain/tools/base.py | 4 +- 5 files changed, 34 insertions(+), 46 deletions(-) diff --git a/docs/modules/models/llms/examples/streaming_llm.ipynb b/docs/modules/models/llms/examples/streaming_llm.ipynb index b2b5620d..19dce087 100644 --- a/docs/modules/models/llms/examples/streaming_llm.ipynb +++ b/docs/modules/models/llms/examples/streaming_llm.ipynb @@ -7,27 +7,27 @@ "source": [ "# How to stream LLM and Chat Model responses\n", "\n", - "LangChain provides streaming support for LLMs. Currently, we support streaming for the `OpenAI`, `ChatOpenAI`, and `Anthropic` implementations, but streaming support for other LLM implementations is on the roadmap. To utilize streaming, use a [`CallbackHandler`](https://github.com/hwchase17/langchain/blob/master/langchain/callbacks/base.py) that implements `on_llm_new_token`. In this example, we are using [`StreamingStdOutCallbackHandler`]()." + "LangChain provides streaming support for LLMs. Currently, we support streaming for the `OpenAI`, `ChatOpenAI`, and `ChatAnthropic` implementations, but streaming support for other LLM implementations is on the roadmap. To utilize streaming, use a [`CallbackHandler`](https://github.com/hwchase17/langchain/blob/master/langchain/callbacks/base.py) that implements `on_llm_new_token`. In this example, we are using [`StreamingStdOutCallbackHandler`]()." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "4ac0ff54-540a-4f2b-8d9a-b590fec7fe07", "metadata": { "tags": [] }, "outputs": [], "source": [ - "from langchain.llms import OpenAI, Anthropic\n", - "from langchain.chat_models import ChatOpenAI\n", + "from langchain.llms import OpenAI\n", + "from langchain.chat_models import ChatOpenAI, ChatAnthropic\n", "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", "from langchain.schema import HumanMessage" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "77f60a4b-f786-41f2-972e-e5bb8a48dcd5", "metadata": { "tags": [] @@ -94,7 +94,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "a35373f1-9ee6-4753-a343-5aee749b8527", "metadata": { "tags": [] @@ -113,10 +113,10 @@ { "data": { "text/plain": [ - "LLMResult(generations=[[Generation(text='\\n\\nQ: What did the fish say when it hit the wall?\\nA: Dam!', generation_info={'finish_reason': None, 'logprobs': None})]], llm_output={'token_usage': {}, 'model_name': 'text-davinci-003'})" + "LLMResult(generations=[[Generation(text='\\n\\nQ: What did the fish say when it hit the wall?\\nA: Dam!', generation_info={'finish_reason': 'stop', 'logprobs': None})]], llm_output={'token_usage': {}, 'model_name': 'text-davinci-003'})" ] }, - "execution_count": 4, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -135,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "22665f16-e05b-473c-a4bd-ad75744ea024", "metadata": { "tags": [] @@ -199,12 +199,12 @@ "id": "909ae48b-0f07-4990-bbff-e627f706c93e", "metadata": {}, "source": [ - "Here is an example with the `Anthropic` LLM implementation, which uses their `claude` model." + "Here is an example with the `ChatAnthropic` chat model implementation, which uses their `claude` model." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "eadae4ba-9f21-4ec8-845d-dd43b0edc2dc", "metadata": { "tags": [] @@ -214,38 +214,26 @@ "name": "stdout", "output_type": "stream", "text": [ + " Here is my attempt at a song about sparkling water:\n", "\n", - "Sparkling water, bubbles so bright,\n", - "\n", - "Fizzing and popping in the light.\n", - "\n", - "No sugar or calories, a healthy delight,\n", - "\n", - "Sparkling water, refreshing and light.\n", - "\n", - "Carbonation that tickles the tongue,\n", - "\n", - "In flavors of lemon and lime unsung.\n", - "\n", - "Sparkling water, a drink quite all right,\n", - "\n", - "Bubbles sparkling in the light." + "Sparkling water, bubbles so bright, \n", + "Dancing in the glass with delight.\n", + "Refreshing and crisp, a fizzy delight,\n", + "Quenching my thirst with each sip I take.\n", + "The carbonation tickles my tongue,\n", + "As the refreshing water song is sung.\n", + "Lime or lemon, a citrus twist,\n", + "Makes sparkling water such a bliss.\n", + "Healthy and hydrating, a drink so pure,\n", + "Sparkling water, always alluring.\n", + "Bubbles ascending in a stream, \n", + "Sparkling water, you're my dream!" ] - }, - { - "data": { - "text/plain": [ - "'\\nSparkling water, bubbles so bright,\\n\\nFizzing and popping in the light.\\n\\nNo sugar or calories, a healthy delight,\\n\\nSparkling water, refreshing and light.\\n\\nCarbonation that tickles the tongue,\\n\\nIn flavors of lemon and lime unsung.\\n\\nSparkling water, a drink quite all right,\\n\\nBubbles sparkling in the light.'" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "llm = Anthropic(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)\n", - "llm(\"Write me a song about sparkling water.\")" + "chat = ChatAnthropic(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)\n", + "resp = chat([HumanMessage(content=\"Write me a song about sparkling water.\")])" ] } ], diff --git a/langchain/chains/base.py b/langchain/chains/base.py index a457934f..b10a87dc 100644 --- a/langchain/chains/base.py +++ b/langchain/chains/base.py @@ -29,8 +29,8 @@ class Chain(BaseModel, ABC): """Base interface that all chains should implement.""" memory: Optional[BaseMemory] = None - callbacks: Callbacks = None - callback_manager: Optional[BaseCallbackManager] = None + callbacks: Callbacks = Field(default=None, exclude=True) + callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True) verbose: bool = Field( default_factory=_get_verbosity ) # Whether to print the response text diff --git a/langchain/chat_models/base.py b/langchain/chat_models/base.py index fbcc08b1..b41aa237 100644 --- a/langchain/chat_models/base.py +++ b/langchain/chat_models/base.py @@ -35,8 +35,8 @@ def _get_verbosity() -> bool: class BaseChatModel(BaseLanguageModel, ABC): verbose: bool = Field(default_factory=_get_verbosity) """Whether to print out response text.""" - callbacks: Callbacks = None - callback_manager: Optional[BaseCallbackManager] = None + callbacks: Callbacks = Field(default=None, exclude=True) + callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True) @root_validator() def raise_deprecation(cls, values: Dict) -> Dict: diff --git a/langchain/llms/base.py b/langchain/llms/base.py index dcab983d..fefda42e 100644 --- a/langchain/llms/base.py +++ b/langchain/llms/base.py @@ -68,8 +68,8 @@ class BaseLLM(BaseLanguageModel, ABC): cache: Optional[bool] = None verbose: bool = Field(default_factory=_get_verbosity) """Whether to print out response text.""" - callbacks: Callbacks = None - callback_manager: Optional[BaseCallbackManager] = None + callbacks: Callbacks = Field(default=None, exclude=True) + callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True) class Config: """Configuration for this pydantic object.""" diff --git a/langchain/tools/base.py b/langchain/tools/base.py index dd311e01..52f959ac 100644 --- a/langchain/tools/base.py +++ b/langchain/tools/base.py @@ -132,9 +132,9 @@ class BaseTool(ABC, BaseModel, metaclass=ToolMetaclass): verbose: bool = False """Whether to log the tool's progress.""" - callbacks: Callbacks = None + callbacks: Callbacks = Field(default=None, exclude=True) """Callbacks to be called during tool execution.""" - callback_manager: Optional[BaseCallbackManager] = None + callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True) """Deprecated. Please use callbacks instead.""" class Config: