mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-05 12:05:56 -04:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5cb270d07f | |||
| 62771058aa | |||
| ca348a6570 | |||
| 44a7fd72e8 | |||
| d8d952d937 |
@@ -0,0 +1,37 @@
|
||||
name: Publish to GitHub Releases
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "llamaindex@*"
|
||||
|
||||
jobs:
|
||||
build-and-publish:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Repo
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- uses: pnpm/action-setup@v3
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version-file: ".nvmrc"
|
||||
cache: "pnpm"
|
||||
|
||||
- name: Install dependencies
|
||||
run: pnpm install
|
||||
|
||||
- name: Build tarball
|
||||
run: |
|
||||
pnpm pack
|
||||
working-directory: packages/core
|
||||
|
||||
- name: Create release
|
||||
uses: ncipollo/release-action@v1
|
||||
with:
|
||||
artifacts: "packages/core/llamaindex-*.tgz"
|
||||
name: Release ${{ github.ref }}
|
||||
bodyFile: "packages/core/CHANGELOG.md"
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
@@ -1,5 +1,19 @@
|
||||
# docs
|
||||
|
||||
## 0.0.7
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6277105]
|
||||
- llamaindex@0.2.13
|
||||
|
||||
## 0.0.6
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [d8d952d]
|
||||
- llamaindex@0.2.12
|
||||
|
||||
## 0.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
```
|
||||
@@ -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);
|
||||
}
|
||||
```
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "docs",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.7",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"docusaurus": "docusaurus",
|
||||
|
||||
@@ -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);
|
||||
@@ -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,3 +1,4 @@
|
||||
.turbo
|
||||
/README.md
|
||||
LICENSE
|
||||
LICENSE
|
||||
*.tgz
|
||||
@@ -1,5 +1,17 @@
|
||||
# llamaindex
|
||||
|
||||
## 0.2.13
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6277105: fix: allow passing empty tools to llms
|
||||
|
||||
## 0.2.12
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- d8d952d: feat: add gemini llm and embedding
|
||||
|
||||
## 0.2.11
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "llamaindex",
|
||||
"version": "0.2.11",
|
||||
"version": "0.2.13",
|
||||
"expectedMinorVersion": "2",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
@@ -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:*",
|
||||
|
||||
@@ -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,4 +1,5 @@
|
||||
export * from "./ClipEmbedding.js";
|
||||
export * from "./GeminiEmbedding.js";
|
||||
export * from "./HuggingFaceEmbedding.js";
|
||||
export * from "./JinaAIEmbedding.js";
|
||||
export * from "./MistralAIEmbedding.js";
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ClientOptions } from "@anthropic-ai/sdk";
|
||||
import { Anthropic as SDKAnthropic } from "@anthropic-ai/sdk";
|
||||
import type {
|
||||
MessageCreateParamsNonStreaming,
|
||||
Tool,
|
||||
ToolResultBlockParam,
|
||||
ToolUseBlock,
|
||||
@@ -264,7 +265,7 @@ export class Anthropic extends ToolCallLLM<AnthropicAdditionalChatOptions> {
|
||||
const anthropic = this.session.anthropic;
|
||||
|
||||
if (tools) {
|
||||
const response = await anthropic.beta.tools.messages.create({
|
||||
const params: MessageCreateParamsNonStreaming = {
|
||||
messages: this.formatMessages<true>(messages),
|
||||
tools: tools.map(Anthropic.toTool),
|
||||
model: this.getModelName(this.model),
|
||||
@@ -272,7 +273,12 @@ export class Anthropic extends ToolCallLLM<AnthropicAdditionalChatOptions> {
|
||||
max_tokens: this.maxTokens ?? 4096,
|
||||
top_p: this.topP,
|
||||
...(systemPrompt && { system: systemPrompt }),
|
||||
});
|
||||
};
|
||||
// Remove tools if there are none, as it will cause an error
|
||||
if (tools.length === 0) {
|
||||
delete params.tools;
|
||||
}
|
||||
const response = await anthropic.beta.tools.messages.create(params);
|
||||
|
||||
const toolUseBlock = response.content.find(
|
||||
(content): content is ToolUseBlock => content.type === "tool_use",
|
||||
|
||||
@@ -0,0 +1,363 @@
|
||||
import {
|
||||
ChatSession,
|
||||
GoogleGenerativeAI,
|
||||
type Content as GeminiMessageContent,
|
||||
type Part,
|
||||
} from "@google/generative-ai";
|
||||
import { getEnv } from "@llamaindex/env";
|
||||
import { ToolCallLLM } from "./base.js";
|
||||
import type {
|
||||
ChatMessage,
|
||||
ChatResponse,
|
||||
ChatResponseChunk,
|
||||
CompletionResponse,
|
||||
LLMChatParamsNonStreaming,
|
||||
LLMChatParamsStreaming,
|
||||
LLMCompletionParamsNonStreaming,
|
||||
LLMCompletionParamsStreaming,
|
||||
LLMMetadata,
|
||||
MessageContent,
|
||||
MessageContentImageDetail,
|
||||
MessageContentTextDetail,
|
||||
MessageType,
|
||||
ToolCallLLMMessageOptions,
|
||||
} from "./types.js";
|
||||
import { streamConverter, wrapLLMEvent } from "./utils.js";
|
||||
|
||||
// Session and Model Type Definitions
|
||||
type GeminiSessionOptions = {
|
||||
apiKey?: string;
|
||||
};
|
||||
|
||||
export enum GEMINI_MODEL {
|
||||
GEMINI_PRO = "gemini-pro",
|
||||
GEMINI_PRO_VISION = "gemini-pro-vision",
|
||||
EMBEDDING_001 = "embedding-001",
|
||||
AQA = "aqa",
|
||||
GEMINI_PRO_LATEST = "gemini-1.5-pro-latest",
|
||||
}
|
||||
|
||||
export interface GeminiModelInfo {
|
||||
contextWindow: number;
|
||||
}
|
||||
|
||||
export const GEMINI_MODEL_INFO_MAP: Record<GEMINI_MODEL, GeminiModelInfo> = {
|
||||
[GEMINI_MODEL.GEMINI_PRO]: { contextWindow: 30720 },
|
||||
[GEMINI_MODEL.GEMINI_PRO_VISION]: { contextWindow: 12288 },
|
||||
[GEMINI_MODEL.EMBEDDING_001]: { contextWindow: 2048 },
|
||||
[GEMINI_MODEL.AQA]: { contextWindow: 7168 },
|
||||
[GEMINI_MODEL.GEMINI_PRO_LATEST]: { contextWindow: 10 ** 6 },
|
||||
};
|
||||
|
||||
const SUPPORT_TOOL_CALL_MODELS: GEMINI_MODEL[] = [
|
||||
GEMINI_MODEL.GEMINI_PRO,
|
||||
GEMINI_MODEL.GEMINI_PRO_VISION,
|
||||
GEMINI_MODEL.EMBEDDING_001,
|
||||
GEMINI_MODEL.AQA,
|
||||
];
|
||||
|
||||
const DEFAULT_GEMINI_PARAMS = {
|
||||
model: GEMINI_MODEL.GEMINI_PRO,
|
||||
temperature: 0.1,
|
||||
topP: 1,
|
||||
maxTokens: undefined,
|
||||
};
|
||||
|
||||
export type GeminiConfig = Partial<typeof DEFAULT_GEMINI_PARAMS> & {
|
||||
session?: GeminiSession;
|
||||
};
|
||||
|
||||
/// Chat Type Definitions
|
||||
type GeminiMessageRole = "user" | "model";
|
||||
|
||||
export type GeminiAdditionalChatOptions = {};
|
||||
|
||||
export type GeminiChatParamsStreaming = LLMChatParamsStreaming<
|
||||
GeminiAdditionalChatOptions,
|
||||
ToolCallLLMMessageOptions
|
||||
>;
|
||||
|
||||
export type GeminiChatStreamResponse = AsyncIterable<
|
||||
ChatResponseChunk<ToolCallLLMMessageOptions>
|
||||
>;
|
||||
|
||||
export type GeminiChatParamsNonStreaming = LLMChatParamsNonStreaming<
|
||||
GeminiAdditionalChatOptions,
|
||||
ToolCallLLMMessageOptions
|
||||
>;
|
||||
|
||||
export type GeminiChatNonStreamResponse =
|
||||
ChatResponse<ToolCallLLMMessageOptions>;
|
||||
|
||||
/**
|
||||
* Gemini Session to manage the connection to the Gemini API
|
||||
*/
|
||||
export class GeminiSession {
|
||||
gemini: GoogleGenerativeAI;
|
||||
|
||||
constructor(options: GeminiSessionOptions) {
|
||||
if (!options.apiKey) {
|
||||
options.apiKey = getEnv("GOOGLE_API_KEY");
|
||||
}
|
||||
if (!options.apiKey) {
|
||||
throw new Error("Set Google API Key in GOOGLE_API_KEY env variable");
|
||||
}
|
||||
this.gemini = new GoogleGenerativeAI(options.apiKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gemini Session Store to manage the current Gemini sessions
|
||||
*/
|
||||
export class GeminiSessionStore {
|
||||
static sessions: Array<{
|
||||
session: GeminiSession;
|
||||
options: GeminiSessionOptions;
|
||||
}> = [];
|
||||
|
||||
private static sessionMatched(
|
||||
o1: GeminiSessionOptions,
|
||||
o2: GeminiSessionOptions,
|
||||
): boolean {
|
||||
return o1.apiKey === o2.apiKey;
|
||||
}
|
||||
|
||||
static get(options: GeminiSessionOptions = {}): GeminiSession {
|
||||
let session = this.sessions.find((session) =>
|
||||
this.sessionMatched(session.options, options),
|
||||
)?.session;
|
||||
if (!session) {
|
||||
session = new GeminiSession(options);
|
||||
this.sessions.push({ session, options });
|
||||
}
|
||||
return session;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper class providing utility functions for Gemini
|
||||
*/
|
||||
class GeminiHelper {
|
||||
// Gemini only has user and model roles. Put the rest in user role.
|
||||
public static readonly ROLES_TO_GEMINI: Record<
|
||||
MessageType,
|
||||
GeminiMessageRole
|
||||
> = {
|
||||
user: "user",
|
||||
system: "user",
|
||||
assistant: "user",
|
||||
memory: "user",
|
||||
};
|
||||
|
||||
public static readonly ROLES_FROM_GEMINI: Record<
|
||||
GeminiMessageRole,
|
||||
MessageType
|
||||
> = {
|
||||
user: "user",
|
||||
model: "assistant",
|
||||
};
|
||||
|
||||
public static mergeNeighboringSameRoleMessages(
|
||||
messages: ChatMessage[],
|
||||
): ChatMessage[] {
|
||||
// Gemini does not support multiple messages of the same role in a row, so we merge them
|
||||
const mergedMessages: ChatMessage[] = [];
|
||||
let i: number = 0;
|
||||
|
||||
while (i < messages.length) {
|
||||
const currentMessage: ChatMessage = messages[i];
|
||||
// Initialize merged content with current message content
|
||||
const mergedContent: MessageContent[] = [currentMessage.content];
|
||||
|
||||
// Check if the next message exists and has the same role
|
||||
while (
|
||||
i + 1 < messages.length &&
|
||||
this.ROLES_TO_GEMINI[messages[i + 1].role] ===
|
||||
this.ROLES_TO_GEMINI[currentMessage.role]
|
||||
) {
|
||||
i++;
|
||||
const nextMessage: ChatMessage = messages[i];
|
||||
mergedContent.push(nextMessage.content);
|
||||
}
|
||||
|
||||
// Create a new ChatMessage object with merged content
|
||||
const mergedMessage: ChatMessage = {
|
||||
role: currentMessage.role,
|
||||
content: mergedContent.join("\n"),
|
||||
};
|
||||
mergedMessages.push(mergedMessage);
|
||||
i++;
|
||||
}
|
||||
|
||||
return mergedMessages;
|
||||
}
|
||||
|
||||
public static messageContentToGeminiParts(content: MessageContent): Part[] {
|
||||
if (typeof content === "string") {
|
||||
return [{ text: content }];
|
||||
}
|
||||
|
||||
const parts: Part[] = [];
|
||||
const imageContents = content.filter(
|
||||
(i) => i.type === "image_url",
|
||||
) as MessageContentImageDetail[];
|
||||
parts.push(
|
||||
...imageContents.map((i) => ({
|
||||
fileData: {
|
||||
mimeType: i.type,
|
||||
fileUri: i.image_url.url,
|
||||
},
|
||||
})),
|
||||
);
|
||||
const textContents = content.filter(
|
||||
(i) => i.type === "text",
|
||||
) as MessageContentTextDetail[];
|
||||
parts.push(...textContents.map((t) => ({ text: t.text })));
|
||||
return parts;
|
||||
}
|
||||
|
||||
public static chatMessageToGemini(
|
||||
message: ChatMessage,
|
||||
): GeminiMessageContent {
|
||||
return {
|
||||
role: this.ROLES_TO_GEMINI[message.role],
|
||||
parts: this.messageContentToGeminiParts(message.content),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ToolCallLLM for Gemini
|
||||
*/
|
||||
export class Gemini extends ToolCallLLM<GeminiAdditionalChatOptions> {
|
||||
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();
|
||||
}
|
||||
|
||||
get supportToolCall(): boolean {
|
||||
return SUPPORT_TOOL_CALL_MODELS.includes(this.model);
|
||||
}
|
||||
|
||||
get metadata(): LLMMetadata {
|
||||
return {
|
||||
model: this.model,
|
||||
temperature: this.temperature,
|
||||
topP: this.topP,
|
||||
maxTokens: this.maxTokens,
|
||||
contextWindow: GEMINI_MODEL_INFO_MAP[this.model].contextWindow,
|
||||
tokenizer: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
private prepareChat(
|
||||
params: GeminiChatParamsStreaming | GeminiChatParamsNonStreaming,
|
||||
): {
|
||||
chat: ChatSession;
|
||||
messageContent: Part[];
|
||||
} {
|
||||
const { messages } = params;
|
||||
const mergedMessages =
|
||||
GeminiHelper.mergeNeighboringSameRoleMessages(messages);
|
||||
const history = mergedMessages.slice(0, -1);
|
||||
const nextMessage = mergedMessages[mergedMessages.length - 1];
|
||||
const messageContent = GeminiHelper.chatMessageToGemini(nextMessage).parts;
|
||||
|
||||
const client = this.session.gemini.getGenerativeModel(this.metadata);
|
||||
|
||||
const chat = client.startChat({
|
||||
history: history.map(GeminiHelper.chatMessageToGemini),
|
||||
});
|
||||
|
||||
return {
|
||||
chat,
|
||||
messageContent,
|
||||
};
|
||||
}
|
||||
|
||||
protected async nonStreamChat(
|
||||
params: GeminiChatParamsNonStreaming,
|
||||
): Promise<GeminiChatNonStreamResponse> {
|
||||
const { chat, messageContent } = this.prepareChat(params);
|
||||
const result = await chat.sendMessage(messageContent);
|
||||
const { response } = result;
|
||||
const topCandidate = response.candidates![0];
|
||||
return {
|
||||
raw: response,
|
||||
message: {
|
||||
content: response.text(),
|
||||
role: GeminiHelper.ROLES_FROM_GEMINI[
|
||||
topCandidate.content.role as GeminiMessageRole
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
protected async *streamChat(
|
||||
params: GeminiChatParamsStreaming,
|
||||
): GeminiChatStreamResponse {
|
||||
const { chat, messageContent } = this.prepareChat(params);
|
||||
const result = await chat.sendMessageStream(messageContent);
|
||||
return streamConverter(result.stream, (response) => {
|
||||
return {
|
||||
text: response.text(),
|
||||
raw: response,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
chat(params: GeminiChatParamsStreaming): Promise<GeminiChatStreamResponse>;
|
||||
chat(
|
||||
params: GeminiChatParamsNonStreaming,
|
||||
): Promise<GeminiChatNonStreamResponse>;
|
||||
@wrapLLMEvent
|
||||
async chat(
|
||||
params: GeminiChatParamsStreaming | GeminiChatParamsNonStreaming,
|
||||
): Promise<GeminiChatStreamResponse | GeminiChatNonStreamResponse> {
|
||||
if (params.stream) return this.streamChat(params);
|
||||
return this.nonStreamChat(params);
|
||||
}
|
||||
|
||||
complete(
|
||||
params: LLMCompletionParamsStreaming,
|
||||
): Promise<AsyncIterable<CompletionResponse>>;
|
||||
complete(
|
||||
params: LLMCompletionParamsNonStreaming,
|
||||
): Promise<CompletionResponse>;
|
||||
async complete(
|
||||
params: LLMCompletionParamsStreaming | LLMCompletionParamsNonStreaming,
|
||||
): Promise<CompletionResponse | AsyncIterable<CompletionResponse>> {
|
||||
const { prompt, stream } = params;
|
||||
const client = this.session.gemini.getGenerativeModel(this.metadata);
|
||||
|
||||
if (stream) {
|
||||
const result = await client.generateContentStream(
|
||||
GeminiHelper.messageContentToGeminiParts(prompt),
|
||||
);
|
||||
return streamConverter(result.stream, (response) => {
|
||||
return {
|
||||
text: response.text(),
|
||||
raw: response,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const result = await client.generateContent(
|
||||
GeminiHelper.messageContentToGeminiParts(prompt),
|
||||
);
|
||||
return {
|
||||
text: result.response.text(),
|
||||
raw: result.response,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ export * from "./openai.js";
|
||||
export { Portkey } from "./portkey.js";
|
||||
export * from "./replicate_ai.js";
|
||||
// Note: The type aliases for replicate are to simplify usage for Llama 2 (we're using replicate for Llama 2 support)
|
||||
export { GEMINI_MODEL, Gemini } from "./gemini.js";
|
||||
export {
|
||||
DeuceChatStrategy,
|
||||
LlamaDeuce,
|
||||
|
||||
@@ -349,6 +349,14 @@ export class OpenAI extends ToolCallLLM<OpenAIAdditionalChatOptions> {
|
||||
...Object.assign({}, this.additionalChatOptions, additionalChatOptions),
|
||||
};
|
||||
|
||||
if (
|
||||
Array.isArray(baseRequestParams.tools) &&
|
||||
baseRequestParams.tools.length === 0
|
||||
) {
|
||||
// remove empty tools array to avoid OpenAI error
|
||||
delete baseRequestParams.tools;
|
||||
}
|
||||
|
||||
// Streaming
|
||||
if (stream) {
|
||||
return this.streamChat(baseRequestParams);
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# test-edge-runtime
|
||||
|
||||
## 0.1.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6277105]
|
||||
- @llamaindex/edge@0.2.13
|
||||
|
||||
## 0.1.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "test-edge-runtime",
|
||||
"version": "0.1.3",
|
||||
"version": "0.1.4",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"name": "@llamaindex/edge",
|
||||
"version": "0.2.11",
|
||||
"version": "0.2.13",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@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:*",
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
# @llamaindex/experimental
|
||||
|
||||
## 0.0.16
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6277105]
|
||||
- llamaindex@0.2.13
|
||||
|
||||
## 0.0.15
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [d8d952d]
|
||||
- llamaindex@0.2.12
|
||||
|
||||
## 0.0.14
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/experimental",
|
||||
"description": "Experimental package for LlamaIndexTS",
|
||||
"version": "0.0.14",
|
||||
"version": "0.0.16",
|
||||
"type": "module",
|
||||
"types": "dist/type/index.d.ts",
|
||||
"main": "dist/cjs/index.js",
|
||||
|
||||
Generated
+996
-1071
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user