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
1b13395e
Commit
1b13395e
authored
1 year ago
by
Elliot Kang
Browse files
Options
Downloads
Patches
Plain Diff
Anthropic steaming support
parent
fe21904b
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/core/src/llm/LLM.ts
+43
-0
43 additions, 0 deletions
packages/core/src/llm/LLM.ts
with
43 additions
and
0 deletions
packages/core/src/llm/LLM.ts
+
43
−
0
View file @
1b13395e
...
...
@@ -4,6 +4,7 @@ import {
Event
,
EventType
,
OpenAIStreamToken
,
AnthropicStreamToken
,
StreamCallbackResponse
,
}
from
"
../callbacks/CallbackManager
"
;
...
...
@@ -606,6 +607,15 @@ export class Anthropic implements LLM {
parentEvent
?:
Event
|
undefined
,
streaming
?:
T
,
):
Promise
<
R
>
{
//Streaming
if
(
streaming
)
{
if
(
!
this
.
hasStreaming
)
{
throw
Error
(
"
No streaming support for this LLM.
"
);
}
return
this
.
streamChat
(
messages
,
parentEvent
)
as
R
;
}
//Non-streaming
const
response
=
await
this
.
session
.
anthropic
.
completions
.
create
({
model
:
this
.
model
,
prompt
:
this
.
mapMessagesToPrompt
(
messages
),
...
...
@@ -620,6 +630,35 @@ export class Anthropic implements LLM {
// That space will be re-added when we generate the next prompt.
}
as
R
;
}
protected
async
*
streamChat
(
messages
:
ChatMessage
[],
parentEvent
?:
Event
|
undefined
):
AsyncGenerator
<
string
,
void
,
unknown
>
{
// AsyncIterable<AnthropicStreamToken>
const
stream
:
AsyncIterable
<
AnthropicStreamToken
>
=
await
this
.
session
.
anthropic
.
completions
.
create
({
model
:
this
.
model
,
prompt
:
this
.
mapMessagesToPrompt
(
messages
),
max_tokens_to_sample
:
this
.
maxTokens
??
100000
,
temperature
:
this
.
temperature
,
top_p
:
this
.
topP
,
streaming
:
true
})
var
idx_counter
:
number
=
0
;
for
await
(
const
part
of
stream
)
{
//Increment
part
.
choices
[
0
].
index
=
idx_counter
;
const
is_done
:
boolean
=
part
.
choices
[
0
].
finish_reason
===
"
stop
"
?
true
:
false
;
//TODO: LLM Stream Callback, pending re-work.
idx_counter
++
;
yield
part
.
choices
[
0
].
delta
.
content
?
part
.
choices
[
0
].
delta
.
content
:
""
;
return
;
}
}
async
complete
<
T
extends
boolean
|
undefined
=
undefined
,
R
=
T
extends
true
?
AsyncGenerator
<
string
,
void
,
unknown
>
:
ChatResponse
,
...
...
@@ -630,4 +669,8 @@ export class Anthropic implements LLM {
):
Promise
<
R
>
{
return
this
.
chat
([{
content
:
prompt
,
role
:
"
user
"
}],
parentEvent
)
as
R
;
}
protected
stream_complete
(
prompt
:
string
,
parentEvent
?:
Event
|
undefined
):
AsyncGenerator
<
string
,
void
,
unknown
>
{
return
this
.
streamChat
([{
content
:
prompt
,
role
:
"
user
"
}],
parentEvent
);
}
}
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