or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

agents.mdchat-engines.mddocument-processing.mdembeddings.mdindex.mdllm-integration.mdquery-engines.mdresponse-synthesis.mdsettings.mdstorage.mdtools.mdvector-indexing.md
tile.json

response-synthesis.mddocs/

0

# Response Synthesis

1

2

Response generation and synthesis strategies for combining retrieved information into coherent answers in LlamaIndex.TS.

3

4

## Import

5

6

```typescript

7

import { ResponseSynthesizer } from "llamaindex";

8

```

9

10

## Overview

11

12

Response synthesis combines retrieved information from multiple sources into coherent, comprehensive answers using various strategies optimized for different use cases.

13

14

## Base Synthesizer Interface

15

16

```typescript { .api }

17

interface BaseSynthesizer {

18

synthesize(query: string, nodes: BaseNode[]): Promise<EngineResponse>;

19

}

20

```

21

22

## Response Synthesizer

23

24

```typescript { .api }

25

class ResponseSynthesizer implements BaseSynthesizer {

26

constructor(options?: {

27

responseMode?: ResponseMode;

28

serviceContext?: ServiceContext;

29

});

30

31

synthesize(query: string, nodes: BaseNode[]): Promise<EngineResponse>;

32

33

responseMode: ResponseMode;

34

}

35

36

type ResponseMode = "refine" | "compact" | "tree_summarize" | "simple_summarize";

37

```

38

39

## Response Modes

40

41

### Tree Summarize

42

Best for comprehensive answers from multiple sources.

43

44

```typescript

45

const treeSynthesizer = new ResponseSynthesizer({

46

responseMode: "tree_summarize",

47

});

48

```

49

50

### Refine Mode

51

Iteratively refines answers with each chunk.

52

53

```typescript

54

const refineSynthesizer = new ResponseSynthesizer({

55

responseMode: "refine",

56

});

57

```

58

59

### Compact Mode

60

Combines chunks to maximize context usage.

61

62

```typescript

63

const compactSynthesizer = new ResponseSynthesizer({

64

responseMode: "compact",

65

});

66

```

67

68

## Usage with Query Engines

69

70

```typescript

71

import { RetrieverQueryEngine, ResponseSynthesizer } from "llamaindex";

72

73

const queryEngine = new RetrieverQueryEngine({

74

retriever: index.asRetriever(),

75

responseSynthesizer: new ResponseSynthesizer({

76

responseMode: "tree_summarize",

77

}),

78

});

79

```

80

81

## Factory Function

82

83

```typescript { .api }

84

function createResponseSynthesizer(mode: ResponseMode): BaseSynthesizer;

85

```

86

87

## Best Practices

88

89

```typescript

90

// Choose mode based on use case

91

const synthesizer = createResponseSynthesizer(

92

documents.length > 10 ? "tree_summarize" : "refine"

93

);

94

```