Skip to content

Commit

Permalink
feat(mistral): Mistral 1.3.1 migration (#7218)
Browse files Browse the repository at this point in the history
Co-authored-by: Ashtian <[email protected]>
Co-authored-by: BaNg-W <[email protected]>
Co-authored-by: CarterMorris <[email protected]>
Co-authored-by: BaNg-W <[email protected]>
Co-authored-by: BaNg-W <[email protected]>
Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
7 people authored Nov 22, 2024
1 parent d2e1f4f commit 78a6995
Show file tree
Hide file tree
Showing 14 changed files with 1,866 additions and 324 deletions.
117 changes: 114 additions & 3 deletions docs/core_docs/docs/integrations/chat/mistral.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"\n",
"| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n",
"| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n",
"| ✅ | ✅ | ✅ | | ❌ | ❌ | ✅ | ✅ | ❌ | \n",
"| ✅ | ✅ | ✅ | | ❌ | ❌ | ✅ | ✅ | ❌ | \n",
"\n",
"## Setup\n",
"\n",
Expand Down Expand Up @@ -88,7 +88,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -323,6 +323,117 @@
"console.log(calcToolRes.tool_calls);"
]
},
{
"cell_type": "markdown",
"id": "85dcbecc",
"metadata": {},
"source": [
"## Hooks\n",
"\n",
"Mistral AI supports custom hooks for three events: beforeRequest, requestError, and reponse. Examples of the function signature for each hook type can be seen below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "74b8b855",
"metadata": {},
"outputs": [],
"source": [
"const beforeRequestHook = (req: Request): Request | void | Promise<Request | void> => {\n",
" // Code to run before a request is processed by Mistral\n",
"};\n",
"\n",
"const requestErrorHook = (err: unknown, req: Request): void | Promise<void> => {\n",
" // Code to run when an error occurs as Mistral is processing a request\n",
"};\n",
"\n",
"const responseHook = (res: Response, req: Request): void | Promise<void> => {\n",
" // Code to run before Mistral sends a successful response\n",
"};"
]
},
{
"cell_type": "markdown",
"id": "930df6c4",
"metadata": {},
"source": [
"To add these hooks to the chat model, either pass them as arguments and they are automatically added:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b8084f6",
"metadata": {},
"outputs": [],
"source": [
"import { ChatMistralAI } from \"@langchain/mistralai\" \n",
"\n",
"const modelWithHooks = new ChatMistralAI({\n",
" model: \"mistral-large-latest\",\n",
" temperature: 0,\n",
" maxRetries: 2,\n",
" beforeRequestHooks: [ beforeRequestHook ],\n",
" requestErrorHooks: [ requestErrorHook ],\n",
" responseHooks: [ responseHook ],\n",
" // other params...\n",
"});"
]
},
{
"cell_type": "markdown",
"id": "cc9478f3",
"metadata": {},
"source": [
"Or assign and add them manually after instantiation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "daa70dc3",
"metadata": {},
"outputs": [],
"source": [
"import { ChatMistralAI } from \"@langchain/mistralai\" \n",
"\n",
"const model = new ChatMistralAI({\n",
" model: \"mistral-large-latest\",\n",
" temperature: 0,\n",
" maxRetries: 2,\n",
" // other params...\n",
"});\n",
"\n",
"model.beforeRequestHooks = [ ...model.beforeRequestHooks, beforeRequestHook ];\n",
"model.requestErrorHooks = [ ...model.requestErrorHooks, requestErrorHook ];\n",
"model.responseHooks = [ ...model.responseHooks, responseHook ];\n",
"\n",
"model.addAllHooksToHttpClient();"
]
},
{
"cell_type": "markdown",
"id": "389f5159",
"metadata": {},
"source": [
"The method addAllHooksToHttpClient clears all currently added hooks before assigning the entire updated hook lists to avoid hook duplication.\n",
"\n",
"Hooks can be removed one at a time, or all hooks can be cleared from the model at once."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a56b64bb",
"metadata": {},
"outputs": [],
"source": [
"model.removeHookFromHttpClient(beforeRequestHook);\n",
"\n",
"model.removeAllHooksFromHttpClient();"
]
},
{
"cell_type": "markdown",
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
Expand Down Expand Up @@ -354,4 +465,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
105 changes: 104 additions & 1 deletion docs/core_docs/docs/integrations/llms/mistral.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,109 @@
"console.log(customOutputParser(resWithParser));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hooks\n",
"\n",
"Mistral AI supports custom hooks for three events: beforeRequest, requestError, and reponse. Examples of the function signature for each hook type can be seen below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"const beforeRequestHook = (req: Request): Request | void | Promise<Request | void> => {\n",
" // Code to run before a request is processed by Mistral\n",
"};\n",
"\n",
"const requestErrorHook = (err: unknown, req: Request): void | Promise<void> => {\n",
" // Code to run when an error occurs as Mistral is processing a request\n",
"};\n",
"\n",
"const responseHook = (res: Response, req: Request): void | Promise<void> => {\n",
" // Code to run before Mistral sends a successful response\n",
"};"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To add these hooks to the chat model, either pass them as arguments and they are automatically added:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import { ChatMistralAI } from \"@langchain/mistralai\" \n",
"\n",
"const modelWithHooks = new ChatMistralAI({\n",
" model: \"mistral-large-latest\",\n",
" temperature: 0,\n",
" maxRetries: 2,\n",
" beforeRequestHooks: [ beforeRequestHook ],\n",
" requestErrorHooks: [ requestErrorHook ],\n",
" responseHooks: [ responseHook ],\n",
" // other params...\n",
"});"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or assign and add them manually after instantiation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import { ChatMistralAI } from \"@langchain/mistralai\" \n",
"\n",
"const model = new ChatMistralAI({\n",
" model: \"mistral-large-latest\",\n",
" temperature: 0,\n",
" maxRetries: 2,\n",
" // other params...\n",
"});\n",
"\n",
"model.beforeRequestHooks = [ ...model.beforeRequestHooks, beforeRequestHook ];\n",
"model.requestErrorHooks = [ ...model.requestErrorHooks, requestErrorHook ];\n",
"model.responseHooks = [ ...model.responseHooks, responseHook ];\n",
"\n",
"model.addAllHooksToHttpClient();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The method addAllHooksToHttpClient clears all currently added hooks before assigning the entire updated hook lists to avoid hook duplication.\n",
"\n",
"Hooks can be removed one at a time, or all hooks can be cleared from the model at once."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.removeHookFromHttpClient(beforeRequestHook);\n",
"\n",
"model.removeAllHooksFromHttpClient();"
]
},
{
"cell_type": "markdown",
"id": "e9bdfcef",
Expand Down Expand Up @@ -307,4 +410,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
105 changes: 104 additions & 1 deletion docs/core_docs/docs/integrations/text_embedding/mistralai.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,109 @@
"console.log(vectors[1].slice(0, 100));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hooks\n",
"\n",
"Mistral AI supports custom hooks for three events: beforeRequest, requestError, and reponse. Examples of the function signature for each hook type can be seen below:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"const beforeRequestHook = (req: Request): Request | void | Promise<Request | void> => {\n",
" // Code to run before a request is processed by Mistral\n",
"};\n",
"\n",
"const requestErrorHook = (err: unknown, req: Request): void | Promise<void> => {\n",
" // Code to run when an error occurs as Mistral is processing a request\n",
"};\n",
"\n",
"const responseHook = (res: Response, req: Request): void | Promise<void> => {\n",
" // Code to run before Mistral sends a successful response\n",
"};"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To add these hooks to the chat model, either pass them as arguments and they are automatically added:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import { ChatMistralAI } from \"@langchain/mistralai\" \n",
"\n",
"const modelWithHooks = new ChatMistralAI({\n",
" model: \"mistral-large-latest\",\n",
" temperature: 0,\n",
" maxRetries: 2,\n",
" beforeRequestHooks: [ beforeRequestHook ],\n",
" requestErrorHooks: [ requestErrorHook ],\n",
" responseHooks: [ responseHook ],\n",
" // other params...\n",
"});"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or assign and add them manually after instantiation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import { ChatMistralAI } from \"@langchain/mistralai\" \n",
"\n",
"const model = new ChatMistralAI({\n",
" model: \"mistral-large-latest\",\n",
" temperature: 0,\n",
" maxRetries: 2,\n",
" // other params...\n",
"});\n",
"\n",
"model.beforeRequestHooks = [ ...model.beforeRequestHooks, beforeRequestHook ];\n",
"model.requestErrorHooks = [ ...model.requestErrorHooks, requestErrorHook ];\n",
"model.responseHooks = [ ...model.responseHooks, responseHook ];\n",
"\n",
"model.addAllHooksToHttpClient();"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The method addAllHooksToHttpClient clears all currently added hooks before assigning the entire updated hook lists to avoid hook duplication.\n",
"\n",
"Hooks can be removed one at a time, or all hooks can be cleared from the model at once."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.removeHookFromHttpClient(beforeRequestHook);\n",
"\n",
"model.removeAllHooksFromHttpClient();"
]
},
{
"cell_type": "markdown",
"id": "8938e581",
Expand Down Expand Up @@ -341,4 +444,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
6 changes: 3 additions & 3 deletions libs/langchain-mistralai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
"author": "LangChain",
"license": "MIT",
"dependencies": {
"@mistralai/mistralai": "^0.4.0",
"@mistralai/mistralai": "^1.3.1",
"uuid": "^10.0.0",
"zod": "^3.22.4",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.22.4"
},
"peerDependencies": {
"@langchain/core": ">=0.2.21 <0.4.0"
"@langchain/core": ">=0.3.7 <0.4.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
Expand Down
Loading

0 comments on commit 78a6995

Please sign in to comment.