-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mistralai[patch]: Translate tool call ids to mistral compat format (#…
…6217) * mistralai[patch]: Translate tool call ids to mistral compat format * chore: lint files * chore: lint files * nits * properly handle * Update libs/langchain-mistralai/src/chat_models.ts
- Loading branch information
1 parent
9ac2f20
commit 00641e7
Showing
4 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
_isValidMistralToolCallId, | ||
_convertToolCallIdToMistralCompatible, | ||
} from "../utils.js"; | ||
|
||
describe("Mistral Tool Call ID Conversion", () => { | ||
test("valid and invalid Mistral tool call IDs", () => { | ||
expect(_isValidMistralToolCallId("ssAbar4Dr")).toBe(true); | ||
expect(_isValidMistralToolCallId("abc123")).toBe(false); | ||
expect(_isValidMistralToolCallId("call_JIIjI55tTipFFzpcP8re3BpM")).toBe( | ||
false | ||
); | ||
}); | ||
|
||
test("tool call ID conversion", () => { | ||
const resultMap: Record<string, string> = { | ||
ssAbar4Dr: "ssAbar4Dr", | ||
abc123: "0001yoN1K", | ||
call_JIIjI55tTipFFzpcP8re3BpM: "0001sqrj5", | ||
12345: "00003akVR", | ||
}; | ||
|
||
for (const [inputId, expectedOutput] of Object.entries(resultMap)) { | ||
const convertedId = _convertToolCallIdToMistralCompatible(inputId); | ||
expect(convertedId).toBe(expectedOutput); | ||
expect(_isValidMistralToolCallId(convertedId)).toBe(true); | ||
} | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Mistral enforces a specific pattern for tool call IDs | ||
const TOOL_CALL_ID_PATTERN = /^[a-zA-Z0-9]{9}$/; | ||
|
||
export function _isValidMistralToolCallId(toolCallId: string): boolean { | ||
return TOOL_CALL_ID_PATTERN.test(toolCallId); | ||
} | ||
|
||
function _base62Encode(num: number): string { | ||
let numCopy = num; | ||
const base62 = | ||
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
if (numCopy === 0) return base62[0]; | ||
const arr: string[] = []; | ||
const base = base62.length; | ||
while (numCopy) { | ||
arr.push(base62[numCopy % base]); | ||
numCopy = Math.floor(numCopy / base); | ||
} | ||
return arr.reverse().join(""); | ||
} | ||
|
||
function _simpleHash(str: string): number { | ||
let hash = 0; | ||
for (let i = 0; i < str.length; i += 1) { | ||
const char = str.charCodeAt(i); | ||
hash = (hash << 5) - hash + char; | ||
hash &= hash; // Convert to 32-bit integer | ||
} | ||
return Math.abs(hash); | ||
} | ||
|
||
export function _convertToolCallIdToMistralCompatible( | ||
toolCallId: string | ||
): string { | ||
if (_isValidMistralToolCallId(toolCallId)) { | ||
return toolCallId; | ||
} else { | ||
const hash = _simpleHash(toolCallId); | ||
const base62Str = _base62Encode(hash); | ||
if (base62Str.length >= 9) { | ||
return base62Str.slice(0, 9); | ||
} else { | ||
return base62Str.padStart(9, "0"); | ||
} | ||
} | ||
} |