All files / shared/src/types/api/routes/completions-api response.ts

100% Statements 45/45
100% Branches 1/1
100% Functions 0/0
100% Lines 45/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 1041x         1x   1x   1x   1x   1x 1x 1x   1x   1x   1x 1x 1x 1x 1x             1x   1x   1x   1x   1x 1x       1x 1x 1x 1x           1x   1x   1x   1x   1x 1x             1x   1x   1x   1x 1x               1x   1x   1x   1x   1x   1x   1x   1x 1x      
import { z } from 'zod';
 
/**
 * Log probability information for a token.
 */
export const CompletionTokenLogprob = z.object({
  /** The token. */
  token: z.string(),
  /** The log probability of this token. */
  logprob: z.number(),
  /** A list of integers representing the UTF-8 bytes representation of the token. */
  bytes: z.array(z.number()).optional(),
  /** List of the most likely tokens and their log probabilities at this token position. */
  top_logprobs: z
    .array(
      z.object({
        /** The token. */
        token: z.string(),
        /** The log probability of this token. */
        logprob: z.number(),
        /** A list of integers representing the UTF-8 bytes representation of the token. */
        bytes: z.array(z.number()).optional(),
      }),
    )
    .optional(),
});
 
export type CompletionTokenLogprob = z.infer<typeof CompletionTokenLogprob>;
 
/**
 * Log probability information for the choice.
 */
export const CompletionChoiceLogprobs = z.object({
  /** The tokens chosen by the completion endpoint. */
  tokens: z.array(z.string()).optional(),
  /** The log probabilities of the tokens chosen by the completion endpoint. */
  token_logprobs: z.array(z.number().nullable()).optional(),
  /** The most likely tokens at each position and their log probabilities. */
  top_logprobs: z.array(z.record(z.string(), z.number()).nullable()).optional(),
  /** The character offsets from the beginning of the returned text for each token. */
  text_offset: z.array(z.number()).optional(),
});
 
export type CompletionChoiceLogprobs = z.infer<typeof CompletionChoiceLogprobs>;
 
export enum CompletionFinishReason {
  STOP = 'stop',
  LENGTH = 'length',
  CONTENT_FILTER = 'content_filter',
}
 
/**
 * A completion choice.
 */
export const CompletionChoice = z.object({
  /** The reason the model stopped generating tokens. */
  finish_reason: z.enum(CompletionFinishReason).nullable(),
  /** The index of the choice in the list of choices. */
  index: z.number(),
  /** Log probability information for the choice. */
  logprobs: CompletionChoiceLogprobs.nullable(),
  /** The generated text completion. */
  text: z.string(),
});
 
export type CompletionChoice = z.infer<typeof CompletionChoice>;
 
/**
 * Usage statistics for the completion request.
 */
export const CompletionUsage = z.object({
  /** Number of tokens in the generated completion. */
  completion_tokens: z.number(),
  /** Number of tokens in the prompt. */
  prompt_tokens: z.number(),
  /** Total number of tokens used in the request (prompt + completion). */
  total_tokens: z.number(),
});
 
export type CompletionUsage = z.infer<typeof CompletionUsage>;
 
/**
 * Represents a completion response from the API.
 * Note: both the streamed and non-streamed response objects share the same shape (unlike the chat endpoint).
 */
export const CompletionResponseBody = z.object({
  /** A unique identifier for the completion. */
  id: z.string(),
  /** The list of completion choices the model generated for the input prompt. */
  choices: z.array(CompletionChoice),
  /** The Unix timestamp (in seconds) of when the completion was created. */
  created: z.number(),
  /** The model used for completion. */
  model: z.string(),
  /** The object type, which is always "text_completion". */
  object: z.literal('text_completion'),
  /** This fingerprint represents the backend configuration that the model runs with. */
  system_fingerprint: z.string().optional(),
  /** Usage statistics for the completion request. */
  usage: CompletionUsage.optional(),
});
 
export type CompletionResponseBody = z.infer<typeof CompletionResponseBody>;