Skip to content

Commit

Permalink
Task: Add usage and timestamp to replicate
Browse files Browse the repository at this point in the history
  • Loading branch information
zya committed Oct 28, 2023
1 parent 616989c commit 207e5a8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/handlers/replicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
HandlerParamsStreaming,
} from '../types';
import { combinePrompts } from '../utils/combinePrompts';
import { toUsage } from '../utils/toUsage';
import { getUnixTimestamp } from '../utils/getUnixTimestamp';

async function sleep(time: number): Promise<unknown> {
return new Promise((res) => {
Expand All @@ -19,6 +21,7 @@ async function sleep(time: number): Promise<unknown> {
}

async function handleNonStreamingPrediction(
prompt: string,
prediction: Prediction,
replicate: Replicate,
): Promise<ResultNotStreaming> {
Expand All @@ -28,6 +31,8 @@ async function handleNonStreamingPrediction(
'',
);
return {
usage: toUsage(prompt, output),
created: getUnixTimestamp(),
choices: [
{
message: {
Expand All @@ -42,6 +47,7 @@ async function handleNonStreamingPrediction(
}

async function* handleStreamingPrediction(
prompt: string,
prediction: Prediction,
): ResultStreaming {
if (!prediction?.urls?.stream) {
Expand Down Expand Up @@ -81,6 +87,8 @@ async function* handleStreamingPrediction(
// flush the results since last yield
const combined = results.reduce((acc, curr) => (acc += curr), '');
yield {
created: getUnixTimestamp(),
usage: toUsage(prompt, combined),
choices: [
{
delta: {
Expand Down Expand Up @@ -128,7 +136,7 @@ export async function ReplicateHandler(
});

if (params.stream) {
return handleStreamingPrediction(prediction);
return handleStreamingPrediction(prompt, prediction);
}
return handleNonStreamingPrediction(prediction, replicate);
return handleNonStreamingPrediction(prompt, prediction, replicate);
}

0 comments on commit 207e5a8

Please sign in to comment.