RunnableFunction.d.mts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { type ChatCompletionRunner } from "./ChatCompletionRunner.mjs";
  2. import { type ChatCompletionStreamingRunner } from "./ChatCompletionStreamingRunner.mjs";
  3. import { JSONSchema } from "./jsonschema.mjs";
  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. export declare class ParsingToolFunction<Args extends object> {
  78. type: 'function';
  79. function: RunnableFunctionWithParse<Args>;
  80. constructor(input: RunnableFunctionWithParse<Args>);
  81. }
  82. export {};
  83. //# sourceMappingURL=RunnableFunction.d.mts.map