outputs.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. export const RUN_KEY = "__run";
  2. /**
  3. * Chunk of a single generation. Used for streaming.
  4. */
  5. export class GenerationChunk {
  6. constructor(fields) {
  7. Object.defineProperty(this, "text", {
  8. enumerable: true,
  9. configurable: true,
  10. writable: true,
  11. value: void 0
  12. });
  13. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  14. Object.defineProperty(this, "generationInfo", {
  15. enumerable: true,
  16. configurable: true,
  17. writable: true,
  18. value: void 0
  19. });
  20. this.text = fields.text;
  21. this.generationInfo = fields.generationInfo;
  22. }
  23. concat(chunk) {
  24. return new GenerationChunk({
  25. text: this.text + chunk.text,
  26. generationInfo: {
  27. ...this.generationInfo,
  28. ...chunk.generationInfo,
  29. },
  30. });
  31. }
  32. }
  33. export class ChatGenerationChunk extends GenerationChunk {
  34. constructor(fields) {
  35. super(fields);
  36. Object.defineProperty(this, "message", {
  37. enumerable: true,
  38. configurable: true,
  39. writable: true,
  40. value: void 0
  41. });
  42. this.message = fields.message;
  43. }
  44. concat(chunk) {
  45. return new ChatGenerationChunk({
  46. text: this.text + chunk.text,
  47. generationInfo: {
  48. ...this.generationInfo,
  49. ...chunk.generationInfo,
  50. },
  51. message: this.message.concat(chunk.message),
  52. });
  53. }
  54. }