diff --git a/apps/docs/docs/end_to_end.md b/apps/docs/docs/end_to_end.md index d30ad16756edb5a9c3d30b870184e43cf1dd8dc5..1b4ca433804742e90d5b8239fea8ac462ac1263b 100644 --- a/apps/docs/docs/end_to_end.md +++ b/apps/docs/docs/end_to_end.md @@ -18,6 +18,10 @@ Create a list index and query it. This example also use the `LLMRetriever`, whic Create a vector index and query it. The vector index will use embeddings to fetch the top k most relevant nodes. By default, the top k is 2. +## [Customized Vector Index](https://github.com/run-llama/LlamaIndexTS/blob/main/apps/simple/vectorIndexCustomize.ts) + +Create a vector index and query it, while also configuring the the `LLM`, the `ServiceContext`, and the `similarity_top_k`. + ## [OpenAI LLM](https://github.com/run-llama/LlamaIndexTS/blob/main/apps/simple/openai.ts) Create an OpenAI LLM and directly use it for chat. diff --git a/apps/docs/docs/modules/high_level/query_engine.md b/apps/docs/docs/modules/high_level/query_engine.md index 20dbe35b2063163d3cbcbd8e693d5653051cf287..c6d6452a199bb73801928ad4bf82879bbd9c020e 100644 --- a/apps/docs/docs/modules/high_level/query_engine.md +++ b/apps/docs/docs/modules/high_level/query_engine.md @@ -8,7 +8,7 @@ A query engine wraps a `Retriever` and a `ResponseSynthesizer` into a pipeline, ```typescript const queryEngine = index.asQueryEngine(); -const response = queryEngine.query("query string"); +const response = await queryEngine.query("query string"); ``` ## Sub Question Query Engine diff --git a/apps/docs/docs/modules/low_level/embedding.md b/apps/docs/docs/modules/low_level/embedding.md index 645f33459cc5ac0a8487f428d60ea89396dacb7e..57f672abef2a36cb3bdf49037a5f4f1b76d9d5f7 100644 --- a/apps/docs/docs/modules/low_level/embedding.md +++ b/apps/docs/docs/modules/low_level/embedding.md @@ -9,11 +9,11 @@ The embedding model in LlamaIndex is responsible for creating numerical represen This can be explicitly set in the `ServiceContext` object. ```typescript -import { OpenAIEmbedding, ServiceContext } from "llamaindex"; +import { OpenAIEmbedding, serviceContextFromDefaults } from "llamaindex"; const openaiEmbeds = new OpenAIEmbedding(); -const serviceContext = new ServiceContext({ embedModel: openaiEmbeds }); +const serviceContext = serviceContextFromDefaults({ embedModel: openaiEmbeds }); ``` ## API Reference diff --git a/apps/docs/docs/modules/low_level/llm.md b/apps/docs/docs/modules/low_level/llm.md index 29e8a8879104cff9a7bd57d4429dcfd59175f6dd..4f0ba2db5a49e523b317b9aea7d010381309d930 100644 --- a/apps/docs/docs/modules/low_level/llm.md +++ b/apps/docs/docs/modules/low_level/llm.md @@ -9,14 +9,14 @@ The LLM is responsible for reading text and generating natural language response The LLM can be explicitly set in the `ServiceContext` object. ```typescript -import { ChatGPTLLMPredictor, ServiceContext } from "llamaindex"; +import { OpenAI, serviceContextFromDefaults } from "llamaindex"; -const openaiLLM = new ChatGPTLLMPredictor({ model: "gpt-3.5-turbo" }); +const openaiLLM = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0 }); -const serviceContext = new ServiceContext({ llmPredictor: openaiLLM }); +const serviceContext = serviceContextFromDefaults({ llm: openaiLLM }); ``` ## API Reference -- [ChatGPTLLMPredictor](../../api/classes/ChatGPTLLMPredictor.md) +- [OpenAI](../../api/classes/OpenAI.md) - [ServiceContext](../../api/interfaces/ServiceContext.md) \ No newline at end of file diff --git a/apps/simple/chatEngine.ts b/apps/simple/chatEngine.ts index 97485b49701bf67ec9a1d96fbc6e02a5da340500..a79bfd88c6065e09b87586da0a13020b4c93709b 100644 --- a/apps/simple/chatEngine.ts +++ b/apps/simple/chatEngine.ts @@ -26,7 +26,7 @@ async function main() { while (true) { const query = await rl.question("Query: "); const response = await chatEngine.chat(query); - console.log(response); + console.log(response.toString()); } } diff --git a/apps/simple/openai.ts b/apps/simple/openai.ts index 12e91f67a8f4461aa5fb68b6185642ebdff6066d..f53709c6495d2a098a439e97b1a1f54321ec6ca3 100644 --- a/apps/simple/openai.ts +++ b/apps/simple/openai.ts @@ -1,19 +1,13 @@ -// @ts-ignore -import process from "node:process"; -import { Configuration, OpenAIWrapper } from "llamaindex/src/llm/openai"; +import { OpenAI } from "llamaindex"; (async () => { - const configuration = new Configuration({ - apiKey: process.env.OPENAI_API_KEY, - }); - - const openai = new OpenAIWrapper(configuration); - - const { data } = await openai.createChatCompletion({ - model: "gpt-3.5-turbo-0613", - messages: [{ role: "user", content: "Hello, world!" }], - }); - - console.log(data); - console.log(data.choices[0].message); + const llm = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0.0 }); + + // complete api + const response1 = await llm.complete("How are you?"); + console.log(response1.message.content); + + // chat api + const response2 = await llm.chat([{ content: "Tell me a joke!", role: "user" }]); + console.log(response2.message.content); })(); diff --git a/apps/simple/subquestion.ts b/apps/simple/subquestion.ts index e386a296c8d2b6cffc23554dfbe799f3f2e337b8..ff7c8225f96823f93c5908c5a6bdad5b67d3281e 100644 --- a/apps/simple/subquestion.ts +++ b/apps/simple/subquestion.ts @@ -22,5 +22,5 @@ import essay from "./essay"; "How was Paul Grahams life different before and after YC?" ); - console.log(response); + console.log(response.toString()); })(); diff --git a/apps/simple/vectorIndexCustomize.ts b/apps/simple/vectorIndexCustomize.ts index 49d5ca4d80be166291502b58f65d2090410ab775..aa7ebc6ae0a0000d46f1686d8052ed2926d423e2 100644 --- a/apps/simple/vectorIndexCustomize.ts +++ b/apps/simple/vectorIndexCustomize.ts @@ -1,14 +1,21 @@ -import { Document, VectorStoreIndex, RetrieverQueryEngine } from "llamaindex"; +import { Document, VectorStoreIndex, RetrieverQueryEngine, OpenAI, serviceContextFromDefaults } from "llamaindex"; import essay from "./essay"; // Customize retrieval and query args async function main() { const document = new Document({ text: essay }); - const index = await VectorStoreIndex.fromDocuments([document]); + + const serviceContext = serviceContextFromDefaults( + { llm: new OpenAI({ model: "gpt-3.5-turbo", temperature: 0.0 }) } + ); + + const index = await VectorStoreIndex.fromDocuments([document], undefined, serviceContext); + const retriever = index.asRetriever(); retriever.similarityTopK = 5; // TODO: cannot pass responseSynthesizer into retriever query engine const queryEngine = new RetrieverQueryEngine(retriever); + const response = await queryEngine.query( "What did the author do growing up?" );