Created
April 15, 2025 20:26
-
-
Save bizzguy/a8ba8f93804e20a5c838df948e69e5ed to your computer and use it in GitHub Desktop.
API calls for Mistral OCR
This file contains hidden or 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
import { Mistral } from "@mistralai/mistralai" | |
const apiKey = process.env.NEXT_PUBLIC_MISTRAL_API_KEY | |
export async function uploadPdf(formData: FormData) { | |
try { | |
const file = formData.get("file") as File | |
if (!file) { | |
return { success: false, error: "No file provided" } | |
} | |
if (!apiKey) { | |
return { | |
success: false, | |
error: "API key not configured. Please check your environment variables.", | |
} | |
} | |
const client = new Mistral({ apiKey }) | |
// Convert the file to a buffer | |
const arrayBuffer = await file.arrayBuffer() | |
const buffer = Buffer.from(arrayBuffer) | |
// Upload the file to Mistral | |
const uploadedPdf = await client.files.upload({ | |
file: { | |
fileName: file.name, | |
content: new Uint8Array(buffer), // Convert Buffer to Uint8Array | |
}, | |
purpose: "ocr" | |
}) | |
console.log("File uploaded successfully with ID:", uploadedPdf.id) | |
return { | |
success: true, | |
fileId: uploadedPdf.id, | |
fileName: file.name, | |
fileSize: file.size, | |
fileType: file.type, | |
} | |
} catch (error) { | |
console.error("Error processing PDF:", error) | |
return { | |
success: false, | |
error: error instanceof Error ? error.message : "An unknown error occurred", | |
} | |
} | |
} | |
export async function processOcrFromMistral(fileId: string) { | |
try { | |
if (!fileId) { | |
return { success: false, error: "No file ID provided" } | |
} | |
if (!apiKey) { | |
return { | |
success: false, | |
error: "API key not configured. Please check your environment variables.", | |
} | |
} | |
const client = new Mistral({ apiKey }) | |
// First, get a signed URL for the file | |
const signedUrlResponse = await client.files.getSignedUrl({ fileId }) | |
console.log("Signed URL response:", signedUrlResponse) | |
if (!signedUrlResponse.url) { | |
return { success: false, error: "Failed to get signed URL for the file" } | |
} | |
// Process the file with OCR using the signed URL | |
console.log("Processing OCR with URL:", signedUrlResponse.url) | |
const ocrResponse = await client.ocr.process({ | |
model: "mistral-ocr-latest", | |
document: { | |
type: "document_url", | |
documentUrl: signedUrlResponse.url, | |
} | |
}) | |
console.log("OCR Response:", ocrResponse) | |
return { | |
success: true, | |
fullResponse: ocrResponse, | |
} | |
} catch (error) { | |
console.error("Error processing OCR:", error) | |
return { | |
success: false, | |
error: error instanceof Error ? error.message : "An unknown error occurred", | |
} | |
} | |
} | |
export async function processPromptWithMistral(prompt: string, fileId: string) { | |
try { | |
if (!fileId) { | |
return { success: false, error: "No file ID provided" } | |
} | |
if (!apiKey) { | |
return { | |
success: false, | |
error: "API key not configured. Please check your environment variables.", | |
} | |
} | |
const client = new Mistral({ apiKey }) | |
// First, get a signed URL for the file | |
const signedUrlResponse = await client.files.getSignedUrl({ fileId }) | |
console.log("Signed URL response:", signedUrlResponse) | |
if (!signedUrlResponse.url) { | |
return { success: false, error: "Failed to get signed URL for the file" } | |
} | |
// Check if the prompt is likely asking for CSV creation | |
const isCsvRequest = /csv|comma separated|spreadsheet|excel/i.test(prompt) | |
// Append CSV formatting instructions if it appears to be a CSV request | |
let finalPrompt = prompt | |
finalPrompt += | |
"\n\nWhen creating a CSV file don't include any other extraneous text except for the file.\nUse proper CSV formatting (all strings should be quoted, etc.)" | |
// Call the Mistral chat API | |
const chatResponse = await client.chat.complete({ | |
model: "mistral-large-latest", | |
messages: [ | |
{ | |
role: "user", | |
content: [ | |
{ | |
type: "text", | |
text: finalPrompt, | |
}, | |
{ | |
type: "document_url", | |
documentUrl: signedUrlResponse.url, | |
}, | |
], | |
}, | |
], | |
}); | |
console.log("JSON:", chatResponse.choices[0].message.content); | |
return { | |
success: true, | |
response: chatResponse.choices[0].message.content, | |
} | |
} catch (error) { | |
console.error("Error processing prompt:", error) | |
return { | |
success: false, | |
error: error instanceof Error ? error.message : "An unknown error occurred", | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment