chore: import upstream snapshot with attribution
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
const DOCX_MIME_TYPE = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
||||
const DOCX_DATA_URI_PREFIX = `data:${DOCX_MIME_TYPE};base64,`;
|
||||
const DEFAULT_APP_BASE_URL = 'https://example-app.promptfoo.app';
|
||||
const DEFAULT_DOMAIN = 'general';
|
||||
const DEFAULT_LEVEL = 'minnow';
|
||||
const DEFAULT_SUMMARY_REQUEST = 'Please summarize the uploaded document in one concise paragraph.';
|
||||
|
||||
function getStringVar(vars, key) {
|
||||
return typeof vars[key] === 'string' && vars[key].trim() ? vars[key] : undefined;
|
||||
}
|
||||
|
||||
function normalizeBaseUrl(baseUrl) {
|
||||
return baseUrl.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
function decodeDocxDataUri(documentInput) {
|
||||
if (!documentInput.startsWith(DOCX_DATA_URI_PREFIX)) {
|
||||
throw new Error(
|
||||
`Expected vars.document to be a DOCX data URI starting with "${DOCX_DATA_URI_PREFIX}". Configure the target input as type: docx.`,
|
||||
);
|
||||
}
|
||||
|
||||
return Buffer.from(documentInput.slice(DOCX_DATA_URI_PREFIX.length), 'base64');
|
||||
}
|
||||
|
||||
class DocumentUploadProvider {
|
||||
constructor(options = {}) {
|
||||
this.providerId = 'example-app-document-upload-provider';
|
||||
this.config = options.config || {};
|
||||
}
|
||||
|
||||
id() {
|
||||
return this.providerId;
|
||||
}
|
||||
|
||||
async uploadDocument(documentInput) {
|
||||
const appBaseUrl = normalizeBaseUrl(this.config.appBaseUrl || DEFAULT_APP_BASE_URL);
|
||||
const docxBuffer = decodeDocxDataUri(documentInput);
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append(
|
||||
'document',
|
||||
new Blob([docxBuffer], { type: DOCX_MIME_TYPE }),
|
||||
`promptfoo-${randomUUID()}.docx`,
|
||||
);
|
||||
|
||||
const response = await fetch(`${appBaseUrl}/documents`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.text();
|
||||
throw new Error(`Document upload failed (${response.status}): ${errorBody}`);
|
||||
}
|
||||
|
||||
const uploadResult = await response.json();
|
||||
if (!uploadResult?.document_id) {
|
||||
throw new Error(
|
||||
`Document upload response did not include document_id: ${JSON.stringify(uploadResult)}`,
|
||||
);
|
||||
}
|
||||
|
||||
return uploadResult.document_id;
|
||||
}
|
||||
|
||||
async callApi(prompt, context) {
|
||||
const vars = context?.vars || {};
|
||||
const documentInput = getStringVar(vars, 'document');
|
||||
if (!documentInput) {
|
||||
throw new Error(
|
||||
'Missing vars.document: Expected a DOCX data URI. Configure the target input "document" with type: docx.',
|
||||
);
|
||||
}
|
||||
const userQuestion = getStringVar(vars, 'question') || DEFAULT_SUMMARY_REQUEST;
|
||||
const appBaseUrl = normalizeBaseUrl(this.config.appBaseUrl || DEFAULT_APP_BASE_URL);
|
||||
const level = this.config.level || DEFAULT_LEVEL;
|
||||
const domain = this.config.domain || DEFAULT_DOMAIN;
|
||||
const documentId = await this.uploadDocument(documentInput);
|
||||
const finalQuestion = [
|
||||
`Use summarize_document with document_id "${documentId}".`,
|
||||
`Then answer the user's request: ${userQuestion}`,
|
||||
].join(' ');
|
||||
|
||||
const response = await fetch(
|
||||
`${appBaseUrl}/${level}/chat?domain=${encodeURIComponent(domain)}&enable_tools=true`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: finalQuestion,
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const errorBody = await response.text();
|
||||
throw new Error(`Chat request failed (${response.status}): ${errorBody}`);
|
||||
}
|
||||
|
||||
const chatResult = await response.json();
|
||||
|
||||
return {
|
||||
output:
|
||||
typeof chatResult?.output === 'string' ? chatResult.output : JSON.stringify(chatResult),
|
||||
raw: JSON.stringify({
|
||||
document_id: documentId,
|
||||
chat_response: chatResult,
|
||||
}),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default DocumentUploadProvider;
|
||||
Reference in New Issue
Block a user