From 9f17d9873692075d713affd4990cd2bad81ef0e3 Mon Sep 17 00:00:00 2001 From: David Corbitt Date: Mon, 7 Aug 2023 17:12:09 -0700 Subject: [PATCH] Attempt to log (without api key) --- client-libs/typescript/openai/index.ts | 89 ++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 7 deletions(-) diff --git a/client-libs/typescript/openai/index.ts b/client-libs/typescript/openai/index.ts index 81df9f3..8490362 100644 --- a/client-libs/typescript/openai/index.ts +++ b/client-libs/typescript/openai/index.ts @@ -1,8 +1,14 @@ import * as openai from "openai-beta"; -import { readEnv } from "openai-beta/core"; +import { + readEnv, + type RequestOptions, +} from "openai-beta/core"; +import { + CompletionCreateParams, +} from "openai-beta/resources/chat/completions"; -// Anything we don't override we want to pass through to openai directly export * as openai from "openai-beta"; +import * as openPipeClient from "../codegen"; interface ClientOptions extends openai.ClientOptions { openPipeApiKey?: string; @@ -10,8 +16,7 @@ interface ClientOptions extends openai.ClientOptions { } export class OpenAI extends openai.OpenAI { - openPipeApiKey: string; - openPipeBaseUrl: string; + public openPipeApi?: openPipeClient.DefaultApi; constructor({ openPipeApiKey = readEnv("OPENPIPE_API_KEY"), @@ -19,13 +24,83 @@ export class OpenAI extends openai.OpenAI { `https://app.openpipe.ai/v1`, ...opts }: ClientOptions = {}) { + super({ ...opts }); + + if (openPipeApiKey) { + this.openPipeApi = new openPipeClient.DefaultApi(new openPipeClient.Configuration({ + apiKey: openPipeApiKey, + basePath: openPipeBaseUrl, + })); + } + + // Override the chat property + this.chat = new ExtendedChat(this); + if (openPipeApiKey === undefined) { console.error( "The OPENPIPE_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenPipe client with an openPipeApiKey option, like new OpenPipe({ openPipeApiKey: undefined })." ); } - super({ - ...opts, - }); + } +} + +class ExtendedChat extends openai.OpenAI.Chat { + completions: ExtendedCompletions; + + constructor(openaiInstance: OpenAI) { + super(openaiInstance); + // Initialize the new completions instance + this.completions = new ExtendedCompletions(openaiInstance); + } +} + +class ExtendedCompletions extends openai.OpenAI.Chat.Completions { + private openaiInstance: OpenAI; + + constructor(openaiInstance: OpenAI) { + super(openaiInstance); + this.openaiInstance = openaiInstance; + } + + async create( + params: + | CompletionCreateParams.CreateChatCompletionRequestNonStreaming + | CompletionCreateParams.CreateChatCompletionRequestStreaming, + options?: RequestOptions, + tags?: Record + ): Promise { + // Your pre API call logic here + console.log("Doing pre API call..."); + + // Determine the type of request + if (params.hasOwnProperty("stream") && params.stream === true) { + const result = await super.create( + params as CompletionCreateParams.CreateChatCompletionRequestStreaming, + options + ); + // Your post API call logic here + console.log("Doing post API call for NonStreaming..."); + return result; + } else { + const startTime = Date.now(); + const result = await super.create( + params as CompletionCreateParams.CreateChatCompletionRequestNonStreaming, + options + ); + console.log('result is this', result) + this.openaiInstance.openPipeApi?.externalApiReport({ + startTime, + endTime: Date.now(), + reqPayload: params, + respPayload: result, + respStatus: 200, + error: undefined, + tags, + }); + + // Your post API call logic here + console.log("Doing post API call for Streaming..."); + return result; + } } }