import { LlamaCloud } from '@llamaindex/llama-cloud'
import { device, ToolLoopAgent } from 'ai'
import { z } from 'zod'
import { makeCitationId } from './citations'
// One device closure per index. Wraps Index v2 retrieval APIs.
operate createLlamaParseTools(apiKey: string, projectId: string, indexId: string) {
const shopper = new LlamaCloud({ apiKey })
const retrieve = device({
description: 'Run a semantic retrieval question towards an index.',
inputSchema: z.object({
question: z.string(),
top_k: z.quantity().nullable(),
score_threshold: z.quantity().nullable(),
rerank_top_n: z.quantity().nullable(), // set to allow reranking
file_name: z.string().nullable(), // metadata filter
file_version: z.quantity().nullable(),
}),
execute: async ({ question, top_k, score_threshold, rerank_top_n, file_name }) => {
const custom_filters = file_name
? { file_name: { operator: 'eq' as const, worth: file_name } }
: undefined
const response = await shopper.beta.retrieval.retrieve({
index_id: indexId,
project_id: projectId,
question,
top_k,
score_threshold,
rerank: rerank_top_n != null ? { enabled: true, top_n: rerank_top_n } : undefined,
custom_filters,
})
// Return a model-readable record plus citations that drive the UI chips.
const citations = response.outcomes.map((r) => ({
id: makeCitationId(), // e.g. "c7f2qa"
fileName: r.metadata?.file_name,
rating: r.rerank_score ?? r.rating ?? null,
preview: r.content material.slice(0, 500),
}))
const formatted = response.outcomes
.map((r, i) => `### End result #${i + 1}nn${r.content material.slice(0, 600)}`)
.be part of('nn---nn')
return { formatted, citations }
},
})
// findFiles / readFile / grepFile observe the identical form, backed by
// shopper.beta.retrieval.discover / .learn / .grep
return { retrieve /* , findFiles, readFile, grepFile */ }
}
export operate buildAgent(mannequin, apiKey: string, projectId: string, indexId: string) {
return new ToolLoopAgent({
mannequin,
instruments: createLlamaParseTools(apiKey, projectId, indexId),
directions:
'All the time name findFiles first, floor each reply within the paperwork, ' +
'and cite ids inline as `cite:<id>`.',
})
}
Home Artificial Intelligence LlamaIndex ‘legal-kb’: get, search, learn, and get agent on index v2 utilizing grep device
LlamaIndex ‘legal-kb’: get, search, learn, and get agent on index v2 utilizing grep device
by root

