Initial commit

This commit is contained in:
Laurie Voss
2024-05-03 15:46:30 -07:00
commit a5ef35b775
17 changed files with 17455 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
node_modules
.env
+64
View File
@@ -0,0 +1,64 @@
import {
OpenAI,
FunctionTool,
ReActAgent,
Settings
} from "llamaindex"
import 'dotenv/config'
Settings.llm = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4-turbo",
})
/*
Set up logging so we can see the work in progress.
Available events:
llm-start
llm-end
agent-start
agent-end
llm-tool-call
llm-tool-result
*/
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log(event.detail.payload)
})
Settings.callbackManager.on("llm-tool-result", (event) => {
console.log(event.detail.payload)
})
const sumNumbers = ({a, b}) => {
return `${a + b}`;
}
const tools = [
FunctionTool.from(
sumNumbers,
{
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "First number to sum"
},
b: {
type: "number",
description: "Second number to sum"
},
},
required: ["a", "b"]
}
}
)
]
const agent = new ReActAgent({tools})
let response = await agent.chat({
message: "Add 101 and 303",
})
console.log(response)
+3395
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -0,0 +1,6 @@
{
"dependencies": {
"dotenv": "^16.4.5",
"llamaindex": "^0.3.1"
}
}
+68
View File
@@ -0,0 +1,68 @@
import {
OpenAI,
FunctionTool,
OpenAIAgent,
Settings,
SimpleDirectoryReader,
HuggingFaceEmbedding,
VectorStoreIndex,
QueryEngineTool
} from "llamaindex"
import 'dotenv/config'
// set LLM and the embedding model
Settings.llm = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4-turbo",
})
Settings.embedModel = new HuggingFaceEmbedding({
modelType: "BAAI/bge-small-en-v1.5",
quantized: false
})
/*
Set up logging so we can see the work in progress.
Available events:
llm-start
llm-end
agent-start
agent-end
llm-tool-call
llm-tool-result
*/
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log(event.detail.payload)
})
Settings.callbackManager.on("llm-tool-result", (event) => {
console.log(event.detail.payload)
})
// load our data and create a query engine
const reader = new SimpleDirectoryReader()
const documents = await reader.loadData("../data")
const index = await VectorStoreIndex.fromDocuments(documents)
const retriever = await index.asRetriever()
retriever.similarityTopK = 10
const queryEngine = await index.asQueryEngine({
retriever
})
// define the query engine as a tool
const tools = [
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "san_francisco_budget_tool",
description: `This tool can answer detailed questions about the individual components of the budget of San Francisco in 2023-2024.`,
},
}),
]
// create the agent
const agent = new OpenAIAgent({tools})
let response = await agent.chat({
message: "What's the budget of San Francisco in 2023-2024?",
})
console.log(response)
+3395
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -0,0 +1,6 @@
{
"dependencies": {
"dotenv": "^16.4.5",
"llamaindex": "^0.3.1"
}
}
+103
View File
@@ -0,0 +1,103 @@
import {
OpenAI,
FunctionTool,
OpenAIAgent,
Settings,
SimpleDirectoryReader,
HuggingFaceEmbedding,
VectorStoreIndex,
QueryEngineTool
} from "llamaindex"
import 'dotenv/config'
// set LLM and the embedding model
Settings.llm = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4-turbo",
})
Settings.embedModel = new HuggingFaceEmbedding({
modelType: "BAAI/bge-small-en-v1.5",
quantized: false
})
/*
Set up logging so we can see the work in progress.
Available events:
llm-start
llm-end
agent-start
agent-end
llm-tool-call
llm-tool-result
*/
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log(event.detail.payload)
})
Settings.callbackManager.on("llm-tool-result", (event) => {
console.log(event.detail.payload)
})
// load our data and create a query engine
const reader = new SimpleDirectoryReader()
const documents = await reader.loadData("../data")
const index = await VectorStoreIndex.fromDocuments(documents)
const retriever = await index.asRetriever()
retriever.similarityTopK = 10
const queryEngine = await index.asQueryEngine({
retriever
})
// define a function to sum up numbers
const sumNumbers = ({ a, b }) => {
return `${a + b}`;
}
// define the query engine as a tool
const tools = [
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "san_francisco_budget_tool",
description: `This tool can answer detailed questions about the individual components of the budget of San Francisco in 2023-2024.`,
},
}),
FunctionTool.from(
sumNumbers,
{
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "First number to sum"
},
b: {
type: "number",
description: "Second number to sum"
},
},
required: ["a", "b"]
}
}
)
]
// create the agent
const agent = new OpenAIAgent({ tools })
let response = await agent.chat({
message: "What's the budget of San Francisco for community health in 2023-24?",
})
console.log(response)
let response2 = await agent.chat({
message: "What's the budget of San Francisco for public protection in 2023-24?",
})
console.log(response2)
let response3 = await agent.chat({
message: "What's the combined budget of San Francisco for community health and public protection in 2023-24?",
})
console.log(response3)
+3395
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -0,0 +1,6 @@
{
"dependencies": {
"dotenv": "^16.4.5",
"llamaindex": "^0.3.1"
}
}
+102
View File
@@ -0,0 +1,102 @@
import {
OpenAI,
FunctionTool,
OpenAIAgent,
Settings,
LlamaParseReader,
HuggingFaceEmbedding,
VectorStoreIndex,
QueryEngineTool
} from "llamaindex"
import 'dotenv/config'
// set LLM and the embedding model
Settings.llm = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4-turbo",
})
Settings.embedModel = new HuggingFaceEmbedding({
modelType: "BAAI/bge-small-en-v1.5",
quantized: false
})
/*
Set up logging so we can see the work in progress.
Available events:
llm-start
llm-end
agent-start
agent-end
llm-tool-call
llm-tool-result
*/
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log(event.detail.payload)
})
Settings.callbackManager.on("llm-tool-result", (event) => {
console.log(event.detail.payload)
})
// load our data and create a query engine
const reader = new LlamaParseReader({ resultType: "markdown" });
const documents = await reader.loadData("../data/sf_budget_2023_2024.pdf");
const index = await VectorStoreIndex.fromDocuments(documents)
const retriever = await index.asRetriever()
retriever.similarityTopK = 10
const queryEngine = await index.asQueryEngine({
retriever
})
// define a function to sum up numbers
const sumNumbers = ({ a, b }) => {
return `${a + b}`;
}
// define the query engine as a tool
const tools = [
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "san_francisco_budget_tool",
description: `This tool can answer detailed questions about the individual components of the budget of San Francisco in 2023-2024.`,
},
}),
FunctionTool.from(
sumNumbers,
{
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "First number to sum"
},
b: {
type: "number",
description: "Second number to sum"
},
},
required: ["a", "b"]
}
}
)
]
// create the agent
const agent = new OpenAIAgent({ tools })
let response = await agent.chat({
message: "What's the budget of San Francisco for the health service system in 2023-24?",
})
console.log(response)
let response2 = await agent.chat({
message: "What's the budget of San Francisco for the police department in 2023-24?",
})
console.log(response2)
let response3 = await agent.chat({
message: "What's the combined budget of San Francisco for the health service system and police department in 2023-24?",
})
console.log(response3)
+3395
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -0,0 +1,6 @@
{
"dependencies": {
"dotenv": "^16.4.5",
"llamaindex": "^0.3.1"
}
}
+111
View File
@@ -0,0 +1,111 @@
import {
OpenAI,
FunctionTool,
OpenAIAgent,
Settings,
LlamaParseReader,
HuggingFaceEmbedding,
VectorStoreIndex,
QueryEngineTool,
QdrantVectorStore
} from "llamaindex"
import 'dotenv/config'
// set LLM and the embedding model
Settings.llm = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4-turbo",
})
Settings.embedModel = new HuggingFaceEmbedding({
modelType: "BAAI/bge-small-en-v1.5",
quantized: false
})
/*
Set up logging so we can see the work in progress.
Available events:
llm-start
llm-end
agent-start
agent-end
llm-tool-call
llm-tool-result
*/
Settings.callbackManager.on("llm-tool-call", (event) => {
console.log(event.detail.payload)
})
Settings.callbackManager.on("llm-tool-result", (event) => {
console.log(event.detail.payload)
})
// initialize qdrant vector store
const vectorStore = new QdrantVectorStore({
url: "http://localhost:6333",
});
// load our data and create a query engine
const reader = new LlamaParseReader({ resultType: "markdown" });
const documents = await reader.loadData("../data/sf_budget_2023_2024.pdf");
const index = await VectorStoreIndex.fromDocuments(
documents,
{vectorStore}
)
const retriever = await index.asRetriever()
retriever.similarityTopK = 10
const queryEngine = await index.asQueryEngine({
retriever
})
// define a function to sum up numbers
const sumNumbers = ({ a, b }) => {
return `${a + b}`;
}
// define the query engine as a tool
const tools = [
new QueryEngineTool({
queryEngine: queryEngine,
metadata: {
name: "san_francisco_budget_tool",
description: `This tool can answer detailed questions about the individual components of the budget of San Francisco in 2023-2024.`,
},
}),
FunctionTool.from(
sumNumbers,
{
name: "sumNumbers",
description: "Use this function to sum two numbers",
parameters: {
type: "object",
properties: {
a: {
type: "number",
description: "First number to sum"
},
b: {
type: "number",
description: "Second number to sum"
},
},
required: ["a", "b"]
}
}
)
]
// create the agent
const agent = new OpenAIAgent({ tools })
let response = await agent.chat({
message: "What's the budget of San Francisco for the health service system in 2023-24?",
})
console.log(response)
let response2 = await agent.chat({
message: "What's the budget of San Francisco for the police department in 2023-24?",
})
console.log(response2)
let response3 = await agent.chat({
message: "What's the combined budget of San Francisco for the health service system and police department in 2023-24?",
})
console.log(response3)
+3395
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -0,0 +1,6 @@
{
"dependencies": {
"dotenv": "^16.4.5",
"llamaindex": "^0.3.1"
}
}
Binary file not shown.