Many-in-one AI client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.4 KiB

  1. import { Message, MessageRole } from './message';
  2. export enum FinishReason {
  3. STOP = 'stop',
  4. LENGTH = 'length',
  5. }
  6. export interface ChoiceBase {
  7. index: number;
  8. }
  9. export interface FinishableChoiceBase extends ChoiceBase {
  10. finish_reason: FinishReason | null;
  11. }
  12. export type DataEventId = string;
  13. export type Timestamp = number;
  14. export interface Usage {
  15. prompt_tokens: number;
  16. completion_tokens: number;
  17. total_tokens: number;
  18. }
  19. export interface UsageMetadata {
  20. usage: Usage;
  21. }
  22. export interface PlatformResponse {
  23. created: Timestamp;
  24. }
  25. export type DoFetch = (
  26. method: string,
  27. path: string,
  28. body: Record<string, unknown>
  29. ) => Promise<Response>;
  30. export type ConsumeStream = (
  31. response: Response,
  32. ) => Promise<void>;
  33. export class PlatformError extends Error {
  34. constructor(message: string, readonly response: Response) {
  35. super(message);
  36. this.name = 'OpenAi.PlatformError';
  37. }
  38. }
  39. export const normalizeChatMessage = (messageRaw: Message | Message[]) => {
  40. if (typeof messageRaw === 'string') {
  41. return [
  42. {
  43. role: MessageRole.USER,
  44. content: messageRaw,
  45. },
  46. ];
  47. }
  48. if (Array.isArray(messageRaw)) {
  49. return messageRaw.map((message) => {
  50. if (typeof message === 'string') {
  51. return {
  52. role: MessageRole.USER,
  53. content: message,
  54. };
  55. }
  56. return message;
  57. });
  58. }
  59. return messageRaw;
  60. };