Skip to content

Commit

Permalink
feat: Add streaming support for QueryEngine (and unify streaming inte…
Browse files Browse the repository at this point in the history
…rface with ChatEngine) (run-llama#393)
  • Loading branch information
marcusschiesser authored Jan 17, 2024
1 parent 9492cc6 commit 844029d
Show file tree
Hide file tree
Showing 38 changed files with 324 additions and 255 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-rice-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---

Add streaming support for QueryEngine (and unify streaming interface with ChatEngine)
5 changes: 5 additions & 0 deletions .changeset/tidy-mayflies-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---

Breaking: Use parameter object for query and chat methods of ChatEngine and QueryEngine
11 changes: 10 additions & 1 deletion apps/docs/docs/modules/high_level/chat_engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ const retriever = index.asRetriever();
const chatEngine = new ContextChatEngine({ retriever });

// start chatting
const response = await chatEngine.chat(query);
const response = await chatEngine.chat({ message: query });
```

The `chat` function also supports streaming, just add `stream: true` as an option:

```typescript
const stream = await chatEngine.chat({ message: query, stream: true });
for await (const chunk of stream) {
process.stdout.write(chunk.response);
}
```

## Api References
Expand Down
11 changes: 10 additions & 1 deletion apps/docs/docs/modules/high_level/query_engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ A query engine wraps a `Retriever` and a `ResponseSynthesizer` into a pipeline,

```typescript
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query("query string");
const response = await queryEngine.query({ query: "query string" });
```

The `query` function also supports streaming, just add `stream: true` as an option:

```typescript
const stream = await queryEngine.query({ query: "query string", stream: true });
for await (const chunk of stream) {
process.stdout.write(chunk.response);
}
```

## Sub Question Query Engine
Expand Down
4 changes: 3 additions & 1 deletion examples/astradb/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ async function main() {

const queryEngine = await index.asQueryEngine({ retriever });

const results = await queryEngine.query("What is the best reviewed movie?");
const results = await queryEngine.query({
query: "What is the best reviewed movie?",
});

console.log(results.response);
} catch (e) {
Expand Down
7 changes: 5 additions & 2 deletions examples/chatEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ async function main() {

while (true) {
const query = await rl.question("Query: ");
const response = await chatEngine.chat(query);
console.log(response.toString());
const stream = await chatEngine.chat({ message: query, stream: true });
console.log();
for await (const chunk of stream) {
process.stdout.write(chunk.response);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/chromadb/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ async function main() {

console.log("Querying index");
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"Tell me about Godfrey Cheshire's rating of La Sapienza.",
);
const response = await queryEngine.query({
query: "Tell me about Godfrey Cheshire's rating of La Sapienza.",
});
console.log(response.toString());
} catch (e) {
console.error(e);
Expand Down
11 changes: 7 additions & 4 deletions examples/huggingface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ async function main() {

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"What did the author do in college?",
);
const stream = await queryEngine.query({
query: "What did the author do in college?",
stream: true,
});

// Output response
console.log(response.toString());
for await (const chunk of stream) {
process.stdout.write(chunk.response);
}
}

main().catch(console.error);
6 changes: 3 additions & 3 deletions examples/keywordIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ async function main() {
mode,
}),
});
const response = await queryEngine.query(
"What did the author do growing up?",
);
const response = await queryEngine.query({
query: "What did the author do growing up?",
});
console.log(response.toString());
});
}
Expand Down
4 changes: 3 additions & 1 deletion examples/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ async function main() {
// Query the index
const queryEngine = index.asQueryEngine();

const response = await queryEngine.query("What does the example code do?");
const response = await queryEngine.query({
query: "What does the example code do?",
});

// Output response
console.log(response.toString());
Expand Down
2 changes: 1 addition & 1 deletion examples/mistral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function rag(llm: LLM, embedModel: BaseEmbedding, query: string) {

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(query);
const response = await queryEngine.query({ query });
return response.response;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function main() {
break;
}

const response = await queryEngine.query(query);
const response = await queryEngine.query({ query });

// Output response
console.log(response.toString());
Expand Down
6 changes: 3 additions & 3 deletions examples/mongodb/3_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ async function query() {

const retriever = index.asRetriever({ similarityTopK: 20 });
const queryEngine = index.asQueryEngine({ retriever });
const result = await queryEngine.query(
"What does the author think of web frameworks?",
);
const result = await queryEngine.query({
query: "What does the author think of web frameworks?",
});
console.log(result.response);
await client.close();
}
Expand Down
6 changes: 3 additions & 3 deletions examples/multimodal/rag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ async function main() {
responseSynthesizer: new MultiModalResponseSynthesizer({ serviceContext }),
retriever: index.asRetriever({ similarityTopK: 3, imageSimilarityTopK: 1 }),
});
const result = await queryEngine.query(
"Tell me more about Vincent van Gogh's famous paintings",
);
const result = await queryEngine.query({
query: "Tell me more about Vincent van Gogh's famous paintings",
});
console.log(result.response, "\n");
images.forEach((image) =>
console.log(`Image retrieved and used in inference: ${image.toString()}`),
Expand Down
2 changes: 1 addition & 1 deletion examples/pg-vector-store/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function main() {
}

try {
const answer = await queryEngine.query(question);
const answer = await queryEngine.query({ query: question });
console.log(answer.response);
} catch (error) {
console.error("Error:", error);
Expand Down
2 changes: 1 addition & 1 deletion examples/pinecone-vector-store/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function main() {
}

try {
const answer = await queryEngine.query(question);
const answer = await queryEngine.query({ query: question });
console.log(answer.response);
} catch (error) {
console.error("Error:", error);
Expand Down
2 changes: 1 addition & 1 deletion examples/readers/load-assemblyai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ program
break;
}

const response = await queryEngine.query(query);
const response = await queryEngine.query({ query });

console.log(response.toString());
}
Expand Down
6 changes: 3 additions & 3 deletions examples/readers/load-csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ Given the CSV file, generate me Typescript code to answer the question: ${query}
const queryEngine = index.asQueryEngine({ responseSynthesizer });

// Query the index
const response = await queryEngine.query(
"What is the correlation between survival and age?",
);
const response = await queryEngine.query({
query: "What is the correlation between survival and age?",
});

// Output response
console.log(response.toString());
Expand Down
2 changes: 1 addition & 1 deletion examples/readers/load-docx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function main() {

// Test query
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(SAMPLE_QUERY);
const response = await queryEngine.query({ query: SAMPLE_QUERY });
console.log(`Test query > ${SAMPLE_QUERY}:\n`, response.toString());
}

Expand Down
6 changes: 3 additions & 3 deletions examples/readers/load-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ async function main() {

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"What were the notable changes in 18.1?",
);
const response = await queryEngine.query({
query: "What were the notable changes in 18.1?",
});

// Output response
console.log(response.toString());
Expand Down
2 changes: 1 addition & 1 deletion examples/readers/load-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function main() {

// Test query
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(SAMPLE_QUERY);
const response = await queryEngine.query({ query: SAMPLE_QUERY });
console.log(`Test query > ${SAMPLE_QUERY}:\n`, response.toString());
}

Expand Down
2 changes: 1 addition & 1 deletion examples/readers/load-notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ program
break;
}

const response = await queryEngine.query(query);
const response = await queryEngine.query({ query });

// Output response
console.log(response.toString());
Expand Down
4 changes: 3 additions & 1 deletion examples/readers/load-pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ async function main() {

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query("What mistakes did they make?");
const response = await queryEngine.query({
query: "What mistakes did they make?",
});

// Output response
console.log(response.toString());
Expand Down
6 changes: 3 additions & 3 deletions examples/sentenceWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ async function main() {
const queryEngine = index.asQueryEngine({
nodePostprocessors: [new MetadataReplacementPostProcessor("window")],
});
const response = await queryEngine.query(
"What did the author do in college?",
);
const response = await queryEngine.query({
query: "What did the author do in college?",
});

// Output response
console.log(response.toString());
Expand Down
12 changes: 6 additions & 6 deletions examples/storageContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ async function main() {

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"What did the author do in college?",
);
const response = await queryEngine.query({
query: "What did the author do in college?",
});

// Output response
console.log(response.toString());
Expand All @@ -35,9 +35,9 @@ async function main() {
storageContext: secondStorageContext,
});
const loadedQueryEngine = loadedIndex.asQueryEngine();
const loadedResponse = await loadedQueryEngine.query(
"What did the author do growing up?",
);
const loadedResponse = await loadedQueryEngine.query({
query: "What did the author do growing up?",
});
console.log(loadedResponse.toString());
}

Expand Down
6 changes: 3 additions & 3 deletions examples/subquestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import essay from "./essay";
],
});

const response = await queryEngine.query(
"How was Paul Grahams life different before and after YC?",
);
const response = await queryEngine.query({
query: "How was Paul Grahams life different before and after YC?",
});

console.log(response.toString());
})();
6 changes: 3 additions & 3 deletions examples/summaryIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ async function main() {
const queryEngine = index.asQueryEngine({
retriever: index.asRetriever({ mode: SummaryRetrieverMode.LLM }),
});
const response = await queryEngine.query(
"What did the author do growing up?",
);
const response = await queryEngine.query({
query: "What did the author do growing up?",
});
console.log(response.toString());
}

Expand Down
6 changes: 3 additions & 3 deletions examples/together-ai/vector-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ async function main() {

const queryEngine = index.asQueryEngine();

const response = await queryEngine.query(
"What did the author do in college?",
);
const response = await queryEngine.query({
query: "What did the author do in college?",
});

console.log(response.toString());
}
Expand Down
6 changes: 3 additions & 3 deletions examples/vectorIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ async function main() {

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query(
"What did the author do in college?",
);
const response = await queryEngine.query({
query: "What did the author do in college?",
});

// Output response
console.log(response.toString());
Expand Down
6 changes: 3 additions & 3 deletions examples/vectorIndexAnthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ async function main() {

// Query the index
const queryEngine = index.asQueryEngine({ responseSynthesizer });
const response = await queryEngine.query(
"What did the author do in college?",
);
const response = await queryEngine.query({
query: "What did the author do in college?",
});

// Output response
console.log(response.toString());
Expand Down
6 changes: 3 additions & 3 deletions examples/vectorIndexCustomize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ async function main() {
[nodePostprocessor],
);

const response = await queryEngine.query(
"What did the author do growing up?",
);
const response = await queryEngine.query({
query: "What did the author do growing up?",
});
console.log(response.response);
}

Expand Down
4 changes: 3 additions & 1 deletion examples/vectorIndexFromVectorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ async function main() {
},
});

const response = await queryEngine.query("How many results do you have?");
const response = await queryEngine.query({
query: "How many results do you have?",
});

console.log(response.toString());
}
Expand Down
Loading

0 comments on commit 844029d

Please sign in to comment.