-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
8,212 additions
and
10,639 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: add gemini llm and embedding |
33 changes: 33 additions & 0 deletions
33
apps/docs/docs/modules/embeddings/available_embeddings/gemini.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,33 @@ | ||
# Gemini | ||
|
||
To use Gemini embeddings, you need to import `GeminiEmbedding` from `llamaindex`. | ||
|
||
```ts | ||
import { GeminiEmbedding, Settings } from "llamaindex"; | ||
|
||
// Update Embed Model | ||
Settings.embedModel = new GeminiEmbedding(); | ||
|
||
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, | ||
}); | ||
``` | ||
|
||
Per default, `GeminiEmbedding` is using the `gemini-pro` model. You can change the model by passing the `model` parameter to the constructor. | ||
For example: | ||
|
||
```ts | ||
import { GEMINI_MODEL, GeminiEmbedding } from "llamaindex"; | ||
|
||
Settings.embedModel = new GeminiEmbedding({ | ||
model: GEMINI_MODEL.GEMINI_PRO_LATEST, | ||
}); | ||
``` |
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 @@ | ||
# Gemini | ||
|
||
## Usage | ||
|
||
```ts | ||
import { Gemini, Settings, GEMINI_MODEL } from "llamaindex"; | ||
|
||
Settings.llm = new Gemini({ | ||
model: GEMINI_MODEL.GEMINI_PRO, | ||
}); | ||
``` | ||
|
||
## 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" }); | ||
|
||
const index = await VectorStoreIndex.fromDocuments([document]); | ||
``` | ||
|
||
## Query | ||
|
||
```ts | ||
const queryEngine = index.asQueryEngine(); | ||
|
||
const query = "What is the meaning of life?"; | ||
|
||
const results = await queryEngine.query({ | ||
query, | ||
}); | ||
``` | ||
|
||
## Full Example | ||
|
||
```ts | ||
import { | ||
Gemini, | ||
Document, | ||
VectorStoreIndex, | ||
Settings, | ||
GEMINI_MODEL, | ||
} from "llamaindex"; | ||
|
||
Settings.llm = new Gemini({ | ||
model: GEMINI_MODEL.GEMINI_PRO, | ||
}); | ||
|
||
async function main() { | ||
const document = new Document({ text: essay, id_: "essay" }); | ||
|
||
// Load and index documents | ||
const index = await VectorStoreIndex.fromDocuments([document]); | ||
|
||
// Create a query engine | ||
const queryEngine = index.asQueryEngine({ | ||
retriever, | ||
}); | ||
|
||
const query = "What is the meaning of life?"; | ||
|
||
// Query | ||
const response = await queryEngine.query({ | ||
query, | ||
}); | ||
|
||
// Log the response | ||
console.log(response.response); | ||
} | ||
``` |
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,15 @@ | ||
import { GEMINI_MODEL, GeminiEmbedding } from "llamaindex"; | ||
|
||
async function main() { | ||
if (!process.env.GOOGLE_API_KEY) { | ||
throw new Error("Please set the GOOGLE_API_KEY environment variable."); | ||
} | ||
const embedModel = new GeminiEmbedding({ | ||
model: GEMINI_MODEL.GEMINI_PRO, | ||
}); | ||
const texts = ["hello", "world"]; | ||
const embeddings = await embedModel.getTextEmbeddingsBatch(texts); | ||
console.log(`\nWe have ${embeddings.length} embeddings`); | ||
} | ||
|
||
main().catch(console.error); |
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 @@ | ||
import { Gemini, GEMINI_MODEL } from "llamaindex"; | ||
|
||
(async () => { | ||
if (!process.env.GOOGLE_API_KEY) { | ||
throw new Error("Please set the GOOGLE_API_KEY environment variable."); | ||
} | ||
const gemini = new Gemini({ | ||
model: GEMINI_MODEL.GEMINI_PRO, | ||
}); | ||
const result = await gemini.chat({ | ||
messages: [ | ||
{ content: "You want to talk in rhymes.", role: "system" }, | ||
{ | ||
content: | ||
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?", | ||
role: "user", | ||
}, | ||
], | ||
}); | ||
console.log(result); | ||
})(); |
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,43 @@ | ||
import { | ||
GEMINI_MODEL, | ||
GeminiSessionStore, | ||
type GeminiConfig, | ||
type GeminiSession, | ||
} from "../llm/gemini.js"; | ||
import { BaseEmbedding } from "./types.js"; | ||
|
||
/** | ||
* GeminiEmbedding is an alias for Gemini that implements the BaseEmbedding interface. | ||
*/ | ||
export class GeminiEmbedding extends BaseEmbedding { | ||
model: GEMINI_MODEL; | ||
temperature: number; | ||
topP: number; | ||
maxTokens?: number; | ||
session: GeminiSession; | ||
|
||
constructor(init?: GeminiConfig) { | ||
super(); | ||
this.model = init?.model ?? GEMINI_MODEL.GEMINI_PRO; | ||
this.temperature = init?.temperature ?? 0.1; | ||
this.topP = init?.topP ?? 1; | ||
this.maxTokens = init?.maxTokens ?? undefined; | ||
this.session = init?.session ?? GeminiSessionStore.get(); | ||
} | ||
|
||
private async getEmbedding(prompt: string): Promise<number[]> { | ||
const client = this.session.gemini.getGenerativeModel({ | ||
model: this.model, | ||
}); | ||
const result = await client.embedContent(prompt); | ||
return result.embedding.values; | ||
} | ||
|
||
getTextEmbedding(text: string): Promise<number[]> { | ||
return this.getEmbedding(text); | ||
} | ||
|
||
getQueryEmbedding(query: string): Promise<number[]> { | ||
return this.getTextEmbedding(query); | ||
} | ||
} |
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.