[Community]add option to delete the prompt from HF output (#22225)

This will help to solve pattern mismatching issue when parsing the
output in Agent.

https://github.com/langchain-ai/langchain/issues/21912
This commit is contained in:
Ethan Yang 2024-06-06 06:38:54 +08:00 committed by GitHub
parent c040dc7017
commit 29064848f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 53 additions and 4 deletions

View File

@ -121,6 +121,28 @@
"print(chain.invoke({\"question\": question}))" "print(chain.invoke({\"question\": question}))"
] ]
}, },
{
"cell_type": "markdown",
"id": "b4a31db5",
"metadata": {},
"source": [
"To get response without prompt, you can bind `skip_prompt=True` with LLM."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e4aaad2",
"metadata": {},
"outputs": [],
"source": [
"chain = prompt | hf.bind(skip_prompt=True)\n",
"\n",
"question = \"What is electroencephalography?\"\n",
"\n",
"print(chain.invoke({\"question\": question}))"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "dbbc3a37", "id": "dbbc3a37",

View File

@ -31,7 +31,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"%pip install --upgrade-strategy eager \"optimum[openvino,nncf]\" --quiet" "%pip install --upgrade-strategy eager \"optimum[openvino,nncf]\" langchain-huggingface --quiet"
] ]
}, },
{ {
@ -130,6 +130,28 @@
"print(chain.invoke({\"question\": question}))" "print(chain.invoke({\"question\": question}))"
] ]
}, },
{
"cell_type": "markdown",
"id": "446a01e0",
"metadata": {},
"source": [
"To get response without prompt, you can bind `skip_prompt=True` with LLM."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e3baeab2",
"metadata": {},
"outputs": [],
"source": [
"chain = prompt | ov_llm.bind(skip_prompt=True)\n",
"\n",
"question = \"What is electroencephalography?\"\n",
"\n",
"print(chain.invoke({\"question\": question}))"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "12524837-e9ab-455a-86be-66b95f4f893a", "id": "12524837-e9ab-455a-86be-66b95f4f893a",
@ -243,7 +265,8 @@
" skip_prompt=True,\n", " skip_prompt=True,\n",
" skip_special_tokens=True,\n", " skip_special_tokens=True,\n",
")\n", ")\n",
"ov_llm.pipeline._forward_params = {\"streamer\": streamer, \"max_new_tokens\": 100}\n", "pipeline_kwargs = {\"pipeline_kwargs\": {\"streamer\": streamer, \"max_new_tokens\": 100}}\n",
"chain = prompt | ov_llm.bind(**pipeline_kwargs)\n",
"\n", "\n",
"t1 = Thread(target=chain.invoke, args=({\"question\": question},))\n", "t1 = Thread(target=chain.invoke, args=({\"question\": question},))\n",
"t1.start()\n", "t1.start()\n",

View File

@ -265,6 +265,7 @@ class HuggingFacePipeline(BaseLLM):
# List to hold all results # List to hold all results
text_generations: List[str] = [] text_generations: List[str] = []
pipeline_kwargs = kwargs.get("pipeline_kwargs", {}) pipeline_kwargs = kwargs.get("pipeline_kwargs", {})
skip_prompt = kwargs.get("skip_prompt", False)
for i in range(0, len(prompts), self.batch_size): for i in range(0, len(prompts), self.batch_size):
batch_prompts = prompts[i : i + self.batch_size] batch_prompts = prompts[i : i + self.batch_size]
@ -294,7 +295,8 @@ class HuggingFacePipeline(BaseLLM):
f"Got invalid task {self.pipeline.task}, " f"Got invalid task {self.pipeline.task}, "
f"currently only {VALID_TASKS} are supported" f"currently only {VALID_TASKS} are supported"
) )
if skip_prompt:
text = text[len(batch_prompts[j]) :]
# Append the processed text to results # Append the processed text to results
text_generations.append(text) text_generations.append(text)

View File

@ -261,6 +261,7 @@ class HuggingFacePipeline(BaseLLM):
# List to hold all results # List to hold all results
text_generations: List[str] = [] text_generations: List[str] = []
pipeline_kwargs = kwargs.get("pipeline_kwargs", {}) pipeline_kwargs = kwargs.get("pipeline_kwargs", {})
skip_prompt = kwargs.get("skip_prompt", False)
for i in range(0, len(prompts), self.batch_size): for i in range(0, len(prompts), self.batch_size):
batch_prompts = prompts[i : i + self.batch_size] batch_prompts = prompts[i : i + self.batch_size]
@ -290,7 +291,8 @@ class HuggingFacePipeline(BaseLLM):
f"Got invalid task {self.pipeline.task}, " f"Got invalid task {self.pipeline.task}, "
f"currently only {VALID_TASKS} are supported" f"currently only {VALID_TASKS} are supported"
) )
if skip_prompt:
text = text[len(batch_prompts[j]) :]
# Append the processed text to results # Append the processed text to results
text_generations.append(text) text_generations.append(text)