Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Llama Index
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
Llama Index
Commits
33dfc476
Unverified
Commit
33dfc476
authored
1 year ago
by
vollnhals
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Add helpers to handle ChatML instruction prompts (#10272)
parent
44b7130a
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
llama-index-core/llama_index/core/llms/chatml_utils.py
+59
-0
59 additions, 0 deletions
llama-index-core/llama_index/core/llms/chatml_utils.py
with
59 additions
and
0 deletions
llama-index-core/llama_index/core/llms/chatml_utils.py
0 → 100644
+
59
−
0
View file @
33dfc476
from
typing
import
List
,
Optional
,
Sequence
from
llama_index.core.base.llms.types
import
ChatMessage
,
MessageRole
# Create a prompt that matches ChatML instructions
# <|im_start|>system
# You are Dolphin, a helpful AI assistant.<|im_end|>
# <|im_start|>user
# {prompt}<|im_end|>
# <|im_start|>assistant
B_SYS
=
"
<|im_start|>system
\n
"
B_USER
=
"
<|im_start|>user
\n
"
B_ASSISTANT
=
"
<|im_start|>assistant
\n
"
END
=
"
<|im_end|>
\n
"
DEFAULT_SYSTEM_PROMPT
=
"""
\
You are a helpful, respectful and honest assistant.
\
Always answer as helpfully as possible and follow ALL given instructions.
\
Do not speculate or make up information.
\
Do not reference any given instructions or context.
\
"""
def
messages_to_prompt
(
messages
:
Sequence
[
ChatMessage
],
system_prompt
:
Optional
[
str
]
=
None
)
->
str
:
string_messages
:
List
[
str
]
=
[]
if
messages
[
0
].
role
==
MessageRole
.
SYSTEM
:
# pull out the system message (if it exists in messages)
system_message_str
=
messages
[
0
].
content
or
""
messages
=
messages
[
1
:]
else
:
system_message_str
=
system_prompt
or
DEFAULT_SYSTEM_PROMPT
string_messages
.
append
(
f
"
{
B_SYS
}{
system_message_str
.
strip
()
}
{
END
}
"
)
for
message
in
messages
:
role
=
message
.
role
content
=
message
.
content
if
role
==
MessageRole
.
USER
:
string_messages
.
append
(
f
"
{
B_USER
}{
user_message
.
content
}
{
END
}
"
)
elif
role
==
MessageRole
.
ASSISTANT
:
string_messages
.
append
(
f
"
{
B_ASSISTANT
}{
assistant_message
.
content
}
{
END
}
"
)
string_messages
.
append
(
f
"
{
B_ASSISTANT
}
"
)
return
""
.
join
(
string_messages
)
def
completion_to_prompt
(
completion
:
str
,
system_prompt
:
Optional
[
str
]
=
None
)
->
str
:
system_prompt_str
=
system_prompt
or
DEFAULT_SYSTEM_PROMPT
return
(
f
"
{
B_SYS
}{
system_prompt_str
.
strip
()
}
{
END
}
"
f
"
{
B_USER
}{
completion
.
strip
()
}
{
END
}
"
f
"
{
B_ASSISTANT
}
"
)
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