openai.d.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { OpenAI } from "openai";
  2. import type { APIPromise } from "openai/core";
  3. import type { RunTreeConfig } from "../index.js";
  4. type OpenAIType = {
  5. beta?: {
  6. chat?: {
  7. completions?: {
  8. parse?: (...args: any[]) => any;
  9. };
  10. };
  11. };
  12. chat: {
  13. completions: {
  14. create: (...args: any[]) => any;
  15. };
  16. };
  17. completions: {
  18. create: (...args: any[]) => any;
  19. };
  20. };
  21. type ExtraRunTreeConfig = Pick<Partial<RunTreeConfig>, "name" | "metadata" | "tags">;
  22. type PatchedOpenAIClient<T extends OpenAIType> = T & {
  23. chat: T["chat"] & {
  24. completions: T["chat"]["completions"] & {
  25. create: {
  26. (arg: OpenAI.ChatCompletionCreateParamsStreaming, arg2?: OpenAI.RequestOptions & {
  27. langsmithExtra?: ExtraRunTreeConfig;
  28. }): APIPromise<AsyncGenerator<OpenAI.ChatCompletionChunk>>;
  29. } & {
  30. (arg: OpenAI.ChatCompletionCreateParamsNonStreaming, arg2?: OpenAI.RequestOptions & {
  31. langsmithExtra?: ExtraRunTreeConfig;
  32. }): APIPromise<OpenAI.ChatCompletion>;
  33. };
  34. };
  35. };
  36. completions: T["completions"] & {
  37. create: {
  38. (arg: OpenAI.CompletionCreateParamsStreaming, arg2?: OpenAI.RequestOptions & {
  39. langsmithExtra?: ExtraRunTreeConfig;
  40. }): APIPromise<AsyncGenerator<OpenAI.Completion>>;
  41. } & {
  42. (arg: OpenAI.CompletionCreateParamsNonStreaming, arg2?: OpenAI.RequestOptions & {
  43. langsmithExtra?: ExtraRunTreeConfig;
  44. }): APIPromise<OpenAI.Completion>;
  45. };
  46. };
  47. };
  48. /**
  49. * Wraps an OpenAI client's completion methods, enabling automatic LangSmith
  50. * tracing. Method signatures are unchanged, with the exception that you can pass
  51. * an additional and optional "langsmithExtra" field within the second parameter.
  52. * @param openai An OpenAI client instance.
  53. * @param options LangSmith options.
  54. * @example
  55. * ```ts
  56. * import { OpenAI } from "openai";
  57. * import { wrapOpenAI } from "langsmith/wrappers/openai";
  58. *
  59. * const patchedClient = wrapOpenAI(new OpenAI());
  60. *
  61. * const patchedStream = await patchedClient.chat.completions.create(
  62. * {
  63. * messages: [{ role: "user", content: `Say 'foo'` }],
  64. * model: "gpt-4.1-mini",
  65. * stream: true,
  66. * },
  67. * {
  68. * langsmithExtra: {
  69. * metadata: {
  70. * additional_data: "bar",
  71. * },
  72. * },
  73. * },
  74. * );
  75. * ```
  76. */
  77. export declare const wrapOpenAI: <T extends OpenAIType>(openai: T, options?: Partial<RunTreeConfig>) => PatchedOpenAIClient<T>;
  78. export {};