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
f11f66ed
Commit
f11f66ed
authored
Jul 24, 2023
by
John Wang
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feat/universal-chat' into deploy/dev
parents
27401bc2
e062b1ee
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
50 additions
and
5 deletions
+50
-5
structured_chat.py
api/core/agent/agent/structured_chat.py
+31
-0
dataset_tool_callback_handler.py
api/core/callback_handler/dataset_tool_callback_handler.py
+2
-1
streamable_chat_anthropic.py
api/core/llm/streamable_chat_anthropic.py
+14
-1
orchestrator_rule_parser.py
api/core/orchestrator_rule_parser.py
+2
-2
dataset_retriever_tool.py
api/core/tool/dataset_retriever_tool.py
+1
-1
No files found.
api/core/agent/agent/structured_chat.py
View file @
f11f66ed
import
re
from
typing
import
List
,
Tuple
,
Any
,
Union
,
Sequence
,
Optional
from
langchain
import
BasePromptTemplate
...
...
@@ -7,6 +8,7 @@ from langchain.base_language import BaseLanguageModel
from
langchain.callbacks.base
import
BaseCallbackManager
from
langchain.callbacks.manager
import
Callbacks
from
langchain.memory.summary
import
SummarizerMixin
from
langchain.prompts
import
SystemMessagePromptTemplate
,
HumanMessagePromptTemplate
,
ChatPromptTemplate
from
langchain.schema
import
AgentAction
,
AgentFinish
,
AIMessage
,
HumanMessage
from
langchain.tools
import
BaseTool
from
langchain.agents.structured_chat.prompt
import
PREFIX
,
SUFFIX
...
...
@@ -121,6 +123,35 @@ class AutoSummarizingStructuredChatAgent(StructuredChatAgent, CalcTokenMixin):
return
self
.
get_full_inputs
([
intermediate_steps
[
-
1
]],
**
kwargs
)
@
classmethod
def
create_prompt
(
cls
,
tools
:
Sequence
[
BaseTool
],
prefix
:
str
=
PREFIX
,
suffix
:
str
=
SUFFIX
,
human_message_template
:
str
=
HUMAN_MESSAGE_TEMPLATE
,
format_instructions
:
str
=
FORMAT_INSTRUCTIONS
,
input_variables
:
Optional
[
List
[
str
]]
=
None
,
memory_prompts
:
Optional
[
List
[
BasePromptTemplate
]]
=
None
,
)
->
BasePromptTemplate
:
tool_strings
=
[]
for
tool
in
tools
:
args_schema
=
re
.
sub
(
"}"
,
"}}}}"
,
re
.
sub
(
"{"
,
"{{{{"
,
str
(
tool
.
args
)))
tool_strings
.
append
(
f
"{tool.name}: {tool.description}, args: {args_schema}"
)
formatted_tools
=
"
\n
"
.
join
(
tool_strings
)
tool_names
=
", "
.
join
([(
'"'
+
tool
.
name
+
'"'
)
for
tool
in
tools
])
format_instructions
=
format_instructions
.
format
(
tool_names
=
tool_names
)
template
=
"
\n\n
"
.
join
([
prefix
,
formatted_tools
,
format_instructions
,
suffix
])
if
input_variables
is
None
:
input_variables
=
[
"input"
,
"agent_scratchpad"
]
_memory_prompts
=
memory_prompts
or
[]
messages
=
[
SystemMessagePromptTemplate
.
from_template
(
template
),
*
_memory_prompts
,
HumanMessagePromptTemplate
.
from_template
(
human_message_template
),
]
return
ChatPromptTemplate
(
input_variables
=
input_variables
,
messages
=
messages
)
@
classmethod
def
from_llm_and_tools
(
cls
,
...
...
api/core/callback_handler/dataset_tool_callback_handler.py
View file @
f11f66ed
...
...
@@ -47,7 +47,8 @@ class DatasetToolCallbackHandler(BaseCallbackHandler):
# tool_name = serialized.get('name')
input_dict
=
json
.
loads
(
input_str
.
replace
(
"'"
,
"
\"
"
))
dataset_id
=
input_dict
.
get
(
'dataset_id'
)
self
.
conversation_message_task
.
on_dataset_query_end
(
DatasetQueryObj
(
dataset_id
=
dataset_id
,
query
=
input_str
))
query
=
input_dict
.
get
(
'query'
)
self
.
conversation_message_task
.
on_dataset_query_end
(
DatasetQueryObj
(
dataset_id
=
dataset_id
,
query
=
query
))
def
on_tool_end
(
self
,
...
...
api/core/llm/streamable_chat_anthropic.py
View file @
f11f66ed
...
...
@@ -2,7 +2,7 @@ from typing import List, Optional, Any, Dict
from
langchain.callbacks.manager
import
Callbacks
from
langchain.chat_models
import
ChatAnthropic
from
langchain.schema
import
BaseMessage
,
LLMResult
from
langchain.schema
import
BaseMessage
,
LLMResult
,
SystemMessage
,
AIMessage
,
HumanMessage
,
ChatMessage
from
pydantic
import
root_validator
from
core.llm.wrappers.anthropic_wrapper
import
handle_anthropic_exceptions
...
...
@@ -44,3 +44,16 @@ class StreamableChatAnthropic(ChatAnthropic):
del
params
[
'presence_penalty'
]
return
params
def
_convert_one_message_to_text
(
self
,
message
:
BaseMessage
)
->
str
:
if
isinstance
(
message
,
ChatMessage
):
message_text
=
f
"
\n\n
{message.role.capitalize()}: {message.content}"
elif
isinstance
(
message
,
HumanMessage
):
message_text
=
f
"{self.HUMAN_PROMPT} {message.content}"
elif
isinstance
(
message
,
AIMessage
):
message_text
=
f
"{self.AI_PROMPT} {message.content}"
elif
isinstance
(
message
,
SystemMessage
):
message_text
=
f
"<admin>{message.content}</admin>"
else
:
raise
ValueError
(
f
"Got unknown type {message}"
)
return
message_text
\ No newline at end of file
api/core/orchestrator_rule_parser.py
View file @
f11f66ed
...
...
@@ -158,7 +158,7 @@ class OrchestratorRuleParser:
tool
=
self
.
to_wikipedia_tool
()
if
tool
:
tool
.
callbacks
=
callbacks
tool
.
callbacks
.
extend
(
callbacks
)
tools
.
append
(
tool
)
return
tools
...
...
@@ -186,7 +186,7 @@ class OrchestratorRuleParser:
tool
=
DatasetRetrieverTool
.
from_dataset
(
dataset
=
dataset
,
k
=
k
,
callbacks
=
[
DatasetToolCallbackHandler
(
conversation_message_task
)
,
DifyStdOutCallbackHandler
()
]
callbacks
=
[
DatasetToolCallbackHandler
(
conversation_message_task
)]
)
return
tool
...
...
api/core/tool/dataset_retriever_tool.py
View file @
f11f66ed
...
...
@@ -32,7 +32,7 @@ class DatasetRetrieverTool(BaseTool):
@
classmethod
def
from_dataset
(
cls
,
dataset
:
Dataset
,
**
kwargs
):
description
=
dataset
.
description
description
=
dataset
.
description
.
replace
(
'
\n
'
,
''
)
.
replace
(
'
\r
'
,
''
)
if
not
description
:
description
=
'useful for when you want to answer queries about the '
+
dataset
.
name
...
...
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