-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
93 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/core/src/apis/google/mapGeminiResponseToToolInvocations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { ToolDescriptor, ToolParam, ToolParameterTypes } from "@typeDefs"; | ||
|
||
export type ToolParamMap = { | ||
[key: string]: { | ||
description: string; | ||
type: ToolParameterTypes; | ||
required: boolean; | ||
}; | ||
}; | ||
|
||
type ExtractArgumentType<T extends ToolParameterTypes> = T extends "STR" | ||
? string | ||
: T extends "NUM" | ||
? number | ||
: T extends "BOOL" | ||
? boolean | ||
: never; | ||
|
||
export type ConvertParamMapToArgs<TParamMap extends ToolParamMap> = { | ||
[K in keyof TParamMap as TParamMap[K]["required"] extends true | ||
? K | ||
: never]: ExtractArgumentType<TParamMap[K]["type"]>; | ||
} & { | ||
[K in keyof TParamMap as TParamMap[K]["required"] extends false | undefined | ||
? K | ||
: never]?: ExtractArgumentType<TParamMap[K]["type"]>; | ||
}; | ||
|
||
export class Tool<TParamMap extends ToolParamMap, TReturns = unknown> { | ||
private invokeFn: (args: ConvertParamMapToArgs<TParamMap>) => TReturns; | ||
|
||
public descriptor: ToolDescriptor; | ||
|
||
constructor( | ||
name: string, | ||
description: string, | ||
paramMap: TParamMap, | ||
invokeFn: (args: ConvertParamMapToArgs<TParamMap>) => TReturns, | ||
) { | ||
this.invokeFn = invokeFn; | ||
this.descriptor = { | ||
name, | ||
description, | ||
parameters: this.createParameters(paramMap), | ||
invocations: [], | ||
}; | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
private createParameters(paramMap: TParamMap): ToolParam[] { | ||
return Object.entries(paramMap).map(([name, paramInfo]) => ({ | ||
name, | ||
description: paramInfo.description, | ||
type: paramInfo.type, | ||
required: paramInfo.required, | ||
})); | ||
} | ||
|
||
public invoke(args: ConvertParamMapToArgs<TParamMap>): TReturns { | ||
const returned = this.invokeFn(args); | ||
|
||
this.descriptor.invocations.push({ | ||
arguments: args, | ||
returned, | ||
}); | ||
|
||
return returned; | ||
} | ||
} | ||
|
||
export const a = new Tool( | ||
"get_current_weather", | ||
"Get the current weather for a given location", | ||
{ | ||
city: { | ||
description: "The city name", | ||
type: "STR", | ||
required: true, | ||
}, | ||
state: { | ||
description: "The state name", | ||
type: "STR", | ||
required: true, | ||
}, | ||
zipcode: { | ||
description: "An optional zipcode", | ||
type: "NUM", | ||
required: false, | ||
}, | ||
}, | ||
// should work: | ||
({ city, state, zipcode }) => { | ||
console.log("Invoking get_current_weather tool...", { | ||
city, | ||
state, | ||
zipcode, | ||
}); | ||
return { | ||
temperature: "70", | ||
}; | ||
}, | ||
); | ||
|
||
a.invoke({ city: "San Francisco", state: "CA" }); | ||
a.invoke({ city: "San Francisco", state: "CA", zipcode: 94105 }); | ||
|
||
// bad: | ||
// a.invoke({ city: "San Francisco" }); | ||
// a.invoke({ city: "San Francisco", state: "CA", zipcode: 94105, xxx: "yyy" }); | ||
// a.invoke({ city: "San Francisco", state: 123 }); | ||
|
||
// export const x1 = new Tool( | ||
// "get_current_weather", | ||
// "Get the current weather for a given location", | ||
// { | ||
// city: { | ||
// description: "The city name", | ||
// type: "STR", | ||
// required: true, | ||
// }, | ||
// state: { | ||
// description: "The state name", | ||
// type: "STR", | ||
// required: true, | ||
// }, | ||
// zipcode: { | ||
// description: "An optional zipcode", | ||
// type: "NUM", | ||
// required: false, | ||
// }, | ||
// }, | ||
// // should break, prop not defined: | ||
// ({ city, state, zipcode, xxx }) => { | ||
// console.log("Invoking get_current_weather tool...", { | ||
// city, | ||
// state, | ||
// zipcode, | ||
// xxx, | ||
// }); | ||
// return { | ||
// temperature: "70", | ||
// }; | ||
// }, | ||
// ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./Template"; | ||
export * from "./Tool"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters