-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mistralai[minor]: Add llms entrypoint, update chat model integration #5603
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
116e01d
mistralai[minor]: Add llms entrypoint
bracesproul 6d5a259
chore: lint files
bracesproul 6a17555
Merge branch 'main' into brace/mistral-llm-completions
bracesproul 4a91836
cr
bracesproul 61c8d36
Merge branch 'main' into brace/mistral-llm-completions
bracesproul 810a0f8
docs
bracesproul 1397691
docs nit
bracesproul 6278b57
docs nit
bracesproul 9dc95f0
update mistral-large models to mistral-large-latest
bracesproul 21fdc59
fix tools
bracesproul 680456e
add codestral chat model tests
bracesproul a19c918
chore: lint files
bracesproul 4e7f47f
cr
bracesproul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file renamed
BIN
+309 KB
...ralai-npm-0.1.3-934551f985-3f8299811b.zip → ...ralai-npm-0.4.0-843348d71c-1b03fc0b55.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,11 @@ import { | |
ChatCompletionResponse, | ||
Function as MistralAIFunction, | ||
ToolCalls as MistralAIToolCalls, | ||
ToolChoice as MistralAIToolChoice, | ||
ResponseFormat, | ||
ChatCompletionResponseChunk, | ||
ToolType, | ||
ChatRequest, | ||
Tool as MistralAITool, | ||
Message as MistralAIMessage, | ||
} from "@mistralai/mistralai"; | ||
import { | ||
MessageType, | ||
|
@@ -44,7 +45,6 @@ import { | |
import { getEnvironmentVariable } from "@langchain/core/utils/env"; | ||
import { NewTokenIndices } from "@langchain/core/callbacks/base"; | ||
import { StructuredTool, StructuredToolInterface } from "@langchain/core/tools"; | ||
import { convertToOpenAITool } from "@langchain/core/utils/function_calling"; | ||
import { z } from "zod"; | ||
import { | ||
type BaseLLMOutputParser, | ||
|
@@ -70,40 +70,15 @@ interface TokenUsage { | |
totalTokens?: number; | ||
} | ||
|
||
type MistralAIInputMessage = { | ||
role: string; | ||
name?: string; | ||
content: string | string[]; | ||
tool_calls?: MistralAIToolCalls[]; | ||
}; | ||
export type MistralAIToolChoice = "auto" | "any" | "none"; | ||
|
||
type MistralAIToolInput = { type: string; function: MistralAIFunction }; | ||
|
||
type MistralAIChatCompletionOptions = { | ||
model: string; | ||
messages: Array<{ | ||
role: string; | ||
name?: string; | ||
content: string | string[]; | ||
tool_calls?: MistralAIToolCalls[]; | ||
}>; | ||
tools?: Array<MistralAIToolInput>; | ||
temperature?: number; | ||
maxTokens?: number; | ||
topP?: number; | ||
randomSeed?: number; | ||
safeMode?: boolean; | ||
safePrompt?: boolean; | ||
toolChoice?: MistralAIToolChoice; | ||
responseFormat?: ResponseFormat; | ||
}; | ||
|
||
interface MistralAICallOptions | ||
extends Omit<BaseLanguageModelCallOptions, "stop"> { | ||
response_format?: { | ||
type: "text" | "json_object"; | ||
}; | ||
tools: StructuredToolInterface[] | MistralAIToolInput[]; | ||
tools: StructuredToolInterface[] | MistralAIToolInput[] | MistralAITool[]; | ||
tool_choice?: MistralAIToolChoice; | ||
} | ||
|
||
|
@@ -178,7 +153,7 @@ export interface ChatMistralAIInput extends BaseChatModelParams { | |
|
||
function convertMessagesToMistralMessages( | ||
messages: Array<BaseMessage> | ||
): Array<MistralAIInputMessage> { | ||
): Array<MistralAIMessage> { | ||
const getRole = (role: MessageType) => { | ||
switch (role) { | ||
case "human": | ||
|
@@ -222,18 +197,28 @@ function convertMessagesToMistralMessages( | |
message.additional_kwargs.tool_calls; | ||
return toolCalls?.map((toolCall) => ({ | ||
id: "null", | ||
type: "function" as ToolType.function, | ||
type: "function", | ||
function: toolCall.function, | ||
})); | ||
}; | ||
|
||
return messages.map((message) => { | ||
const toolCalls = getTools(message); | ||
const content = toolCalls === undefined ? getContent(message.content) : ""; | ||
const role = getRole(message._getType()); | ||
|
||
if (role === "tool" && toolCalls && toolCalls.length > 0) { | ||
return { | ||
role: "tool", | ||
content, | ||
name: toolCalls[0].function.name, | ||
tool_call_id: toolCalls[0].id, | ||
}; | ||
} | ||
|
||
return { | ||
role: getRole(message._getType()), | ||
role: role as "system" | "user" | "assistant", | ||
content, | ||
tool_calls: toolCalls, | ||
}; | ||
}); | ||
} | ||
|
@@ -270,7 +255,10 @@ function mistralAIResponseToChatMessage( | |
tool_calls: toolCalls, | ||
invalid_tool_calls: invalidToolCalls, | ||
additional_kwargs: { | ||
tool_calls: rawToolCalls, | ||
tool_calls: rawToolCalls.map((toolCall) => ({ | ||
...toolCall, | ||
type: "function", | ||
})), | ||
}, | ||
}); | ||
} | ||
|
@@ -350,8 +338,18 @@ function _convertDeltaToMessageChunk(delta: { | |
|
||
function _convertStructuredToolToMistralTool( | ||
tools: StructuredToolInterface[] | ||
): MistralAIToolInput[] { | ||
return tools.map((tool) => convertToOpenAITool(tool) as MistralAIToolInput); | ||
): MistralAITool[] { | ||
return tools.map((tool) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New |
||
const description = tool.description ?? `Tool: ${tool.name}`; | ||
return { | ||
type: "function", | ||
function: { | ||
name: tool.name, | ||
description, | ||
parameters: zodToJsonSchema(tool.schema), | ||
}, | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -439,17 +437,27 @@ export class ChatMistralAI< | |
*/ | ||
invocationParams( | ||
options?: this["ParsedCallOptions"] | ||
): Omit<MistralAIChatCompletionOptions, "messages"> { | ||
): Omit<ChatRequest, "messages"> { | ||
const { response_format, tools, tool_choice } = options ?? {}; | ||
const mistralAITools = tools | ||
const mistralAITools: Array<MistralAITool> | undefined = tools | ||
?.map((tool) => { | ||
if ("lc_namespace" in tool) { | ||
return _convertStructuredToolToMistralTool([tool]); | ||
} | ||
return tool; | ||
if (!tool.function.description) { | ||
return { | ||
type: "function", | ||
function: { | ||
name: tool.function.name, | ||
description: `Tool: ${tool.function.name}`, | ||
parameters: tool.function.parameters, | ||
}, | ||
} as MistralAITool; | ||
} | ||
return tool as MistralAITool; | ||
}) | ||
.flat(); | ||
const params: Omit<MistralAIChatCompletionOptions, "messages"> = { | ||
const params: Omit<ChatRequest, "messages"> = { | ||
model: this.model, | ||
tools: mistralAITools, | ||
temperature: this.temperature, | ||
|
@@ -484,21 +492,21 @@ export class ChatMistralAI< | |
|
||
/** | ||
* Calls the MistralAI API with retry logic in case of failures. | ||
* @param {MistralAIChatCompletionOptions} input The input to send to the MistralAI API. | ||
* @param {ChatRequest} input The input to send to the MistralAI API. | ||
* @returns {Promise<MistralAIChatCompletionResult | AsyncGenerator<MistralAIChatCompletionResult>>} The response from the MistralAI API. | ||
*/ | ||
async completionWithRetry( | ||
input: MistralAIChatCompletionOptions, | ||
input: ChatRequest, | ||
streaming: true | ||
): Promise<AsyncGenerator<ChatCompletionResponseChunk>>; | ||
|
||
async completionWithRetry( | ||
input: MistralAIChatCompletionOptions, | ||
input: ChatRequest, | ||
streaming: false | ||
): Promise<ChatCompletionResponse>; | ||
|
||
async completionWithRetry( | ||
input: MistralAIChatCompletionOptions, | ||
input: ChatRequest, | ||
streaming: boolean | ||
): Promise< | ||
ChatCompletionResponse | AsyncGenerator<ChatCompletionResponseChunk> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./chat_models.js"; | ||
export * from "./embeddings.js"; | ||
export * from "./llms.js"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to include the old type for back-compat