2023-04-25 23:11:14 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"id": "bf733a38-db84-4363-89e2-de6735c37230",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Anthropic\n",
|
|
|
|
"\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"This notebook covers how to get started with Anthropic chat models."
|
2023-04-25 23:11:14 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 1,
|
|
|
|
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from langchain.chat_models import ChatAnthropic\n",
|
|
|
|
"from langchain.prompts.chat import (\n",
|
|
|
|
" ChatPromptTemplate,\n",
|
|
|
|
" SystemMessagePromptTemplate,\n",
|
|
|
|
" AIMessagePromptTemplate,\n",
|
|
|
|
" HumanMessagePromptTemplate,\n",
|
|
|
|
")\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
"from langchain.schema import AIMessage, HumanMessage, SystemMessage"
|
2023-04-25 23:11:14 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 2,
|
|
|
|
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"chat = ChatAnthropic()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 3,
|
|
|
|
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2023-07-06 01:02:35 +00:00
|
|
|
"AIMessage(content=\" J'aime la programmation.\", additional_kwargs={}, example=False)"
|
2023-04-25 23:11:14 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 3,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"messages = [\n",
|
2023-06-16 18:52:56 +00:00
|
|
|
" HumanMessage(\n",
|
|
|
|
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
|
|
|
" )\n",
|
2023-04-25 23:11:14 +00:00
|
|
|
"]\n",
|
|
|
|
"chat(messages)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"id": "c361ab1e-8c0c-4206-9e3c-9d1424a12b9c",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## `ChatAnthropic` also supports async and streaming functionality:"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 4,
|
|
|
|
"id": "93a21c5c-6ef9-4688-be60-b2e1f94842fb",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2023-05-02 23:20:16 +00:00
|
|
|
"from langchain.callbacks.manager import CallbackManager\n",
|
2023-04-25 23:11:14 +00:00
|
|
|
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 5,
|
|
|
|
"id": "c5fac0e9-05a4-4fc1-a3b3-e5bbb24b971b",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2023-07-06 01:02:35 +00:00
|
|
|
"LLMResult(generations=[[ChatGeneration(text=\" J'aime programmer.\", generation_info=None, message=AIMessage(content=\" J'aime programmer.\", additional_kwargs={}, example=False))]], llm_output={}, run=[RunInfo(run_id=UUID('8cc8fb68-1c35-439c-96a0-695036a93652'))])"
|
2023-04-25 23:11:14 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 5,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
|
|
|
"await chat.agenerate([messages])"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": 6,
|
|
|
|
"id": "025be980-e50d-4a68-93dc-c9c7b500ce34",
|
|
|
|
"metadata": {
|
|
|
|
"tags": []
|
|
|
|
},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "stdout",
|
|
|
|
"output_type": "stream",
|
|
|
|
"text": [
|
2023-07-06 01:02:35 +00:00
|
|
|
" J'aime la programmation."
|
2023-04-25 23:11:14 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"text/plain": [
|
2023-07-06 01:02:35 +00:00
|
|
|
"AIMessage(content=\" J'aime la programmation.\", additional_kwargs={}, example=False)"
|
2023-04-25 23:11:14 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
"execution_count": 6,
|
|
|
|
"metadata": {},
|
|
|
|
"output_type": "execute_result"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"source": [
|
2023-06-16 18:52:56 +00:00
|
|
|
"chat = ChatAnthropic(\n",
|
|
|
|
" streaming=True,\n",
|
|
|
|
" verbose=True,\n",
|
|
|
|
" callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n",
|
|
|
|
")\n",
|
2023-04-25 23:11:14 +00:00
|
|
|
"chat(messages)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
2023-07-06 01:02:35 +00:00
|
|
|
"id": "c253883f",
|
2023-04-25 23:11:14 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": []
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {
|
|
|
|
"kernelspec": {
|
|
|
|
"display_name": "Python 3 (ipykernel)",
|
|
|
|
"language": "python",
|
|
|
|
"name": "python3"
|
|
|
|
},
|
|
|
|
"language_info": {
|
|
|
|
"codemirror_mode": {
|
|
|
|
"name": "ipython",
|
|
|
|
"version": 3
|
|
|
|
},
|
|
|
|
"file_extension": ".py",
|
|
|
|
"mimetype": "text/x-python",
|
|
|
|
"name": "python",
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
"pygments_lexer": "ipython3",
|
2023-07-06 01:02:35 +00:00
|
|
|
"version": "3.9.1"
|
2023-04-25 23:11:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 5
|
|
|
|
}
|