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
1da51b49
Commit
1da51b49
authored
1 week ago
by
thucpn
Browse files
Options
Downloads
Patches
Plain Diff
move tool call to tools package
parent
8f56465a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
packages/server/src/index.ts
+0
-1
0 additions, 1 deletion
packages/server/src/index.ts
packages/tools/src/index.ts
+2
-0
2 additions, 0 deletions
packages/tools/src/index.ts
packages/tools/src/tool-call.ts
+20
-69
20 additions, 69 deletions
packages/tools/src/tool-call.ts
with
22 additions
and
70 deletions
packages/server/src/index.ts
+
0
−
1
View file @
1da51b49
export
*
from
"
./server
"
;
export
*
from
"
./server
"
;
export
*
from
"
./workflow/stream
"
;
export
*
from
"
./workflow/stream
"
;
export
*
from
"
./workflow/tools
"
;
export
*
from
"
./workflow/type
"
;
export
*
from
"
./workflow/type
"
;
This diff is collapsed.
Click to expand it.
packages/tools/src/index.ts
+
2
−
0
View file @
1da51b49
...
@@ -7,3 +7,5 @@ export * from "./tools/interpreter";
...
@@ -7,3 +7,5 @@ export * from "./tools/interpreter";
export
*
from
"
./tools/openapi-action
"
;
export
*
from
"
./tools/openapi-action
"
;
export
*
from
"
./tools/weather
"
;
export
*
from
"
./tools/weather
"
;
export
*
from
"
./tools/wikipedia
"
;
export
*
from
"
./tools/wikipedia
"
;
export
*
from
"
./tool-call
"
;
This diff is collapsed.
Click to expand it.
packages/
server/src/workflow/tools
.ts
→
packages/
tools/src/tool-call
.ts
+
20
−
69
View file @
1da51b49
import
{
callTool
}
from
"
@llamaindex/core/agent
"
;
import
{
import
{
type
BaseToolWithCall
,
type
BaseToolWithCall
,
callTool
,
type
ChatMessage
,
type
ChatMessage
,
type
ChatResponse
,
type
ChatResponse
,
type
ChatResponseChunk
,
type
ChatResponseChunk
,
type
HandlerContext
,
type
PartialToolCall
,
type
PartialToolCall
,
type
ToolCall
,
type
ToolCall
,
ToolCallLLM
,
ToolCallLLM
,
type
ToolCallLLMMessageOptions
,
type
ToolCallLLMMessageOptions
,
}
from
"
llamaindex
"
;
}
from
"
@llamaindex/core/llms
"
;
import
crypto
from
"
node:crypto
"
;
import
{
AgentRunEvent
}
from
"
./type
"
;
/**
export
async
function
callTools
({
* Call multiple tools and return the tool messages
*/
export
const
callTools
=
async
<
T
>
({
tools
,
tools
,
toolCalls
,
toolCalls
,
ctx
,
writeEvent
,
agentName
,
writeEvent
=
true
,
}:
{
}:
{
toolCalls
:
ToolCall
[];
toolCalls
:
ToolCall
[];
tools
:
BaseToolWithCall
[];
tools
:
BaseToolWithCall
[];
ctx
:
HandlerContext
<
T
>
;
writeEvent
?:
(
text
:
string
,
step
:
number
,
totalSteps
:
number
)
=>
void
;
agentName
:
string
;
}):
Promise
<
ChatMessage
[]
>
{
writeEvent
?:
boolean
;
if
(
toolCalls
.
length
===
0
)
return
[]
;
}):
Promise
<
ChatMessage
[]
>
=>
{
const
toolMsgs
:
ChatMessage
[]
=
[];
const
toolMsgs
:
ChatMessage
[]
=
[];
if
(
toolCalls
.
length
===
0
)
{
return
toolMsgs
;
}
if
(
toolCalls
.
length
===
1
&&
toolCalls
[
0
])
{
const
tool
=
tools
.
find
(
(
tool
)
=>
tool
.
metadata
.
name
===
toolCalls
[
0
]
!
.
name
,
);
if
(
!
tool
)
{
throw
new
Error
(
`Tool
${
toolCalls
[
0
].
name
}
not found`
);
}
return
[
await
callSingleTool
(
tool
,
toolCalls
[
0
],
writeEvent
?
(
msg
:
string
)
=>
{
ctx
.
sendEvent
(
new
AgentRunEvent
({
agent
:
agentName
,
text
:
msg
,
type
:
"
text
"
,
}),
);
}
:
undefined
,
),
];
}
// Multiple tool calls, show events in progress
const
progressId
=
crypto
.
randomUUID
();
const
totalSteps
=
toolCalls
.
length
;
const
totalSteps
=
toolCalls
.
length
;
let
currentStep
=
0
;
for
(
let
step
=
0
;
step
<
totalSteps
;
step
++
)
{
for
(
const
toolCall
of
toolCalls
)
{
const
toolCall
=
toolCalls
[
step
]
!
;
const
tool
=
tools
.
find
((
tool
)
=>
tool
.
metadata
.
name
===
toolCall
.
name
);
const
tool
=
tools
.
find
((
tool
)
=>
tool
.
metadata
.
name
===
toolCall
.
name
);
if
(
!
tool
)
{
if
(
!
tool
)
throw
new
Error
(
`Tool
${
toolCall
.
name
}
not found`
);
throw
new
Error
(
`Tool
${
toolCall
.
name
}
not found`
);
}
const
toolMsg
=
await
callSingleTool
(
tool
,
toolCall
,
(
text
)
=>
{
const
toolMsg
=
await
callSingleTool
(
tool
,
toolCall
,
(
msg
:
string
)
=>
{
writeEvent
?.(
text
,
step
,
totalSteps
);
ctx
.
sendEvent
(
new
AgentRunEvent
({
agent
:
agentName
,
text
:
msg
,
type
:
"
progress
"
,
data
:
{
id
:
progressId
,
total
:
totalSteps
,
current
:
currentStep
,
},
}),
);
currentStep
++
;
});
});
toolMsgs
.
push
(
toolMsg
);
toolMsgs
.
push
(
toolMsg
);
}
}
return
toolMsgs
;
return
toolMsgs
;
}
;
}
export
const
callSingleTool
=
async
(
export
async
function
callSingleTool
(
tool
:
BaseToolWithCall
,
tool
:
BaseToolWithCall
,
toolCall
:
ToolCall
,
toolCall
:
ToolCall
,
eventEmitter
?:
(
msg
:
string
)
=>
void
,
eventEmitter
?:
(
msg
:
string
)
=>
void
,
):
Promise
<
ChatMessage
>
=>
{
):
Promise
<
ChatMessage
>
{
if
(
eventEmitter
)
{
if
(
eventEmitter
)
{
eventEmitter
(
eventEmitter
(
`Calling tool
${
toolCall
.
name
}
with input:
${
JSON
.
stringify
(
toolCall
.
input
)}
`
,
`Calling tool
${
toolCall
.
name
}
with input:
${
JSON
.
stringify
(
toolCall
.
input
)}
`
,
...
@@ -135,7 +86,7 @@ export const callSingleTool = async (
...
@@ -135,7 +86,7 @@ export const callSingleTool = async (
},
},
},
},
};
};
}
;
}
class
ChatWithToolsResponse
{
class
ChatWithToolsResponse
{
toolCalls
:
ToolCall
[];
toolCalls
:
ToolCall
[];
...
...
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