Logo

With TypeScript

In this guide, you'll learn how to use LlamaIndex with TypeScript

LlamaIndex.TS is written in TypeScript and designed to be used in TypeScript projects.

We do lots of work on strong typing to make sure you have a great typing experience with LlamaIndex.TS.

import {  } from 'llamaindex'
const  = new ({
  : `Context information from multiple sources is below.
---------------------
{context}
---------------------
Given the information from multiple sources and not prior knowledge.
Answer the query in the style of a Shakespeare play"
Query: {query}
Answer:`,
	: ["context", "query"],
});
.({
	c
  • context
})
const  = .({
	: .(),
	: .(),
})
 
type  = .<typeof >
 
.<>(() => {
	.t
  • time
}, { : 'getWeather', : 'Get the weather information', : , })

Enable TypeScript

{
  compilerOptions: {
    // ⬇️ add this line to your tsconfig.json
    moduleResolution: "bundler", // or "node16"
  },
}

Enable AsyncIterable for Web Stream API

Some modules uses Web Stream API like ReadableStream and WritableStream, you need to enable DOM.AsyncIterable in your tsconfig.json.

{
  compilerOptions: {
    // ⬇️ add this lib to your tsconfig.json
    lib: ["DOM.AsyncIterable"],
  },
}
import {  } from '@llamaindex/openai'
 
const  = new ({
  : []
})
 
const  = await .({
  : 'Hello, how are you?',
  : true
})
for await (const  of 
const response: ReadableStream<EngineResponse>
response
) {
// ... }

Run TypeScript Script in Node.js

We recommend to use tsx to run TypeScript script in Node.js.

node --import tsx ./my-script.ts
Edit on GitHub

Last updated on

On this page