|
|
|
@ -3,10 +3,14 @@
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "raw",
|
|
|
|
|
"id": "fbc66410",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"metadata": {
|
|
|
|
|
"vscode": {
|
|
|
|
|
"languageId": "raw"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"source": [
|
|
|
|
|
"---\n",
|
|
|
|
|
"sidebar_label: Bedrock Chat\n",
|
|
|
|
|
"sidebar_label: Bedrock\n",
|
|
|
|
|
"---"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
@ -15,7 +19,7 @@
|
|
|
|
|
"id": "bf733a38-db84-4363-89e2-de6735c37230",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"# BedrockChat\n",
|
|
|
|
|
"# ChatBedrock\n",
|
|
|
|
|
"\n",
|
|
|
|
|
">[Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that offers a choice of \n",
|
|
|
|
|
"> high-performing foundation models (FMs) from leading AI companies like `AI21 Labs`, `Anthropic`, `Cohere`, \n",
|
|
|
|
@ -30,42 +34,53 @@
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"execution_count": 2,
|
|
|
|
|
"id": "d51edc81",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"Note: you may need to restart the kernel to use updated packages.\n"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"%pip install --upgrade --quiet boto3"
|
|
|
|
|
"%pip install --upgrade --quiet langchain-aws"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"execution_count": 1,
|
|
|
|
|
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
|
|
|
|
|
"metadata": {
|
|
|
|
|
"tags": []
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"from langchain_community.chat_models import BedrockChat\n",
|
|
|
|
|
"from langchain_aws import ChatBedrock\n",
|
|
|
|
|
"from langchain_core.messages import HumanMessage"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 2,
|
|
|
|
|
"execution_count": 11,
|
|
|
|
|
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
|
|
|
|
|
"metadata": {
|
|
|
|
|
"tags": []
|
|
|
|
|
},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"chat = BedrockChat(model_id=\"anthropic.claude-v2\", model_kwargs={\"temperature\": 0.1})"
|
|
|
|
|
"chat = ChatBedrock(\n",
|
|
|
|
|
" model_id=\"anthropic.claude-3-sonnet-20240229-v1:0\",\n",
|
|
|
|
|
" model_kwargs={\"temperature\": 0.1},\n",
|
|
|
|
|
")"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 3,
|
|
|
|
|
"execution_count": 12,
|
|
|
|
|
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
|
|
|
|
|
"metadata": {
|
|
|
|
|
"tags": []
|
|
|
|
@ -74,10 +89,10 @@
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"text/plain": [
|
|
|
|
|
"AIMessage(content=\" Voici la traduction en français : J'adore programmer.\", additional_kwargs={}, example=False)"
|
|
|
|
|
"AIMessage(content=\"Voici la traduction en français :\\n\\nJ'aime la programmation.\", additional_kwargs={'usage': {'prompt_tokens': 20, 'completion_tokens': 21, 'total_tokens': 41}}, response_metadata={'model_id': 'anthropic.claude-3-sonnet-20240229-v1:0', 'usage': {'prompt_tokens': 20, 'completion_tokens': 21, 'total_tokens': 41}}, id='run-994f0362-0e50-4524-afad-3c4f5bb11328-0')"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"execution_count": 3,
|
|
|
|
|
"execution_count": 12,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "execute_result"
|
|
|
|
|
}
|
|
|
|
@ -88,7 +103,7 @@
|
|
|
|
|
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
|
|
|
|
" )\n",
|
|
|
|
|
"]\n",
|
|
|
|
|
"chat(messages)"
|
|
|
|
|
"chat.invoke(messages)"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
@ -97,39 +112,30 @@
|
|
|
|
|
"id": "a4a4f4d4",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"### For BedrockChat with Streaming"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"id": "c253883f",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
|
|
|
|
|
"### Streaming\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"chat = BedrockChat(\n",
|
|
|
|
|
" model_id=\"anthropic.claude-v2\",\n",
|
|
|
|
|
" streaming=True,\n",
|
|
|
|
|
" callbacks=[StreamingStdOutCallbackHandler()],\n",
|
|
|
|
|
" model_kwargs={\"temperature\": 0.1},\n",
|
|
|
|
|
")"
|
|
|
|
|
"To stream responses, you can use the runnable `.stream()` method."
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"execution_count": 14,
|
|
|
|
|
"id": "d9e52838",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"Voici la traduction en français :\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"J'aime la programmation."
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"messages = [\n",
|
|
|
|
|
" HumanMessage(\n",
|
|
|
|
|
" content=\"Translate this sentence from English to French. I love programming.\"\n",
|
|
|
|
|
" )\n",
|
|
|
|
|
"]\n",
|
|
|
|
|
"chat(messages)"
|
|
|
|
|
"for chunk in chat.stream(messages):\n",
|
|
|
|
|
" print(chunk.content, end=\"\", flush=True)"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
@ -149,7 +155,7 @@
|
|
|
|
|
"name": "python",
|
|
|
|
|
"nbconvert_exporter": "python",
|
|
|
|
|
"pygments_lexer": "ipython3",
|
|
|
|
|
"version": "3.10.12"
|
|
|
|
|
"version": "3.11.4"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"nbformat": 4,
|
|
|
|
|