fix json saving, update docs to reference anthropic chat model (#4364)

Fixes # (issue)
https://github.com/hwchase17/langchain/issues/4085
This commit is contained in:
Ankush Gola 2023-05-08 15:30:52 -07:00 committed by GitHub
parent b04d84f6b3
commit b3ecce0545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 46 deletions

View File

@ -7,27 +7,27 @@
"source": [ "source": [
"# How to stream LLM and Chat Model responses\n", "# How to stream LLM and Chat Model responses\n",
"\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", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"id": "4ac0ff54-540a-4f2b-8d9a-b590fec7fe07", "id": "4ac0ff54-540a-4f2b-8d9a-b590fec7fe07",
"metadata": { "metadata": {
"tags": [] "tags": []
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"from langchain.llms import OpenAI, Anthropic\n", "from langchain.llms import OpenAI\n",
"from langchain.chat_models import ChatOpenAI\n", "from langchain.chat_models import ChatOpenAI, ChatAnthropic\n",
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
"from langchain.schema import HumanMessage" "from langchain.schema import HumanMessage"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"id": "77f60a4b-f786-41f2-972e-e5bb8a48dcd5", "id": "77f60a4b-f786-41f2-972e-e5bb8a48dcd5",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -94,7 +94,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"id": "a35373f1-9ee6-4753-a343-5aee749b8527", "id": "a35373f1-9ee6-4753-a343-5aee749b8527",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -113,10 +113,10 @@
{ {
"data": { "data": {
"text/plain": [ "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": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -135,7 +135,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"id": "22665f16-e05b-473c-a4bd-ad75744ea024", "id": "22665f16-e05b-473c-a4bd-ad75744ea024",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -199,12 +199,12 @@
"id": "909ae48b-0f07-4990-bbff-e627f706c93e", "id": "909ae48b-0f07-4990-bbff-e627f706c93e",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"id": "eadae4ba-9f21-4ec8-845d-dd43b0edc2dc", "id": "eadae4ba-9f21-4ec8-845d-dd43b0edc2dc",
"metadata": { "metadata": {
"tags": [] "tags": []
@ -214,38 +214,26 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
" Here is my attempt at a song about sparkling water:\n",
"\n", "\n",
"Sparkling water, bubbles so bright,\n", "Sparkling water, bubbles so bright, \n",
"\n", "Dancing in the glass with delight.\n",
"Fizzing and popping in the light.\n", "Refreshing and crisp, a fizzy delight,\n",
"\n", "Quenching my thirst with each sip I take.\n",
"No sugar or calories, a healthy delight,\n", "The carbonation tickles my tongue,\n",
"\n", "As the refreshing water song is sung.\n",
"Sparkling water, refreshing and light.\n", "Lime or lemon, a citrus twist,\n",
"\n", "Makes sparkling water such a bliss.\n",
"Carbonation that tickles the tongue,\n", "Healthy and hydrating, a drink so pure,\n",
"\n", "Sparkling water, always alluring.\n",
"In flavors of lemon and lime unsung.\n", "Bubbles ascending in a stream, \n",
"\n", "Sparkling water, you're my dream!"
"Sparkling water, a drink quite all right,\n",
"\n",
"Bubbles sparkling in the light."
] ]
},
{
"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": [ "source": [
"llm = Anthropic(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)\n", "chat = ChatAnthropic(streaming=True, callbacks=[StreamingStdOutCallbackHandler()], temperature=0)\n",
"llm(\"Write me a song about sparkling water.\")" "resp = chat([HumanMessage(content=\"Write me a song about sparkling water.\")])"
] ]
} }
], ],

View File

@ -29,8 +29,8 @@ class Chain(BaseModel, ABC):
"""Base interface that all chains should implement.""" """Base interface that all chains should implement."""
memory: Optional[BaseMemory] = None memory: Optional[BaseMemory] = None
callbacks: Callbacks = None callbacks: Callbacks = Field(default=None, exclude=True)
callback_manager: Optional[BaseCallbackManager] = None callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
verbose: bool = Field( verbose: bool = Field(
default_factory=_get_verbosity default_factory=_get_verbosity
) # Whether to print the response text ) # Whether to print the response text

View File

@ -35,8 +35,8 @@ def _get_verbosity() -> bool:
class BaseChatModel(BaseLanguageModel, ABC): class BaseChatModel(BaseLanguageModel, ABC):
verbose: bool = Field(default_factory=_get_verbosity) verbose: bool = Field(default_factory=_get_verbosity)
"""Whether to print out response text.""" """Whether to print out response text."""
callbacks: Callbacks = None callbacks: Callbacks = Field(default=None, exclude=True)
callback_manager: Optional[BaseCallbackManager] = None callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
@root_validator() @root_validator()
def raise_deprecation(cls, values: Dict) -> Dict: def raise_deprecation(cls, values: Dict) -> Dict:

View File

@ -68,8 +68,8 @@ class BaseLLM(BaseLanguageModel, ABC):
cache: Optional[bool] = None cache: Optional[bool] = None
verbose: bool = Field(default_factory=_get_verbosity) verbose: bool = Field(default_factory=_get_verbosity)
"""Whether to print out response text.""" """Whether to print out response text."""
callbacks: Callbacks = None callbacks: Callbacks = Field(default=None, exclude=True)
callback_manager: Optional[BaseCallbackManager] = None callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
class Config: class Config:
"""Configuration for this pydantic object.""" """Configuration for this pydantic object."""

View File

@ -132,9 +132,9 @@ class BaseTool(ABC, BaseModel, metaclass=ToolMetaclass):
verbose: bool = False verbose: bool = False
"""Whether to log the tool's progress.""" """Whether to log the tool's progress."""
callbacks: Callbacks = None callbacks: Callbacks = Field(default=None, exclude=True)
"""Callbacks to be called during tool execution.""" """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.""" """Deprecated. Please use callbacks instead."""
class Config: class Config: