Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dify
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ai-tech
dify
Commits
1b6e3ef9
Unverified
Commit
1b6e3ef9
authored
May 21, 2023
by
John Wang
Committed by
GitHub
May 21, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Feat: optimize inner prompt (#121)
parent
4779fcf6
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
27 deletions
+29
-27
completion.py
api/core/completion.py
+29
-27
No files found.
api/core/completion.py
View file @
1b6e3ef9
...
@@ -125,13 +125,17 @@ class Completion:
...
@@ -125,13 +125,17 @@ class Completion:
pre_prompt
=
PromptBuilder
.
process_template
(
pre_prompt
)
if
pre_prompt
else
pre_prompt
pre_prompt
=
PromptBuilder
.
process_template
(
pre_prompt
)
if
pre_prompt
else
pre_prompt
if
mode
==
'completion'
:
if
mode
==
'completion'
:
prompt_template
=
OutLinePromptTemplate
.
from_template
(
prompt_template
=
OutLinePromptTemplate
.
from_template
(
template
=
(
"Use the following pieces of [CONTEXT] to answer the question at the end. "
template
=
(
"""Use the following CONTEXT as your learned knowledge:
"If you don't know the answer, "
[CONTEXT]
"just say that you don't know, don't try to make up an answer.
\n
"
{context}
"```
\n
"
[END CONTEXT]
"[CONTEXT]
\n
"
"{context}
\n
"
When answer to user:
"```
\n
"
if
chain_output
else
""
)
- If you don't know, just say that you don't know.
- If you don't know when you are not sure, ask for clarification.
Avoid mentioning that you obtained the information from the context.
And answer according to the language of the user's question.
"""
if
chain_output
else
""
)
+
(
pre_prompt
+
"
\n
"
if
pre_prompt
else
""
)
+
(
pre_prompt
+
"
\n
"
if
pre_prompt
else
""
)
+
"{query}
\n
"
+
"{query}
\n
"
)
)
...
@@ -153,38 +157,36 @@ class Completion:
...
@@ -153,38 +157,36 @@ class Completion:
else
:
else
:
messages
:
List
[
BaseMessage
]
=
[]
messages
:
List
[
BaseMessage
]
=
[]
system_message
=
None
if
pre_prompt
:
if
pre_prompt
:
# append pre prompt as system message
# append pre prompt as system message
system_message
=
PromptBuilder
.
to_system_message
(
pre_prompt
,
inputs
)
system_message
=
PromptBuilder
.
to_system_message
(
pre_prompt
,
inputs
)
messages
.
append
(
system_message
)
human_inputs
=
{
"query"
:
query
}
human_message_prompt
=
"{query}"
if
chain_output
:
if
chain_output
:
# append context as system message, currently only use simple stuff prompt
human_inputs
[
'context'
]
=
chain_output
context_message
=
PromptBuilder
.
to_system_message
(
human_message_prompt
=
"""Use the following CONTEXT as your learned knowledge.
"""Use the following pieces of [CONTEXT] to answer the users question.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
```
[CONTEXT]
[CONTEXT]
{context}
{context}
```"""
,
[END CONTEXT]
{
'context'
:
chain_output
}
)
if
not
system_message
:
system_message
=
context_message
else
:
system_message
.
content
=
context_message
.
content
+
"
\n\n
"
+
system_message
.
content
if
system_message
:
When answer to user:
messages
.
append
(
system_message
)
- If you don't know, just say that you don't know.
- If you don't know when you are not sure, ask for clarification.
Avoid mentioning that you obtained the information from the context.
And answer according to the language of the user's question.
human_inputs
=
{
Q:{query}
"query"
:
query
A:"""
}
# construct main prompt
# construct main prompt
human_message
=
PromptBuilder
.
to_human_message
(
human_message
=
PromptBuilder
.
to_human_message
(
prompt_content
=
"{query}"
,
prompt_content
=
human_message_prompt
,
inputs
=
human_inputs
inputs
=
human_inputs
)
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment