feat: Add factory convenience factory for each LLM provider, e.g. you… (#1731)

This commit is contained in:
Marcus Schiesser
2025-03-11 13:42:09 +07:00
committed by GitHub
parent e66c6e25fb
commit aea550aff4
30 changed files with 171 additions and 52 deletions
+20
View File
@@ -0,0 +1,20 @@
---
"@llamaindex/huggingface": patch
"@llamaindex/portkey-ai": patch
"@llamaindex/anthropic": patch
"@llamaindex/deepinfra": patch
"@llamaindex/fireworks": patch
"@llamaindex/replicate": patch
"@llamaindex/deepseek": patch
"@llamaindex/together": patch
"@llamaindex/mistral": patch
"@llamaindex/google": patch
"@llamaindex/ollama": patch
"@llamaindex/openai": patch
"@llamaindex/vercel": patch
"@llamaindex/groq": patch
"@llamaindex/vllm": patch
"@llamaindex/examples": patch
---
Add factory convenience factory for each LLM provider, e.g. you can use openai instead of 'new OpenAI'
+1 -1
View File
@@ -4,4 +4,4 @@
"@llamaindex/core": patch
---
Add factory methods agent and multiAgent to simplify agent usage
Add factory methods tool, agent and multiAgent to simplify agent usage
+2 -2
View File
@@ -1,4 +1,4 @@
import { OpenAI } from "@llamaindex/openai";
import { openai } from "@llamaindex/openai";
import fs from "fs";
import {
agent,
@@ -11,7 +11,7 @@ import os from "os";
import { z } from "zod";
import { WikipediaTool } from "../wiki";
const llm = new OpenAI({
const llm = openai({
model: "gpt-4o-mini",
});
+2 -2
View File
@@ -3,7 +3,7 @@
* 1. FetchWeatherAgent - Fetches the weather in a city
* 2. TemperatureConverterAgent - Converts the temperature from Fahrenheit to Celsius
*/
import { OpenAI } from "@llamaindex/openai";
import { openai } from "@llamaindex/openai";
import {
agent,
AgentInput,
@@ -17,7 +17,7 @@ import {
} from "llamaindex";
import { z } from "zod";
const llm = new OpenAI({
const llm = openai({
model: "gpt-4o-mini",
});
+2 -2
View File
@@ -1,11 +1,11 @@
/**
* This example shows how to use AgentWorkflow as a single agent with tools
*/
import { OpenAI } from "@llamaindex/openai";
import { openai } from "@llamaindex/openai";
import { Settings, agent } from "llamaindex";
import { getWeatherTool } from "../agent/utils/tools";
Settings.llm = new OpenAI({
Settings.llm = openai({
model: "gpt-4o",
});
+5 -5
View File
@@ -8,11 +8,7 @@ import {
} from "llamaindex";
import { z } from "zod";
import { Anthropic } from "@llamaindex/anthropic";
const llm = new Anthropic({
model: "claude-3-5-sonnet",
});
import { anthropic } from "@llamaindex/anthropic";
const weatherTool = tool({
name: "weather",
@@ -57,6 +53,10 @@ const saveFileTool = tool({
});
async function main() {
const llm = anthropic({
model: "claude-3-5-sonnet",
});
const reportAgent = agent({
name: "ReportAgent",
description:
+2 -14
View File
@@ -1,14 +1,2 @@
export {
AnthropicAgent,
AnthropicAgentWorker,
type AnthropicAgentParams,
} from "./agent";
export {
ALL_AVAILABLE_ANTHROPIC_LEGACY_MODELS,
ALL_AVAILABLE_ANTHROPIC_MODELS,
ALL_AVAILABLE_V3_5_MODELS,
ALL_AVAILABLE_V3_MODELS,
Anthropic,
AnthropicSession,
type AnthropicAdditionalChatOptions,
} from "./llm";
export * from "./agent";
export * from "./llm";
+9 -1
View File
@@ -60,7 +60,7 @@ const defaultAnthropicSession: {
* @param options
* @returns
*/
export function getAnthropicSession(options: ClientOptions = {}) {
function getAnthropicSession(options: ClientOptions = {}) {
let session = defaultAnthropicSession.find((session) => {
return isDeepEqual(session.options, options);
})?.session;
@@ -586,3 +586,11 @@ export class Anthropic extends ToolCallLLM<
};
}
}
/**
* Convenience function to create a new Anthropic instance.
* @param init - Optional initialization parameters for the Anthropic instance.
* @returns A new Anthropic instance.
*/
export const anthropic = (init?: ConstructorParameters<typeof Anthropic>[0]) =>
new Anthropic(init);
+1 -1
View File
@@ -3,4 +3,4 @@ export {
type DeepInfraEmbeddingResponse,
type InferenceStatus,
} from "./embedding";
export { DeepInfra } from "./llm";
export * from "./llm";
+8
View File
@@ -31,3 +31,11 @@ export class DeepInfra extends OpenAI {
});
}
}
/**
* Convenience function to create a new DeepInfra instance.
* @param init - Optional initialization parameters for the DeepInfra instance.
* @returns A new DeepInfra instance.
*/
export const deepinfra = (init?: ConstructorParameters<typeof DeepInfra>[0]) =>
new DeepInfra(init);
+8
View File
@@ -35,3 +35,11 @@ export class DeepSeekLLM extends OpenAI {
});
}
}
/**
* Convenience function to create a new DeepSeekLLM instance.
* @param init - Optional initialization parameters for the DeepSeekLLM instance.
* @returns A new DeepSeekLLM instance.
*/
export const deepseek = (init?: ConstructorParameters<typeof DeepSeekLLM>[0]) =>
new DeepSeekLLM(init);
+9
View File
@@ -26,3 +26,12 @@ export class FireworksLLM extends OpenAI {
});
}
}
/**
* Convenience function to create a new FireworksLLM instance.
* @param init - Optional initialization parameters for the FireworksLLM instance.
* @returns A new FireworksLLM instance.
*/
export const fireworks = (
init?: ConstructorParameters<typeof FireworksLLM>[0],
) => new FireworksLLM(init);
+8
View File
@@ -336,3 +336,11 @@ export class Gemini extends ToolCallLLM<GeminiAdditionalChatOptions> {
};
}
}
/**
* Convenience function to create a new Gemini instance.
* @param init - Optional initialization parameters for the Gemini instance.
* @returns A new Gemini instance.
*/
export const gemini = (init?: ConstructorParameters<typeof Gemini>[0]) =>
new Gemini(init);
+1 -1
View File
@@ -1 +1 @@
export { Groq } from "./llm";
export * from "./llm";
+8
View File
@@ -29,3 +29,11 @@ export class Groq extends OpenAI {
}) as never;
}
}
/**
* Convenience function to create a new Groq instance.
* @param init - Optional initialization parameters for the Groq instance.
* @returns A new Groq instance.
*/
export const groq = (init?: ConstructorParameters<typeof Groq>[0]) =>
new Groq(init);
+1 -1
View File
@@ -2,7 +2,7 @@ export {
HuggingFaceEmbedding,
type HuggingFaceEmbeddingParams,
} from "./embedding";
export { HuggingFaceLLM, type HFLLMConfig } from "./llm";
export * from "./llm";
export {
HuggingFaceEmbeddingModelType,
HuggingFaceInferenceAPI,
@@ -146,3 +146,12 @@ export class HuggingFaceLLM extends BaseLLM {
throw new Error("Method not implemented.");
}
}
/**
* Convenience function to create a new HuggingFaceLLM instance.
* @param init - Optional initialization parameters for the HuggingFaceLLM instance.
* @returns A new HuggingFaceLLM instance.
*/
export const huggingface = (
init?: ConstructorParameters<typeof HuggingFaceLLM>[0],
) => new HuggingFaceLLM(init);
+8
View File
@@ -136,3 +136,11 @@ export class MistralAI extends BaseLLM {
return;
}
}
/**
* Convenience function to create a new MistralAI instance.
* @param init - Optional initialization parameters for the MistralAI instance.
* @returns A new MistralAI instance.
*/
export const mistral = (init?: ConstructorParameters<typeof MistralAI>[0]) =>
new MistralAI(init);
+1 -1
View File
@@ -4,4 +4,4 @@ export {
type OllamaAgentParams,
} from "./agent";
export { OllamaEmbedding } from "./embedding";
export { Ollama, type OllamaParams } from "./llm";
export * from "./llm";
+8
View File
@@ -222,3 +222,11 @@ export class Ollama extends ToolCallLLM {
};
}
}
/**
* Convenience function to create a new Ollama instance.
* @param init - Optional initialization parameters for the Ollama instance.
* @returns A new Ollama instance.
*/
export const ollama = (init: ConstructorParameters<typeof Ollama>[0]) =>
new Ollama(init);
+3 -15
View File
@@ -1,15 +1,3 @@
export {
OpenAIAgent,
OpenAIAgentWorker,
type OpenAIAgentParams,
} from "./agent";
export { ALL_OPENAI_EMBEDDING_MODELS, OpenAIEmbedding } from "./embedding";
export {
ALL_AVAILABLE_OPENAI_MODELS,
GPT35_MODELS,
GPT4_MODELS,
O1_MODELS,
OpenAI,
type OpenAIAdditionalChatOptions,
type OpenAIAdditionalMetadata,
} from "./llm";
export * from "./agent";
export * from "./embedding";
export * from "./llm";
+11 -3
View File
@@ -142,7 +142,7 @@ export const ALL_AVAILABLE_OPENAI_MODELS = {
...O3_MODELS,
} satisfies Record<ChatModel, { contextWindow: number }>;
export function isFunctionCallingModel(llm: LLM): llm is OpenAI {
function isFunctionCallingModel(llm: LLM): llm is OpenAI {
let model: string;
if (llm instanceof OpenAI) {
model = llm.model;
@@ -157,13 +157,13 @@ export function isFunctionCallingModel(llm: LLM): llm is OpenAI {
return isChatModel && !isOld && !isO1;
}
export function isReasoningModel(model: ChatModel | string): boolean {
function isReasoningModel(model: ChatModel | string): boolean {
const isO1 = model.startsWith("o1");
const isO3 = model.startsWith("o3");
return isO1 || isO3;
}
export function isTemperatureSupported(model: ChatModel | string): boolean {
function isTemperatureSupported(model: ChatModel | string): boolean {
return !model.startsWith("o3");
}
@@ -540,3 +540,11 @@ export class OpenAI extends ToolCallLLM<OpenAIAdditionalChatOptions> {
};
}
}
/**
* Convenience function to create a new OpenAI instance.
* @param init - Optional initialization parameters for the OpenAI instance.
* @returns A new OpenAI instance.
*/
export const openai = (init?: ConstructorParameters<typeof OpenAI>[0]) =>
new OpenAI(init);
+1 -1
View File
@@ -1 +1 @@
export { Portkey, PortkeySession, getPortkeySession } from "./portkey";
export * from "./portkey";
@@ -133,3 +133,11 @@ export class Portkey extends BaseLLM {
return;
}
}
/**
* Convenience function to create a new Portkey instance.
* @param init - Optional initialization parameters for the Portkey instance.
* @returns A new Portkey instance.
*/
export const portkey = (init?: ConstructorParameters<typeof Portkey>[0]) =>
new Portkey(init);
+9
View File
@@ -380,3 +380,12 @@ If a question does not make any sense, or is not factually coherent, explain why
}
export const LlamaDeuce = ReplicateLLM;
/**
* Convenience function to create a new ReplicateLLM instance.
* @param init - Optional initialization parameters for the ReplicateLLM instance.
* @returns A new ReplicateLLM instance.
*/
export const replicate = (
init?: ConstructorParameters<typeof ReplicateLLM>[0],
) => new ReplicateLLM(init);
+8
View File
@@ -25,3 +25,11 @@ export class TogetherLLM extends OpenAI {
});
}
}
/**
* Convenience function to create a new TogetherLLM instance.
* @param init - Optional initialization parameters for the TogetherLLM instance.
* @returns A new TogetherLLM instance.
*/
export const together = (init?: ConstructorParameters<typeof TogetherLLM>[0]) =>
new TogetherLLM(init);
+1 -1
View File
@@ -1,2 +1,2 @@
export { VercelLLM } from "./llm";
export * from "./llm";
export { llamaindex } from "./tool";
+8
View File
@@ -181,3 +181,11 @@ export class VercelLLM extends ToolCallLLM<VercelAdditionalChatOptions> {
};
}
}
/**
* Convenience function to create a new VercelLLM instance.
* @param init - initialization parameters for the VercelLLM instance.
* @returns A new VercelLLM instance.
*/
export const vercel = (init: ConstructorParameters<typeof VercelLLM>[0]) =>
new VercelLLM(init);
+1 -1
View File
@@ -1 +1 @@
export { VLLM, type VLLMParams } from "./llm";
export * from "./llm";
+8
View File
@@ -23,3 +23,11 @@ export class VLLM extends OpenAI {
});
}
}
/**
* Convenience function to create a new VLLM instance.
* @param init - initialization parameters for the VLLM instance.
* @returns A new VLLM instance.
*/
export const vllm = (init: ConstructorParameters<typeof VLLM>[0]) =>
new VLLM(init);