Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 209d4cddd1 | |||
| 04c99208f1 |
@@ -84,16 +84,9 @@ jobs:
|
||||
name: typecheck-build-dist
|
||||
path: ./packages/core/dist
|
||||
if-no-files-found: error
|
||||
e2e-core-examples:
|
||||
strategy:
|
||||
matrix:
|
||||
packages:
|
||||
- cloudflare-worker-agent
|
||||
- nextjs-agent
|
||||
- nextjs-edge-runtime
|
||||
- waku-query-engine
|
||||
core-edge-runtime:
|
||||
runs-on: ubuntu-latest
|
||||
name: Build Core Example (${{ matrix.packages }})
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: pnpm/action-setup@v3
|
||||
@@ -104,12 +97,11 @@ jobs:
|
||||
cache: "pnpm"
|
||||
- name: Install dependencies
|
||||
run: pnpm install
|
||||
- name: Build llamaindex
|
||||
run: pnpm run build --filter llamaindex
|
||||
- name: Build ${{ matrix.packages }}
|
||||
- name: Build
|
||||
run: pnpm run build --filter @llamaindex/edge
|
||||
- name: Build Edge Runtime
|
||||
run: pnpm run build
|
||||
working-directory: packages/core/e2e/examples/${{ matrix.packages }}
|
||||
|
||||
working-directory: ./packages/edge/e2e/test-edge-runtime
|
||||
typecheck-examples:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
|
||||
@@ -19,29 +19,25 @@ Try examples online:
|
||||
|
||||
LlamaIndex.TS aims to be a lightweight, easy to use set of libraries to help you integrate large language models into your applications with your own data.
|
||||
|
||||
## Multiple JS Environment Support
|
||||
## Getting started with an example:
|
||||
|
||||
LlamaIndex.TS supports multiple JS environments, including:
|
||||
LlamaIndex.TS requires Node v18 or higher. You can download it from https://nodejs.org or use https://nvm.sh (our preferred option).
|
||||
|
||||
- Node.js (18, 20, 22) ✅
|
||||
- Deno ✅
|
||||
- Bun ✅
|
||||
- React Server Components (Next.js) ✅
|
||||
In a new folder:
|
||||
|
||||
For now, browser support is limited due to the lack of support for [AsyncLocalStorage-like APIs](https://github.com/tc39/proposal-async-context)
|
||||
|
||||
## Getting started
|
||||
|
||||
```shell
|
||||
npm install llamaindex
|
||||
```bash
|
||||
export OPENAI_API_KEY="sk-......" # Replace with your key from https://platform.openai.com/account/api-keys
|
||||
pnpm init
|
||||
pnpm install typescript
|
||||
pnpm exec tsc --init # if needed
|
||||
pnpm install llamaindex
|
||||
yarn add llamaindex
|
||||
jsr install @llamaindex/core
|
||||
pnpm install @types/node
|
||||
```
|
||||
|
||||
### Node.js
|
||||
Create the file example.ts
|
||||
|
||||
```ts
|
||||
// example.ts
|
||||
import fs from "fs/promises";
|
||||
import { Document, VectorStoreIndex } from "llamaindex";
|
||||
|
||||
@@ -71,110 +67,10 @@ async function main() {
|
||||
main();
|
||||
```
|
||||
|
||||
Then you can run it using
|
||||
|
||||
```bash
|
||||
# `pnpm install tsx` before running the script
|
||||
node --import tsx ./main.ts
|
||||
```
|
||||
|
||||
### Next.js
|
||||
|
||||
You can combine `ai` with `llamaindex` in Next.js with RSC (React Server Components).
|
||||
|
||||
```tsx
|
||||
// src/apps/page.tsx
|
||||
"use client";
|
||||
import { chatWithAgent } from "@/actions";
|
||||
import type { JSX } from "react";
|
||||
import { useFormState } from "react-dom";
|
||||
|
||||
// You can use the Edge runtime in Next.js by adding this line:
|
||||
// export const runtime = "edge";
|
||||
|
||||
export default function Home() {
|
||||
const [ui, action] = useFormState<JSX.Element | null>(async () => {
|
||||
return chatWithAgent("hello!", []);
|
||||
}, null);
|
||||
return (
|
||||
<main>
|
||||
{ui}
|
||||
<form action={action}>
|
||||
<button>Chat</button>
|
||||
</form>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
```tsx
|
||||
// src/actions/index.ts
|
||||
"use server";
|
||||
import { createStreamableUI } from "ai/rsc";
|
||||
import { OpenAIAgent } from "llamaindex";
|
||||
import type { ChatMessage } from "llamaindex/llm/types";
|
||||
|
||||
export async function chatWithAgent(
|
||||
question: string,
|
||||
prevMessages: ChatMessage[] = [],
|
||||
) {
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [
|
||||
// ... adding your tools here
|
||||
],
|
||||
});
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: question,
|
||||
chatHistory: prevMessages,
|
||||
});
|
||||
const uiStream = createStreamableUI(<div>loading...</div>);
|
||||
responseStream
|
||||
.pipeTo(
|
||||
new WritableStream({
|
||||
start: () => {
|
||||
uiStream.update("response:");
|
||||
},
|
||||
write: async (message) => {
|
||||
uiStream.append(message.response.delta);
|
||||
},
|
||||
}),
|
||||
)
|
||||
.catch(console.error);
|
||||
return uiStream.value;
|
||||
}
|
||||
```
|
||||
|
||||
### Cloudflare Workers
|
||||
|
||||
```ts
|
||||
// src/index.ts
|
||||
export default {
|
||||
async fetch(
|
||||
request: Request,
|
||||
env: Env,
|
||||
ctx: ExecutionContext,
|
||||
): Promise<Response> {
|
||||
const { setEnvs } = await import("@llamaindex/env");
|
||||
// set environment variables so that the OpenAIAgent can use them
|
||||
setEnvs(env);
|
||||
const { OpenAIAgent } = await import("llamaindex");
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [],
|
||||
});
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: "Hello? What is the weather today?",
|
||||
});
|
||||
const textEncoder = new TextEncoder();
|
||||
const response = responseStream.pipeThrough(
|
||||
new TransformStream({
|
||||
transform: (chunk, controller) => {
|
||||
controller.enqueue(textEncoder.encode(chunk.response.delta));
|
||||
},
|
||||
}),
|
||||
);
|
||||
return new Response(response);
|
||||
},
|
||||
};
|
||||
pnpm dlx ts-node example.ts
|
||||
```
|
||||
|
||||
## Playground
|
||||
@@ -197,25 +93,79 @@ Check out our NextJS playground at https://llama-playground.vercel.app/. The sou
|
||||
|
||||
- [SimplePrompt](/packages/core/src/Prompt.ts): A simple standardized function call definition that takes in inputs and formats them in a template literal. SimplePrompts can be specialized using currying and combined using other SimplePrompt functions.
|
||||
|
||||
## Tips when using in non-Node.js environments
|
||||
## Using NextJS
|
||||
|
||||
When you are importing `llamaindex` in a non-Node.js environment(such as React Server Components, Cloudflare Workers, etc.)
|
||||
Some classes are not exported from top-level entry file.
|
||||
If you're using the NextJS App Router, you can choose between the Node.js and the [Edge runtime](https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes#edge-runtime).
|
||||
|
||||
The reason is that some classes are only compatible with Node.js runtime,(e.g. `PDFReader`) which uses Node.js specific APIs(like `fs`, `child_process`, `crypto`).
|
||||
|
||||
If you need any of those classes, you have to import them instead directly though their file path in the package.
|
||||
Here's an example for importing the `PineconeVectorStore` class:
|
||||
With NextJS 13 and 14, using the Node.js runtime is the default. You can explicitly set the Edge runtime in your [router handler](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) by adding this line:
|
||||
|
||||
```typescript
|
||||
import { PineconeVectorStore } from "llamaindex/storage/vectorStore/PineconeVectorStore";
|
||||
export const runtime = "edge";
|
||||
```
|
||||
|
||||
The following sections explain further differences in using the Node.js or Edge runtime.
|
||||
|
||||
### Using the Node.js runtime
|
||||
|
||||
Add the following config to your `next.config.js` to ignore specific packages in the server-side bundling:
|
||||
|
||||
```js
|
||||
// next.config.js
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {
|
||||
experimental: {
|
||||
serverComponentsExternalPackages: [
|
||||
"pdf2json",
|
||||
"@zilliz/milvus2-sdk-node",
|
||||
"sharp",
|
||||
"onnxruntime-node",
|
||||
],
|
||||
},
|
||||
webpack: (config) => {
|
||||
config.externals.push({
|
||||
pdf2json: "commonjs pdf2json",
|
||||
"@zilliz/milvus2-sdk-node": "commonjs @zilliz/milvus2-sdk-node",
|
||||
sharp: "commonjs sharp",
|
||||
"onnxruntime-node": "commonjs onnxruntime-node",
|
||||
});
|
||||
|
||||
return config;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = nextConfig;
|
||||
```
|
||||
|
||||
### Using the Edge runtime
|
||||
|
||||
We publish a dedicated package (`@llamaindex/edge` instead of `llamaindex`) for using the Edge runtime. To use it, first install the package:
|
||||
|
||||
```shell
|
||||
pnpm install @llamaindex/edge
|
||||
```
|
||||
|
||||
> _Note_: Ensure that your `package.json` doesn't include the `llamaindex` package if you're using `@llamaindex/edge`.
|
||||
|
||||
Then make sure to use the correct import statement in your code:
|
||||
|
||||
```typescript
|
||||
// replace 'llamaindex' with '@llamaindex/edge'
|
||||
import {} from "@llamaindex/edge";
|
||||
```
|
||||
|
||||
A further difference is that the `@llamaindex/edge` package doesn't export classes from the `readers` or `storage` folders. The reason is that most of these classes are not compatible with the Edge runtime.
|
||||
|
||||
If you need any of those classes, you have to import them instead directly. Here's an example for importing the `PineconeVectorStore` class:
|
||||
|
||||
```typescript
|
||||
import { PineconeVectorStore } from "@llamaindex/edge/storage/vectorStore/PineconeVectorStore";
|
||||
```
|
||||
|
||||
As the `PDFReader` is not working with the Edge runtime, here's how to use the `SimpleDirectoryReader` with the `LlamaParseReader` to load PDFs:
|
||||
|
||||
```typescript
|
||||
import { SimpleDirectoryReader } from "llamaindex/readers/SimpleDirectoryReader";
|
||||
import { LlamaParseReader } from "llamaindex/readers/LlamaParseReader";
|
||||
import { SimpleDirectoryReader } from "@llamaindex/edge/readers/SimpleDirectoryReader";
|
||||
import { LlamaParseReader } from "@llamaindex/edge/readers/LlamaParseReader";
|
||||
|
||||
export const DATA_DIR = "./data";
|
||||
|
||||
@@ -233,7 +183,7 @@ export async function getDocuments() {
|
||||
|
||||
> _Note_: Reader classes have to be added explictly to the `fileExtToReader` map in the Edge version of the `SimpleDirectoryReader`.
|
||||
|
||||
You'll find a complete example with LlamaIndexTS here: https://github.com/run-llama/create_llama_projects/tree/main/nextjs-edge-llamaparse
|
||||
You'll find a complete example of using the Edge runtime with LlamaIndexTS here: https://github.com/run-llama/create_llama_projects/tree/main/nextjs-edge-llamaparse
|
||||
|
||||
## Supported LLMs:
|
||||
|
||||
|
||||
@@ -1,45 +1,5 @@
|
||||
# docs
|
||||
|
||||
## 0.0.12
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [1dce275]
|
||||
- Updated dependencies [d10533e]
|
||||
- Updated dependencies [2008efe]
|
||||
- Updated dependencies [5e61934]
|
||||
- Updated dependencies [9e74a43]
|
||||
- Updated dependencies [ee719a1]
|
||||
- llamaindex@0.3.4
|
||||
|
||||
## 0.0.11
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [e8c41c5]
|
||||
- llamaindex@0.3.3
|
||||
|
||||
## 0.0.10
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [61103b6]
|
||||
- llamaindex@0.3.2
|
||||
|
||||
## 0.0.9
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [46227f2]
|
||||
- llamaindex@0.3.1
|
||||
|
||||
## 0.0.8
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [5016f21]
|
||||
- llamaindex@0.3.0
|
||||
|
||||
## 0.0.7
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,493 +0,0 @@
|
||||
---
|
||||
title: LlamaIndexTS v0.3.0
|
||||
description: This is my first post on Docusaurus.
|
||||
slug: welcome-llamaindexts-v0.3
|
||||
authors:
|
||||
- name: Alex Yang
|
||||
title: LlamaIndexTS maintainer, Node.js Member
|
||||
url: https://github.com/himself65
|
||||
image_url: https://github.com/himself65.png
|
||||
tags: [llamaindex, agent]
|
||||
hide_table_of_contents: false
|
||||
---
|
||||
|
||||
- [What's new in LlamaIndexTS v0.3.0](#whats-new-in-llamaindexts-v030)
|
||||
- [Improvement in LlamaIndexTS v0.3.0](#improvement-in-llamaindexts-v030)
|
||||
- [What's the next?](#whats-the-next)
|
||||
|
||||
## What's new in LlamaIndexTS v0.3.0
|
||||
|
||||
## Agents
|
||||
|
||||
In this release, we've not only ported the Agent module from the LlamaIndex Python version but have significantly
|
||||
enhanced it to be more powerful and user-friendly for JavaScript/TypeScript applications.
|
||||
|
||||
Starting from v0.3.0, we are introducing multiple agents specifically designed for RAG applications, including:
|
||||
|
||||
- `OpenAIAgent`
|
||||
- `AnthropicAgent`
|
||||
- `ReActAgent`:
|
||||
|
||||
```ts
|
||||
import { OpenAIAgent } from "llamaindex";
|
||||
import { tools } from "./tools";
|
||||
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [...tools],
|
||||
});
|
||||
const { response } = await agent.chat({
|
||||
message: "What is weather today?",
|
||||
stream: false,
|
||||
});
|
||||
|
||||
console.log(response.message.content);
|
||||
```
|
||||
|
||||
We are also introducing the abstract AgentRunner class, which allows you to create your own agent by simply implementing
|
||||
the task handler.
|
||||
|
||||
```ts
|
||||
import { AgentRunner, OpenAI } from "llamaindex";
|
||||
|
||||
class MyLLM extends OpenAI {}
|
||||
|
||||
export class MyAgentWorker extends AgentWorker<MyLLM> {
|
||||
taskHandler = MyAgent.taskHandler;
|
||||
}
|
||||
|
||||
export class MyAgent extends AgentRunner<MyLLM> {
|
||||
constructor(params: Params) {
|
||||
super({
|
||||
llm: params.llm,
|
||||
chatHistory: params.chatHistory ?? [],
|
||||
systemPrompt: params.systemPrompt ?? null,
|
||||
runner: new MyAgentWorker(),
|
||||
tools:
|
||||
"tools" in params
|
||||
? params.tools
|
||||
: params.toolRetriever.retrieve.bind(params.toolRetriever),
|
||||
});
|
||||
}
|
||||
|
||||
// create store is a function to create a store for each task, by default it only includes `messages` and `toolOutputs`
|
||||
createStore = AgentRunner.defaultCreateStore;
|
||||
|
||||
static taskHandler: TaskHandler<Anthropic> = async (step, enqueueOutput) => {
|
||||
const { llm, stream } = step.context;
|
||||
// initialize the input
|
||||
const response = await llm.chat({
|
||||
stream,
|
||||
messages: step.context.store.messages,
|
||||
});
|
||||
// store the response for next task step
|
||||
step.context.store.messages = [
|
||||
...step.context.store.messages,
|
||||
response.message,
|
||||
];
|
||||
// your logic here to decide whether to continue the task
|
||||
const shouldContinue = Math.random(); /* <-- replace with your logic here */
|
||||
enqueueOutput({
|
||||
taskStep: step,
|
||||
output: response,
|
||||
isLast: !shouldContinue,
|
||||
});
|
||||
if (shouldContinue) {
|
||||
const content = await someHeavyFunctionCall();
|
||||
// if you want to continue the task, you can insert your new context for the next task step
|
||||
step.context.store.messages = [
|
||||
...step.context.store.messages,
|
||||
{
|
||||
content,
|
||||
role: "user",
|
||||
},
|
||||
];
|
||||
}
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
### Web Stream API for Streaming response
|
||||
|
||||
Web Stream is a web standard utilized in many modern web frameworks and libraries (like React 19, Deno, Node 22). We
|
||||
have migrated streaming responses to Web Stream to ensure broader compatibility.
|
||||
|
||||
For instance, you can use the streaming response in a simple HTTP Server:
|
||||
|
||||
```ts
|
||||
import { createServer } from "http";
|
||||
import { OpenAIAgent } from "llamaindex";
|
||||
import { OpenAIStream, streamToResponse } from "ai";
|
||||
import { tools } from "./tools";
|
||||
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [...tools],
|
||||
});
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
const response = await agent.chat({
|
||||
message: "What is weather today?",
|
||||
stream: true,
|
||||
});
|
||||
|
||||
// Transform the response into a string readable stream
|
||||
const stream: ReadableStream<string> = response.pipeThrough(
|
||||
new TransformStream({
|
||||
transform: (chunk, controller) => {
|
||||
controller.enqueue(chunk.response.delta);
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
// Pipe the stream to the response
|
||||
streamToResponse(stream, res);
|
||||
});
|
||||
|
||||
server.listen(3000);
|
||||
```
|
||||
|
||||
Or it can be integrated into React Server Components (RSC) in Next.js:
|
||||
|
||||
```tsx
|
||||
// app/actions/index.tsx
|
||||
"use server";
|
||||
import { createStreamableUI } from "ai/rsc";
|
||||
import { OpenAIAgent } from "llamaindex";
|
||||
import type { ChatMessage } from "llamaindex/llm/types";
|
||||
|
||||
export async function chatWithAgent(
|
||||
question: string,
|
||||
prevMessages: ChatMessage[] = [],
|
||||
) {
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [],
|
||||
});
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: question,
|
||||
chatHistory: prevMessages,
|
||||
});
|
||||
const uiStream = createStreamableUI(<div>loading...</div>);
|
||||
responseStream
|
||||
.pipeTo(
|
||||
new WritableStream({
|
||||
start: () => {
|
||||
uiStream.update("response:");
|
||||
},
|
||||
write: async (message) => {
|
||||
uiStream.append(message.response.delta);
|
||||
},
|
||||
}),
|
||||
)
|
||||
.catch(uiStream.error);
|
||||
return uiStream.value;
|
||||
}
|
||||
```
|
||||
|
||||
```tsx
|
||||
// app/src/page.tsx
|
||||
"use client";
|
||||
import { chatWithAgent } from "@/actions";
|
||||
import type { JSX } from "react";
|
||||
import { useFormState } from "react-dom";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
||||
export default function Home() {
|
||||
const [state, action] = useFormState<JSX.Element | null>(async () => {
|
||||
return chatWithAgent("hello!", []);
|
||||
}, null);
|
||||
return (
|
||||
<main>
|
||||
{state}
|
||||
<form action={action}>
|
||||
<button>Chat</button>
|
||||
</form>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## Improvement in LlamaIndexTS v0.3.0
|
||||
|
||||
### Better TypeScript support
|
||||
|
||||
We have made significant improvements to the type system to ensure that all code is thoroughly checked before it is
|
||||
published. This ongoing enhancement has already resulted in better module reliability and developer experience.
|
||||
|
||||
For example, we have improved `FunctionTool` type with generic support:
|
||||
|
||||
```ts
|
||||
type Input = {
|
||||
a: number;
|
||||
b: number;
|
||||
};
|
||||
|
||||
const sumNumbers = FunctionTool.from<Input>(
|
||||
({ a, b }) => `${a + b}`, // a and b will be checked as number
|
||||
// JSON schema will be an error if you type wrong.
|
||||
{
|
||||
name: "sumNumbers",
|
||||
description: "Use this function to sum two numbers",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
a: {
|
||||
type: "number",
|
||||
description: "The first number",
|
||||
},
|
||||
b: {
|
||||
type: "number",
|
||||
description: "The second number",
|
||||
},
|
||||
},
|
||||
required: ["a", "b"],
|
||||
},
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||

|
||||
|
||||
### Better Next.js, Deno, Cloudflare Worker, and Waku(Vite) support
|
||||
|
||||
In addition to Node.js, LlamaIndexTS now offers enhanced support for Next.js, Deno, and Cloudflare Workers, making it
|
||||
more versatile across different platforms.
|
||||
|
||||
For now, you can install llamaindex and directly import it into your existing Next.js, Deno or Cloudflare Worker project
|
||||
**without any extra configuration**.
|
||||
|
||||
#### [Deno](https://deno.com/)
|
||||
|
||||
You can use LlamaIndexTS in Deno by installation through JSR:
|
||||
|
||||
```sh
|
||||
jsr add @llamaindex/core
|
||||
```
|
||||
|
||||
#### [Cloudflare Worker](https://developers.cloudflare.com/workers/)
|
||||
|
||||
For Cloudflare Workers, here is a starter template:
|
||||
|
||||
```typescript
|
||||
export default {
|
||||
async fetch(
|
||||
request: Request,
|
||||
env: Env,
|
||||
ctx: ExecutionContext,
|
||||
): Promise<Response> {
|
||||
const { setEnvs } = await import("@llamaindex/env");
|
||||
setEnvs(env);
|
||||
const { OpenAIAgent } = await import("llamaindex");
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [],
|
||||
});
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: "Hello? What is the weather today?",
|
||||
});
|
||||
const textEncoder = new TextEncoder();
|
||||
const response = responseStream.pipeThrough(
|
||||
new TransformStream({
|
||||
transform: (chunk, controller) => {
|
||||
controller.enqueue(textEncoder.encode(chunk.response.delta));
|
||||
},
|
||||
}),
|
||||
);
|
||||
return new Response(response);
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### [Waku (Vite)](https://waku.gg/)
|
||||
|
||||
Waku powered by Vite is a minimal React framework that supports multiple JS environments, including Deno, Cloudflare, and
|
||||
Node.js.
|
||||
|
||||
You can use LlamaIndexTS with Node.js output to enable full Node.js support with React.
|
||||
|
||||
```sh
|
||||
npm install llamaindex
|
||||
```
|
||||
|
||||
```ts
|
||||
// file: src/actions.ts
|
||||
"use server";
|
||||
import { Document, VectorStoreIndex } from "llamaindex";
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
const path = "node_modules/llamaindex/examples/abramov.txt";
|
||||
|
||||
const essay = await readFile(path, "utf-8");
|
||||
|
||||
// Create Document object with essay
|
||||
const document = new Document({ text: essay, id_: path });
|
||||
|
||||
// Split text and create embeddings. Store them in a VectorStoreIndex
|
||||
const index = await VectorStoreIndex.fromDocuments([document]);
|
||||
|
||||
const queryEngine = index.asQueryEngine();
|
||||
|
||||
export async function chatWithAI(question: string): Promise<string> {
|
||||
const { response } = await queryEngine.query({ query: question });
|
||||
return response;
|
||||
}
|
||||
```
|
||||
|
||||
```tsx
|
||||
// file: src/pages/index.tsx
|
||||
import { chatWithAI } from "./actions";
|
||||
|
||||
export default async function HomePage() {
|
||||
return (
|
||||
<div>
|
||||
<Chat askQuestion={chatWithAI} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
```tsx
|
||||
// file: src/components/Chat.tsx
|
||||
"use client";
|
||||
|
||||
export type ChatProps = {
|
||||
askQuestion: (question: string) => Promise<string>;
|
||||
};
|
||||
|
||||
export const Chat = (props: ChatProps) => {
|
||||
const [response, setResponse] = useState<string | null>(null);
|
||||
|
||||
return (
|
||||
<section className="border-blue-400 -mx-4 mt-4 rounded border border-dashed p-4">
|
||||
<h2 className="text-lg font-bold">Chat with AI</h2>
|
||||
{response ? (
|
||||
<p className="text-sm text-gray-600 max-w-sm">{response}</p>
|
||||
) : null}
|
||||
<form
|
||||
action={async (formData) => {
|
||||
const question = formData.get("question") as string | null;
|
||||
if (question) {
|
||||
setResponse(await props.askQuestion(question));
|
||||
}
|
||||
}}
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
name="question"
|
||||
className="border border-gray-400 rounded-sm px-2 py-0.5 text-sm"
|
||||
/>
|
||||
<button className="rounded-sm bg-black px-2 py-0.5 text-sm text-white">
|
||||
Ask
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
```shell
|
||||
waku dev # development mode
|
||||
waku build # build for production
|
||||
waku start # start the production server
|
||||
```
|
||||
|
||||
Note that not all the modules are supported in all JS environments because of
|
||||
lack of the file system, network API,
|
||||
and incompatibility with the Node.js API by upstream dependencies.
|
||||
|
||||
But we are trying to make it more compatible with all the environments.
|
||||
|
||||
## What's the next?
|
||||
|
||||
As we continue to develop LlamaIndexTS, our focus remains on providing more comprehensive and powerful tools for
|
||||
creating custom agents.
|
||||
|
||||
### Align with the Python `llama-index`
|
||||
|
||||
We aim to align LlamaIndexTS with the Python version to ensure API consistency and ease of use for developers familiar
|
||||
with the Python ecosystem.
|
||||
|
||||
### Align with the Web Standard and JS development
|
||||
|
||||
Not all python APIs are compatible and easy to use in JavaScript/TypeScript.
|
||||
We are trying to make the API more compatible with the Web Standard and JavaScript modern development.
|
||||
|
||||
### More Agents
|
||||
|
||||
Future releases will introduce more agents from the Python Llama-Index and explore APIs tailored to real-world use
|
||||
cases.
|
||||
|
||||
### 🧪 `@llamaindex/tool`
|
||||
|
||||
We are exploring innovative ways to create tools for agents. The `@llamaindex/tool` library allows you to transform any
|
||||
function into a tool for an agent, simplifying the development process and reducing runtime costs.
|
||||
|
||||
```ts
|
||||
export function getWeather(city: string) {
|
||||
return `The weather in ${city} is sunny.`;
|
||||
}
|
||||
|
||||
// you don't need to worry about the shcema with different llm tools
|
||||
export function getTemperature(city: string) {
|
||||
return `The temperature in ${city} is 25°C.`;
|
||||
}
|
||||
|
||||
export function getCurrentCity() {
|
||||
return "New York";
|
||||
}
|
||||
```
|
||||
|
||||
These functions can be easily integrated into your applications, such as Next.js:
|
||||
|
||||
```ts
|
||||
"use server";
|
||||
import { OpenAI } from "openai";
|
||||
import { getTools } from "@llamaindex/tool";
|
||||
|
||||
export async function chat(message: string) {
|
||||
const openai = new OpenAI();
|
||||
openai.chat.completions.create({
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "What is the weather in the current city?",
|
||||
},
|
||||
],
|
||||
tools: getTools("openai"),
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
```ts
|
||||
// next.config.js
|
||||
const withTool = require("@llamaindex/tool/next");
|
||||
|
||||
const config = {
|
||||
// Your original Next.js config
|
||||
};
|
||||
module.exports = withTool(config);
|
||||
```
|
||||
|
||||
The functions are automatically transformed into tools for the agent at compile time, which eliminates any extra runtime
|
||||
costs. This feature is particularly beneficial when you need to debug or deploy your assistant.
|
||||
|
||||
For deploying your local functions into OpenAI, you can use a simple command:
|
||||
|
||||
```sh
|
||||
npm install -g @llamaindex/tool
|
||||
mkai --tools ./src/index.llama.ts
|
||||
# Successfully created assistant: asst_XXX
|
||||
# chat with your assistant by `chatai --assistant asst_XXX`
|
||||
chatai --assistant asst_XXX
|
||||
# Open your browser and chat with your assistant
|
||||
# Running at http://localhost:3000
|
||||
```
|
||||
|
||||
This deployment process simplifies the testing and implementation of your custom tools in a live environment.
|
||||
|
||||
As this project is still in its early stages, we continue to explore the best ways to create and integrate tools for
|
||||
agents. For more information and updates, visit the @llamaindex/tool repository.
|
||||
|
||||
This release of LlamaIndexTS v0.3.0 marks a significant step forward in our journey to provide developers with robust,
|
||||
flexible tools for building advanced agents. We are excited to see how our community utilizes these new capabilities to
|
||||
create innovative solutions and look forward to continuing to support and enhance LlamaIndexTS in future updates.
|
||||
|
Before Width: | Height: | Size: 178 KiB |
@@ -66,11 +66,7 @@ const config = {
|
||||
[require("@docusaurus/remark-plugin-npm2yarn"), { sync: true }],
|
||||
],
|
||||
},
|
||||
blog: {
|
||||
blogTitle: "LlamaIndexTS blog",
|
||||
blogDescription: "The official blog of LlamaIndexTS",
|
||||
postsPerPage: "ALL",
|
||||
},
|
||||
blog: false,
|
||||
gtag: {
|
||||
trackingID: "G-NB9B8LW9W5",
|
||||
anonymizeIP: true,
|
||||
@@ -101,7 +97,6 @@ const config = {
|
||||
type: "localeDropdown",
|
||||
position: "left",
|
||||
},
|
||||
{ to: "blog", label: "Blog", position: "right" },
|
||||
{
|
||||
href: "https://github.com/run-llama/LlamaIndexTS",
|
||||
label: "GitHub",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "docs",
|
||||
"version": "0.0.12",
|
||||
"version": "0.0.7",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"docusaurus": "docusaurus",
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
DEBUG=llamaindex
|
||||
@@ -1,39 +0,0 @@
|
||||
import { ChatResponseChunk, OpenAIAgent } from "llamaindex";
|
||||
import { ReadableStream } from "node:stream/web";
|
||||
import {
|
||||
getCurrentIDTool,
|
||||
getUserInfoTool,
|
||||
getWeatherTool,
|
||||
} from "./utils/tools";
|
||||
|
||||
async function main() {
|
||||
// Create an OpenAIAgent with the function tools
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [getCurrentIDTool, getUserInfoTool, getWeatherTool],
|
||||
});
|
||||
|
||||
const task = await agent.createTask(
|
||||
"What is my current address weather based on my profile?",
|
||||
true,
|
||||
);
|
||||
|
||||
for await (const stepOutput of task) {
|
||||
const stream = stepOutput.output as ReadableStream<ChatResponseChunk>;
|
||||
if (stepOutput.isLast) {
|
||||
for await (const chunk of stream) {
|
||||
process.stdout.write(chunk.delta);
|
||||
}
|
||||
process.stdout.write("\n");
|
||||
} else {
|
||||
// handing function call
|
||||
console.log("handling function call...");
|
||||
for await (const chunk of stream) {
|
||||
console.log("debug:", JSON.stringify(chunk.raw));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main().then(() => {
|
||||
console.log("Done");
|
||||
});
|
||||
@@ -53,7 +53,7 @@ async function main() {
|
||||
message: "How much is 5 + 5? then divide by 2",
|
||||
});
|
||||
|
||||
console.log(response.response.message);
|
||||
console.log(String(response));
|
||||
}
|
||||
|
||||
void main().then(() => {
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import { ChatResponseChunk, ReActAgent } from "llamaindex";
|
||||
import { ReadableStream } from "node:stream/web";
|
||||
import {
|
||||
getCurrentIDTool,
|
||||
getUserInfoTool,
|
||||
getWeatherTool,
|
||||
} from "./utils/tools";
|
||||
|
||||
async function main() {
|
||||
// Create an OpenAIAgent with the function tools
|
||||
const agent = new ReActAgent({
|
||||
tools: [getCurrentIDTool, getUserInfoTool, getWeatherTool],
|
||||
});
|
||||
|
||||
const task = await agent.createTask(
|
||||
"What is my current address weather based on my profile?",
|
||||
true,
|
||||
);
|
||||
|
||||
for await (const stepOutput of task) {
|
||||
const stream = stepOutput.output as ReadableStream<ChatResponseChunk>;
|
||||
if (stepOutput.isLast) {
|
||||
for await (const chunk of stream) {
|
||||
process.stdout.write(chunk.delta);
|
||||
}
|
||||
process.stdout.write("\n");
|
||||
} else {
|
||||
// handing function call
|
||||
console.log("handling function call...");
|
||||
for await (const chunk of stream) {
|
||||
console.log("debug:", JSON.stringify(chunk.raw));
|
||||
}
|
||||
}
|
||||
console.log("---");
|
||||
}
|
||||
}
|
||||
|
||||
void main().then(() => {
|
||||
console.log("Done");
|
||||
});
|
||||
@@ -0,0 +1,97 @@
|
||||
import { FunctionTool, OpenAIAgent } from "llamaindex";
|
||||
import { ReadableStream } from "node:stream/web";
|
||||
|
||||
// Define a function to sum two numbers
|
||||
function sumNumbers({ a, b }: { a: number; b: number }) {
|
||||
return `${a + b}`;
|
||||
}
|
||||
|
||||
// Define a function to divide two numbers
|
||||
function divideNumbers({ a, b }: { a: number; b: number }) {
|
||||
return `${a / b}`;
|
||||
}
|
||||
|
||||
// Define the parameters of the sum function as a JSON schema
|
||||
const sumJSON = {
|
||||
type: "object",
|
||||
properties: {
|
||||
a: {
|
||||
type: "number",
|
||||
description: "The first number",
|
||||
},
|
||||
b: {
|
||||
type: "number",
|
||||
description: "The second number",
|
||||
},
|
||||
},
|
||||
required: ["a", "b"],
|
||||
} as const;
|
||||
|
||||
const divideJSON = {
|
||||
type: "object",
|
||||
properties: {
|
||||
a: {
|
||||
type: "number",
|
||||
description: "The dividend",
|
||||
},
|
||||
b: {
|
||||
type: "number",
|
||||
description: "The divisor",
|
||||
},
|
||||
},
|
||||
required: ["a", "b"],
|
||||
} as const;
|
||||
|
||||
async function main() {
|
||||
// Create a function tool from the sum function
|
||||
const functionTool = new FunctionTool(sumNumbers, {
|
||||
name: "sumNumbers",
|
||||
description: "Use this function to sum two numbers",
|
||||
parameters: sumJSON,
|
||||
});
|
||||
|
||||
// Create a function tool from the divide function
|
||||
const functionTool2 = new FunctionTool(divideNumbers, {
|
||||
name: "divideNumbers",
|
||||
description: "Use this function to divide two numbers",
|
||||
parameters: divideJSON,
|
||||
});
|
||||
|
||||
// Create an OpenAIAgent with the function tools
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [functionTool, functionTool2],
|
||||
});
|
||||
|
||||
// Create a task to sum and divide numbers
|
||||
const task = await agent.createTask("How much is 5 + 5? then divide by 2");
|
||||
|
||||
let count = 0;
|
||||
|
||||
for await (const stepOutput of task) {
|
||||
console.log(`Runnning step ${count++}`);
|
||||
console.log(`======== OUTPUT ==========`);
|
||||
const output = stepOutput.output;
|
||||
if (output instanceof ReadableStream) {
|
||||
for await (const chunk of output) {
|
||||
process.stdout.write(chunk.delta);
|
||||
}
|
||||
} else {
|
||||
console.log(output);
|
||||
}
|
||||
console.log(`==========================`);
|
||||
|
||||
if (stepOutput.isLast) {
|
||||
if (stepOutput.output instanceof ReadableStream) {
|
||||
for await (const chunk of stepOutput.output) {
|
||||
process.stdout.write(chunk.delta);
|
||||
}
|
||||
} else {
|
||||
console.log(stepOutput.output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main().then(() => {
|
||||
console.log("Done");
|
||||
});
|
||||
@@ -1,54 +0,0 @@
|
||||
import { FunctionTool } from "llamaindex";
|
||||
|
||||
export const getCurrentIDTool = FunctionTool.from(
|
||||
() => {
|
||||
console.log("Getting user id...");
|
||||
return crypto.randomUUID();
|
||||
},
|
||||
{
|
||||
name: "get_user_id",
|
||||
description: "Get a random user id",
|
||||
},
|
||||
);
|
||||
|
||||
export const getUserInfoTool = FunctionTool.from(
|
||||
({ userId }: { userId: string }) => {
|
||||
console.log("Getting user info...", userId);
|
||||
return `Name: Alex; Address: 1234 Main St, CA; User ID: ${userId}`;
|
||||
},
|
||||
{
|
||||
name: "get_user_info",
|
||||
description: "Get user info",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
userId: {
|
||||
type: "string",
|
||||
description: "The user id",
|
||||
},
|
||||
},
|
||||
required: ["userId"],
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
export const getWeatherTool = FunctionTool.from(
|
||||
({ address }: { address: string }) => {
|
||||
console.log("Getting weather...", address);
|
||||
return `${address} is in a sunny location!`;
|
||||
},
|
||||
{
|
||||
name: "get_weather",
|
||||
description: "Get the current weather for a location",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
address: {
|
||||
type: "string",
|
||||
description: "The address",
|
||||
},
|
||||
},
|
||||
required: ["address"],
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -1,11 +1,11 @@
|
||||
import { GEMINI_EMBEDDING_MODEL, GeminiEmbedding } from "llamaindex";
|
||||
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_EMBEDDING_MODEL.EMBEDDING_001,
|
||||
model: GEMINI_MODEL.GEMINI_PRO,
|
||||
});
|
||||
const texts = ["hello", "world"];
|
||||
const embeddings = await embedModel.getTextEmbeddingsBatch(texts);
|
||||
@@ -1,22 +0,0 @@
|
||||
import { HuggingFaceInferenceAPI } from "llamaindex";
|
||||
|
||||
(async () => {
|
||||
if (!process.env.HUGGING_FACE_TOKEN) {
|
||||
throw new Error("Please set the HUGGING_FACE_TOKEN environment variable.");
|
||||
}
|
||||
const hf = new HuggingFaceInferenceAPI({
|
||||
accessToken: process.env.HUGGING_FACE_TOKEN,
|
||||
model: "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
||||
});
|
||||
const result = await hf.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);
|
||||
})();
|
||||
@@ -3,7 +3,7 @@
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "turbo run build",
|
||||
"build:release": "turbo run build lint test --filter=\"!docs\" --filter=\"!*-test\"",
|
||||
"build:release": "turbo run build lint test --filter=\"!docs\"",
|
||||
"dev": "turbo run dev",
|
||||
"format": "prettier --ignore-unknown --cache --check .",
|
||||
"format:write": "prettier --ignore-unknown --write .",
|
||||
|
||||
@@ -1,45 +1,5 @@
|
||||
# llamaindex
|
||||
|
||||
## 0.3.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 1dce275: fix: export `StorageContext` on edge runtime
|
||||
- d10533e: feat: add hugging face llm
|
||||
- 2008efe: feat: add verbose mode to Agent
|
||||
- 5e61934: fix: remove clone object in `CallbackManager.dispatchEvent`
|
||||
- 9e74a43: feat: add top k to `asQueryEngine`
|
||||
- ee719a1: fix: streaming for ReAct Agent
|
||||
|
||||
## 0.3.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- e8c41c5: fix: wrong gemini streaming chat response
|
||||
|
||||
## 0.3.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 61103b6: fix: streaming for `Agent.createTask` API
|
||||
|
||||
## 0.3.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 46227f2: fix: build error on next.js nodejs runtime
|
||||
|
||||
## 0.3.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- 5016f21: feat: improve next.js/cloudflare/vite support
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [5016f21]
|
||||
- @llamaindex/env@0.1.0
|
||||
|
||||
## 0.2.13
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
# @llamaindex/core-e2e
|
||||
|
||||
## 0.0.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 61103b6: fix: streaming for `Agent.createTask` API
|
||||
@@ -1,4 +0,0 @@
|
||||
# Examples
|
||||
|
||||
This directory contains examples of how to use the core package.
|
||||
Each example is not for production use, but rather to show how to use the core package at minimum.
|
||||
@@ -1,172 +0,0 @@
|
||||
# Logs
|
||||
|
||||
logs
|
||||
_.log
|
||||
npm-debug.log_
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# Runtime data
|
||||
|
||||
pids
|
||||
_.pid
|
||||
_.seed
|
||||
\*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
|
||||
coverage
|
||||
\*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
|
||||
\*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
|
||||
.eslintcache
|
||||
|
||||
# Optional stylelint cache
|
||||
|
||||
.stylelintcache
|
||||
|
||||
# Microbundle cache
|
||||
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
|
||||
\*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variable files
|
||||
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
|
||||
.cache
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
|
||||
.cache/
|
||||
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
|
||||
.vuepress/dist
|
||||
|
||||
# vuepress v2.x temp and cache directory
|
||||
|
||||
.temp
|
||||
.cache
|
||||
|
||||
# Docusaurus cache and generated files
|
||||
|
||||
.docusaurus
|
||||
|
||||
# Serverless directories
|
||||
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.\*
|
||||
|
||||
# wrangler project
|
||||
|
||||
.dev.vars
|
||||
.wrangler/
|
||||
@@ -1,41 +0,0 @@
|
||||
# @llamaindex/cloudflare-worker-agent-test
|
||||
|
||||
## 0.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [1dce275]
|
||||
- Updated dependencies [d10533e]
|
||||
- Updated dependencies [2008efe]
|
||||
- Updated dependencies [5e61934]
|
||||
- Updated dependencies [9e74a43]
|
||||
- Updated dependencies [ee719a1]
|
||||
- llamaindex@0.3.4
|
||||
|
||||
## 0.0.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [e8c41c5]
|
||||
- llamaindex@0.3.3
|
||||
|
||||
## 0.0.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [61103b6]
|
||||
- llamaindex@0.3.2
|
||||
|
||||
## 0.0.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [46227f2]
|
||||
- llamaindex@0.3.1
|
||||
|
||||
## 0.0.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [5016f21]
|
||||
- llamaindex@0.3.0
|
||||
@@ -1,24 +0,0 @@
|
||||
{
|
||||
"name": "@llamaindex/cloudflare-worker-agent-test",
|
||||
"version": "0.0.5",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"deploy": "wrangler deploy",
|
||||
"dev": "wrangler dev",
|
||||
"build": "wrangler deploy --dry-run --outdir dist",
|
||||
"start": "wrangler dev",
|
||||
"test": "vitest",
|
||||
"cf-typegen": "wrangler types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cloudflare/vitest-pool-workers": "^0.2.3",
|
||||
"@cloudflare/workers-types": "^4.20240423.0",
|
||||
"typescript": "^5.4.5",
|
||||
"vitest": "1.3.0",
|
||||
"wrangler": "^3.52.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"llamaindex": "workspace:*"
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
export default {
|
||||
async fetch(
|
||||
request: Request,
|
||||
env: Env,
|
||||
ctx: ExecutionContext,
|
||||
): Promise<Response> {
|
||||
const { setEnvs } = await import("@llamaindex/env");
|
||||
setEnvs(env);
|
||||
const { OpenAIAgent } = await import("llamaindex");
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [],
|
||||
});
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: "Hello? What is the weather today?",
|
||||
});
|
||||
const textEncoder = new TextEncoder();
|
||||
const response = responseStream.pipeThrough<Uint8Array>(
|
||||
// @ts-expect-error: see https://github.com/cloudflare/workerd/issues/2067
|
||||
new TransformStream({
|
||||
transform: (chunk, controller) => {
|
||||
controller.enqueue(textEncoder.encode(chunk.response.delta));
|
||||
},
|
||||
}),
|
||||
);
|
||||
// @ts-expect-error: see https://github.com/cloudflare/workerd/issues/2067
|
||||
return new Response(response);
|
||||
},
|
||||
};
|
||||
@@ -1,23 +0,0 @@
|
||||
import {
|
||||
createExecutionContext,
|
||||
env,
|
||||
waitOnExecutionContext,
|
||||
} from "cloudflare:test";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import worker from "../src/index";
|
||||
|
||||
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
|
||||
|
||||
describe("Hello World worker", () => {
|
||||
// FIXME: https://github.com/cloudflare/workers-sdk/issues/5646
|
||||
it.fails("responds with Hello World! (unit style)", async () => {
|
||||
const request = new IncomingRequest("http://example.com");
|
||||
// Create an empty context to pass to `worker.fetch()`.
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, env, ctx);
|
||||
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
|
||||
await waitOnExecutionContext(ctx);
|
||||
// fixme: should be not "Hello World!"
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"types": [
|
||||
"@cloudflare/workers-types/experimental",
|
||||
"@cloudflare/vitest-pool-workers"
|
||||
]
|
||||
},
|
||||
"include": ["./**/*.ts", "../src/env.d.ts"],
|
||||
"exclude": []
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
||||
|
||||
/* Projects */
|
||||
// "incremental": true, /* Enable incremental compilation */
|
||||
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
||||
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
||||
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
||||
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
||||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
||||
|
||||
/* Language and Environment */
|
||||
"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
||||
"lib": [
|
||||
"es2021"
|
||||
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
|
||||
"jsx": "react" /* Specify what JSX code is generated. */,
|
||||
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
||||
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
||||
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
||||
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
||||
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
||||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
||||
|
||||
/* Modules */
|
||||
"module": "es2022" /* Specify what module code is generated. */,
|
||||
// "rootDir": "./", /* Specify the root folder within your source files. */
|
||||
"moduleResolution": "Bundler" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
||||
"types": [
|
||||
"@cloudflare/workers-types/2023-07-01"
|
||||
] /* Specify type package names to be included without being referenced in a source file. */,
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
"resolveJsonModule": true /* Enable importing .json files */,
|
||||
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
||||
|
||||
/* JavaScript Support */
|
||||
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
|
||||
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
|
||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
||||
|
||||
/* Emit */
|
||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
||||
// "outDir": "./", /* Specify an output folder for all emitted files. */
|
||||
// "removeComments": true, /* Disable emitting comments. */
|
||||
"noEmit": true /* Disable emitting files from a compilation. */,
|
||||
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
||||
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
||||
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
||||
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
||||
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
||||
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
||||
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
||||
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
||||
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
||||
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
||||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
||||
|
||||
/* Interop Constraints */
|
||||
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
|
||||
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
|
||||
// "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
||||
|
||||
/* Type Checking */
|
||||
"strict": true /* Enable all strict type-checking options. */,
|
||||
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
||||
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
||||
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
||||
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
||||
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
||||
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
||||
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
||||
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
||||
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
||||
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
||||
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
||||
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
||||
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
||||
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
||||
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
||||
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
||||
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
||||
|
||||
/* Completeness */
|
||||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||
},
|
||||
"exclude": ["test"]
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
|
||||
|
||||
export default defineWorkersConfig({
|
||||
test: {
|
||||
poolOptions: {
|
||||
workers: {
|
||||
wrangler: { configPath: "./wrangler.toml" },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -1,3 +0,0 @@
|
||||
// Generated by Wrangler
|
||||
// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen`
|
||||
interface Env {}
|
||||
@@ -1,108 +0,0 @@
|
||||
#:schema node_modules/wrangler/config-schema.json
|
||||
name = "agent"
|
||||
main = "src/index.ts"
|
||||
compatibility_date = "2024-04-23"
|
||||
compatibility_flags = ["nodejs_compat"]
|
||||
|
||||
# Automatically place your workloads in an optimal location to minimize latency.
|
||||
# If you are running back-end logic in a Worker, running it closer to your back-end infrastructure
|
||||
# rather than the end user may result in better performance.
|
||||
# Docs: https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
|
||||
# [placement]
|
||||
# mode = "smart"
|
||||
|
||||
# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
|
||||
# Docs:
|
||||
# - https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables
|
||||
# Note: Use secrets to store sensitive data.
|
||||
# - https://developers.cloudflare.com/workers/configuration/secrets/
|
||||
# [vars]
|
||||
# MY_VARIABLE = "production_value"
|
||||
|
||||
# Bind the Workers AI model catalog. Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#workers-ai
|
||||
# [ai]
|
||||
# binding = "AI"
|
||||
|
||||
# Bind an Analytics Engine dataset. Use Analytics Engine to write analytics within your Pages Function.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#analytics-engine-datasets
|
||||
# [[analytics_engine_datasets]]
|
||||
# binding = "MY_DATASET"
|
||||
|
||||
# Bind a headless browser instance running on Cloudflare's global network.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#browser-rendering
|
||||
# [browser]
|
||||
# binding = "MY_BROWSER"
|
||||
|
||||
# Bind a D1 database. D1 is Cloudflare’s native serverless SQL database.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#d1-databases
|
||||
# [[d1_databases]]
|
||||
# binding = "MY_DB"
|
||||
# database_name = "my-database"
|
||||
# database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
|
||||
|
||||
# Bind a dispatch namespace. Use Workers for Platforms to deploy serverless functions programmatically on behalf of your customers.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#dispatch-namespace-bindings-workers-for-platforms
|
||||
# [[dispatch_namespaces]]
|
||||
# binding = "MY_DISPATCHER"
|
||||
# namespace = "my-namespace"
|
||||
|
||||
# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
|
||||
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects
|
||||
# [[durable_objects.bindings]]
|
||||
# name = "MY_DURABLE_OBJECT"
|
||||
# class_name = "MyDurableObject"
|
||||
|
||||
# Durable Object migrations.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations
|
||||
# [[migrations]]
|
||||
# tag = "v1"
|
||||
# new_classes = ["MyDurableObject"]
|
||||
|
||||
# Bind a Hyperdrive configuration. Use to accelerate access to your existing databases from Cloudflare Workers.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#hyperdrive
|
||||
# [[hyperdrive]]
|
||||
# binding = "MY_HYPERDRIVE"
|
||||
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||
|
||||
# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces
|
||||
# [[kv_namespaces]]
|
||||
# binding = "MY_KV_NAMESPACE"
|
||||
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||
|
||||
# Bind an mTLS certificate. Use to present a client certificate when communicating with another service.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates
|
||||
# [[mtls_certificates]]
|
||||
# binding = "MY_CERTIFICATE"
|
||||
# certificate_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
|
||||
|
||||
# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues
|
||||
# [[queues.producers]]
|
||||
# binding = "MY_QUEUE"
|
||||
# queue = "my-queue"
|
||||
|
||||
# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues
|
||||
# [[queues.consumers]]
|
||||
# queue = "my-queue"
|
||||
|
||||
# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets
|
||||
# [[r2_buckets]]
|
||||
# binding = "MY_BUCKET"
|
||||
# bucket_name = "my-bucket"
|
||||
|
||||
# Bind another Worker service. Use this binding to call another Worker without network overhead.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings
|
||||
# [[services]]
|
||||
# binding = "MY_SERVICE"
|
||||
# service = "my-service"
|
||||
|
||||
# Bind a Vectorize index. Use to store and query vector embeddings for semantic search, classification and other vector search use-cases.
|
||||
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#vectorize-indexes
|
||||
# [[vectorize]]
|
||||
# binding = "MY_INDEX"
|
||||
# index_name = "my-index"
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"root": false,
|
||||
"extends": "next/core-web-vitals"
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
# @llamaindex/next-agent-test
|
||||
|
||||
## 0.1.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [1dce275]
|
||||
- Updated dependencies [d10533e]
|
||||
- Updated dependencies [2008efe]
|
||||
- Updated dependencies [5e61934]
|
||||
- Updated dependencies [9e74a43]
|
||||
- Updated dependencies [ee719a1]
|
||||
- llamaindex@0.3.4
|
||||
|
||||
## 0.1.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [e8c41c5]
|
||||
- llamaindex@0.3.3
|
||||
|
||||
## 0.1.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [61103b6]
|
||||
- llamaindex@0.3.2
|
||||
|
||||
## 0.1.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [46227f2]
|
||||
- llamaindex@0.3.1
|
||||
|
||||
## 0.1.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [5016f21]
|
||||
- llamaindex@0.3.0
|
||||
@@ -1,36 +0,0 @@
|
||||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
|
||||
|
||||
## Getting Started
|
||||
|
||||
First, run the development server:
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
# or
|
||||
yarn dev
|
||||
# or
|
||||
pnpm dev
|
||||
# or
|
||||
bun dev
|
||||
```
|
||||
|
||||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
||||
|
||||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
|
||||
|
||||
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
|
||||
|
||||
## Learn More
|
||||
|
||||
To learn more about Next.js, take a look at the following resources:
|
||||
|
||||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
||||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
||||
|
||||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
|
||||
|
||||
## Deploy on Vercel
|
||||
|
||||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
||||
|
||||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
|
||||
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"name": "@llamaindex/next-agent-test",
|
||||
"version": "0.1.5",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "next lint"
|
||||
},
|
||||
"dependencies": {
|
||||
"ai": "^3.0.34",
|
||||
"llamaindex": "workspace:*",
|
||||
"next": "14.2.3",
|
||||
"react": "18.2.0",
|
||||
"react-dom": "18.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20",
|
||||
"@types/react": "^18.2.79",
|
||||
"@types/react-dom": "^18.2.25",
|
||||
"eslint": "^8",
|
||||
"eslint-config-next": "14.2.3",
|
||||
"postcss": "^8",
|
||||
"tailwindcss": "^3.4.1",
|
||||
"typescript": "^5"
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
/** @type {import('postcss-load-config').Config} */
|
||||
const config = {
|
||||
plugins: {
|
||||
tailwindcss: {},
|
||||
},
|
||||
};
|
||||
|
||||
export default config;
|
||||
@@ -1,32 +0,0 @@
|
||||
"use server";
|
||||
import { createStreamableUI } from "ai/rsc";
|
||||
import { OpenAIAgent } from "llamaindex";
|
||||
import type { ChatMessage } from "llamaindex/llm/types";
|
||||
|
||||
export async function chatWithAgent(
|
||||
question: string,
|
||||
prevMessages: ChatMessage[] = [],
|
||||
) {
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [],
|
||||
});
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: question,
|
||||
chatHistory: prevMessages,
|
||||
});
|
||||
const uiStream = createStreamableUI(<div>loading...</div>);
|
||||
responseStream
|
||||
.pipeTo(
|
||||
new WritableStream({
|
||||
start: () => {
|
||||
uiStream.update("response:");
|
||||
},
|
||||
write: async (message) => {
|
||||
uiStream.append(message.response.delta);
|
||||
},
|
||||
}),
|
||||
)
|
||||
.catch(uiStream.error);
|
||||
return uiStream.value;
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
@@ -1,22 +0,0 @@
|
||||
import type { Metadata } from "next";
|
||||
import { Inter } from "next/font/google";
|
||||
import "./globals.css";
|
||||
|
||||
const inter = Inter({ subsets: ["latin"] });
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "Create Next App",
|
||||
description: "Generated by create next app",
|
||||
};
|
||||
|
||||
export default function RootLayout({
|
||||
children,
|
||||
}: Readonly<{
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<body className={inter.className}>{children}</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
"use client";
|
||||
import { chatWithAgent } from "@/actions";
|
||||
import type { JSX } from "react";
|
||||
import { useFormState } from "react-dom";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
||||
export default function Home() {
|
||||
const [state, action] = useFormState<JSX.Element | null>(async () => {
|
||||
return chatWithAgent("hello!", []);
|
||||
}, null);
|
||||
return (
|
||||
<main>
|
||||
{state}
|
||||
<form action={action}>
|
||||
<button>Chat</button>
|
||||
</form>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
import "llamaindex";
|
||||
|
||||
export default function Page() {
|
||||
return "hello world!";
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
import type { Config } from "tailwindcss";
|
||||
|
||||
const config: Config = {
|
||||
content: [
|
||||
"./src/pages/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
"./src/components/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
"./src/app/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
],
|
||||
theme: {
|
||||
extend: {
|
||||
backgroundImage: {
|
||||
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
|
||||
"gradient-conic":
|
||||
"conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: [],
|
||||
};
|
||||
export default config;
|
||||
@@ -1,27 +0,0 @@
|
||||
{
|
||||
"extends": "../../../../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"lib": ["dom", "dom.iterable", "esnext"],
|
||||
"allowJs": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"noEmit": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"jsx": "preserve",
|
||||
"incremental": true,
|
||||
"plugins": [
|
||||
{
|
||||
"name": "next"
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
.yarn/install-state.gz
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# next.js
|
||||
/.next/
|
||||
/out/
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
*.pem
|
||||
|
||||
# debug
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# local env files
|
||||
.env*.local
|
||||
|
||||
# vercel
|
||||
.vercel
|
||||
|
||||
# typescript
|
||||
*.tsbuildinfo
|
||||
next-env.d.ts
|
||||
@@ -1,4 +0,0 @@
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {};
|
||||
|
||||
export default nextConfig;
|
||||
@@ -1 +0,0 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 394 80"><path fill="#000" d="M262 0h68.5v12.7h-27.2v66.6h-13.6V12.7H262V0ZM149 0v12.7H94v20.4h44.3v12.6H94v21h55v12.6H80.5V0h68.7zm34.3 0h-17.8l63.8 79.4h17.9l-32-39.7 32-39.6h-17.9l-23 28.6-23-28.6zm18.3 56.7-9-11-27.1 33.7h17.8l18.3-22.7z"/><path fill="#000" d="M81 79.3 17 0H0v79.3h13.6V17l50.2 62.3H81Zm252.6-.4c-1 0-1.8-.4-2.5-1s-1.1-1.6-1.1-2.6.3-1.8 1-2.5 1.6-1 2.6-1 1.8.3 2.5 1a3.4 3.4 0 0 1 .6 4.3 3.7 3.7 0 0 1-3 1.8zm23.2-33.5h6v23.3c0 2.1-.4 4-1.3 5.5a9.1 9.1 0 0 1-3.8 3.5c-1.6.8-3.5 1.3-5.7 1.3-2 0-3.7-.4-5.3-1s-2.8-1.8-3.7-3.2c-.9-1.3-1.4-3-1.4-5h6c.1.8.3 1.6.7 2.2s1 1.2 1.6 1.5c.7.4 1.5.5 2.4.5 1 0 1.8-.2 2.4-.6a4 4 0 0 0 1.6-1.8c.3-.8.5-1.8.5-3V45.5zm30.9 9.1a4.4 4.4 0 0 0-2-3.3 7.5 7.5 0 0 0-4.3-1.1c-1.3 0-2.4.2-3.3.5-.9.4-1.6 1-2 1.6a3.5 3.5 0 0 0-.3 4c.3.5.7.9 1.3 1.2l1.8 1 2 .5 3.2.8c1.3.3 2.5.7 3.7 1.2a13 13 0 0 1 3.2 1.8 8.1 8.1 0 0 1 3 6.5c0 2-.5 3.7-1.5 5.1a10 10 0 0 1-4.4 3.5c-1.8.8-4.1 1.2-6.8 1.2-2.6 0-4.9-.4-6.8-1.2-2-.8-3.4-2-4.5-3.5a10 10 0 0 1-1.7-5.6h6a5 5 0 0 0 3.5 4.6c1 .4 2.2.6 3.4.6 1.3 0 2.5-.2 3.5-.6 1-.4 1.8-1 2.4-1.7a4 4 0 0 0 .8-2.4c0-.9-.2-1.6-.7-2.2a11 11 0 0 0-2.1-1.4l-3.2-1-3.8-1c-2.8-.7-5-1.7-6.6-3.2a7.2 7.2 0 0 1-2.4-5.7 8 8 0 0 1 1.7-5 10 10 0 0 1 4.3-3.5c2-.8 4-1.2 6.4-1.2 2.3 0 4.4.4 6.2 1.2 1.8.8 3.2 2 4.3 3.4 1 1.4 1.5 3 1.5 5h-5.8z"/></svg>
|
||||
|
Before Width: | Height: | Size: 1.3 KiB |
@@ -1 +0,0 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 283 64"><path fill="black" d="M141 16c-11 0-19 7-19 18s9 18 20 18c7 0 13-3 16-7l-7-5c-2 3-6 4-9 4-5 0-9-3-10-7h28v-3c0-11-8-18-19-18zm-9 15c1-4 4-7 9-7s8 3 9 7h-18zm117-15c-11 0-19 7-19 18s9 18 20 18c6 0 12-3 16-7l-8-5c-2 3-5 4-8 4-5 0-9-3-11-7h28l1-3c0-11-8-18-19-18zm-10 15c2-4 5-7 10-7s8 3 9 7h-19zm-39 3c0 6 4 10 10 10 4 0 7-2 9-5l8 5c-3 5-9 8-17 8-11 0-19-7-19-18s8-18 19-18c8 0 14 3 17 8l-8 5c-2-3-5-5-9-5-6 0-10 4-10 10zm83-29v46h-9V5h9zM37 0l37 64H0L37 0zm92 5-27 48L74 5h10l18 30 17-30h10zm59 12v10l-3-1c-6 0-10 4-10 10v15h-9V17h9v9c0-5 6-9 13-9z"/></svg>
|
||||
|
Before Width: | Height: | Size: 629 B |
|
Before Width: | Height: | Size: 25 KiB |
@@ -1,7 +0,0 @@
|
||||
node_modules
|
||||
dist
|
||||
.env*
|
||||
*.tsbuildinfo
|
||||
.cache
|
||||
.DS_Store
|
||||
*.pem
|
||||
@@ -1,41 +0,0 @@
|
||||
# @llamaindex/waku-query-engine-test
|
||||
|
||||
## 0.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [1dce275]
|
||||
- Updated dependencies [d10533e]
|
||||
- Updated dependencies [2008efe]
|
||||
- Updated dependencies [5e61934]
|
||||
- Updated dependencies [9e74a43]
|
||||
- Updated dependencies [ee719a1]
|
||||
- llamaindex@0.3.4
|
||||
|
||||
## 0.0.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [e8c41c5]
|
||||
- llamaindex@0.3.3
|
||||
|
||||
## 0.0.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [61103b6]
|
||||
- llamaindex@0.3.2
|
||||
|
||||
## 0.0.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [46227f2]
|
||||
- llamaindex@0.3.1
|
||||
|
||||
## 0.0.1
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [5016f21]
|
||||
- llamaindex@0.3.0
|
||||
@@ -1,25 +0,0 @@
|
||||
{
|
||||
"name": "@llamaindex/waku-query-engine-test",
|
||||
"version": "0.0.5",
|
||||
"type": "module",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "waku dev",
|
||||
"build": "waku build",
|
||||
"start": "waku start"
|
||||
},
|
||||
"dependencies": {
|
||||
"llamaindex": "workspace:*",
|
||||
"react": "19.0.0-canary-e3ebcd54b-20240405",
|
||||
"react-dom": "19.0.0-canary-e3ebcd54b-20240405",
|
||||
"react-server-dom-webpack": "19.0.0-canary-e3ebcd54b-20240405",
|
||||
"waku": "0.20.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "18.2.74",
|
||||
"@types/react-dom": "18.2.24",
|
||||
"autoprefixer": "10.4.19",
|
||||
"tailwindcss": "3.4.3",
|
||||
"typescript": "5.4.4"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
/** @type {import('postcss-load-config').Config} */
|
||||
export default {
|
||||
plugins: {
|
||||
tailwindcss: {},
|
||||
autoprefixer: {},
|
||||
},
|
||||
};
|
||||
|
Before Width: | Height: | Size: 5.6 KiB |
@@ -1,27 +0,0 @@
|
||||
"use server";
|
||||
import { Document, VectorStoreIndex, type QueryEngine } from "llamaindex";
|
||||
import { readFile } from "node:fs/promises";
|
||||
let _queryEngine: QueryEngine;
|
||||
|
||||
async function lazyLoadQueryEngine() {
|
||||
if (!_queryEngine) {
|
||||
const path = "node_modules/llamaindex/examples/abramov.txt";
|
||||
|
||||
const essay = await readFile(path, "utf-8");
|
||||
|
||||
// Create Document object with essay
|
||||
const document = new Document({ text: essay, id_: path });
|
||||
|
||||
// Split text and create embeddings. Store them in a VectorStoreIndex
|
||||
const index = await VectorStoreIndex.fromDocuments([document]);
|
||||
|
||||
_queryEngine = index.asQueryEngine();
|
||||
}
|
||||
return _queryEngine;
|
||||
}
|
||||
|
||||
export async function chatWithAI(question: string): Promise<string> {
|
||||
const queryEngine = await lazyLoadQueryEngine();
|
||||
const { response } = await queryEngine.query({ query: question });
|
||||
return response;
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
|
||||
export type ChatProps = {
|
||||
askQuestion: (question: string) => Promise<string>;
|
||||
};
|
||||
|
||||
export const Chat = (props: ChatProps) => {
|
||||
const [response, setResponse] = useState<string | null>(null);
|
||||
|
||||
return (
|
||||
<section className="border-blue-400 -mx-4 mt-4 rounded border border-dashed p-4">
|
||||
<h2 className="text-lg font-bold">Chat with AI</h2>
|
||||
{response ? (
|
||||
<p className="text-sm text-gray-600 max-w-sm">{response}</p>
|
||||
) : null}
|
||||
<form
|
||||
action={async (formData) => {
|
||||
const question = formData.get("question") as string | null;
|
||||
if (question) {
|
||||
setResponse(await props.askQuestion(question));
|
||||
}
|
||||
}}
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
name="question"
|
||||
className="border border-gray-400 rounded-sm px-2 py-0.5 text-sm"
|
||||
/>
|
||||
<button className="rounded-sm bg-black px-2 py-0.5 text-sm text-white">
|
||||
Ask
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
);
|
||||
};
|
||||
@@ -1,18 +0,0 @@
|
||||
export const Footer = () => {
|
||||
return (
|
||||
<footer className="p-6 lg:fixed lg:bottom-0 lg:left-0">
|
||||
<div>
|
||||
visit{" "}
|
||||
<a
|
||||
href="https://waku.gg/"
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
className="mt-4 inline-block underline"
|
||||
>
|
||||
waku.gg
|
||||
</a>{" "}
|
||||
to learn more
|
||||
</div>
|
||||
</footer>
|
||||
);
|
||||
};
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Link } from "waku";
|
||||
|
||||
export const Header = () => {
|
||||
return (
|
||||
<header className="flex items-center gap-4 p-6 lg:fixed lg:left-0 lg:top-0">
|
||||
<h2 className="text-lg font-bold tracking-tight">
|
||||
<Link to="/">Waku starter</Link>
|
||||
</h2>
|
||||
</header>
|
||||
);
|
||||
};
|
||||
@@ -1,39 +0,0 @@
|
||||
import "../styles.css";
|
||||
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
import { Footer } from "../components/footer";
|
||||
import { Header } from "../components/header";
|
||||
|
||||
type RootLayoutProps = { children: ReactNode };
|
||||
|
||||
export default async function RootLayout({ children }: RootLayoutProps) {
|
||||
const data = await getData();
|
||||
|
||||
return (
|
||||
<div className="font-['Nunito']">
|
||||
<meta property="description" content={data.description} />
|
||||
<link rel="icon" type="image/png" href={data.icon} />
|
||||
<Header />
|
||||
<main className="m-6 flex items-center *:min-h-64 *:min-w-64 lg:m-0 lg:min-h-svh lg:justify-center">
|
||||
{children}
|
||||
</main>
|
||||
<Footer />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const getData = async () => {
|
||||
const data = {
|
||||
description: "An internet website!",
|
||||
icon: "/images/favicon.png",
|
||||
};
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getConfig = async () => {
|
||||
return {
|
||||
render: "static",
|
||||
};
|
||||
};
|
||||
@@ -1,36 +0,0 @@
|
||||
import { Link } from "waku";
|
||||
|
||||
import { chatWithAI } from "../actions";
|
||||
import { Chat } from "../components/chat";
|
||||
|
||||
export default async function HomePage() {
|
||||
const data = await getData();
|
||||
|
||||
return (
|
||||
<div>
|
||||
<title>{data.title}</title>
|
||||
<h1 className="text-4xl font-bold tracking-tight">{data.headline}</h1>
|
||||
<p>{data.body}</p>
|
||||
<Chat askQuestion={chatWithAI} />
|
||||
<Link to="/about" className="mt-4 inline-block underline">
|
||||
About page
|
||||
</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const getData = async () => {
|
||||
const data = {
|
||||
title: "Waku",
|
||||
headline: "Waku",
|
||||
body: "Hello world!",
|
||||
};
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const getConfig = async () => {
|
||||
return {
|
||||
render: "static",
|
||||
};
|
||||
};
|
||||
@@ -1,4 +0,0 @@
|
||||
@import url("https://fonts.googleapis.com/css2?family=Nunito:ital,wght@0,400;0,700;1,400;1,700&display=swap");
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
@@ -1,4 +0,0 @@
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
export default {
|
||||
content: ["./src/**/*.{js,jsx,ts,tsx}"],
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"target": "esnext",
|
||||
"downlevelIteration": true,
|
||||
"esModuleInterop": true,
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"skipLibCheck": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"exactOptionalPropertyTypes": true,
|
||||
"types": ["react/experimental"],
|
||||
"jsx": "react-jsx"
|
||||
}
|
||||
}
|
||||
@@ -27,23 +27,3 @@ await test("react agent", async (t) => {
|
||||
ok(extractText(response.message.content).includes("72"));
|
||||
});
|
||||
});
|
||||
|
||||
await test("react agent stream", async (t) => {
|
||||
await mockLLMEvent(t, "react-agent-stream");
|
||||
await t.test("get weather", async () => {
|
||||
const agent = new ReActAgent({
|
||||
tools: [getWeatherTool],
|
||||
});
|
||||
|
||||
const stream = await agent.chat({
|
||||
stream: true,
|
||||
message: "What is the weather like in San Francisco?",
|
||||
});
|
||||
|
||||
let content = "";
|
||||
for await (const { response } of stream) {
|
||||
content += response.delta;
|
||||
}
|
||||
ok(content.includes("72"));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,488 +0,0 @@
|
||||
{
|
||||
"llmEventStart": [
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": "You are designed to help with a variety of tasks, from answering questions to providing summaries to other types of analyses.\n\n## Tools\nYou have access to a wide variety of tools. You are responsible for using\nthe tools in any sequence you deem appropriate to complete the task at hand.\nThis may require breaking the task into subtasks and using different tools\nto complete each subtask.\n\nYou have access to the following tools:\n- getWeather: Get the weather for a city with schema: {\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\",\"description\":\"The city to get the weather for\"}},\"required\":[\"city\"]}\n\n## Output Format\nTo answer the question, please use the following format.\n\n\"\"\"\nThought: I need to use a tool to help me answer the question.\nAction: tool name (one of getWeather) if using a tool.\nAction Input: the input to the tool, in a JSON format representing the kwargs (e.g. {{\"input\": \"hello world\", \"num_beams\": 5}})\n\"\"\"\n\nPlease ALWAYS start with a Thought.\n\nPlease use a valid JSON format for the Action Input. Do NOT do this {{'input': 'hello world', 'num_beams': 5}}.\n\nIf this format is used, the user will respond in the following format:\n\n\"\"\"\"\nObservation: tool response\n\"\"\"\"\n\nYou should keep repeating the above format until you have enough information\nto answer the question without using any more tools. At that point, you MUST respond\nin the one of the following two formats:\n\n\"\"\"\"\nThought: I can answer without using any more tools.\nAnswer: [your answer here]\n\"\"\"\"\n\n\"\"\"\"\nThought: I cannot answer the question with the provided tools.\nAnswer: Sorry, I cannot answer your query.\n\"\"\"\"\n\n## Current Conversation\nBelow is the current conversation consisting of interleaving human and assistant messages."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "What is the weather like in San Francisco?"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": "You are designed to help with a variety of tasks, from answering questions to providing summaries to other types of analyses.\n\n## Tools\nYou have access to a wide variety of tools. You are responsible for using\nthe tools in any sequence you deem appropriate to complete the task at hand.\nThis may require breaking the task into subtasks and using different tools\nto complete each subtask.\n\nYou have access to the following tools:\n- getWeather: Get the weather for a city with schema: {\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\",\"description\":\"The city to get the weather for\"}},\"required\":[\"city\"]}\n\n## Output Format\nTo answer the question, please use the following format.\n\n\"\"\"\nThought: I need to use a tool to help me answer the question.\nAction: tool name (one of getWeather) if using a tool.\nAction Input: the input to the tool, in a JSON format representing the kwargs (e.g. {{\"input\": \"hello world\", \"num_beams\": 5}})\n\"\"\"\n\nPlease ALWAYS start with a Thought.\n\nPlease use a valid JSON format for the Action Input. Do NOT do this {{'input': 'hello world', 'num_beams': 5}}.\n\nIf this format is used, the user will respond in the following format:\n\n\"\"\"\"\nObservation: tool response\n\"\"\"\"\n\nYou should keep repeating the above format until you have enough information\nto answer the question without using any more tools. At that point, you MUST respond\nin the one of the following two formats:\n\n\"\"\"\"\nThought: I can answer without using any more tools.\nAnswer: [your answer here]\n\"\"\"\"\n\n\"\"\"\"\nThought: I cannot answer the question with the provided tools.\nAnswer: Sorry, I cannot answer your query.\n\"\"\"\"\n\n## Current Conversation\nBelow is the current conversation consisting of interleaving human and assistant messages."
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "What is the weather like in San Francisco?"
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "Thought: I need to use a tool to help me answer the question.\nAction: getWeather\nInput: {\n city: San Francisco\n}"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Observation: The weather in San Francisco is 72 degrees"
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"llmEventEnd": [
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"response": {
|
||||
"raw": null,
|
||||
"message": {
|
||||
"content": "Thought: I need to use a tool to help me answer the question.\nAction: getWeather\nAction Input: {\"city\": \"San Francisco\"}",
|
||||
"role": "assistant",
|
||||
"options": {}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"response": {
|
||||
"raw": null,
|
||||
"message": {
|
||||
"content": "Thought: I can answer without using any more tools.\nAnswer: The weather in San Francisco is 72 degrees.",
|
||||
"role": "assistant",
|
||||
"options": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"llmEventStream": [
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "Thought"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": ":"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " I"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " need"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " to"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " use"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " a"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " tool"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " to"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " help"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " me"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " answer"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " the"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " question"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": ".\n"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "Action"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": ":"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " get"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "Weather"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "\n"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "Action"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " Input"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": ":"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " {\""
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "city"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "\":"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " \""
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "San"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " Francisco"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_0",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "\"}"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "Thought"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": ":"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " I"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " can"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " answer"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " without"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " using"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " any"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " more"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " tools"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": ".\n"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "Answer"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": ":"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " The"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " weather"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " in"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " San"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " Francisco"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " is"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " "
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "72"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": " degrees"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "PRESERVE_1",
|
||||
"chunk": {
|
||||
"raw": null,
|
||||
"options": {},
|
||||
"delta": "."
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/core-e2e",
|
||||
"private": true,
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.2",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"e2e": "node --import tsx --import ./mock-register.js --test ./node/*.e2e.ts",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/core",
|
||||
"version": "0.3.4",
|
||||
"version": "0.2.3",
|
||||
"exports": "./src/index.ts",
|
||||
"imports": {
|
||||
"@llamaindex/env": "jsr:@llamaindex/env@0.0.6"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "llamaindex",
|
||||
"version": "0.3.4",
|
||||
"expectedMinorVersion": "3",
|
||||
"version": "0.2.13",
|
||||
"expectedMinorVersion": "2",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
@@ -10,7 +10,6 @@
|
||||
"@datastax/astra-db-ts": "^1.0.1",
|
||||
"@google/generative-ai": "^0.8.0",
|
||||
"@grpc/grpc-js": "^1.10.6",
|
||||
"@huggingface/inference": "^2.6.7",
|
||||
"@llamaindex/cloud": "0.0.5",
|
||||
"@llamaindex/env": "workspace:*",
|
||||
"@mistralai/mistralai": "^0.1.3",
|
||||
@@ -64,18 +63,6 @@
|
||||
"main": "./dist/cjs/index.js",
|
||||
"exports": {
|
||||
".": {
|
||||
"react-server": {
|
||||
"types": "./dist/type/index.react-server.d.ts",
|
||||
"default": "./dist/index.react-server.js"
|
||||
},
|
||||
"workerd": {
|
||||
"types": "./dist/type/index.workerd.d.ts",
|
||||
"default": "./dist/index.workerd.js"
|
||||
},
|
||||
"edge-light": {
|
||||
"types": "./dist/type/index.edge.d.ts",
|
||||
"default": "./dist/index.edge.js"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/type/index.d.ts",
|
||||
"default": "./dist/index.js"
|
||||
@@ -89,24 +76,6 @@
|
||||
"import": "./dist/not-allow.js",
|
||||
"require": "./dist/cjs/not-allow.js"
|
||||
},
|
||||
"./readers/SimpleDirectoryReader": {
|
||||
"workerd": {
|
||||
"types": "./dist/type/readers/SimpleDirectoryReader.edge.d.ts",
|
||||
"default": "./dist/readers/SimpleDirectoryReader.edge.js"
|
||||
},
|
||||
"edge-light": {
|
||||
"types": "./dist/type/readers/SimpleDirectoryReader.edge.d.ts",
|
||||
"default": "./dist/readers/SimpleDirectoryReader.edge.js"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/type/readers/SimpleDirectoryReader.d.ts",
|
||||
"default": "./dist/readers/SimpleDirectoryReader.js"
|
||||
},
|
||||
"require": {
|
||||
"types": "./dist/type/readers/SimpleDirectoryReader.d.ts",
|
||||
"default": "./dist/cjs/readers/SimpleDirectoryReader.js"
|
||||
}
|
||||
},
|
||||
"./*": {
|
||||
"import": {
|
||||
"types": "./dist/type/*.d.ts",
|
||||
|
||||
@@ -55,9 +55,9 @@ class GlobalSettings implements Config {
|
||||
get debug() {
|
||||
const debug = getEnv("DEBUG");
|
||||
return (
|
||||
(Boolean(debug) && debug?.includes("llamaindex")) ||
|
||||
debug === "*" ||
|
||||
debug === "true"
|
||||
getEnv("NODE_ENV") === "development" &&
|
||||
Boolean(debug) &&
|
||||
debug?.includes("llamaindex")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,6 @@ export class AnthropicAgent extends AgentRunner<Anthropic> {
|
||||
"tools" in params
|
||||
? params.tools
|
||||
: params.toolRetriever.retrieve.bind(params.toolRetriever),
|
||||
verbose: params.verbose ?? false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -68,8 +67,12 @@ export class AnthropicAgent extends AgentRunner<Anthropic> {
|
||||
return super.chat(params);
|
||||
}
|
||||
|
||||
static taskHandler: TaskHandler<Anthropic> = async (step, enqueueOutput) => {
|
||||
static taskHandler: TaskHandler<Anthropic> = async (step) => {
|
||||
const { input } = step;
|
||||
const { llm, getTools, stream } = step.context;
|
||||
if (input) {
|
||||
step.context.store.messages = [...step.context.store.messages, input];
|
||||
}
|
||||
const lastMessage = step.context.store.messages.at(-1)!.content;
|
||||
const tools = await getTools(lastMessage);
|
||||
if (stream === true) {
|
||||
@@ -85,36 +88,37 @@ export class AnthropicAgent extends AgentRunner<Anthropic> {
|
||||
response.message,
|
||||
];
|
||||
const options = response.message.options ?? {};
|
||||
enqueueOutput({
|
||||
taskStep: step,
|
||||
output: response,
|
||||
isLast: !("toolCall" in options),
|
||||
});
|
||||
if ("toolCall" in options) {
|
||||
const { toolCall } = options;
|
||||
const targetTool = tools.find(
|
||||
(tool) => tool.metadata.name === toolCall.name,
|
||||
);
|
||||
const toolOutput = await callTool(
|
||||
targetTool,
|
||||
toolCall,
|
||||
step.context.logger,
|
||||
);
|
||||
const toolOutput = await callTool(targetTool, toolCall);
|
||||
step.context.store.toolOutputs.push(toolOutput);
|
||||
step.context.store.messages = [
|
||||
...step.context.store.messages,
|
||||
{
|
||||
content: stringifyJSONToMessageContent(toolOutput.output),
|
||||
role: "user",
|
||||
options: {
|
||||
toolResult: {
|
||||
result: toolOutput.output,
|
||||
isError: toolOutput.isError,
|
||||
id: toolCall.id,
|
||||
return {
|
||||
taskStep: step,
|
||||
output: {
|
||||
raw: response.raw,
|
||||
message: {
|
||||
content: stringifyJSONToMessageContent(toolOutput.output),
|
||||
role: "user",
|
||||
options: {
|
||||
toolResult: {
|
||||
result: toolOutput.output,
|
||||
isError: toolOutput.isError,
|
||||
id: toolCall.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
isLast: false,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
taskStep: step,
|
||||
output: response,
|
||||
isLast: true,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,14 +4,12 @@ import {
|
||||
pipeline,
|
||||
randomUUID,
|
||||
} from "@llamaindex/env";
|
||||
import { Settings } from "../Settings.js";
|
||||
import {
|
||||
type ChatEngine,
|
||||
type ChatEngineParamsNonStreaming,
|
||||
type ChatEngineParamsStreaming,
|
||||
} from "../engines/chat/index.js";
|
||||
import { wrapEventCaller } from "../internal/context/EventCaller.js";
|
||||
import { consoleLogger, emptyLogger } from "../internal/logger.js";
|
||||
import { getCallbackManager } from "../internal/settings/CallbackManager.js";
|
||||
import { isAsyncIterable } from "../internal/utils.js";
|
||||
import type {
|
||||
@@ -29,10 +27,14 @@ import type {
|
||||
TaskStep,
|
||||
TaskStepOutput,
|
||||
} from "./types.js";
|
||||
import { consumeAsyncIterable } from "./utils.js";
|
||||
|
||||
export const MAX_TOOL_CALLS = 10;
|
||||
|
||||
export function createTaskOutputStream<
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export async function* createTaskImpl<
|
||||
Model extends LLM,
|
||||
Store extends object = {},
|
||||
AdditionalMessageOptions extends object = Model extends LLM<
|
||||
@@ -44,67 +46,65 @@ export function createTaskOutputStream<
|
||||
>(
|
||||
handler: TaskHandler<Model, Store, AdditionalMessageOptions>,
|
||||
context: AgentTaskContext<Model, Store, AdditionalMessageOptions>,
|
||||
): ReadableStream<TaskStepOutput<Model, Store, AdditionalMessageOptions>> {
|
||||
const steps: TaskStep<Model, Store, AdditionalMessageOptions>[] = [];
|
||||
return new ReadableStream<
|
||||
TaskStepOutput<Model, Store, AdditionalMessageOptions>
|
||||
>({
|
||||
pull: async (controller) => {
|
||||
const step: TaskStep<Model, Store, AdditionalMessageOptions> = {
|
||||
id: randomUUID(),
|
||||
context,
|
||||
prevStep: null,
|
||||
nextSteps: new Set(),
|
||||
};
|
||||
if (steps.length > 0) {
|
||||
step.prevStep = steps[steps.length - 1];
|
||||
}
|
||||
const taskOutputs: TaskStepOutput<
|
||||
Model,
|
||||
Store,
|
||||
AdditionalMessageOptions
|
||||
>[] = [];
|
||||
steps.push(step);
|
||||
const enqueueOutput = (
|
||||
output: TaskStepOutput<Model, Store, AdditionalMessageOptions>,
|
||||
) => {
|
||||
context.logger.log("Enqueueing output for step(id, %s).", step.id);
|
||||
taskOutputs.push(output);
|
||||
controller.enqueue(output);
|
||||
};
|
||||
_input: ChatMessage<AdditionalMessageOptions>,
|
||||
): AsyncGenerator<TaskStepOutput<Model, Store, AdditionalMessageOptions>> {
|
||||
let isFirst = true;
|
||||
let isDone = false;
|
||||
let input: ChatMessage<AdditionalMessageOptions> | null = _input;
|
||||
let prevStep: TaskStep<Model, Store, AdditionalMessageOptions> | null = null;
|
||||
while (!isDone) {
|
||||
const step: TaskStep<Model, Store, AdditionalMessageOptions> = {
|
||||
id: randomUUID(),
|
||||
input,
|
||||
context,
|
||||
prevStep,
|
||||
nextSteps: new Set(),
|
||||
};
|
||||
if (prevStep) {
|
||||
prevStep.nextSteps.add(step);
|
||||
}
|
||||
const prevToolCallCount = step.context.toolCallCount;
|
||||
if (!step.context.shouldContinue(step)) {
|
||||
throw new Error("Tool call count exceeded limit");
|
||||
}
|
||||
if (isFirst) {
|
||||
getCallbackManager().dispatchEvent("agent-start", {
|
||||
payload: {
|
||||
startStep: step,
|
||||
},
|
||||
});
|
||||
|
||||
context.logger.log("Starting step(id, %s).", step.id);
|
||||
await handler(step, enqueueOutput);
|
||||
context.logger.log("Finished step(id, %s).", step.id);
|
||||
// fixme: support multi-thread when there are multiple outputs
|
||||
// todo: for now we pretend there is only one task output
|
||||
const { isLast, taskStep } = taskOutputs[0];
|
||||
context = {
|
||||
...taskStep.context,
|
||||
store: {
|
||||
...taskStep.context.store,
|
||||
},
|
||||
toolCallCount: 1,
|
||||
};
|
||||
if (isLast) {
|
||||
context.logger.log(
|
||||
"Final step(id, %s) reached, closing task.",
|
||||
step.id,
|
||||
);
|
||||
getCallbackManager().dispatchEvent("agent-end", {
|
||||
payload: {
|
||||
endStep: step,
|
||||
},
|
||||
});
|
||||
controller.close();
|
||||
isFirst = false;
|
||||
}
|
||||
const taskOutput = await handler(step);
|
||||
const { isLast, output, taskStep } = taskOutput;
|
||||
// do not consume last output
|
||||
if (!isLast) {
|
||||
if (output) {
|
||||
input = isAsyncIterable(output)
|
||||
? await consumeAsyncIterable(output)
|
||||
: output.message;
|
||||
} else {
|
||||
input = null;
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
context = {
|
||||
...taskStep.context,
|
||||
store: {
|
||||
...taskStep.context.store,
|
||||
},
|
||||
toolCallCount: prevToolCallCount + 1,
|
||||
};
|
||||
if (isLast) {
|
||||
isDone = true;
|
||||
getCallbackManager().dispatchEvent("agent-end", {
|
||||
payload: {
|
||||
endStep: step,
|
||||
},
|
||||
});
|
||||
}
|
||||
prevStep = taskStep;
|
||||
yield taskOutput;
|
||||
}
|
||||
}
|
||||
|
||||
export type AgentStreamChatResponse<Options extends object> = {
|
||||
@@ -134,7 +134,6 @@ export type AgentRunnerParams<
|
||||
tools:
|
||||
| BaseToolWithCall[]
|
||||
| ((query: MessageContent) => Promise<BaseToolWithCall[]>);
|
||||
verbose: boolean;
|
||||
};
|
||||
|
||||
export type AgentParamsBase<
|
||||
@@ -149,7 +148,6 @@ export type AgentParamsBase<
|
||||
llm?: AI;
|
||||
chatHistory?: ChatMessage<AdditionalMessageOptions>[];
|
||||
systemPrompt?: MessageContent;
|
||||
verbose?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -172,16 +170,15 @@ export abstract class AgentWorker<
|
||||
query: string,
|
||||
context: AgentTaskContext<AI, Store, AdditionalMessageOptions>,
|
||||
): ReadableStream<TaskStepOutput<AI, Store, AdditionalMessageOptions>> {
|
||||
context.store.messages.push({
|
||||
const taskGenerator = createTaskImpl(this.taskHandler, context, {
|
||||
role: "user",
|
||||
content: query,
|
||||
});
|
||||
const taskOutputStream = createTaskOutputStream(this.taskHandler, context);
|
||||
return new ReadableStream<
|
||||
TaskStepOutput<AI, Store, AdditionalMessageOptions>
|
||||
>({
|
||||
start: async (controller) => {
|
||||
for await (const stepOutput of taskOutputStream) {
|
||||
for await (const stepOutput of taskGenerator) {
|
||||
this.#taskSet.add(stepOutput.taskStep);
|
||||
controller.enqueue(stepOutput);
|
||||
if (stepOutput.isLast) {
|
||||
@@ -229,7 +226,6 @@ export abstract class AgentRunner<
|
||||
readonly #systemPrompt: MessageContent | null = null;
|
||||
#chatHistory: ChatMessage<AdditionalMessageOptions>[];
|
||||
readonly #runner: AgentWorker<AI, Store, AdditionalMessageOptions>;
|
||||
readonly #verbose: boolean;
|
||||
|
||||
// create extra store
|
||||
abstract createStore(): Store;
|
||||
@@ -241,15 +237,14 @@ export abstract class AgentRunner<
|
||||
protected constructor(
|
||||
params: AgentRunnerParams<AI, Store, AdditionalMessageOptions>,
|
||||
) {
|
||||
const { llm, chatHistory, systemPrompt, runner, tools, verbose } = params;
|
||||
const { llm, chatHistory, runner, tools } = params;
|
||||
this.#llm = llm;
|
||||
this.#chatHistory = chatHistory;
|
||||
this.#runner = runner;
|
||||
if (systemPrompt) {
|
||||
this.#systemPrompt = systemPrompt;
|
||||
if (params.systemPrompt) {
|
||||
this.#systemPrompt = params.systemPrompt;
|
||||
}
|
||||
this.#tools = tools;
|
||||
this.#verbose = verbose;
|
||||
}
|
||||
|
||||
get llm() {
|
||||
@@ -260,10 +255,6 @@ export abstract class AgentRunner<
|
||||
return this.#chatHistory;
|
||||
}
|
||||
|
||||
get verbose(): boolean {
|
||||
return Settings.debug || this.#verbose;
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this.#chatHistory = [];
|
||||
}
|
||||
@@ -287,11 +278,8 @@ export abstract class AgentRunner<
|
||||
return task.context.toolCallCount < MAX_TOOL_CALLS;
|
||||
}
|
||||
|
||||
createTask(
|
||||
message: MessageContent,
|
||||
stream: boolean = false,
|
||||
verbose: boolean | undefined = undefined,
|
||||
) {
|
||||
// fixme: this shouldn't be async
|
||||
async createTask(message: MessageContent, stream: boolean = false) {
|
||||
const initialMessages = [...this.#chatHistory];
|
||||
if (this.#systemPrompt !== null) {
|
||||
const systemPrompt = this.#systemPrompt;
|
||||
@@ -316,13 +304,6 @@ export abstract class AgentRunner<
|
||||
toolOutputs: [] as ToolOutput[],
|
||||
},
|
||||
shouldContinue: AgentRunner.shouldContinue,
|
||||
logger:
|
||||
// disable verbose if explicitly set to false
|
||||
verbose === false
|
||||
? emptyLogger
|
||||
: verbose || this.verbose
|
||||
? consoleLogger
|
||||
: emptyLogger,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -339,7 +320,7 @@ export abstract class AgentRunner<
|
||||
| AgentChatResponse<AdditionalMessageOptions>
|
||||
| ReadableStream<AgentStreamChatResponse<AdditionalMessageOptions>>
|
||||
> {
|
||||
const task = this.createTask(params.message, !!params.stream);
|
||||
const task = await this.createTask(params.message, !!params.stream);
|
||||
const stepOutput = await pipeline(
|
||||
task,
|
||||
async (
|
||||
|
||||
@@ -46,14 +46,17 @@ export class OpenAIAgent extends AgentRunner<OpenAI> {
|
||||
"tools" in params
|
||||
? params.tools
|
||||
: params.toolRetriever.retrieve.bind(params.toolRetriever),
|
||||
verbose: params.verbose ?? false,
|
||||
});
|
||||
}
|
||||
|
||||
createStore = AgentRunner.defaultCreateStore;
|
||||
|
||||
static taskHandler: TaskHandler<OpenAI> = async (step, enqueueOutput) => {
|
||||
static taskHandler: TaskHandler<OpenAI> = async (step) => {
|
||||
const { input } = step;
|
||||
const { llm, stream, getTools } = step.context;
|
||||
if (input) {
|
||||
step.context.store.messages = [...step.context.store.messages, input];
|
||||
}
|
||||
const lastMessage = step.context.store.messages.at(-1)!.content;
|
||||
const tools = await getTools(lastMessage);
|
||||
const response = await llm.chat({
|
||||
@@ -68,36 +71,37 @@ export class OpenAIAgent extends AgentRunner<OpenAI> {
|
||||
response.message,
|
||||
];
|
||||
const options = response.message.options ?? {};
|
||||
enqueueOutput({
|
||||
taskStep: step,
|
||||
output: response,
|
||||
isLast: !("toolCall" in options),
|
||||
});
|
||||
if ("toolCall" in options) {
|
||||
const { toolCall } = options;
|
||||
const targetTool = tools.find(
|
||||
(tool) => tool.metadata.name === toolCall.name,
|
||||
);
|
||||
const toolOutput = await callTool(
|
||||
targetTool,
|
||||
toolCall,
|
||||
step.context.logger,
|
||||
);
|
||||
const toolOutput = await callTool(targetTool, toolCall);
|
||||
step.context.store.toolOutputs.push(toolOutput);
|
||||
step.context.store.messages = [
|
||||
...step.context.store.messages,
|
||||
{
|
||||
role: "user" as const,
|
||||
content: stringifyJSONToMessageContent(toolOutput.output),
|
||||
options: {
|
||||
toolResult: {
|
||||
result: toolOutput.output,
|
||||
isError: toolOutput.isError,
|
||||
id: toolCall.id,
|
||||
return {
|
||||
taskStep: step,
|
||||
output: {
|
||||
raw: response.raw,
|
||||
message: {
|
||||
content: stringifyJSONToMessageContent(toolOutput.output),
|
||||
role: "user",
|
||||
options: {
|
||||
toolResult: {
|
||||
result: toolOutput.output,
|
||||
isError: toolOutput.isError,
|
||||
id: toolCall.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
isLast: false,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
taskStep: step,
|
||||
output: response,
|
||||
isLast: true,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
const responseChunkStream = new ReadableStream<
|
||||
@@ -122,11 +126,6 @@ export class OpenAIAgent extends AgentRunner<OpenAI> {
|
||||
// check if first chunk has tool calls, if so, this is a function call
|
||||
// otherwise, it's a regular message
|
||||
const hasToolCall = !!(value.options && "toolCall" in value.options);
|
||||
enqueueOutput({
|
||||
taskStep: step,
|
||||
output: finalStream,
|
||||
isLast: !hasToolCall,
|
||||
});
|
||||
|
||||
if (hasToolCall) {
|
||||
// you need to consume the response to get the full toolCalls
|
||||
@@ -159,11 +158,7 @@ export class OpenAIAgent extends AgentRunner<OpenAI> {
|
||||
},
|
||||
},
|
||||
];
|
||||
const toolOutput = await callTool(
|
||||
targetTool,
|
||||
toolCall,
|
||||
step.context.logger,
|
||||
);
|
||||
const toolOutput = await callTool(targetTool, toolCall);
|
||||
step.context.store.messages = [
|
||||
...step.context.store.messages,
|
||||
{
|
||||
@@ -180,6 +175,17 @@ export class OpenAIAgent extends AgentRunner<OpenAI> {
|
||||
];
|
||||
step.context.store.toolOutputs.push(toolOutput);
|
||||
}
|
||||
return {
|
||||
taskStep: step,
|
||||
output: null,
|
||||
isLast: false,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
taskStep: step,
|
||||
output: finalStream,
|
||||
isLast: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { randomUUID, ReadableStream } from "@llamaindex/env";
|
||||
import { pipeline, randomUUID } from "@llamaindex/env";
|
||||
import { Settings } from "../Settings.js";
|
||||
import { getReACTAgentSystemHeader } from "../internal/prompt/react.js";
|
||||
import {
|
||||
isAsyncIterable,
|
||||
@@ -12,7 +13,6 @@ import {
|
||||
} from "../llm/index.js";
|
||||
import { extractText } from "../llm/utils.js";
|
||||
import { ObjectRetriever } from "../objects/index.js";
|
||||
import { Settings } from "../Settings.js";
|
||||
import type {
|
||||
BaseTool,
|
||||
BaseToolWithCall,
|
||||
@@ -60,7 +60,7 @@ type ActionReason = BaseReason & {
|
||||
type ResponseReason = BaseReason & {
|
||||
type: "response";
|
||||
thought: string;
|
||||
response: ChatResponse;
|
||||
response: ChatResponse | AsyncIterable<ChatResponseChunk>;
|
||||
};
|
||||
|
||||
type Reason = ObservationReason | ActionReason | ResponseReason;
|
||||
@@ -74,9 +74,16 @@ function reasonFormatter(reason: Reason): string | Promise<string> {
|
||||
reason.input,
|
||||
)}`;
|
||||
case "response": {
|
||||
return `Thought: ${reason.thought}\nAnswer: ${extractText(
|
||||
reason.response.message.content,
|
||||
)}`;
|
||||
if (isAsyncIterable(reason.response)) {
|
||||
return consumeAsyncIterable(reason.response).then(
|
||||
(message) =>
|
||||
`Thought: ${reason.thought}\nAnswer: ${extractText(message.content)}`,
|
||||
);
|
||||
} else {
|
||||
return `Thought: ${reason.thought}\nAnswer: ${extractText(
|
||||
reason.response.message.content,
|
||||
)}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -139,52 +146,35 @@ function actionInputParser(jsonStr: string): JSONObject {
|
||||
|
||||
type ReACTOutputParser = <Options extends object>(
|
||||
output: ChatResponse<Options> | AsyncIterable<ChatResponseChunk<Options>>,
|
||||
onResolveType: (
|
||||
type: "action" | "thought" | "answer",
|
||||
response:
|
||||
| ChatResponse<Options>
|
||||
| ReadableStream<ChatResponseChunk<Options>>,
|
||||
) => void,
|
||||
) => Promise<Reason>;
|
||||
|
||||
const reACTOutputParser: ReACTOutputParser = async (
|
||||
output,
|
||||
onResolveType,
|
||||
): Promise<Reason> => {
|
||||
let reason: Reason | null = null;
|
||||
|
||||
if (isAsyncIterable(output)) {
|
||||
const [peakStream, finalStream] = createReadableStream(output).tee();
|
||||
const reader = peakStream.getReader();
|
||||
let type: "action" | "thought" | "answer" | null = null;
|
||||
let content = "";
|
||||
do {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
const type = await pipeline(peakStream, async (iter) => {
|
||||
let content = "";
|
||||
for await (const chunk of iter) {
|
||||
content += chunk.delta;
|
||||
if (content.includes("Action:")) {
|
||||
return "action";
|
||||
} else if (content.includes("Answer:")) {
|
||||
return "answer";
|
||||
} else if (content.includes("Thought:")) {
|
||||
return "thought";
|
||||
}
|
||||
}
|
||||
content += value.delta;
|
||||
if (content.includes("Action:")) {
|
||||
type = "action";
|
||||
} else if (content.includes("Answer:")) {
|
||||
type = "answer";
|
||||
}
|
||||
} while (true);
|
||||
if (type === null) {
|
||||
// `Thought:` is always present at the beginning of the output.
|
||||
type = "thought";
|
||||
}
|
||||
reader.releaseLock();
|
||||
if (!type) {
|
||||
throw new Error("Could not determine type of output");
|
||||
}
|
||||
onResolveType(type, finalStream);
|
||||
});
|
||||
// step 2: do the parsing from content
|
||||
switch (type) {
|
||||
case "action": {
|
||||
// have to consume the stream to get the full content
|
||||
const response = await consumeAsyncIterable(peakStream, content);
|
||||
const [thought, action, input] = extractToolUse(response.content);
|
||||
const response = await consumeAsyncIterable(finalStream);
|
||||
const { content } = response;
|
||||
const [thought, action, input] = extractToolUse(content);
|
||||
const jsonStr = extractJsonStr(input);
|
||||
let json: JSONObject;
|
||||
try {
|
||||
@@ -202,20 +192,18 @@ const reACTOutputParser: ReACTOutputParser = async (
|
||||
}
|
||||
case "thought": {
|
||||
const thought = "(Implicit) I can answer without any more tools!";
|
||||
const response = await consumeAsyncIterable(peakStream, content);
|
||||
reason = {
|
||||
type: "response",
|
||||
thought,
|
||||
response: {
|
||||
raw: peakStream,
|
||||
message: response,
|
||||
},
|
||||
// bypass the response, because here we don't need to do anything with it
|
||||
response: finalStream,
|
||||
};
|
||||
break;
|
||||
}
|
||||
case "answer": {
|
||||
const response = await consumeAsyncIterable(peakStream, content);
|
||||
const [thought, answer] = extractFinalResponse(response.content);
|
||||
const response = await consumeAsyncIterable(finalStream);
|
||||
const { content } = response;
|
||||
const [thought, answer] = extractFinalResponse(content);
|
||||
reason = {
|
||||
type: "response",
|
||||
thought,
|
||||
@@ -239,9 +227,7 @@ const reACTOutputParser: ReACTOutputParser = async (
|
||||
? "answer"
|
||||
: content.includes("Action:")
|
||||
? "action"
|
||||
: // `Thought:` is always present at the beginning of the output.
|
||||
"thought";
|
||||
onResolveType(type, output);
|
||||
: "thought";
|
||||
|
||||
// step 2: do the parsing from content
|
||||
switch (type) {
|
||||
@@ -354,7 +340,6 @@ export class ReActAgent extends AgentRunner<LLM, ReACTAgentStore> {
|
||||
"tools" in params
|
||||
? params.tools
|
||||
: params.toolRetriever.retrieve.bind(params.toolRetriever),
|
||||
verbose: params.verbose ?? false,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -364,11 +349,12 @@ export class ReActAgent extends AgentRunner<LLM, ReACTAgentStore> {
|
||||
};
|
||||
}
|
||||
|
||||
static taskHandler: TaskHandler<LLM, ReACTAgentStore> = async (
|
||||
step,
|
||||
enqueueOutput,
|
||||
) => {
|
||||
static taskHandler: TaskHandler<LLM, ReACTAgentStore> = async (step) => {
|
||||
const { llm, stream, getTools } = step.context;
|
||||
const input = step.input;
|
||||
if (input) {
|
||||
step.context.store.messages.push(input);
|
||||
}
|
||||
const lastMessage = step.context.store.messages.at(-1)!.content;
|
||||
const tools = await getTools(lastMessage);
|
||||
const messages = await chatFormatter(
|
||||
@@ -381,33 +367,35 @@ export class ReActAgent extends AgentRunner<LLM, ReACTAgentStore> {
|
||||
stream,
|
||||
messages,
|
||||
});
|
||||
const reason = await reACTOutputParser(response, (type, response) => {
|
||||
enqueueOutput({
|
||||
taskStep: step,
|
||||
output: response,
|
||||
isLast: type !== "action",
|
||||
});
|
||||
});
|
||||
step.context.logger.log("current reason: %O", reason);
|
||||
const reason = await reACTOutputParser(response);
|
||||
step.context.store.reasons = [...step.context.store.reasons, reason];
|
||||
if (reason.type === "action") {
|
||||
const tool = tools.find((tool) => tool.metadata.name === reason.action);
|
||||
const toolOutput = await callTool(
|
||||
tool,
|
||||
{
|
||||
if (reason.type === "response") {
|
||||
return {
|
||||
isLast: true,
|
||||
output: response,
|
||||
taskStep: step,
|
||||
};
|
||||
} else {
|
||||
if (reason.type === "action") {
|
||||
const tool = tools.find((tool) => tool.metadata.name === reason.action);
|
||||
const toolOutput = await callTool(tool, {
|
||||
id: randomUUID(),
|
||||
input: reason.input,
|
||||
name: reason.action,
|
||||
},
|
||||
step.context.logger,
|
||||
);
|
||||
step.context.store.reasons = [
|
||||
...step.context.store.reasons,
|
||||
{
|
||||
type: "observation",
|
||||
observation: toolOutput.output,
|
||||
},
|
||||
];
|
||||
});
|
||||
step.context.store.reasons = [
|
||||
...step.context.store.reasons,
|
||||
{
|
||||
type: "observation",
|
||||
observation: toolOutput.output,
|
||||
},
|
||||
];
|
||||
}
|
||||
return {
|
||||
isLast: false,
|
||||
output: null,
|
||||
taskStep: step,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { ReadableStream } from "@llamaindex/env";
|
||||
import type { Logger } from "../internal/logger.js";
|
||||
import type { BaseEvent } from "../internal/type.js";
|
||||
import type {
|
||||
ChatMessage,
|
||||
@@ -33,7 +32,6 @@ export type AgentTaskContext<
|
||||
toolOutputs: ToolOutput[];
|
||||
messages: ChatMessage<AdditionalMessageOptions>[];
|
||||
} & Store;
|
||||
logger: Readonly<Logger>;
|
||||
};
|
||||
|
||||
export type TaskStep<
|
||||
@@ -47,6 +45,7 @@ export type TaskStep<
|
||||
: never,
|
||||
> = {
|
||||
id: UUID;
|
||||
input: ChatMessage<AdditionalMessageOptions> | null;
|
||||
context: AgentTaskContext<Model, Store, AdditionalMessageOptions>;
|
||||
|
||||
// linked list
|
||||
@@ -63,14 +62,22 @@ export type TaskStepOutput<
|
||||
>
|
||||
? AdditionalMessageOptions
|
||||
: never,
|
||||
> = {
|
||||
taskStep: TaskStep<Model, Store, AdditionalMessageOptions>;
|
||||
// output shows the response to the user
|
||||
output:
|
||||
| ChatResponse<AdditionalMessageOptions>
|
||||
| ReadableStream<ChatResponseChunk<AdditionalMessageOptions>>;
|
||||
isLast: boolean;
|
||||
};
|
||||
> =
|
||||
| {
|
||||
taskStep: TaskStep<Model, Store, AdditionalMessageOptions>;
|
||||
output:
|
||||
| null
|
||||
| ChatResponse<AdditionalMessageOptions>
|
||||
| ReadableStream<ChatResponseChunk<AdditionalMessageOptions>>;
|
||||
isLast: false;
|
||||
}
|
||||
| {
|
||||
taskStep: TaskStep<Model, Store, AdditionalMessageOptions>;
|
||||
output:
|
||||
| ChatResponse<AdditionalMessageOptions>
|
||||
| ReadableStream<ChatResponseChunk<AdditionalMessageOptions>>;
|
||||
isLast: true;
|
||||
};
|
||||
|
||||
export type TaskHandler<
|
||||
Model extends LLM,
|
||||
@@ -83,10 +90,7 @@ export type TaskHandler<
|
||||
: never,
|
||||
> = (
|
||||
step: TaskStep<Model, Store, AdditionalMessageOptions>,
|
||||
enqueueOutput: (
|
||||
taskOutput: TaskStepOutput<Model, Store, AdditionalMessageOptions>,
|
||||
) => void,
|
||||
) => Promise<void>;
|
||||
) => Promise<TaskStepOutput<Model, Store, AdditionalMessageOptions>>;
|
||||
|
||||
export type AgentStartEvent = BaseEvent<{
|
||||
startStep: TaskStep;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { ReadableStream } from "@llamaindex/env";
|
||||
import type { Logger } from "../internal/logger.js";
|
||||
import { getCallbackManager } from "../internal/settings/CallbackManager.js";
|
||||
import { isAsyncIterable, prettifyError } from "../internal/utils.js";
|
||||
import type {
|
||||
@@ -14,14 +13,12 @@ import type { BaseTool, JSONObject, JSONValue, ToolOutput } from "../types.js";
|
||||
export async function callTool(
|
||||
tool: BaseTool | undefined,
|
||||
toolCall: ToolCall | PartialToolCall,
|
||||
logger: Logger,
|
||||
): Promise<ToolOutput> {
|
||||
const input: JSONObject =
|
||||
typeof toolCall.input === "string"
|
||||
? JSON.parse(toolCall.input)
|
||||
: toolCall.input;
|
||||
if (!tool) {
|
||||
logger.error(`Tool ${toolCall.name} does not exist.`);
|
||||
const output = `Tool ${toolCall.name} does not exist.`;
|
||||
return {
|
||||
tool,
|
||||
@@ -33,9 +30,6 @@ export async function callTool(
|
||||
const call = tool.call;
|
||||
let output: JSONValue;
|
||||
if (!call) {
|
||||
logger.error(
|
||||
`Tool ${tool.metadata.name} (remote:${toolCall.name}) does not have a implementation.`,
|
||||
);
|
||||
output = `Tool ${tool.metadata.name} (remote:${toolCall.name}) does not have a implementation.`;
|
||||
return {
|
||||
tool,
|
||||
@@ -51,10 +45,6 @@ export async function callTool(
|
||||
},
|
||||
});
|
||||
output = await call.call(tool, input);
|
||||
logger.log(
|
||||
`Tool ${tool.metadata.name} (remote:${toolCall.name}) succeeded.`,
|
||||
);
|
||||
logger.log(`Output: ${JSON.stringify(output)}`);
|
||||
const toolOutput: ToolOutput = {
|
||||
tool,
|
||||
input,
|
||||
@@ -70,9 +60,6 @@ export async function callTool(
|
||||
return toolOutput;
|
||||
} catch (e) {
|
||||
output = prettifyError(e);
|
||||
logger.error(
|
||||
`Tool ${tool.metadata.name} (remote:${toolCall.name}) failed: ${output}`,
|
||||
);
|
||||
}
|
||||
return {
|
||||
tool,
|
||||
@@ -84,19 +71,16 @@ export async function callTool(
|
||||
|
||||
export async function consumeAsyncIterable<Options extends object>(
|
||||
input: ChatMessage<Options>,
|
||||
previousContent?: string,
|
||||
): Promise<ChatMessage<Options>>;
|
||||
export async function consumeAsyncIterable<Options extends object>(
|
||||
input: AsyncIterable<ChatResponseChunk<Options>>,
|
||||
previousContent?: string,
|
||||
): Promise<TextChatMessage<Options>>;
|
||||
export async function consumeAsyncIterable<Options extends object>(
|
||||
input: ChatMessage<Options> | AsyncIterable<ChatResponseChunk<Options>>,
|
||||
previousContent: string = "",
|
||||
): Promise<ChatMessage<Options>> {
|
||||
if (isAsyncIterable(input)) {
|
||||
const result: ChatMessage<Options> = {
|
||||
content: previousContent,
|
||||
content: "",
|
||||
// only assistant will give streaming response
|
||||
role: "assistant",
|
||||
options: {} as Options,
|
||||
|
||||
@@ -212,13 +212,10 @@ export class CallbackManager implements CallbackManagerMethods {
|
||||
if (!handlers) {
|
||||
return;
|
||||
}
|
||||
const clone = structuredClone(detail);
|
||||
queueMicrotask(() => {
|
||||
handlers.forEach((handler) =>
|
||||
handler(
|
||||
LlamaIndexCustomEvent.fromEvent(event, {
|
||||
...detail,
|
||||
}),
|
||||
),
|
||||
handler(LlamaIndexCustomEvent.fromEvent(event, clone)),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,20 +1,6 @@
|
||||
import _ from "lodash";
|
||||
import type { ImageType } from "../Node.js";
|
||||
import { MultiModalEmbedding } from "./MultiModalEmbedding.js";
|
||||
|
||||
async function readImage(input: ImageType) {
|
||||
const { RawImage } = await import(
|
||||
/* webpackIgnore: true */
|
||||
"@xenova/transformers"
|
||||
);
|
||||
if (input instanceof Blob) {
|
||||
return await RawImage.fromBlob(input);
|
||||
} else if (_.isString(input) || input instanceof URL) {
|
||||
return await RawImage.fromURL(input);
|
||||
} else {
|
||||
throw new Error(`Unsupported input type: ${typeof input}`);
|
||||
}
|
||||
}
|
||||
import { readImage } from "./utils.js";
|
||||
|
||||
export enum ClipEmbeddingModelType {
|
||||
XENOVA_CLIP_VIT_BASE_PATCH32 = "Xenova/clip-vit-base-patch32",
|
||||
@@ -32,10 +18,7 @@ export class ClipEmbedding extends MultiModalEmbedding {
|
||||
|
||||
async getTokenizer() {
|
||||
if (!this.tokenizer) {
|
||||
const { AutoTokenizer } = await import(
|
||||
/* webpackIgnore: true */
|
||||
"@xenova/transformers"
|
||||
);
|
||||
const { AutoTokenizer } = await import("@xenova/transformers");
|
||||
this.tokenizer = await AutoTokenizer.from_pretrained(this.modelType);
|
||||
}
|
||||
return this.tokenizer;
|
||||
@@ -43,10 +26,7 @@ export class ClipEmbedding extends MultiModalEmbedding {
|
||||
|
||||
async getProcessor() {
|
||||
if (!this.processor) {
|
||||
const { AutoProcessor } = await import(
|
||||
/* webpackIgnore: true */
|
||||
"@xenova/transformers"
|
||||
);
|
||||
const { AutoProcessor } = await import("@xenova/transformers");
|
||||
this.processor = await AutoProcessor.from_pretrained(this.modelType);
|
||||
}
|
||||
return this.processor;
|
||||
@@ -55,7 +35,6 @@ export class ClipEmbedding extends MultiModalEmbedding {
|
||||
async getVisionModel() {
|
||||
if (!this.visionModel) {
|
||||
const { CLIPVisionModelWithProjection } = await import(
|
||||
/* webpackIgnore: true */
|
||||
"@xenova/transformers"
|
||||
);
|
||||
this.visionModel = await CLIPVisionModelWithProjection.from_pretrained(
|
||||
@@ -69,7 +48,6 @@ export class ClipEmbedding extends MultiModalEmbedding {
|
||||
async getTextModel() {
|
||||
if (!this.textModel) {
|
||||
const { CLIPTextModelWithProjection } = await import(
|
||||
/* webpackIgnore: true */
|
||||
"@xenova/transformers"
|
||||
);
|
||||
this.textModel = await CLIPTextModelWithProjection.from_pretrained(
|
||||
|
||||
@@ -1,21 +1,27 @@
|
||||
import { GeminiSessionStore, type GeminiSession } from "../llm/gemini.js";
|
||||
import {
|
||||
GEMINI_MODEL,
|
||||
GeminiSessionStore,
|
||||
type GeminiConfig,
|
||||
type GeminiSession,
|
||||
} from "../llm/gemini.js";
|
||||
import { BaseEmbedding } from "./types.js";
|
||||
|
||||
export enum GEMINI_EMBEDDING_MODEL {
|
||||
EMBEDDING_001 = "embedding-001",
|
||||
TEXT_EMBEDDING_004 = "text-embedding-004",
|
||||
}
|
||||
|
||||
/**
|
||||
* GeminiEmbedding is an alias for Gemini that implements the BaseEmbedding interface.
|
||||
*/
|
||||
export class GeminiEmbedding extends BaseEmbedding {
|
||||
model: GEMINI_EMBEDDING_MODEL;
|
||||
model: GEMINI_MODEL;
|
||||
temperature: number;
|
||||
topP: number;
|
||||
maxTokens?: number;
|
||||
session: GeminiSession;
|
||||
|
||||
constructor(init?: Partial<GeminiEmbedding>) {
|
||||
constructor(init?: GeminiConfig) {
|
||||
super();
|
||||
this.model = init?.model ?? GEMINI_EMBEDDING_MODEL.EMBEDDING_001;
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
export * from "./ClipEmbedding.js";
|
||||
export * from "./GeminiEmbedding.js";
|
||||
export * from "./JinaAIEmbedding.js";
|
||||
export * from "./MistralAIEmbedding.js";
|
||||
export * from "./MultiModalEmbedding.js";
|
||||
export { OllamaEmbedding } from "./OllamaEmbedding.js";
|
||||
export * from "./OpenAIEmbedding.js";
|
||||
export { FireworksEmbedding } from "./fireworks.js";
|
||||
export { TogetherEmbedding } from "./together.js";
|
||||
|
||||
@@ -208,6 +208,17 @@ async function blobToDataUrl(input: Blob) {
|
||||
return "data:" + mimes[0] + ";base64," + buffer.toString("base64");
|
||||
}
|
||||
|
||||
export async function readImage(input: ImageType) {
|
||||
const { RawImage } = await import("@xenova/transformers");
|
||||
if (input instanceof Blob) {
|
||||
return await RawImage.fromBlob(input);
|
||||
} else if (_.isString(input) || input instanceof URL) {
|
||||
return await RawImage.fromURL(input);
|
||||
} else {
|
||||
throw new Error(`Unsupported input type: ${typeof input}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function imageToString(input: ImageType): Promise<string> {
|
||||
if (input instanceof Blob) {
|
||||
// if the image is a Blob, convert it to a base64 data URL
|
||||
|
||||
@@ -13,11 +13,6 @@ export interface ChatEngineParamsBase {
|
||||
* Optional chat history if you want to customize the chat history.
|
||||
*/
|
||||
chatHistory?: ChatMessage[] | ChatHistory;
|
||||
/**
|
||||
* Optional flag to enable verbose mode.
|
||||
* @default false
|
||||
*/
|
||||
verbose?: boolean;
|
||||
}
|
||||
|
||||
export interface ChatEngineParamsStreaming extends ChatEngineParamsBase {
|
||||
|
||||
@@ -27,7 +27,6 @@ export * from "./objects/index.js";
|
||||
export * from "./postprocessors/index.js";
|
||||
export * from "./prompts/index.js";
|
||||
export * from "./selectors/index.js";
|
||||
export * from "./storage/StorageContext.js";
|
||||
export * from "./synthesizers/index.js";
|
||||
export * from "./tools/index.js";
|
||||
export * from "./types.js";
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export * from "./index.edge.js";
|
||||
@@ -2,13 +2,9 @@ export * from "./index.edge.js";
|
||||
export * from "./readers/index.js";
|
||||
export * from "./storage/index.js";
|
||||
// Exports modules that doesn't support non-node.js runtime
|
||||
export {
|
||||
ClipEmbedding,
|
||||
ClipEmbeddingModelType,
|
||||
} from "./embeddings/ClipEmbedding.js";
|
||||
// Ollama is only compatible with the Node.js runtime
|
||||
export {
|
||||
HuggingFaceEmbedding,
|
||||
HuggingFaceEmbeddingModelType,
|
||||
} from "./embeddings/HuggingFaceEmbedding.js";
|
||||
export { OllamaEmbedding } from "./embeddings/OllamaEmbedding.js";
|
||||
export { Ollama, type OllamaParams } from "./llm/ollama.js";
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
export * from "./index.edge.js";
|
||||
@@ -1,4 +1,5 @@
|
||||
import rake from "../../internal/deps/rake-modified.js";
|
||||
// @ts-ignore
|
||||
import rake from "rake-modified";
|
||||
|
||||
// Get subtokens from a list of tokens., filtering for stopwords.
|
||||
export function expandTokensWithSubtokens(tokens: Set<string>): Set<string> {
|
||||
|
||||
@@ -13,11 +13,11 @@ import {
|
||||
nodeParserFromSettingsOrContext,
|
||||
} from "../../Settings.js";
|
||||
import { DEFAULT_SIMILARITY_TOP_K } from "../../constants.js";
|
||||
import { ClipEmbedding } from "../../embeddings/ClipEmbedding.js";
|
||||
import type {
|
||||
BaseEmbedding,
|
||||
MultiModalEmbedding,
|
||||
} from "../../embeddings/index.js";
|
||||
import { ClipEmbedding } from "../../embeddings/index.js";
|
||||
import { RetrieverQueryEngine } from "../../engines/query/RetrieverQueryEngine.js";
|
||||
import { runTransformations } from "../../ingestion/IngestionPipeline.js";
|
||||
import {
|
||||
@@ -279,29 +279,18 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
|
||||
return new VectorIndexRetriever({ index: this, ...options });
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a RetrieverQueryEngine.
|
||||
* similarityTopK is only used if no existing retriever is provided.
|
||||
*/
|
||||
asQueryEngine(options?: {
|
||||
retriever?: BaseRetriever;
|
||||
responseSynthesizer?: BaseSynthesizer;
|
||||
preFilters?: MetadataFilters;
|
||||
nodePostprocessors?: BaseNodePostprocessor[];
|
||||
similarityTopK?: number;
|
||||
}): QueryEngine & RetrieverQueryEngine {
|
||||
const {
|
||||
retriever,
|
||||
responseSynthesizer,
|
||||
preFilters,
|
||||
nodePostprocessors,
|
||||
similarityTopK,
|
||||
} = options ?? {};
|
||||
const { retriever, responseSynthesizer } = options ?? {};
|
||||
return new RetrieverQueryEngine(
|
||||
retriever ?? this.asRetriever({ similarityTopK }),
|
||||
retriever ?? this.asRetriever(),
|
||||
responseSynthesizer,
|
||||
preFilters,
|
||||
nodePostprocessors,
|
||||
options?.preFilters,
|
||||
options?.nodePostprocessors,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
export default function rake(words: string): {
|
||||
[index: string]: unknown;
|
||||
};
|
||||
@@ -1,630 +0,0 @@
|
||||
// generate from "tsup ./src/index.js --format esm"
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __commonJS = (cb, mod) =>
|
||||
function __require() {
|
||||
return (
|
||||
mod ||
|
||||
(0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod),
|
||||
mod.exports
|
||||
);
|
||||
};
|
||||
|
||||
// src/stopwords.js
|
||||
var require_stopwords = __commonJS({
|
||||
"src/stopwords.js"(exports, module) {
|
||||
"use strict";
|
||||
module.exports = {
|
||||
stopwords: [
|
||||
"a",
|
||||
"about",
|
||||
"above",
|
||||
"across",
|
||||
"after",
|
||||
"again",
|
||||
"against",
|
||||
"all",
|
||||
"almost",
|
||||
"alone",
|
||||
"along",
|
||||
"already",
|
||||
"also",
|
||||
"although",
|
||||
"always",
|
||||
"among",
|
||||
"an",
|
||||
"and",
|
||||
"another",
|
||||
"any",
|
||||
"anybody",
|
||||
"anyone",
|
||||
"anything",
|
||||
"anywhere",
|
||||
"are",
|
||||
"area",
|
||||
"areas",
|
||||
"around",
|
||||
"as",
|
||||
"ask",
|
||||
"asked",
|
||||
"asking",
|
||||
"asks",
|
||||
"at",
|
||||
"away",
|
||||
"b",
|
||||
"back",
|
||||
"backed",
|
||||
"backing",
|
||||
"backs",
|
||||
"be",
|
||||
"because",
|
||||
"become",
|
||||
"becomes",
|
||||
"became",
|
||||
"been",
|
||||
"before",
|
||||
"began",
|
||||
"behind",
|
||||
"being",
|
||||
"beings",
|
||||
"best",
|
||||
"better",
|
||||
"between",
|
||||
"big",
|
||||
"both",
|
||||
"but",
|
||||
"by",
|
||||
"c",
|
||||
"came",
|
||||
"can",
|
||||
"cannot",
|
||||
"case",
|
||||
"cases",
|
||||
"certain",
|
||||
"certainly",
|
||||
"clear",
|
||||
"clearly",
|
||||
"come",
|
||||
"contains",
|
||||
"could",
|
||||
"d",
|
||||
"did",
|
||||
"differ",
|
||||
"different",
|
||||
"differently",
|
||||
"do",
|
||||
"does",
|
||||
"done",
|
||||
"down",
|
||||
"downed",
|
||||
"downing",
|
||||
"downs",
|
||||
"during",
|
||||
"e",
|
||||
"each",
|
||||
"early",
|
||||
"either",
|
||||
"end",
|
||||
"ended",
|
||||
"ending",
|
||||
"ends",
|
||||
"enough",
|
||||
"even",
|
||||
"evenly",
|
||||
"ever",
|
||||
"every",
|
||||
"everybody",
|
||||
"everyone",
|
||||
"everything",
|
||||
"everywhere",
|
||||
"f",
|
||||
"face",
|
||||
"faces",
|
||||
"fact",
|
||||
"facts",
|
||||
"far",
|
||||
"felt",
|
||||
"few",
|
||||
"find",
|
||||
"finds",
|
||||
"first",
|
||||
"for",
|
||||
"four",
|
||||
"from",
|
||||
"full",
|
||||
"fully",
|
||||
"further",
|
||||
"furthered",
|
||||
"furthering",
|
||||
"furthers",
|
||||
"g",
|
||||
"gave",
|
||||
"general",
|
||||
"generally",
|
||||
"get",
|
||||
"gets",
|
||||
"give",
|
||||
"given",
|
||||
"gives",
|
||||
"go",
|
||||
"going",
|
||||
"good",
|
||||
"goods",
|
||||
"got",
|
||||
"great",
|
||||
"greater",
|
||||
"greatest",
|
||||
"group",
|
||||
"grouped",
|
||||
"grouping",
|
||||
"groups",
|
||||
"h",
|
||||
"had",
|
||||
"has",
|
||||
"have",
|
||||
"having",
|
||||
"he",
|
||||
"her",
|
||||
"herself",
|
||||
"here",
|
||||
"high",
|
||||
"higher",
|
||||
"highest",
|
||||
"him",
|
||||
"himself",
|
||||
"his",
|
||||
"how",
|
||||
"however",
|
||||
"i",
|
||||
"if",
|
||||
"important",
|
||||
"in",
|
||||
"interest",
|
||||
"interested",
|
||||
"interesting",
|
||||
"interests",
|
||||
"into",
|
||||
"is",
|
||||
"it",
|
||||
"its",
|
||||
"itself",
|
||||
"j",
|
||||
"just",
|
||||
"k",
|
||||
"keep",
|
||||
"keeps",
|
||||
"kind",
|
||||
"knew",
|
||||
"know",
|
||||
"known",
|
||||
"knows",
|
||||
"l",
|
||||
"large",
|
||||
"largely",
|
||||
"last",
|
||||
"later",
|
||||
"latest",
|
||||
"least",
|
||||
"less",
|
||||
"let",
|
||||
"lets",
|
||||
"like",
|
||||
"likely",
|
||||
"long",
|
||||
"longer",
|
||||
"longest",
|
||||
"m",
|
||||
"made",
|
||||
"make",
|
||||
"making",
|
||||
"man",
|
||||
"many",
|
||||
"may",
|
||||
"me",
|
||||
"member",
|
||||
"members",
|
||||
"men",
|
||||
"might",
|
||||
"more",
|
||||
"most",
|
||||
"mostly",
|
||||
"mr",
|
||||
"mrs",
|
||||
"much",
|
||||
"must",
|
||||
"my",
|
||||
"myself",
|
||||
"n",
|
||||
"necessary",
|
||||
"need",
|
||||
"needed",
|
||||
"needing",
|
||||
"needs",
|
||||
"never",
|
||||
"new",
|
||||
"newer",
|
||||
"newest",
|
||||
"next",
|
||||
"no",
|
||||
"non",
|
||||
"not",
|
||||
"nobody",
|
||||
"noone",
|
||||
"nothing",
|
||||
"now",
|
||||
"nowhere",
|
||||
"number",
|
||||
"numbers",
|
||||
"o",
|
||||
"of",
|
||||
"off",
|
||||
"often",
|
||||
"old",
|
||||
"older",
|
||||
"oldest",
|
||||
"on",
|
||||
"once",
|
||||
"one",
|
||||
"only",
|
||||
"open",
|
||||
"opened",
|
||||
"opening",
|
||||
"opens",
|
||||
"or",
|
||||
"order",
|
||||
"ordered",
|
||||
"ordering",
|
||||
"orders",
|
||||
"other",
|
||||
"others",
|
||||
"our",
|
||||
"out",
|
||||
"over",
|
||||
"p",
|
||||
"part",
|
||||
"parted",
|
||||
"parting",
|
||||
"parts",
|
||||
"per",
|
||||
"perhaps",
|
||||
"place",
|
||||
"places",
|
||||
"point",
|
||||
"pointed",
|
||||
"pointing",
|
||||
"points",
|
||||
"possible",
|
||||
"present",
|
||||
"presented",
|
||||
"presenting",
|
||||
"presents",
|
||||
"problem",
|
||||
"problems",
|
||||
"put",
|
||||
"puts",
|
||||
"q",
|
||||
"quite",
|
||||
"r",
|
||||
"rather",
|
||||
"really",
|
||||
"right",
|
||||
"room",
|
||||
"rooms",
|
||||
"s",
|
||||
"said",
|
||||
"same",
|
||||
"saw",
|
||||
"say",
|
||||
"says",
|
||||
"second",
|
||||
"seconds",
|
||||
"see",
|
||||
"sees",
|
||||
"seem",
|
||||
"seemed",
|
||||
"seeming",
|
||||
"seems",
|
||||
"several",
|
||||
"shall",
|
||||
"she",
|
||||
"should",
|
||||
"show",
|
||||
"showed",
|
||||
"showing",
|
||||
"shows",
|
||||
"side",
|
||||
"sides",
|
||||
"since",
|
||||
"small",
|
||||
"smaller",
|
||||
"smallest",
|
||||
"so",
|
||||
"some",
|
||||
"somebody",
|
||||
"someone",
|
||||
"something",
|
||||
"somewhere",
|
||||
"state",
|
||||
"states",
|
||||
"still",
|
||||
"such",
|
||||
"sure",
|
||||
"t",
|
||||
"take",
|
||||
"taken",
|
||||
"than",
|
||||
"that",
|
||||
"the",
|
||||
"their",
|
||||
"them",
|
||||
"then",
|
||||
"there",
|
||||
"therefore",
|
||||
"these",
|
||||
"they",
|
||||
"thing",
|
||||
"things",
|
||||
"think",
|
||||
"thinks",
|
||||
"this",
|
||||
"those",
|
||||
"though",
|
||||
"thought",
|
||||
"thoughts",
|
||||
"three",
|
||||
"through",
|
||||
"thus",
|
||||
"to",
|
||||
"today",
|
||||
"together",
|
||||
"too",
|
||||
"took",
|
||||
"toward",
|
||||
"turn",
|
||||
"turned",
|
||||
"turning",
|
||||
"turns",
|
||||
"two",
|
||||
"u",
|
||||
"under",
|
||||
"until",
|
||||
"up",
|
||||
"upon",
|
||||
"us",
|
||||
"use",
|
||||
"uses",
|
||||
"used",
|
||||
"v",
|
||||
"very",
|
||||
"w",
|
||||
"want",
|
||||
"wanted",
|
||||
"wanting",
|
||||
"wants",
|
||||
"was",
|
||||
"way",
|
||||
"ways",
|
||||
"we",
|
||||
"well",
|
||||
"wells",
|
||||
"went",
|
||||
"were",
|
||||
"what",
|
||||
"when",
|
||||
"where",
|
||||
"whether",
|
||||
"which",
|
||||
"while",
|
||||
"who",
|
||||
"whole",
|
||||
"whose",
|
||||
"why",
|
||||
"will",
|
||||
"with",
|
||||
"within",
|
||||
"without",
|
||||
"work",
|
||||
"worked",
|
||||
"working",
|
||||
"works",
|
||||
"would",
|
||||
"y",
|
||||
"year",
|
||||
"years",
|
||||
"yet",
|
||||
"you",
|
||||
"young",
|
||||
"younger",
|
||||
"youngest",
|
||||
"your",
|
||||
"yours",
|
||||
"eoc",
|
||||
"mu",
|
||||
"sigma",
|
||||
"mu sigma",
|
||||
"musigma",
|
||||
"client",
|
||||
"clients",
|
||||
"capabilities",
|
||||
"capability",
|
||||
"firm",
|
||||
"firms",
|
||||
"biggest",
|
||||
"-",
|
||||
],
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
// src/index.js
|
||||
import _ from "lodash";
|
||||
const { fromPairs, sortBy, toPairs } = _;
|
||||
var stopwords = require_stopwords();
|
||||
function isNumber(str) {
|
||||
return /\d/.test(str);
|
||||
}
|
||||
function isAcceptable(phrase, minCharLength, maxWordsLength) {
|
||||
if (phrase < minCharLength) {
|
||||
return false;
|
||||
}
|
||||
let words = phrase.split(" ");
|
||||
if (words.length > maxWordsLength) {
|
||||
return false;
|
||||
}
|
||||
let digits = 0;
|
||||
let alpha = 0;
|
||||
for (let i = 0; i < phrase.length; i++) {
|
||||
if (/\d/.test(phrase[i])) digits += 1;
|
||||
if (/[a-zA-Z]/.test(phrase[i])) alpha += 1;
|
||||
}
|
||||
if (alpha == 0) {
|
||||
return false;
|
||||
}
|
||||
if (digits > alpha) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function countOccurances(haystack, needle) {
|
||||
return haystack.reduce((n, value) => {
|
||||
return n + (value === needle);
|
||||
}, 0);
|
||||
}
|
||||
function generateCandidateKeywordScores(
|
||||
phraseList,
|
||||
wordScore,
|
||||
minKeywordFrequency = 1,
|
||||
) {
|
||||
let keywordCandidates = {};
|
||||
phraseList.forEach((phrase) => {
|
||||
if (minKeywordFrequency > 1) {
|
||||
if (countOccurances(phraseList, phrase) < minKeywordFrequency) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
phrase in keywordCandidates || (keywordCandidates[phrase] = 0);
|
||||
let wordList = separateWords(phrase, 0);
|
||||
let candidateScore = 0;
|
||||
wordList.forEach((word) => {
|
||||
candidateScore += wordScore[word];
|
||||
keywordCandidates[phrase] = candidateScore;
|
||||
});
|
||||
});
|
||||
return keywordCandidates;
|
||||
}
|
||||
function separateWords(text, minWordReturnSize) {
|
||||
let wordDelimiters = /[^a-zA-Z0-9_\+\-/]/;
|
||||
let words = [];
|
||||
text.split(wordDelimiters).forEach((singleWord) => {
|
||||
let currentWord = singleWord.trim().toLowerCase();
|
||||
if (
|
||||
currentWord.length > minWordReturnSize &&
|
||||
currentWord != "" &&
|
||||
!isNumber(currentWord)
|
||||
) {
|
||||
words.push(currentWord);
|
||||
}
|
||||
});
|
||||
return words;
|
||||
}
|
||||
function calculateWordScores(phraseList) {
|
||||
let wordFrequency = {};
|
||||
let wordDegree = {};
|
||||
phraseList.forEach((phrase) => {
|
||||
let wordList = separateWords(phrase, 0);
|
||||
let wordListLength = wordList.length;
|
||||
let wordListDegree = wordListLength - 1;
|
||||
wordList.forEach((word) => {
|
||||
word in wordFrequency || (wordFrequency[word] = 0);
|
||||
wordFrequency[word] += 1;
|
||||
word in wordDegree || (wordDegree[word] = 0);
|
||||
wordDegree[word] += wordListDegree;
|
||||
});
|
||||
});
|
||||
Object.keys(wordFrequency).forEach((item) => {
|
||||
wordDegree[item] = wordDegree[item] + wordFrequency[item];
|
||||
});
|
||||
let wordScore = {};
|
||||
Object.keys(wordFrequency).forEach((item) => {
|
||||
item in wordScore || (wordScore[item] = 0);
|
||||
wordScore[item] = wordDegree[item] / (wordFrequency[item] * 1);
|
||||
});
|
||||
return wordScore;
|
||||
}
|
||||
function generateCandidateKeywords(
|
||||
sentenceList,
|
||||
stopWordPattern,
|
||||
minCharLength = 1,
|
||||
maxWordsLength = 5,
|
||||
) {
|
||||
let phraseList = [];
|
||||
sentenceList.forEach((sentence) => {
|
||||
let tmp = stopWordPattern[Symbol.replace](sentence, "|");
|
||||
let phrases = tmp.split("|");
|
||||
phrases.forEach((ph) => {
|
||||
let phrase = ph.trim().toLowerCase();
|
||||
if (phrase != "" && isAcceptable(phrase, minCharLength, maxWordsLength)) {
|
||||
phraseList.push(phrase);
|
||||
} else {
|
||||
}
|
||||
});
|
||||
});
|
||||
return phraseList;
|
||||
}
|
||||
function buildStopWordRegex(path) {
|
||||
let stopWordList = loadStopWords(path);
|
||||
let stopWordRegexList = [];
|
||||
stopWordList.forEach((word) => {
|
||||
if (/\w+/.test(word)) {
|
||||
let wordRegex = `\\b${word}\\b`;
|
||||
stopWordRegexList.push(wordRegex);
|
||||
}
|
||||
});
|
||||
let stopWordPattern = new RegExp(stopWordRegexList.join("|"), "ig");
|
||||
return stopWordPattern;
|
||||
}
|
||||
function splitSentences(text) {
|
||||
let sentenceDelimiters = /[\[\]\n.!?,;:\t\\-\\"\\(\\)\\\'\u2019\u2013]/;
|
||||
return text.split(sentenceDelimiters);
|
||||
}
|
||||
function loadStopWords(path) {
|
||||
let contents = stopwords.stopwords;
|
||||
return contents;
|
||||
}
|
||||
function rake(
|
||||
text,
|
||||
stopWordsPath,
|
||||
minCharLength = 3,
|
||||
maxWordsLength = 5,
|
||||
minKeywordFrequency = 1,
|
||||
) {
|
||||
let stopWordPattern = buildStopWordRegex(stopWordsPath);
|
||||
let sentenceList = splitSentences(text);
|
||||
let phraseList = generateCandidateKeywords(
|
||||
sentenceList,
|
||||
stopWordPattern,
|
||||
minCharLength,
|
||||
maxWordsLength,
|
||||
);
|
||||
let wordScores = calculateWordScores(phraseList);
|
||||
let keywordCandidates = generateCandidateKeywordScores(
|
||||
phraseList,
|
||||
wordScores,
|
||||
minKeywordFrequency,
|
||||
);
|
||||
let sortedKeywords = fromPairs(
|
||||
sortBy(toPairs(keywordCandidates), (pair) => pair[1]).reverse(),
|
||||
);
|
||||
return sortedKeywords;
|
||||
}
|
||||
export {
|
||||
buildStopWordRegex,
|
||||
calculateWordScores,
|
||||
countOccurances,
|
||||
rake as default,
|
||||
generateCandidateKeywordScores,
|
||||
generateCandidateKeywords,
|
||||
isAcceptable,
|
||||
loadStopWords,
|
||||
separateWords,
|
||||
splitSentences,
|
||||
};
|
||||
@@ -1,22 +0,0 @@
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2017 Mike Williamson <mike@korora.ca>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
@@ -1,17 +0,0 @@
|
||||
export type Logger = {
|
||||
log: (...args: unknown[]) => void;
|
||||
error: (...args: unknown[]) => void;
|
||||
warn: (...args: unknown[]) => void;
|
||||
};
|
||||
|
||||
export const emptyLogger: Logger = Object.freeze({
|
||||
log: () => {},
|
||||
error: () => {},
|
||||
warn: () => {},
|
||||
});
|
||||
|
||||
export const consoleLogger: Logger = Object.freeze({
|
||||
log: console.log.bind(console),
|
||||
error: console.error.bind(console),
|
||||
warn: console.warn.bind(console),
|
||||
});
|
||||
@@ -32,6 +32,8 @@ type GeminiSessionOptions = {
|
||||
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",
|
||||
}
|
||||
|
||||
@@ -42,12 +44,16 @@ export interface GeminiModelInfo {
|
||||
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 = {
|
||||
@@ -302,10 +308,12 @@ export class Gemini extends ToolCallLLM<GeminiAdditionalChatOptions> {
|
||||
): GeminiChatStreamResponse {
|
||||
const { chat, messageContent } = this.prepareChat(params);
|
||||
const result = await chat.sendMessageStream(messageContent);
|
||||
yield* streamConverter(result.stream, (response) => ({
|
||||
delta: response.text(),
|
||||
raw: response,
|
||||
}));
|
||||
return streamConverter(result.stream, (response) => {
|
||||
return {
|
||||
text: response.text(),
|
||||
raw: response,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
chat(params: GeminiChatParamsStreaming): Promise<GeminiChatStreamResponse>;
|
||||
|
||||
@@ -1,141 +0,0 @@
|
||||
import {
|
||||
HfInference,
|
||||
type Options as HfInferenceOptions,
|
||||
} from "@huggingface/inference";
|
||||
import { BaseLLM } from "./base.js";
|
||||
import type {
|
||||
ChatMessage,
|
||||
ChatResponse,
|
||||
ChatResponseChunk,
|
||||
LLMChatParamsNonStreaming,
|
||||
LLMChatParamsStreaming,
|
||||
LLMMetadata,
|
||||
ToolCallLLMMessageOptions,
|
||||
} from "./types.js";
|
||||
import { streamConverter, wrapLLMEvent } from "./utils.js";
|
||||
|
||||
const DEFAULT_PARAMS = {
|
||||
temperature: 0.1,
|
||||
topP: 1,
|
||||
maxTokens: undefined,
|
||||
contextWindow: 3900,
|
||||
};
|
||||
export type HFConfig = Partial<typeof DEFAULT_PARAMS> &
|
||||
HfInferenceOptions & {
|
||||
model: string;
|
||||
accessToken: string;
|
||||
endpoint?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
Wrapper on the Hugging Face's Inference API.
|
||||
API Docs: https://huggingface.co/docs/huggingface.js/inference/README
|
||||
List of tasks with models: huggingface.co/api/tasks
|
||||
|
||||
Note that Conversational API is not yet supported by the Inference API.
|
||||
They recommend using the text generation API instead.
|
||||
See: https://github.com/huggingface/huggingface.js/issues/586#issuecomment-2024059308
|
||||
*/
|
||||
export class HuggingFaceInferenceAPI extends BaseLLM {
|
||||
model: string;
|
||||
temperature: number;
|
||||
topP: number;
|
||||
maxTokens?: number;
|
||||
contextWindow: number;
|
||||
hf: HfInference;
|
||||
|
||||
constructor(init: HFConfig) {
|
||||
super();
|
||||
const {
|
||||
model,
|
||||
temperature,
|
||||
topP,
|
||||
maxTokens,
|
||||
contextWindow,
|
||||
accessToken,
|
||||
endpoint,
|
||||
...hfInferenceOpts
|
||||
} = init;
|
||||
this.hf = new HfInference(accessToken, hfInferenceOpts);
|
||||
this.model = model;
|
||||
this.temperature = temperature ?? DEFAULT_PARAMS.temperature;
|
||||
this.topP = topP ?? DEFAULT_PARAMS.topP;
|
||||
this.maxTokens = maxTokens ?? DEFAULT_PARAMS.maxTokens;
|
||||
this.contextWindow = contextWindow ?? DEFAULT_PARAMS.contextWindow;
|
||||
if (endpoint) this.hf.endpoint(endpoint);
|
||||
}
|
||||
|
||||
get metadata(): LLMMetadata {
|
||||
return {
|
||||
model: this.model,
|
||||
temperature: this.temperature,
|
||||
topP: this.topP,
|
||||
maxTokens: this.maxTokens,
|
||||
contextWindow: this.contextWindow,
|
||||
tokenizer: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
chat(
|
||||
params: LLMChatParamsStreaming,
|
||||
): Promise<AsyncIterable<ChatResponseChunk>>;
|
||||
chat(params: LLMChatParamsNonStreaming): Promise<ChatResponse>;
|
||||
@wrapLLMEvent
|
||||
async chat(
|
||||
params: LLMChatParamsStreaming | LLMChatParamsNonStreaming,
|
||||
): Promise<AsyncIterable<ChatResponseChunk> | ChatResponse<object>> {
|
||||
if (params.stream) return this.streamChat(params);
|
||||
return this.nonStreamChat(params);
|
||||
}
|
||||
|
||||
private messagesToPrompt(messages: ChatMessage<ToolCallLLMMessageOptions>[]) {
|
||||
let prompt = "";
|
||||
for (const message of messages) {
|
||||
if (message.role === "system") {
|
||||
prompt += `<|system|>\n${message.content}</s>\n`;
|
||||
} else if (message.role === "user") {
|
||||
prompt += `<|user|>\n${message.content}</s>\n`;
|
||||
} else if (message.role === "assistant") {
|
||||
prompt += `<|assistant|>\n${message.content}</s>\n`;
|
||||
}
|
||||
}
|
||||
// ensure we start with a system prompt, insert blank if needed
|
||||
if (!prompt.startsWith("<|system|>\n")) {
|
||||
prompt = "<|system|>\n</s>\n" + prompt;
|
||||
}
|
||||
// add final assistant prompt
|
||||
prompt = prompt + "<|assistant|>\n";
|
||||
return prompt;
|
||||
}
|
||||
|
||||
protected async nonStreamChat(
|
||||
params: LLMChatParamsNonStreaming,
|
||||
): Promise<ChatResponse> {
|
||||
const res = await this.hf.textGeneration({
|
||||
model: this.model,
|
||||
inputs: this.messagesToPrompt(params.messages),
|
||||
parameters: this.metadata,
|
||||
});
|
||||
return {
|
||||
raw: res,
|
||||
message: {
|
||||
content: res.generated_text,
|
||||
role: "assistant",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
protected async *streamChat(
|
||||
params: LLMChatParamsStreaming,
|
||||
): AsyncIterable<ChatResponseChunk> {
|
||||
const stream = this.hf.textGenerationStream({
|
||||
model: this.model,
|
||||
inputs: this.messagesToPrompt(params.messages),
|
||||
parameters: this.metadata,
|
||||
});
|
||||
yield* streamConverter(stream, (chunk) => ({
|
||||
delta: chunk.token.text,
|
||||
raw: chunk,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ export {
|
||||
export { FireworksLLM } from "./fireworks.js";
|
||||
export { GEMINI_MODEL, Gemini } from "./gemini.js";
|
||||
export { Groq } from "./groq.js";
|
||||
export { HuggingFaceInferenceAPI } from "./huggingface.js";
|
||||
export {
|
||||
ALL_AVAILABLE_MISTRAL_MODELS,
|
||||
MistralAI,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { BaseNode, Metadata } from "../Node.js";
|
||||
import { TextNode } from "../Node.js";
|
||||
import type { BaseRetriever } from "../Retriever.js";
|
||||
import type { VectorStoreIndex } from "../indices/vectorStore/index.js";
|
||||
import type { VectorStoreIndex } from "../indices/index.js";
|
||||
import type { MessageContent } from "../llm/index.js";
|
||||
import { extractText } from "../llm/utils.js";
|
||||
import type { BaseTool } from "../types.js";
|
||||
|
||||
@@ -1,19 +1,5 @@
|
||||
# test-edge-runtime
|
||||
|
||||
## 0.1.6
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [46227f2]
|
||||
- @llamaindex/edge@0.3.1
|
||||
|
||||
## 0.1.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [5016f21]
|
||||
- @llamaindex/edge@0.3.0
|
||||
|
||||
## 0.1.4
|
||||
|
||||
### Patch Changes
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/nextjs-edge-runtime-test",
|
||||
"version": "0.1.6",
|
||||
"name": "test-edge-runtime",
|
||||
"version": "0.1.4",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
@@ -15,8 +15,8 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.12.7",
|
||||
"@types/react": "^18.2.79",
|
||||
"@types/react-dom": "^18.2.25",
|
||||
"@types/react": "^18",
|
||||
"@types/react-dom": "^18",
|
||||
"typescript": "^5"
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 629 B After Width: | Height: | Size: 629 B |