Skip to content

Commit 1787c47

Browse files
fpinghamlesscomfortable
andauthored
Custom prompt option for llm_bash and api chains (#612)
Co-authored-by: lesscomfortable <[email protected]>
1 parent 67808ba commit 1787c47

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

docs/modules/chains/examples/llm_bash.ipynb

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"\n",
2929
"Answer: \u001b[33;1m\u001b[1;3mHello World\n",
3030
"\u001b[0m\n",
31-
"\u001b[1m> Finished LLMBashChain chain.\u001b[0m\n"
31+
"\u001b[1m> Finished chain.\u001b[0m\n"
3232
]
3333
},
3434
{
@@ -55,12 +55,83 @@
5555
"bash_chain.run(text)"
5656
]
5757
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"## Customize Prompt\n",
63+
"You can also customize the prompt that is used. Here is an example prompting to avoid using the 'echo' utility"
64+
]
65+
},
5866
{
5967
"cell_type": "code",
60-
"execution_count": null,
68+
"execution_count": 28,
6169
"metadata": {},
6270
"outputs": [],
63-
"source": []
71+
"source": [
72+
"from langchain.prompts.prompt import PromptTemplate\n",
73+
"\n",
74+
"_PROMPT_TEMPLATE = \"\"\"If someone asks you to perform a task, your job is to come up with a series of bash commands that will perform the task. There is no need to put \"#!/bin/bash\" in your answer. Make sure to reason step by step, using this format:\n",
75+
"Question: \"copy the files in the directory named 'target' into a new directory at the same level as target called 'myNewDirectory'\"\n",
76+
"I need to take the following actions:\n",
77+
"- List all files in the directory\n",
78+
"- Create a new directory\n",
79+
"- Copy the files from the first directory into the second directory\n",
80+
"```bash\n",
81+
"ls\n",
82+
"mkdir myNewDirectory\n",
83+
"cp -r target/* myNewDirectory\n",
84+
"```\n",
85+
"\n",
86+
"Do not use 'echo' when writing the script.\n",
87+
"\n",
88+
"That is the format. Begin!\n",
89+
"Question: {question}\"\"\"\n",
90+
"\n",
91+
"PROMPT = PromptTemplate(input_variables=[\"question\"], template=_PROMPT_TEMPLATE)"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 29,
97+
"metadata": {},
98+
"outputs": [
99+
{
100+
"name": "stdout",
101+
"output_type": "stream",
102+
"text": [
103+
"\n",
104+
"\n",
105+
"\u001b[1m> Entering new LLMBashChain chain...\u001b[0m\n",
106+
"Please write a bash script that prints 'Hello World' to the console.\u001b[32;1m\u001b[1;3m\n",
107+
"\n",
108+
"```bash\n",
109+
"printf \"Hello World\\n\"\n",
110+
"```\u001b[0m['```bash', 'printf \"Hello World\\\\n\"', '```']\n",
111+
"\n",
112+
"Answer: \u001b[33;1m\u001b[1;3mHello World\n",
113+
"\u001b[0m\n",
114+
"\u001b[1m> Finished chain.\u001b[0m\n"
115+
]
116+
},
117+
{
118+
"data": {
119+
"text/plain": [
120+
"'Hello World\\n'"
121+
]
122+
},
123+
"execution_count": 29,
124+
"metadata": {},
125+
"output_type": "execute_result"
126+
}
127+
],
128+
"source": [
129+
"bash_chain = LLMBashChain(llm=llm, prompt=PROMPT, verbose=True)\n",
130+
"\n",
131+
"text = \"Please write a bash script that prints 'Hello World' to the console.\"\n",
132+
"\n",
133+
"bash_chain.run(text)"
134+
]
64135
}
65136
],
66137
"metadata": {
@@ -79,7 +150,7 @@
79150
"name": "python",
80151
"nbconvert_exporter": "python",
81152
"pygments_lexer": "ipython3",
82-
"version": "3.10.9"
153+
"version": "3.10.6"
83154
}
84155
},
85156
"nbformat": 4,

langchain/chains/api/base.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from langchain.chains.base import Chain
1010
from langchain.chains.llm import LLMChain
1111
from langchain.llms.base import BaseLLM
12+
from langchain.prompts import BasePromptTemplate
1213
from langchain.requests import RequestsWrapper
1314

1415

@@ -80,12 +81,18 @@ def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
8081

8182
@classmethod
8283
def from_llm_and_api_docs(
83-
cls, llm: BaseLLM, api_docs: str, headers: Optional[dict] = None, **kwargs: Any
84+
cls,
85+
llm: BaseLLM,
86+
api_docs: str,
87+
headers: Optional[dict] = None,
88+
api_url_prompt: BasePromptTemplate = API_URL_PROMPT,
89+
api_response_prompt: BasePromptTemplate = API_RESPONSE_PROMPT,
90+
**kwargs: Any,
8491
) -> APIChain:
8592
"""Load chain from just an LLM and the api docs."""
86-
get_request_chain = LLMChain(llm=llm, prompt=API_URL_PROMPT)
93+
get_request_chain = LLMChain(llm=llm, prompt=api_url_prompt)
8794
requests_wrapper = RequestsWrapper(headers=headers)
88-
get_answer_chain = LLMChain(llm=llm, prompt=API_RESPONSE_PROMPT)
95+
get_answer_chain = LLMChain(llm=llm, prompt=api_response_prompt)
8996
return cls(
9097
api_request_chain=get_request_chain,
9198
api_answer_chain=get_answer_chain,

langchain/chains/llm_bash/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from langchain.chains.llm import LLMChain
88
from langchain.chains.llm_bash.prompt import PROMPT
99
from langchain.llms.base import BaseLLM
10+
from langchain.prompts.base import BasePromptTemplate
1011
from langchain.utilities.bash import BashProcess
1112

1213

@@ -24,6 +25,7 @@ class LLMBashChain(Chain, BaseModel):
2425
"""LLM wrapper to use."""
2526
input_key: str = "question" #: :meta private:
2627
output_key: str = "answer" #: :meta private:
28+
prompt: BasePromptTemplate = PROMPT
2729

2830
class Config:
2931
"""Configuration for this pydantic object."""
@@ -48,7 +50,7 @@ def output_keys(self) -> List[str]:
4850
return [self.output_key]
4951

5052
def _call(self, inputs: Dict[str, str]) -> Dict[str, str]:
51-
llm_executor = LLMChain(prompt=PROMPT, llm=self.llm)
53+
llm_executor = LLMChain(prompt=self.prompt, llm=self.llm)
5254
bash_executor = BashProcess()
5355
if self.verbose:
5456
self.callback_manager.on_text(inputs[self.input_key])

0 commit comments

Comments
 (0)