Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@valyu/ai-sdk",
"version": "1.0.7",
"version": "1.1.0",
"description": "Valyu search tools for Vercel AI SDK",
"files": [
"dist"
Expand Down
151 changes: 151 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions src/paper-search.ts → src/academic-search.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { tool } from "ai";
import { z } from "zod";
import type { ValyuPaperSearchConfig } from "./types.js";
import type { ValyuAcademicSearchConfig } from "./types.js";

/**
* Creates a research paper search tool powered by Valyu for use with Vercel AI SDK
* Creates an academic paper search tool powered by Valyu for use with Vercel AI SDK
*
* @param config - Configuration options for the Valyu paper search
* @param config - Configuration options for the Valyu academic search
* @returns A tool that can be used with AI SDK's generateText, streamText, etc.
*
* @example
* ```ts
* import { generateText } from "ai";
* import { paperSearch } from "@valyu/ai-sdk";
* import { academicSearch } from "@valyu/ai-sdk";
* import { openai } from "@ai-sdk/openai";
*
* const { text } = await generateText({
* model: openai('gpt-5'),
* prompt: 'Find recent research papers on transformer architectures',
* tools: {
* paperSearch: paperSearch({ maxNumResults: 5 }),
* academicSearch: academicSearch({ maxNumResults: 5 }),
* },
* });
* ```
*/
export function paperSearch(config: ValyuPaperSearchConfig = {}) {
export function academicSearch(config: ValyuAcademicSearchConfig = {}) {
const {
apiKey = process.env.VALYU_API_KEY,
searchType = "proprietary",
Expand All @@ -38,24 +38,22 @@ export function paperSearch(config: ValyuPaperSearchConfig = {}) {
} = config;

return tool({
description: "Search academic papers from arXiv, PubMed, bioRxiv, and medRxiv. The API handles semantic search - use simple natural language, not keyword stuffing.",
description: "Search academic papers and preprints from arXiv, PubMed, bioRxiv, and medRxiv. Use for research papers across all scientific disciplines.",
inputSchema: z.object({
query: z.string().min(1).max(500).describe("Natural language query (e.g., 'psilocybin effects on lifespan in mice', 'CRISPR cancer therapy trials')"),
query: z.string().min(1).max(500).describe("Natural language query (e.g., 'transformer architectures for protein folding', 'CRISPR gene editing mechanisms')"),
}),
execute: async ({ query }) => {
if (!apiKey) {
throw new Error("VALYU_API_KEY is required. Set it in environment variables or pass it in config.");
}

// Build the request body for Valyu API
const requestBody: any = {
query,
search_type: searchType,
max_num_results: maxNumResults,
included_sources: includedSources,
};

// Add optional parameters
if (otherOptions.maxPrice !== undefined) {
requestBody.max_price = otherOptions.maxPrice;
}
Expand All @@ -69,7 +67,6 @@ export function paperSearch(config: ValyuPaperSearchConfig = {}) {
requestBody.response_length = otherOptions.responseLength;
}

// Call Valyu API
try {
const response = await fetch("https://api.valyu.ai/v1/deepsearch", {
method: "POST",
Expand All @@ -85,12 +82,11 @@ export function paperSearch(config: ValyuPaperSearchConfig = {}) {
throw new Error(`Valyu API error: ${response.status} - ${errorText}`);
}

// Return the full API response
const data = await response.json();
return data;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to search research papers with Valyu: ${error.message}`);
throw new Error(`Failed to search academic papers with Valyu: ${error.message}`);
}
throw error;
}
Expand Down
74 changes: 74 additions & 0 deletions src/contents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { tool } from "ai";
import { z } from "zod";
import type { ValyuContentsConfig } from "./types.js";

/**
* Creates a content extraction tool powered by Valyu for use with Vercel AI SDK
*
* @param config - Configuration options for the Valyu contents extraction
* @returns A tool that can be used with AI SDK's generateText, streamText, etc.
*
* @example
* ```ts
* import { generateText } from "ai";
* import { contents } from "@valyu/ai-sdk";
* import { openai } from "@ai-sdk/openai";
*
* const { text } = await generateText({
* model: openai('gpt-5'),
* prompt: 'Extract and summarize this article: https://example.com/article',
* tools: {
* contents: contents(),
* },
* });
* ```
*/
export function contents(config: ValyuContentsConfig = {}) {
const {
apiKey = process.env.VALYU_API_KEY,
responseLength = "max",
extractEffort = "auto",
} = config;

return tool({
description: "Extract clean, structured content from URLs. Use this to read and process web pages, articles, and documents.",
inputSchema: z.object({
urls: z.array(z.string().url()).min(1).max(10).describe("Array of URLs to extract content from (1-10 URLs)"),
}),
execute: async ({ urls }) => {
if (!apiKey) {
throw new Error("VALYU_API_KEY is required. Set it in environment variables or pass it in config.");
}

const requestBody: any = {
urls,
response_length: responseLength,
extract_effort: extractEffort,
};

try {
const response = await fetch("https://api.valyu.ai/v1/contents", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
},
body: JSON.stringify(requestBody),
});

if (!response.ok) {
const errorText = await response.text();
throw new Error(`Valyu API error: ${response.status} - ${errorText}`);
}

const data = await response.json();
return data;
} catch (error) {
if (error instanceof Error) {
throw new Error(`Failed to extract content with Valyu: ${error.message}`);
}
throw error;
}
},
});
}
5 changes: 1 addition & 4 deletions src/finance-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ export function financeSearch(config: ValyuFinanceSearchConfig = {}) {
"valyu/valyu-market-movers-US",
"valyu/valyu-crypto",
"valyu/valyu-forex",
"valyu/valyu-bls",
"valyu/valyu-fred",
"valyu/valyu-world-bank",
],
...otherOptions
} = config;

return tool({
description: "Search financial data: stock prices, earnings, balance sheets, income statements, cash flows, SEC filings, dividends, insider transactions, crypto, forex, and economic indicators. The API handles natural language - ask your full question in one query per topic.",
description: "Search financial data: stock prices, earnings, balance sheets, income statements, cash flows, SEC filings, dividends, insider transactions, crypto, and forex. Use economicsSearch for BLS, FRED, and World Bank data.",
inputSchema: z.object({
query: z.string().min(1).max(500).describe("Natural language query (e.g., 'Apple stock price Q1-Q3 2020', 'Tesla revenue last 4 quarters')"),
}),
Expand Down
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
// Export all search tools
export { search } from "./search.js";
export { webSearch } from "./web-search.js";
export { financeSearch } from "./finance-search.js";
export { paperSearch } from "./paper-search.js";
export { bioSearch } from "./bio-search.js";
export { academicSearch } from "./academic-search.js";
export { lifeSciencesSearch } from "./life-sciences-search.js";
export { patentSearch } from "./patent-search.js";
export { secSearch } from "./sec-search.js";
export { economicsSearch } from "./economics-search.js";
export { companyResearch } from "./company-research.js";
export { contents } from "./contents.js";

// Backwards-compatible aliases (deprecated)
export { lifeSciencesSearch as bioSearch } from "./life-sciences-search.js";
export { academicSearch as paperSearch } from "./academic-search.js";

// Export datasources discovery tools
export { datasources, datasourcesCategories } from "./datasources.js";

// Export all types
export type {
ValyuBaseConfig,
ValyuSearchConfig,
ValyuWebSearchConfig,
ValyuFinanceSearchConfig,
ValyuPaperSearchConfig,
ValyuBioSearchConfig,
ValyuAcademicSearchConfig,
ValyuLifeSciencesSearchConfig,
ValyuPatentSearchConfig,
ValyuSecSearchConfig,
ValyuEconomicsSearchConfig,
ValyuCompanyResearchConfig,
ValyuContentsConfig,
ValyuSearchResult,
ValyuApiResponse,
ValyuResultsBySource,
ValyuSourceType,
ValyuDataType,
ValyuResponseLength,
ValyuResponseLengthPreset,
// Backwards-compatible type aliases (deprecated)
ValyuLifeSciencesSearchConfig as ValyuBioSearchConfig,
ValyuAcademicSearchConfig as ValyuPaperSearchConfig,
} from "./types.js";
export type { ValyuDatasourcesConfig } from "./datasources.js";
Loading