-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into himself65/20240422/http-support
- Loading branch information
Showing
24 changed files
with
407 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"llamaindex": patch | ||
--- | ||
|
||
feat: support jina ai embedding and reranker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
apps/docs/docs/modules/embeddings/available_embeddings/jinaai.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Jina AI | ||
|
||
To use Jina AI embeddings, you need to import `JinaAIEmbedding` from `llamaindex`. | ||
|
||
```ts | ||
import { JinaAIEmbedding, Settings } from "llamaindex"; | ||
|
||
Settings.embedModel = new JinaAIEmbedding(); | ||
|
||
const document = new Document({ text: essay, id_: "essay" }); | ||
|
||
const index = await VectorStoreIndex.fromDocuments([document]); | ||
|
||
const queryEngine = index.asQueryEngine(); | ||
|
||
const query = "What is the meaning of life?"; | ||
|
||
const results = await queryEngine.query({ | ||
query, | ||
}); | ||
``` |
71 changes: 71 additions & 0 deletions
71
apps/docs/docs/modules/node_postprocessors/jinaai_reranker.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Jina AI Reranker | ||
|
||
The Jina AI Reranker is a postprocessor that uses the Jina AI Reranker API to rerank the results of a search query. | ||
|
||
## Setup | ||
|
||
Firstly, you will need to install the `llamaindex` package. | ||
|
||
```bash | ||
pnpm install llamaindex | ||
``` | ||
|
||
Now, you will need to sign up for an API key at [Jina AI](https://jina.ai/reranker). Once you have your API key you can import the necessary modules and create a new instance of the `JinaAIReranker` class. | ||
|
||
```ts | ||
import { | ||
JinaAIReranker, | ||
Document, | ||
OpenAI, | ||
VectorStoreIndex, | ||
Settings, | ||
} from "llamaindex"; | ||
``` | ||
|
||
## Load and index documents | ||
|
||
For this example, we will use a single document. In a real-world scenario, you would have multiple documents to index. | ||
|
||
```ts | ||
const document = new Document({ text: essay, id_: "essay" }); | ||
|
||
Settings.llm = new OpenAI({ model: "gpt-3.5-turbo", temperature: 0.1 }); | ||
|
||
const index = await VectorStoreIndex.fromDocuments([document]); | ||
``` | ||
|
||
## Increase similarity topK to retrieve more results | ||
|
||
The default value for `similarityTopK` is 2. This means that only the most similar document will be returned. To retrieve more results, you can increase the value of `similarityTopK`. | ||
|
||
```ts | ||
const retriever = index.asRetriever(); | ||
retriever.similarityTopK = 5; | ||
``` | ||
|
||
## Create a new instance of the JinaAIReranker class | ||
|
||
Then you can create a new instance of the `JinaAIReranker` class and pass in the number of results you want to return. | ||
The Jina AI Reranker API key is set in the `JINAAI_API_KEY` environment variable. | ||
|
||
```bash | ||
export JINAAI_API_KEY=<YOUR API KEY> | ||
``` | ||
|
||
```ts | ||
const nodePostprocessor = new JinaAIReranker({ | ||
topN: 5, | ||
}); | ||
``` | ||
|
||
## Create a query engine with the retriever and node postprocessor | ||
|
||
```ts | ||
const queryEngine = index.asQueryEngine({ | ||
retriever, | ||
nodePostprocessors: [nodePostprocessor], | ||
}); | ||
|
||
// log the response | ||
const response = await queryEngine.query("Where did the author grown up?"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { getEnv } from "@llamaindex/env"; | ||
import { OpenAIEmbedding } from "./OpenAIEmbedding.js"; | ||
|
||
export class JinaAIEmbedding extends OpenAIEmbedding { | ||
constructor(init?: Partial<OpenAIEmbedding>) { | ||
const { | ||
apiKey = getEnv("JINAAI_API_KEY"), | ||
additionalSessionOptions = {}, | ||
model = "jina-embeddings-v2-base-en", | ||
...rest | ||
} = init ?? {}; | ||
|
||
if (!apiKey) { | ||
throw new Error( | ||
"Set Jina AI API Key in JINAAI_API_KEY env variable. Get one for free or top up your key at https://jina.ai/embeddings", | ||
); | ||
} | ||
|
||
additionalSessionOptions.baseURL = | ||
additionalSessionOptions.baseURL ?? "https://api.jina.ai/v1"; | ||
|
||
super({ | ||
apiKey, | ||
additionalSessionOptions, | ||
model, | ||
...rest, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.