Skip to content

Commit

Permalink
feat: init gemini llm (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn authored Apr 26, 2024
1 parent 216ba1f commit d8d952d
Show file tree
Hide file tree
Showing 12 changed files with 8,212 additions and 10,639 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-ears-begin.md
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 apps/docs/docs/modules/embeddings/available_embeddings/gemini.md
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,
});
```
71 changes: 71 additions & 0 deletions apps/docs/docs/modules/llms/available_llms/gemini.md
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);
}
```
15 changes: 15 additions & 0 deletions examples/gemini.ts
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);
21 changes: 21 additions & 0 deletions examples/gemini/chat.ts
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);
})();
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@anthropic-ai/sdk": "^0.20.6",
"@aws-crypto/sha256-js": "^5.2.0",
"@datastax/astra-db-ts": "^1.0.1",
"@google/generative-ai": "^0.8.0",
"@grpc/grpc-js": "^1.10.6",
"@llamaindex/cloud": "0.0.5",
"@llamaindex/env": "workspace:*",
Expand Down
43 changes: 43 additions & 0 deletions packages/core/src/embeddings/GeminiEmbedding.ts
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);
}
}
1 change: 1 addition & 0 deletions packages/core/src/embeddings/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./ClipEmbedding.js";
export * from "./GeminiEmbedding.js";
export * from "./HuggingFaceEmbedding.js";
export * from "./JinaAIEmbedding.js";
export * from "./MistralAIEmbedding.js";
Expand Down
Loading

0 comments on commit d8d952d

Please sign in to comment.