Spaces:
Running
Running
File size: 3,761 Bytes
c2b7eb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | //#region src/tools/fileSearch.ts
/**
* Converts ranking options to the API format.
*/
function convertRankingOptions(options) {
if (!options) return void 0;
return {
ranker: options.ranker,
score_threshold: options.scoreThreshold,
hybrid_search: options.hybridSearch ? {
embedding_weight: options.hybridSearch.embeddingWeight,
text_weight: options.hybridSearch.textWeight
} : void 0
};
}
/**
* Creates a File Search tool that allows models to search your files
* for relevant information using semantic and keyword search.
*
* File Search enables models to retrieve information from a knowledge base
* of previously uploaded files stored in vector stores. This is a hosted tool
* managed by OpenAI - you don't need to implement the search execution yourself.
*
* **Prerequisites**: Before using File Search, you must:
* 1. Upload files to the File API with `purpose: "assistants"`
* 2. Create a vector store
* 3. Add files to the vector store
*
* @see {@link https://platform.openai.com/docs/guides/tools-file-search | OpenAI File Search Documentation}
*
* @param options - Configuration options for the File Search tool
* @returns A File Search tool definition to be passed to the OpenAI Responses API
*
* @example
* ```typescript
* import { ChatOpenAI, tools } from "@langchain/openai";
*
* const model = new ChatOpenAI({ model: "gpt-4.1" });
*
* // Basic usage with a vector store
* const response = await model.invoke(
* "What is deep research by OpenAI?",
* {
* tools: [tools.fileSearch({
* vectorStoreIds: ["vs_abc123"]
* })]
* }
* );
*
* // Limit the number of results for lower latency
* const response = await model.invoke(
* "Find information about pricing",
* {
* tools: [tools.fileSearch({
* vectorStoreIds: ["vs_abc123"],
* maxNumResults: 5
* })]
* }
* );
*
* // With metadata filtering
* const response = await model.invoke(
* "Find recent blog posts about AI",
* {
* tools: [tools.fileSearch({
* vectorStoreIds: ["vs_abc123"],
* filters: {
* type: "eq",
* key: "category",
* value: "blog"
* }
* })]
* }
* );
*
* // With compound filters (AND/OR)
* const response = await model.invoke(
* "Find technical docs from 2024",
* {
* tools: [tools.fileSearch({
* vectorStoreIds: ["vs_abc123"],
* filters: {
* type: "and",
* filters: [
* { type: "eq", key: "category", value: "technical" },
* { type: "gte", key: "year", value: 2024 }
* ]
* }
* })]
* }
* );
*
* // With ranking options for more relevant results
* const response = await model.invoke(
* "Find the most relevant information",
* {
* tools: [tools.fileSearch({
* vectorStoreIds: ["vs_abc123"],
* rankingOptions: {
* scoreThreshold: 0.8,
* ranker: "auto"
* }
* })]
* }
* );
*
* // Search multiple vector stores
* const response = await model.invoke(
* "Search across all knowledge bases",
* {
* tools: [tools.fileSearch({
* vectorStoreIds: ["vs_abc123", "vs_def456"]
* })]
* }
* );
* ```
*
* @remarks
* - Vector stores must be created and populated before using this tool
* - The tool returns file citations in the response annotations
* - Use `include: ["file_search_call.results"]` in the API call to get search results
* - Supported file types include PDF, DOCX, TXT, MD, and many code file formats
*/
function fileSearch(options) {
return {
type: "file_search",
vector_store_ids: options.vectorStoreIds,
max_num_results: options.maxNumResults,
filters: options.filters,
ranking_options: convertRankingOptions(options.rankingOptions)
};
}
//#endregion
export { fileSearch };
//# sourceMappingURL=fileSearch.js.map |