forked from shroominic/codeinterpreter-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# agent_executor.py | ||
# https://github.com/langchain-ai/langchain/blob/3ee07473821906a29d944866a2ededb41148f234/libs/experimental/langchain_experimental/plan_and_execute/executors/agent_executor.py |
74 changes: 74 additions & 0 deletions
74
src/codeinterpreterapi/agents/plan_and_execute/agent_executor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from typing import List, Optional | ||
|
||
from langchain.agents.agent import AgentExecutor, AgentOutputParser | ||
from langchain.agents.structured_chat.base import StructuredChatAgent | ||
from langchain.tools import BaseTool | ||
from langchain_core.callbacks import BaseCallbackManager | ||
from langchain_core.language_models import BaseLanguageModel | ||
from langchain_experimental.plan_and_execute.executors.base import ChainExecutor | ||
|
||
from codeinterpreterapi.agents.plan_and_execute.prompts import ( | ||
FORMAT_INSTRUCTIONS, | ||
FORMAT_INSTRUCTIONS_JA, | ||
HUMAN_MESSAGE_TEMPLATE, | ||
SUFFIX, | ||
SUFFIX_JA, | ||
TASK_PREFIX, | ||
TOOLS_PREFIX, | ||
TOOLS_PREFIX_JA, | ||
) | ||
|
||
|
||
def load_agent_executor( | ||
llm: BaseLanguageModel, | ||
tools: List[BaseTool], | ||
callback_manager: Optional[BaseCallbackManager] = None, | ||
output_parser: Optional[AgentOutputParser] = None, | ||
verbose: bool = False, | ||
include_task_in_prompt: bool = False, | ||
is_ja: str = True, | ||
) -> ChainExecutor: | ||
""" | ||
Load an agent executor. | ||
Args: | ||
llm: BaseLanguageModel | ||
tools: List[BaseTool] | ||
verbose: bool. Defaults to False. | ||
include_task_in_prompt: bool. Defaults to False. | ||
Returns: | ||
ChainExecutor | ||
""" | ||
input_variables = ["previous_steps", "current_step", "agent_scratchpad"] | ||
|
||
# message_template | ||
message_template = "" | ||
if include_task_in_prompt: | ||
input_variables.append("objective") | ||
message_template += TASK_PREFIX | ||
message_template += HUMAN_MESSAGE_TEMPLATE | ||
|
||
# format_instructions, tools_prefix, suffix | ||
if is_ja: | ||
format_instructions = FORMAT_INSTRUCTIONS_JA | ||
tools_prefix = TOOLS_PREFIX_JA | ||
suffix = SUFFIX_JA | ||
else: | ||
format_instructions = FORMAT_INSTRUCTIONS | ||
tools_prefix = TOOLS_PREFIX | ||
suffix = SUFFIX | ||
agent = StructuredChatAgent.from_llm_and_tools( | ||
llm=llm, | ||
tools=tools, | ||
callback_manager=callback_manager, | ||
output_parser=output_parser, | ||
prefix=tools_prefix, | ||
suffix=suffix, | ||
human_message_template=message_template, | ||
format_instructions=format_instructions, | ||
input_variables=input_variables, | ||
# memory_prompts = memory_prompts, | ||
) | ||
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=verbose) | ||
return ChainExecutor(chain=agent_executor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from textwrap import dedent | ||
|
||
from langchain_core.prompts import PromptTemplate | ||
from langchain_experimental.tot.prompts import JSONListOutputParser | ||
|
||
HUMAN_MESSAGE_TEMPLATE = """Previous steps: {previous_steps} | ||
Current objective: {current_step} | ||
{agent_scratchpad}""" | ||
|
||
TASK_PREFIX = """{objective} | ||
""" | ||
|
||
TOOLS_PREFIX = ( | ||
"""Respond to the human as helpfully and accurately as possible. You have access to the following tools:""" | ||
) | ||
FORMAT_INSTRUCTIONS = """Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input). | ||
Valid "action" values: "Final Answer" or {tool_names} | ||
Provide only ONE action per $JSON_BLOB, as shown: | ||
``` | ||
{{{{ | ||
"action": $TOOL_NAME, | ||
"action_input": $INPUT | ||
}}}} | ||
``` | ||
Follow this format: | ||
Question: input question to answer | ||
Thought: consider previous and subsequent steps | ||
Action: | ||
``` | ||
$JSON_BLOB | ||
``` | ||
Observation: action result | ||
... (repeat Thought/Action/Observation N times) | ||
Thought: I know what to respond | ||
Action: | ||
``` | ||
{{{{ | ||
"action": "Final Answer", | ||
"action_input": "Final response to human" | ||
}}}} | ||
```""" | ||
SUFFIX = """Begin! Reminder to ALWAYS respond with a valid json blob of a single action. | ||
Use tools if necessary. Respond directly if appropriate. | ||
Format is like this. | ||
Action:```$JSON_BLOB``` | ||
Observation: | ||
Thought:""" | ||
|
||
TOOLS_PREFIX_JA = """できる限り丁寧かつ正確に人間の質問に答えてください。以下のツールが利用可能です:""" | ||
|
||
FORMAT_INSTRUCTIONS_JA = """ | ||
始めましょう!常に単一のアクションの有効なJSONブロブで応答することを忘れないでください。 | ||
必要に応じてツールを使用してください。適切な場合は直接回答してください。 | ||
フォーマットは次のようにしてください。 | ||
Action:```$JSON_BLOB``` | ||
Observation: | ||
Thought: | ||
""" | ||
SUFFIX_JA = """それでは始めましょう。常に単一のアクションの有効なJSON_BLOBで応答してください。 | ||
必要に応じてツールを使用してください。適切な場合は直接回答してください。 | ||
フォーマットは次のようにしてください。 | ||
Action:```$JSON_BLOB``` | ||
Observation: | ||
Thought:""" | ||
|
||
|
||
def get_tools_prefix_prompt(is_ja: bool) -> str: | ||
"""Get the prefix prompt for plan_and_execute.""" | ||
if is_ja: | ||
return TOOLS_PREFIX_JA | ||
else: | ||
return TOOLS_PREFIX | ||
|
||
|
||
def get_plan_and_execute_prompt(is_ja: bool) -> str: | ||
"""Get the main prompt for plan_and_execute.""" | ||
if is_ja: | ||
return FORMAT_INSTRUCTIONS_JA | ||
else: | ||
return FORMAT_INSTRUCTIONS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters