forked from zc277584121/akcio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_agent.py
55 lines (47 loc) · 2.1 KB
/
chat_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import Any, List, Optional, Sequence
from pydantic import Field
from langchain.schema.language_model import BaseLanguageModel
from langchain.callbacks.base import BaseCallbackManager
from langchain.agents import ConversationalChatAgent, AgentOutputParser, Agent
from langchain.prompts.base import BasePromptTemplate
from langchain.schema import BaseOutputParser
from langchain.tools.base import BaseTool
from .output_parser import OutputParser
from .prompt import PREFIX, SUFFIX, TEMPLATE_TOOL_RESPONSE
class ChatAgent(ConversationalChatAgent):
'''Customize LangChain ConversationalChatAgent'''
output_parser: AgentOutputParser = Field(default_factory=OutputParser)
template_tool_response: str = TEMPLATE_TOOL_RESPONSE
@classmethod
def _get_default_output_parser(cls, **kwargs: Any) -> OutputParser:
return OutputParser(**kwargs)
@classmethod
def from_llm_and_tools(cls,
llm: BaseLanguageModel,
tools: Sequence[BaseTool],
callback_manager: Optional[BaseCallbackManager] = None,
output_parser: Optional[BaseOutputParser] = OutputParser(),
system_message: str = PREFIX,
human_message: str = SUFFIX,
input_variables: Optional[List[str]] = None,
**kwargs: Any
) -> Agent:
return super().from_llm_and_tools(
llm,
tools,
callback_manager,
output_parser,
system_message,
human_message,
input_variables,
**kwargs
)
@classmethod
def create_prompt(
cls,
tools: Sequence[BaseTool],
system_message: str = PREFIX,
human_message: str = SUFFIX,
input_variables: Optional[List[str]] = None,
output_parser: Optional[BaseOutputParser] = None) -> BasePromptTemplate:
return super().create_prompt(tools, system_message, human_message, input_variables, output_parser)