RunnableFunction.d.ts 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { type ChatCompletionRunner } from "./ChatCompletionRunner.js";
  2. import { type ChatCompletionStreamingRunner } from "./ChatCompletionStreamingRunner.js";
  3. import { JSONSchema } from "./jsonschema.js";
  4. type PromiseOrValue<T> = T | Promise<T>;
  5. export type RunnableFunctionWithParse<Args extends object> = {
  6. /**
  7. * @param args the return value from `parse`.
  8. * @param runner the runner evaluating this callback.
  9. * @returns a string to send back to OpenAI.
  10. */
  11. function: (args: Args, runner: ChatCompletionRunner<unknown> | ChatCompletionStreamingRunner<unknown>) => PromiseOrValue<unknown>;
  12. /**
  13. * @param input the raw args from the OpenAI function call.
  14. * @returns the parsed arguments to pass to `function`
  15. */
  16. parse: (input: string) => PromiseOrValue<Args>;
  17. /**
  18. * The parameters the function accepts, describes as a JSON Schema object.
  19. */
  20. parameters: JSONSchema;
  21. /**
  22. * A description of what the function does, used by the model to choose when and how to call the function.
  23. */
  24. description: string;
  25. /**
  26. * The name of the function to be called. Will default to function.name if omitted.
  27. */
  28. name?: string | undefined;
  29. strict?: boolean | undefined;
  30. };
  31. export type RunnableFunctionWithoutParse = {
  32. /**
  33. * @param args the raw args from the OpenAI function call.
  34. * @returns a string to send back to OpenAI
  35. */
  36. function: (args: string, runner: ChatCompletionRunner<unknown> | ChatCompletionStreamingRunner<unknown>) => PromiseOrValue<unknown>;
  37. /**
  38. * The parameters the function accepts, describes as a JSON Schema object.
  39. */
  40. parameters: JSONSchema;
  41. /**
  42. * A description of what the function does, used by the model to choose when and how to call the function.
  43. */
  44. description: string;
  45. /**
  46. * The name of the function to be called. Will default to function.name if omitted.
  47. */
  48. name?: string | undefined;
  49. strict?: boolean | undefined;
  50. };
  51. export type RunnableFunction<Args extends object | string> = Args extends string ? RunnableFunctionWithoutParse : Args extends object ? RunnableFunctionWithParse<Args> : never;
  52. export type RunnableToolFunction<Args extends object | string> = Args extends string ? RunnableToolFunctionWithoutParse : Args extends object ? RunnableToolFunctionWithParse<Args> : never;
  53. export type RunnableToolFunctionWithoutParse = {
  54. type: 'function';
  55. function: RunnableFunctionWithoutParse;
  56. };
  57. export type RunnableToolFunctionWithParse<Args extends object> = {
  58. type: 'function';
  59. function: RunnableFunctionWithParse<Args>;
  60. };
  61. export declare function isRunnableFunctionWithParse<Args extends object>(fn: any): fn is RunnableFunctionWithParse<Args>;
  62. export type BaseFunctionsArgs = readonly (object | string)[];
  63. export type RunnableFunctions<FunctionsArgs extends BaseFunctionsArgs> = [
  64. any[]
  65. ] extends [FunctionsArgs] ? readonly RunnableFunction<any>[] : {
  66. [Index in keyof FunctionsArgs]: Index extends number ? RunnableFunction<FunctionsArgs[Index]> : FunctionsArgs[Index];
  67. };
  68. export type RunnableTools<FunctionsArgs extends BaseFunctionsArgs> = [
  69. any[]
  70. ] extends [FunctionsArgs] ? readonly RunnableToolFunction<any>[] : {
  71. [Index in keyof FunctionsArgs]: Index extends number ? RunnableToolFunction<FunctionsArgs[Index]> : FunctionsArgs[Index];
  72. };
  73. /**
  74. * This is helper class for passing a `function` and `parse` where the `function`
  75. * argument type matches the `parse` return type.
  76. *
  77. * @deprecated - please use ParsingToolFunction instead.
  78. */
  79. export declare class ParsingFunction<Args extends object> {
  80. function: RunnableFunctionWithParse<Args>['function'];
  81. parse: RunnableFunctionWithParse<Args>['parse'];
  82. parameters: RunnableFunctionWithParse<Args>['parameters'];
  83. description: RunnableFunctionWithParse<Args>['description'];
  84. name?: RunnableFunctionWithParse<Args>['name'];
  85. constructor(input: RunnableFunctionWithParse<Args>);
  86. }
  87. /**
  88. * This is helper class for passing a `function` and `parse` where the `function`
  89. * argument type matches the `parse` return type.
  90. */
  91. export declare class ParsingToolFunction<Args extends object> {
  92. type: 'function';
  93. function: RunnableFunctionWithParse<Args>;
  94. constructor(input: RunnableFunctionWithParse<Args>);
  95. }
  96. export {};
  97. //# sourceMappingURL=RunnableFunction.d.ts.map