| 84 | options?: Core.RequestOptions, |
| 85 | ): Core.APIPromise<Stream<ChatCompletionChunk> | ChatCompletion>; |
| 86 | async create( |
| 87 | { openpipe: rawOpenpipe, ...body }: ChatCompletionCreateParams & OpenPipeArgs, |
| 88 | options?: Core.RequestOptions, |
| 89 | ): Promise< |
| 90 | Core.APIPromise< |
| 91 | | (ChatCompletion & { openpipe?: OpenPipeMeta }) |
| 92 | | (Stream<ChatCompletionChunk> & { openpipe?: OpenPipeMeta }) |
| 93 | > |
| 94 | > { |
| 95 | const openpipe = { logRequest: true, ...rawOpenpipe }; |
| 96 | const requestedAt = Date.now(); |
| 97 | let reportingFinished: OpenPipeMeta["reportingFinished"] = Promise.resolve(); |
| 98 | |
| 99 | if (body.model.startsWith("openpipe:")) { |
| 100 | if (!this.opCompletionClient) throw new Error("OpenPipe client not set"); |
| 101 | return this.opCompletionClient?.chat.completions.create(body, { |
| 102 | ...options, |
| 103 | headers: { |
| 104 | ...options?.headers, |
| 105 | "op-log-request": openpipe.logRequest ? "true" : "false", |
| 106 | "op-cache": openpipe.cache ? "true" : "false", |
| 107 | "op-tags": JSON.stringify(getTags(openpipe)), |
| 108 | }, |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | try { |
| 113 | if (body.stream) { |
| 114 | const stream = await super.create(body, options); |
| 115 | try { |
| 116 | return new WrappedStream(stream, (response) => { |
| 117 | if (!openpipe.logRequest) return Promise.resolve(); |
| 118 | return this._report({ |
| 119 | requestedAt, |
| 120 | receivedAt: Date.now(), |
| 121 | reqPayload: body, |
| 122 | respPayload: response, |
| 123 | statusCode: 200, |
| 124 | tags: getTags(openpipe), |
| 125 | }); |
| 126 | }); |
| 127 | } catch (e) { |
| 128 | console.error("OpenPipe: error creating wrapped stream"); |
| 129 | console.error(e); |
| 130 | throw e; |
| 131 | } |
| 132 | } else { |
| 133 | const response = await super.create(body, options); |
| 134 | |
| 135 | reportingFinished = openpipe.logRequest |
| 136 | ? this._report({ |
| 137 | requestedAt, |
| 138 | receivedAt: Date.now(), |
| 139 | reqPayload: body, |
| 140 | respPayload: response, |
| 141 | statusCode: 200, |
| 142 | tags: getTags(openpipe), |
| 143 | }) |