Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LlamaIndexTS
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
run-llama
LlamaIndexTS
Commits
a285f8ba
Unverified
Commit
a285f8ba
authored
11 months ago
by
Alex Yang
Committed by
GitHub
11 months ago
Browse files
Options
Downloads
Patches
Plain Diff
feat: improve `ToolsFactory` type (#713)
parent
663821cd
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/core/src/tools/ToolsFactory.ts
+43
-0
43 additions, 0 deletions
packages/core/src/tools/ToolsFactory.ts
packages/core/tests/tools.test.ts
+32
-0
32 additions, 0 deletions
packages/core/tests/tools.test.ts
with
75 additions
and
0 deletions
packages/core/src/tools/ToolsFactory.ts
0 → 100644
+
43
−
0
View file @
a285f8ba
import
{
WikipediaTool
}
from
"
./WikipediaTool.js
"
;
export
namespace
ToolsFactory
{
type
ToolsMap
=
{
[
Tools
.
Wikipedia
]:
typeof
WikipediaTool
;
};
export
enum
Tools
{
Wikipedia
=
"
wikipedia.WikipediaToolSpec
"
,
}
export
async
function
createTool
<
Tool
extends
Tools
>
(
key
:
Tool
,
...
params
:
ConstructorParameters
<
ToolsMap
[
Tool
]
>
):
Promise
<
InstanceType
<
ToolsMap
[
Tool
]
>>
{
if
(
key
===
Tools
.
Wikipedia
)
{
return
new
WikipediaTool
(...
params
)
as
InstanceType
<
ToolsMap
[
Tool
]
>
;
}
throw
new
Error
(
`Sorry! Tool
${
key
}
is not supported yet. Options:
${
params
}
`
,
);
}
export
async
function
createTools
<
const
Tool
extends
Tools
>
(
record
:
{
[
key
in
Tool
]:
ConstructorParameters
<
ToolsMap
[
Tool
]
>
[
1
]
extends
any
// backward compatibility for `create-llama` script // if parameters are an array, use them as is
?
ConstructorParameters
<
ToolsMap
[
Tool
]
>
[
0
]
:
ConstructorParameters
<
ToolsMap
[
Tool
]
>
;
}):
Promise
<
InstanceType
<
ToolsMap
[
Tool
]
>
[]
>
{
const
tools
:
InstanceType
<
ToolsMap
[
Tool
]
>
[]
=
[];
for
(
const
key
in
record
)
{
const
params
=
record
[
key
];
tools
.
push
(
await
createTool
(
key
,
// @ts-expect-error
Array
.
isArray
(
params
)
?
params
:
[
params
],
),
);
}
return
tools
;
}
}
This diff is collapsed.
Click to expand it.
packages/core/tests/tools.test.ts
0 → 100644
+
32
−
0
View file @
a285f8ba
import
{
ToolsFactory
}
from
"
llamaindex/tools/ToolsFactory
"
;
import
{
WikipediaTool
}
from
"
llamaindex/tools/WikipediaTool
"
;
import
{
assertType
,
describe
,
test
}
from
"
vitest
"
;
describe
(
"
ToolsFactory
"
,
async
()
=>
{
test
(
"
createTool
"
,
async
()
=>
{
await
ToolsFactory
.
createTool
(
ToolsFactory
.
Tools
.
Wikipedia
,
{
metadata
:
{
name
:
"
wikipedia_tool
"
,
description
:
"
A tool that uses a query engine to search Wikipedia.
"
,
},
});
});
test
(
"
createTools
"
,
async
()
=>
{
await
ToolsFactory
.
createTools
({
[
ToolsFactory
.
Tools
.
Wikipedia
]:
{
metadata
:
{
name
:
"
wikipedia_tool
"
,
description
:
"
A tool that uses a query engine to search Wikipedia.
"
,
},
},
});
});
test
(
"
type
"
,
()
=>
{
assertType
<
(
key
:
ToolsFactory
.
Tools
.
Wikipedia
,
params
:
ConstructorParameters
<
typeof
WikipediaTool
>
[
0
],
)
=>
Promise
<
WikipediaTool
>
>
(
ToolsFactory
.
createTool
<
ToolsFactory
.
Tools
.
Wikipedia
>
);
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment