Many-in-one AI client.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

embedding.ts 1.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { DoFetch } from '../../../packages/request';
  2. import { EmbeddingModel } from '../models';
  3. import { PlatformApiError } from '../../../common';
  4. import { ResponseObjectType } from '../common';
  5. import { PromptUsage, UsageMetadata } from '../usage';
  6. export enum DataEventObjectType {
  7. EMBEDDING = 'embedding',
  8. }
  9. export interface CreateEmbeddingParams {
  10. model: EmbeddingModel;
  11. input: string | number[] | string[] | number[][];
  12. user?: string;
  13. }
  14. export interface Embedding {
  15. object: DataEventObjectType;
  16. embedding: number[];
  17. index: number;
  18. }
  19. export interface CreateEmbeddingResponse extends UsageMetadata<PromptUsage> {
  20. object: ResponseObjectType;
  21. data: Embedding[];
  22. model: EmbeddingModel;
  23. }
  24. export function createEmbedding(
  25. this: NodeJS.EventEmitter,
  26. doFetch: DoFetch,
  27. params: CreateEmbeddingParams,
  28. ) {
  29. doFetch('POST', '/embeddings', {
  30. model: params.model,
  31. input: params.input,
  32. user: params.user,
  33. })
  34. .then(async (response) => {
  35. if (!response.ok) {
  36. this.emit('error', new PlatformApiError(
  37. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  38. `Create chat completion returned with status: ${response.status}`,
  39. response,
  40. ));
  41. this.emit('end');
  42. return;
  43. }
  44. const responseData = await response.json() as CreateEmbeddingResponse;
  45. this.emit('data', responseData);
  46. this.emit('end');
  47. })
  48. .catch((err) => {
  49. this.emit('error', err as Error);
  50. this.emit('end');
  51. });
  52. return this;
  53. }