Skip to content

Commit

Permalink
feat: enable verbatimModuleSyntax (run-llama#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Feb 24, 2024
1 parent beb3e5c commit 3a6e287
Show file tree
Hide file tree
Showing 139 changed files with 527 additions and 491 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-bulldogs-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---

build: improve tree-shake & reduce unused package import
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ module.exports = {
"max-params": ["error", 4],
"prefer-const": "error",
},
ignorePatterns: ["dist/"],
ignorePatterns: ["dist/", "lib/"],
};
9 changes: 3 additions & 6 deletions packages/core/src/ChatHistory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { OpenAI } from "./llm/LLM.js";
import { ChatMessage, LLM, MessageType } from "./llm/types.js";
import {
defaultSummaryPrompt,
messagesToHistoryStr,
SummaryPrompt,
} from "./Prompt.js";
import type { ChatMessage, LLM, MessageType } from "./llm/types.js";
import type { SummaryPrompt } from "./Prompt.js";
import { defaultSummaryPrompt, messagesToHistoryStr } from "./Prompt.js";

/**
* A ChatHistory is used to keep the state of back and forth chat messages
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/GlobalsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { encodingForModel } from "js-tiktoken";

import { randomUUID } from "@llamaindex/env";
import { Event, EventTag, EventType } from "./callbacks/CallbackManager.js";
import type {
Event,
EventTag,
EventType,
} from "./callbacks/CallbackManager.js";

export enum Tokenizers {
CL100K_BASE = "cl100k_base",
Expand Down Expand Up @@ -32,7 +36,7 @@ class GlobalsHelper {
};
}

tokenizer(encoding?: string) {
tokenizer(encoding?: Tokenizers) {
if (encoding && encoding !== Tokenizers.CL100K_BASE) {
throw new Error(`Tokenizer encoding ${encoding} not yet supported`);
}
Expand All @@ -43,7 +47,7 @@ class GlobalsHelper {
return this.defaultTokenizer!.encode.bind(this.defaultTokenizer);
}

tokenizerDecoder(encoding?: string) {
tokenizerDecoder(encoding?: Tokenizers) {
if (encoding && encoding !== Tokenizers.CL100K_BASE) {
throw new Error(`Tokenizer encoding ${encoding} not yet supported`);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ export abstract class BaseNode<T extends Metadata = Metadata> {

abstract getContent(metadataMode: MetadataMode): string;
abstract getMetadataStr(metadataMode: MetadataMode): string;
abstract setContent(value: any): void;
// todo: set value as a generic type
abstract setContent(value: unknown): void;

get sourceNode(): RelatedNodeInfo<T> | undefined {
const relationship = this.relationships[NodeRelationship.SOURCE];
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/OutputParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SubQuestion } from "./engines/query/types.js";
import { BaseOutputParser, StructuredOutput } from "./types.js";
import type { SubQuestion } from "./engines/query/types.js";
import type { BaseOutputParser, StructuredOutput } from "./types.js";

/**
* Error class for output parsing. Due to the nature of LLMs, anytime we use LLM
Expand Down Expand Up @@ -44,8 +44,8 @@ export function parseJsonMarkdown(text: string) {
const left_square = text.indexOf("[");
const left_brace = text.indexOf("{");

var left: number;
var right: number;
let left: number;
let right: number;
if (left_square < left_brace && left_square != -1) {
left = left_square;
right = text.lastIndexOf("]");
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Prompt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SubQuestion } from "./engines/query/types.js";
import { ChatMessage } from "./llm/types.js";
import { ToolMetadata } from "./types.js";
import type { SubQuestion } from "./engines/query/types.js";
import type { ChatMessage } from "./llm/types.js";
import type { ToolMetadata } from "./types.js";

/**
* A SimplePrompt is a function that takes a dictionary of inputs and returns a string.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/PromptHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { globalsHelper } from "./GlobalsHelper.js";
import { SimplePrompt } from "./Prompt.js";
import type { SimplePrompt } from "./Prompt.js";
import { SentenceSplitter } from "./TextSplitter.js";
import {
DEFAULT_CHUNK_OVERLAP_RATIO,
Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/QuestionGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { SubQuestionOutputParser } from "./OutputParser.js";
import {
SubQuestionPrompt,
buildToolsText,
defaultSubQuestionPrompt,
} from "./Prompt.js";
import { BaseQuestionGenerator, SubQuestion } from "./engines/query/types.js";
import type { SubQuestionPrompt } from "./Prompt.js";
import { buildToolsText, defaultSubQuestionPrompt } from "./Prompt.js";
import type {
BaseQuestionGenerator,
SubQuestion,
} from "./engines/query/types.js";
import { OpenAI } from "./llm/LLM.js";
import { LLM } from "./llm/types.js";
import type { LLM } from "./llm/types.js";
import { PromptMixin } from "./prompts/index.js";
import { BaseOutputParser, StructuredOutput, ToolMetadata } from "./types.js";
import type {
BaseOutputParser,
StructuredOutput,
ToolMetadata,
} from "./types.js";

/**
* LLMQuestionGenerator uses the LLM to generate new questions for the LLM using tools and a user query.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseNode } from "./Node.js";
import type { BaseNode } from "./Node.js";

/**
* Response is the output of a LLM
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Retriever.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Event } from "./callbacks/CallbackManager.js";
import { NodeWithScore } from "./Node.js";
import { ServiceContext } from "./ServiceContext.js";
import type { Event } from "./callbacks/CallbackManager.js";
import type { NodeWithScore } from "./Node.js";
import type { ServiceContext } from "./ServiceContext.js";

/**
* Retrievers retrieve the nodes that most closely match our query in similarity.
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/ServiceContext.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { PromptHelper } from "./PromptHelper.js";
import { CallbackManager } from "./callbacks/CallbackManager.js";
import { OpenAIEmbedding } from "./embeddings/OpenAIEmbedding.js";
import { BaseEmbedding } from "./embeddings/types.js";
import { LLM, OpenAI } from "./llm/index.js";
import type { BaseEmbedding } from "./embeddings/types.js";
import type { LLM } from "./llm/index.js";
import { OpenAI } from "./llm/index.js";
import { SimpleNodeParser } from "./nodeParsers/SimpleNodeParser.js";
import { NodeParser } from "./nodeParsers/types.js";
import type { NodeParser } from "./nodeParsers/types.js";

/**
* The ServiceContext is a collection of components that are used in different parts of the application.
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/agent/openai/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { CallbackManager } from "../../callbacks/CallbackManager.js";
import { ChatMessage, OpenAI } from "../../llm/index.js";
import { ObjectRetriever } from "../../objects/base.js";
import { BaseTool } from "../../types.js";
import type { CallbackManager } from "../../callbacks/CallbackManager.js";
import type { ChatMessage } from "../../llm/index.js";
import { OpenAI } from "../../llm/index.js";
import type { ObjectRetriever } from "../../objects/base.js";
import type { BaseTool } from "../../types.js";
import { AgentRunner } from "../runner/base.js";
import { OpenAIAgentWorker } from "./worker.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/agent/openai/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ToolMetadata } from "../../types.js";
import type { ToolMetadata } from "../../types.js";

export type OpenAIFunction = {
type: "function";
Expand Down
17 changes: 9 additions & 8 deletions packages/core/src/agent/openai/worker.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
// Assuming that the necessary interfaces and classes (like BaseTool, OpenAI, ChatMessage, CallbackManager, etc.) are defined elsewhere

import { randomUUID } from "@llamaindex/env";
import { CallbackManager } from "../../callbacks/CallbackManager.js";
import type { CallbackManager } from "../../callbacks/CallbackManager.js";
import {
AgentChatResponse,
ChatResponseMode,
} from "../../engines/chat/types.js";
import {
import type {
ChatMessage,
ChatResponse,
ChatResponseChunk,
OpenAI,
} from "../../llm/index.js";
import { OpenAI } from "../../llm/index.js";
import { ChatMemoryBuffer } from "../../memory/ChatMemoryBuffer.js";
import { ObjectRetriever } from "../../objects/base.js";
import { ToolOutput } from "../../tools/types.js";
import type { ObjectRetriever } from "../../objects/base.js";
import type { ToolOutput } from "../../tools/types.js";
import { callToolWithErrorHandling } from "../../tools/utils.js";
import { BaseTool } from "../../types.js";
import { AgentWorker, Task, TaskStep, TaskStepOutput } from "../types.js";
import type { BaseTool } from "../../types.js";
import type { AgentWorker, Task } from "../types.js";
import { TaskStep, TaskStepOutput } from "../types.js";
import { addUserStepToMemory, getFunctionByName } from "../utils.js";
import { OpenAIToolCall } from "./types/chat.js";
import type { OpenAIToolCall } from "./types/chat.js";
import { toOpenAiTool } from "./utils.js";

const DEFAULT_MAX_FUNCTION_CALLS = 5;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/agent/react/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CallbackManager } from "../../callbacks/CallbackManager.js";
import { ChatMessage, LLM } from "../../llm/index.js";
import { ObjectRetriever } from "../../objects/base.js";
import { BaseTool } from "../../types.js";
import type { CallbackManager } from "../../callbacks/CallbackManager.js";
import type { ChatMessage, LLM } from "../../llm/index.js";
import type { ObjectRetriever } from "../../objects/base.js";
import type { BaseTool } from "../../types.js";
import { AgentRunner } from "../runner/base.js";
import { ReActAgentWorker } from "./worker.js";

Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/agent/react/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ChatMessage } from "../../llm/index.js";
import { BaseTool } from "../../types.js";
import type { ChatMessage } from "../../llm/index.js";
import type { BaseTool } from "../../types.js";
import { getReactChatSystemHeader } from "./prompts.js";
import { BaseReasoningStep, ObservationReasoningStep } from "./types.js";
import type { BaseReasoningStep } from "./types.js";
import { ObservationReasoningStep } from "./types.js";

function getReactToolDescriptions(tools: BaseTool[]): string[] {
const toolDescs: string[] = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/agent/react/outputParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BaseReasoningStep } from "./types.js";
import {
ActionReasoningStep,
BaseOutputParser,
BaseReasoningStep,
ResponseReasoningStep,
} from "./types.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/agent/react/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChatMessage } from "../../llm/index.js";
import type { ChatMessage } from "../../llm/index.js";

export interface BaseReasoningStep {
getContent(): string;
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/agent/react/worker.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { randomUUID } from "crypto";
import { CallbackManager } from "../../callbacks/CallbackManager.js";
import { AgentChatResponse } from "../../engines/chat/index.js";
import { ChatResponse, LLM, OpenAI } from "../../llm/index.js";
import type { ChatResponse, LLM } from "../../llm/index.js";
import { OpenAI } from "../../llm/index.js";
import { ChatMemoryBuffer } from "../../memory/ChatMemoryBuffer.js";
import { ObjectRetriever } from "../../objects/base.js";
import type { ObjectRetriever } from "../../objects/base.js";
import { ToolOutput } from "../../tools/index.js";
import { BaseTool } from "../../types.js";
import { AgentWorker, Task, TaskStep, TaskStepOutput } from "../types.js";
import type { BaseTool } from "../../types.js";
import type { AgentWorker, Task } from "../types.js";
import { TaskStep, TaskStepOutput } from "../types.js";
import { ReActChatFormatter } from "./formatter.js";
import { ReActOutputParser } from "./outputParser.js";
import type { BaseReasoningStep } from "./types.js";
import {
ActionReasoningStep,
BaseReasoningStep,
ObservationReasoningStep,
ResponseReasoningStep,
} from "./types.js";
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/agent/runner/base.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { randomUUID } from "crypto";
import { CallbackManager } from "../../callbacks/CallbackManager.js";
import type { ChatEngineAgentParams } from "../../engines/chat/index.js";
import {
AgentChatResponse,
ChatEngineAgentParams,
ChatResponseMode,
} from "../../engines/chat/index.js";
import { ChatMessage, LLM } from "../../llm/index.js";
import type { ChatMessage, LLM } from "../../llm/index.js";
import { ChatMemoryBuffer } from "../../memory/ChatMemoryBuffer.js";
import { BaseMemory } from "../../memory/types.js";
import { AgentWorker, Task, TaskStep, TaskStepOutput } from "../types.js";
import type { BaseMemory } from "../../memory/types.js";
import type { AgentWorker, TaskStepOutput } from "../types.js";
import { Task, TaskStep } from "../types.js";
import { AgentState, BaseAgentRunner, TaskState } from "./types.js";

const validateStepFromArgs = (
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/agent/runner/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AgentChatResponse } from "../../engines/chat/index.js";
import { BaseAgent, Task, TaskStep, TaskStepOutput } from "../types.js";
import type { AgentChatResponse } from "../../engines/chat/index.js";
import type { Task, TaskStep, TaskStepOutput } from "../types.js";
import { BaseAgent } from "../types.js";

export class TaskState {
task!: Task;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agent/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
import type {
AgentChatResponse,
ChatEngineAgentParams,
} from "../engines/chat/index.js";
import { QueryEngineParamsNonStreaming } from "../types.js";
import type { QueryEngineParamsNonStreaming } from "../types.js";

export interface AgentWorker {
initializeStep(task: Task, kwargs?: any): TaskStep;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/agent/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChatMessage } from "../llm/index.js";
import { ChatMemoryBuffer } from "../memory/ChatMemoryBuffer.js";
import { BaseTool } from "../types.js";
import { TaskStep } from "./types.js";
import type { ChatMessage } from "../llm/index.js";
import type { ChatMemoryBuffer } from "../memory/ChatMemoryBuffer.js";
import type { BaseTool } from "../types.js";
import type { TaskStep } from "./types.js";

/**
* Adds the user's input to the memory.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/callbacks/CallbackManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Anthropic } from "@anthropic-ai/sdk";
import { NodeWithScore } from "../Node.js";
import type { NodeWithScore } from "../Node.js";

/*
An event is a wrapper that groups related operations.
Expand Down
13 changes: 7 additions & 6 deletions packages/core/src/cloud/LlamaCloudIndex.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BaseRetriever } from "../Retriever.js";
import type { BaseRetriever } from "../Retriever.js";
import { RetrieverQueryEngine } from "../engines/query/RetrieverQueryEngine.js";
import { BaseNodePostprocessor } from "../postprocessors/types.js";
import { BaseSynthesizer } from "../synthesizers/types.js";
import { BaseQueryEngine } from "../types.js";
import { LlamaCloudRetriever, RetrieveParams } from "./LlamaCloudRetriever.js";
import { CloudConstructorParams } from "./types.js";
import type { BaseNodePostprocessor } from "../postprocessors/types.js";
import type { BaseSynthesizer } from "../synthesizers/types.js";
import type { BaseQueryEngine } from "../types.js";
import type { RetrieveParams } from "./LlamaCloudRetriever.js";
import { LlamaCloudRetriever } from "./LlamaCloudRetriever.js";
import type { CloudConstructorParams } from "./types.js";

export class LlamaCloudIndex {
params: CloudConstructorParams;
Expand Down
22 changes: 9 additions & 13 deletions packages/core/src/cloud/LlamaCloudRetriever.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { PlatformApi, PlatformApiClient } from "@llamaindex/cloud";
import type { PlatformApi, PlatformApiClient } from "@llamaindex/cloud";
import { globalsHelper } from "../GlobalsHelper.js";
import { NodeWithScore, ObjectType, jsonToNode } from "../Node.js";
import { BaseRetriever } from "../Retriever.js";
import {
ServiceContext,
serviceContextFromDefaults,
} from "../ServiceContext.js";
import { Event } from "../callbacks/CallbackManager.js";
import {
ClientParams,
CloudConstructorParams,
DEFAULT_PROJECT_NAME,
} from "./types.js";
import type { NodeWithScore } from "../Node.js";
import { ObjectType, jsonToNode } from "../Node.js";
import type { BaseRetriever } from "../Retriever.js";
import type { ServiceContext } from "../ServiceContext.js";
import { serviceContextFromDefaults } from "../ServiceContext.js";
import type { Event } from "../callbacks/CallbackManager.js";
import type { ClientParams, CloudConstructorParams } from "./types.js";
import { DEFAULT_PROJECT_NAME } from "./types.js";
import { getClient } from "./utils.js";

export type RetrieveParams = Omit<
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/cloud/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ServiceContext } from "../ServiceContext.js";
import type { ServiceContext } from "../ServiceContext.js";

export const DEFAULT_PROJECT_NAME = "default";
export const DEFAULT_BASE_URL = "https://api.cloud.llamaindex.ai";
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/cloud/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PlatformApiClient } from "@llamaindex/cloud";
import { ClientParams, DEFAULT_BASE_URL } from "./types.js";
import type { PlatformApiClient } from "@llamaindex/cloud";
import type { ClientParams } from "./types.js";
import { DEFAULT_BASE_URL } from "./types.js";

export async function getClient({
apiKey,
Expand Down
Loading

0 comments on commit 3a6e287

Please sign in to comment.