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
c3d9b2f1
Commit
c3d9b2f1
authored
5 days ago
by
thucpn
Browse files
Options
Downloads
Patches
Plain Diff
support agent workflow
parent
68377421
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/handlers/chat.ts
+6
-12
6 additions, 12 deletions
packages/server/src/handlers/chat.ts
packages/server/src/types.ts
+6
-4
6 additions, 4 deletions
packages/server/src/types.ts
packages/server/src/utils/workflow.ts
+24
-2
24 additions, 2 deletions
packages/server/src/utils/workflow.ts
with
36 additions
and
18 deletions
packages/server/src/handlers/chat.ts
+
6
−
12
View file @
c3d9b2f1
import
{
LlamaIndexAdapter
}
from
"
ai
"
;
import
{
type
Message
}
from
"
ai
"
;
import
{
IncomingMessage
,
ServerResponse
}
from
"
http
"
;
import
{
IncomingMessage
,
ServerResponse
}
from
"
http
"
;
import
{
type
ChatMessage
}
from
"
llamaindex
"
;
import
{
type
ChatMessage
}
from
"
llamaindex
"
;
import
type
{
ServerWorkflow
}
from
"
../types
"
;
import
type
{
ServerWorkflow
}
from
"
../types
"
;
...
@@ -7,7 +7,7 @@ import {
...
@@ -7,7 +7,7 @@ import {
pipeResponse
,
pipeResponse
,
sendJSONResponse
,
sendJSONResponse
,
}
from
"
../utils/request
"
;
}
from
"
../utils/request
"
;
import
{
createStreamFrom
Workflow
Context
}
from
"
../utils/
stream
"
;
import
{
run
Workflow
}
from
"
../utils/
workflow
"
;
export
const
handleChat
=
async
(
export
const
handleChat
=
async
(
workflow
:
ServerWorkflow
,
workflow
:
ServerWorkflow
,
...
@@ -16,7 +16,7 @@ export const handleChat = async (
...
@@ -16,7 +16,7 @@ export const handleChat = async (
)
=>
{
)
=>
{
try
{
try
{
const
body
=
await
parseRequestBody
(
req
);
const
body
=
await
parseRequestBody
(
req
);
const
{
messages
}
=
body
as
{
messages
:
Chat
Message
[]
};
const
{
messages
}
=
body
as
{
messages
:
Message
[]
};
const
lastMessage
=
messages
[
messages
.
length
-
1
];
const
lastMessage
=
messages
[
messages
.
length
-
1
];
if
(
lastMessage
?.
role
!==
"
user
"
)
{
if
(
lastMessage
?.
role
!==
"
user
"
)
{
...
@@ -25,15 +25,9 @@ export const handleChat = async (
...
@@ -25,15 +25,9 @@ export const handleChat = async (
});
});
}
}
const
userMessage
=
lastMessage
.
content
;
const
userInput
=
lastMessage
.
content
;
const
chatHistory
=
messages
.
slice
(
0
,
-
1
);
const
chatHistory
=
messages
.
slice
(
0
,
-
1
)
as
ChatMessage
[];
const
streamResponse
=
await
runWorkflow
(
workflow
,
userInput
,
chatHistory
);
const
context
=
workflow
.
run
({
userMessage
,
chatHistory
});
const
{
stream
,
dataStream
}
=
await
createStreamFromWorkflowContext
(
context
);
const
streamResponse
=
LlamaIndexAdapter
.
toDataStreamResponse
(
stream
,
{
data
:
dataStream
,
});
pipeResponse
(
res
,
streamResponse
);
pipeResponse
(
res
,
streamResponse
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
"
Chat error:
"
,
error
);
console
.
error
(
"
Chat error:
"
,
error
);
...
...
This diff is collapsed.
Click to expand it.
packages/server/src/types.ts
+
6
−
4
View file @
c3d9b2f1
import
{
import
{
AgentWorkflow
,
Workflow
,
Workflow
,
type
ChatMessage
,
type
ChatMessage
,
type
ChatResponseChunk
,
type
ChatResponseChunk
,
type
MessageContent
,
}
from
"
llamaindex
"
;
}
from
"
llamaindex
"
;
export
type
AgentInput
=
{
export
type
AgentInput
=
{
user
Message
:
M
essage
C
ontent
;
user
Input
:
string
;
// the last m
essage
c
ontent
from the user
chatHistory
:
ChatMessage
[];
chatHistory
:
ChatMessage
[];
// the previous chat history (not including the last message)
};
};
export
type
ServerWorkflow
=
Workflow
<
null
,
AgentInput
,
ChatResponseChunk
>
;
export
type
ServerWorkflow
=
|
Workflow
<
null
,
AgentInput
,
ChatResponseChunk
>
|
AgentWorkflow
;
This diff is collapsed.
Click to expand it.
packages/server/src/utils/
stream
.ts
→
packages/server/src/utils/
workflow
.ts
+
24
−
2
View file @
c3d9b2f1
import
{
StreamData
,
type
JSONValue
}
from
"
ai
"
;
import
{
LlamaIndexAdapter
,
StreamData
,
type
JSONValue
}
from
"
ai
"
;
import
{
import
{
AgentWorkflow
,
EngineResponse
,
EngineResponse
,
StopEvent
,
StopEvent
,
WorkflowContext
,
WorkflowContext
,
WorkflowEvent
,
WorkflowEvent
,
type
ChatMessage
,
type
ChatResponseChunk
,
type
ChatResponseChunk
,
}
from
"
llamaindex
"
;
}
from
"
llamaindex
"
;
import
{
ReadableStream
}
from
"
stream/web
"
;
import
{
ReadableStream
}
from
"
stream/web
"
;
import
type
{
ServerWorkflow
}
from
"
../types
"
;
export
async
function
createStreamFromWorkflowContext
<
Input
,
Output
,
Context
>
(
export
async
function
runWorkflow
(
workflow
:
ServerWorkflow
,
userInput
:
string
,
chatHistory
:
ChatMessage
[],
)
{
if
(
workflow
instanceof
AgentWorkflow
)
{
const
context
=
workflow
.
run
(
userInput
,
{
chatHistory
});
const
{
stream
,
dataStream
}
=
await
createStreamFromWorkflowContext
(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
context
as
any
,
);
return
LlamaIndexAdapter
.
toDataStreamResponse
(
stream
,
{
data
:
dataStream
});
}
const
context
=
workflow
.
run
({
userInput
,
chatHistory
});
const
{
stream
,
dataStream
}
=
await
createStreamFromWorkflowContext
(
context
);
return
LlamaIndexAdapter
.
toDataStreamResponse
(
stream
,
{
data
:
dataStream
});
}
async
function
createStreamFromWorkflowContext
<
Input
,
Output
,
Context
>
(
context
:
WorkflowContext
<
Input
,
Output
,
Context
>
,
context
:
WorkflowContext
<
Input
,
Output
,
Context
>
,
):
Promise
<
{
stream
:
ReadableStream
<
EngineResponse
>
;
dataStream
:
StreamData
}
>
{
):
Promise
<
{
stream
:
ReadableStream
<
EngineResponse
>
;
dataStream
:
StreamData
}
>
{
const
dataStream
=
new
StreamData
();
const
dataStream
=
new
StreamData
();
...
...
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