docs: update mistral docs for version 0.1+ (#17011)

**Description:**
Updated integration page for mistralai.
pull/16757/head
Marc Mahe 5 months ago committed by GitHub
parent 929f071513
commit f08a9139d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -15,28 +15,53 @@
"id": "bf733a38-db84-4363-89e2-de6735c37230",
"metadata": {},
"source": [
"# ChatMistralAI\n",
"# MistralAI\n",
"\n",
"This notebook covers how to get started with MistralAI chat models, via their [API](https://docs.mistral.ai/api/).\n",
"\n",
"A valid [API key](https://console.mistral.ai/users/api-keys/) is needed to communicate with the API.\n",
"\n",
"You will need the `langchain-mistralai` package to use the API. You can install it via pip:"
"Head to the [API reference](https://api.python.langchain.com/en/latest/chat_models/langchain_mistralai.chat_models.ChatMistralAI.html) for detailed documentation of all attributes and methods."
]
},
{
"cell_type": "markdown",
"id": "cc686b8f",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"You will need the `langchain-core` and `langchain-mistralai` package to use the API. You can install these with:\n",
"\n",
"```bash\n",
"pip install -U langchain-core langchain-mistralai\n",
"\n",
"We'll also need to get a [Mistral API key](https://console.mistral.ai/users/api-keys/)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb978a7e",
"execution_count": 7,
"id": "c3fd4184",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-core langchain-mistralai"
"import getpass\n",
"\n",
"mistral_api_key = getpass.getpass()"
]
},
{
"cell_type": "markdown",
"id": "502127fd",
"metadata": {},
"source": [
"## Usage"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
"metadata": {
"tags": []
@ -49,23 +74,20 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 8,
"id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"\n",
"mistral_api_key = os.environ.get(\"MISTRAL_API_KEY\")\n",
"# If mistral_api_key is not passed, default behavior is to use the `MISTRAL_API_KEY` environment variable.\n",
"chat = ChatMistralAI(mistral_api_key=mistral_api_key)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 9,
"id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c",
"metadata": {
"tags": []
@ -74,16 +96,16 @@
{
"data": {
"text/plain": [
"AIMessage(content=\"Hello! I'm here to assist you. How can I help you today? If you have any questions or need information on a particular topic, feel free to ask. I'm ready to provide accurate and helpful answers to the best of my ability.\")"
"AIMessage(content=\"Who's there? I was just about to ask the same thing! How can I assist you today?\")"
]
},
"execution_count": 3,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"messages = [HumanMessage(content=\"say a brief hello\")]\n",
"messages = [HumanMessage(content=\"knock knock\")]\n",
"chat.invoke(messages)"
]
},
@ -92,12 +114,12 @@
"id": "c361ab1e-8c0c-4206-9e3c-9d1424a12b9c",
"metadata": {},
"source": [
"## `ChatMistralAI` also supports async and streaming functionality:"
"### Async"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 10,
"id": "c5fac0e9-05a4-4fc1-a3b3-e5bbb24b971b",
"metadata": {
"tags": []
@ -106,10 +128,10 @@
{
"data": {
"text/plain": [
"AIMessage(content=\"Hello! I'm glad you're here. If you have any questions or need assistance with something related to programming or software development, feel free to ask. I'll do my best to help you out. Have a great day!\")"
"AIMessage(content='Who\\'s there?\\n\\n(You can then continue the \"knock knock\" joke by saying the name of the person or character who should be responding. For example, if I say \"Banana,\" you could respond with \"Banana who?\" and I would say \"Banana bunch! Get it? Because a group of bananas is called a \\'bunch\\'!\" and then we would both laugh and have a great time. But really, you can put anything you want in the spot where I put \"Banana\" and it will still technically be a \"knock knock\" joke. The possibilities are endless!)')"
]
},
"execution_count": 4,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@ -118,9 +140,17 @@
"await chat.ainvoke(messages)"
]
},
{
"cell_type": "markdown",
"id": "86ccef97",
"metadata": {},
"source": [
"### Streaming\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 11,
"id": "025be980-e50d-4a68-93dc-c9c7b500ce34",
"metadata": {
"tags": []
@ -130,7 +160,27 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Hello! I'm happy to assist you. Is there a specific question or topic you would like to discuss? I can provide information and answer questions on a wide variety of subjects."
"Who's there?\n",
"\n",
"(After this, the conversation can continue as a call and response \"who's there\" joke. Here is an example of how it could go:\n",
"\n",
"You say: Orange.\n",
"I say: Orange who?\n",
"You say: Orange you glad I didn't say banana!?)\n",
"\n",
"But since you asked for a knock knock joke specifically, here's one for you:\n",
"\n",
"Knock knock.\n",
"\n",
"Me: Who's there?\n",
"\n",
"You: Lettuce.\n",
"\n",
"Me: Lettuce who?\n",
"\n",
"You: Lettuce in, it's too cold out here!\n",
"\n",
"I hope this brings a smile to your face! Do you have a favorite knock knock joke you'd like to share? I'd love to hear it."
]
}
],
@ -138,6 +188,79 @@
"for chunk in chat.stream(messages):\n",
" print(chunk.content, end=\"\")"
]
},
{
"cell_type": "markdown",
"id": "f6189577",
"metadata": {},
"source": [
"### Batch"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "e63aebcb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[AIMessage(content=\"Who's there? I was just about to ask the same thing! Go ahead and tell me who's there. I love a good knock-knock joke.\")]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat.batch([messages])"
]
},
{
"cell_type": "markdown",
"id": "38e39e71",
"metadata": {},
"source": [
"## Chaining\n",
"\n",
"You can also easily combine with a prompt template for easy structuring of user input. We can do this using [LCEL](/docs/expression_language)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "ee43a1ae",
"metadata": {},
"outputs": [],
"source": [
"from langchain_core.prompts import ChatPromptTemplate\n",
"\n",
"prompt = ChatPromptTemplate.from_template(\"Tell me a joke about {topic}\")\n",
"chain = prompt | chat"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "0dc49212",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='Why do bears hate shoes so much? They like to run around in their bear feet.')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain.invoke({\"topic\": \"bears\"})"
]
}
],
"metadata": {
@ -156,7 +279,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
"version": "3.10.12"
}
},
"nbformat": 4,

Loading…
Cancel
Save