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.

67 lines
1.5 KiB

  1. import {
  2. ChoiceBase,
  3. DoFetch,
  4. PlatformError,
  5. PlatformResponse,
  6. UsageMetadata,
  7. } from '../common';
  8. import { EditModel } from '../models';
  9. export enum DataEventObjectType {
  10. EDIT = 'edit',
  11. }
  12. export interface CreateEditParams {
  13. model: EditModel;
  14. input?: string;
  15. instruction: string;
  16. n?: number;
  17. temperature?: number;
  18. topP?: number;
  19. }
  20. export interface EditChoice extends ChoiceBase {
  21. text: string;
  22. }
  23. export interface CreateEditDataEvent extends PlatformResponse, UsageMetadata {
  24. object: DataEventObjectType;
  25. choices: EditChoice[];
  26. }
  27. export function createEdit(
  28. this: NodeJS.EventEmitter,
  29. doFetch: DoFetch,
  30. params: CreateEditParams,
  31. ) {
  32. doFetch('POST', '/edits', {
  33. model: params.model ?? EditModel.TEXT_DAVINCI_EDIT_001,
  34. input: params.input ?? '',
  35. instruction: params.instruction,
  36. n: params.n ?? 1,
  37. temperature: params.temperature ?? 1,
  38. top_p: params.topP ?? 1,
  39. })
  40. .then(async (response) => {
  41. if (!response.ok) {
  42. this.emit('error', new PlatformError(
  43. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  44. `Request from platform returned with status: ${response.status}`,
  45. response,
  46. ));
  47. this.emit('end');
  48. return;
  49. }
  50. const responseData = await response.json() as Record<string, unknown>;
  51. this.emit('data', responseData);
  52. this.emit('end');
  53. })
  54. .catch((err) => {
  55. this.emit('error', err as Error);
  56. this.emit('end');
  57. });
  58. return this;
  59. }