Skip to content

Commit

Permalink
fix(tools): add proper wikipedia result types (#267)
Browse files Browse the repository at this point in the history
Signed-off-by: Radek Ježek <[email protected]>
  • Loading branch information
jezekra1 authored Jan 7, 2025
1 parent c989e27 commit 873b38d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/agents/granite/granite_wiki_bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function wikipediaRetrivalTool(passageSize: number, overlap: number, maxResults:
query: input.query,
documents: output.results.flatMap((document) =>
Array.from(
splitString(document.fields.markdown as string, {
splitString(document.fields.markdown ?? "", {
size: passageSize * charsPerToken,
overlap: overlap * charsPerToken,
}),
Expand Down
2 changes: 1 addition & 1 deletion examples/tools/custom/piping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const wikipediaWithSimilarity = wikipedia
.pipe(similarity, (input, output) => ({
query: input.query,
documents: output.results.flatMap((document) =>
Array.from(splitString(document.fields.markdown as string, { size: 1000, overlap: 50 })).map(
Array.from(splitString(document.fields.markdown ?? "", { size: 1000, overlap: 50 })).map(
(chunk) => ({
text: chunk,
source: document,
Expand Down
2 changes: 1 addition & 1 deletion src/internals/helpers/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export async function* emitterToGenerator<T, R>(fn: EmitterToGeneratorFn<T, R>)

export async function asyncProperties<T extends NonNullable<unknown>>(
obj: T,
): Promise<{ [K in keyof T]: T[K] extends Promise<infer P> ? P : T[K] }> {
): Promise<{ [K in keyof T]: Awaited<T[K]> }> {
return Object.fromEntries(
await Promise.all(Object.entries(obj).map(async ([key, value]) => [key, await value])),
);
Expand Down
14 changes: 9 additions & 5 deletions src/tools/search/wikipedia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

import type { Page, pageFunctions, searchOptions } from "wikipedia";
import wiki from "wikipedia";
import { Cache } from "@/cache/decoratorCache.js";
import * as R from "remeda";
import type { Page, pageFunctions, searchOptions } from "wikipedia";
import { keys, mapValues } from "remeda";
import { ArrayKeys, Common } from "@/internals/types.js";
import {
SearchToolOptions,
Expand All @@ -27,11 +28,10 @@ import {
} from "./base.js";
import { asyncProperties } from "@/internals/helpers/promise.js";
import { z } from "zod";
import { ToolEmitter, Tool, ToolInput } from "@/tools/base.js";
import { Tool, ToolEmitter, ToolInput } from "@/tools/base.js";
import Turndown from "turndown";
// @ts-expect-error missing types
import turndownPlugin from "joplin-turndown-plugin-gfm";
import { keys, mapValues } from "remeda";
import stringComparison from "string-comparison";
import { pageResult } from "wikipedia/dist/resultTypes.js";
import { Emitter } from "@/emitter/emitter.js";
Expand Down Expand Up @@ -80,8 +80,12 @@ export interface WikipediaToolRunOptions extends SearchToolRunOptions {
output?: OutputOptions;
}

type PageWithMarkdown = Page & { markdown: () => Promise<string> };

type ResultFields = { [K in keyof PageFunctions]: Awaited<ReturnType<PageWithMarkdown[K]>> };

export interface WikipediaToolResult extends SearchToolResult {
fields: Partial<Record<keyof PageFunctions, unknown>>;
fields: Partial<ResultFields>;
}

export class WikipediaToolOutput extends SearchToolOutput<WikipediaToolResult> {
Expand Down Expand Up @@ -382,7 +386,7 @@ export class WikipediaTool extends Tool<
mapValues(runOptions?.extraction?.fields ?? {}, (value, key) =>
this._mappers[key](page, runOptions)
.then((response) => (value.transform ? value.transform(response) : response))
.catch(() => null),
.catch(() => undefined),
),
),
});
Expand Down

0 comments on commit 873b38d

Please sign in to comment.