[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
erick/docs-update-chatbedrock-with-tool-calling-docs-do-not-use
Ethan Yang 3 weeks ago committed by GitHub
parent c040dc7017
commit 29064848f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -121,6 +121,28 @@
"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",
"id": "dbbc3a37",

@ -31,7 +31,7 @@
},
"outputs": [],
"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}))"
]
},
{
"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",
"id": "12524837-e9ab-455a-86be-66b95f4f893a",
@ -243,7 +265,8 @@
" skip_prompt=True,\n",
" skip_special_tokens=True,\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",
"t1 = Thread(target=chain.invoke, args=({\"question\": question},))\n",
"t1.start()\n",

@ -265,6 +265,7 @@ class HuggingFacePipeline(BaseLLM):
# List to hold all results
text_generations: List[str] = []
pipeline_kwargs = kwargs.get("pipeline_kwargs", {})
skip_prompt = kwargs.get("skip_prompt", False)
for i in range(0, len(prompts), 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"currently only {VALID_TASKS} are supported"
)
if skip_prompt:
text = text[len(batch_prompts[j]) :]
# Append the processed text to results
text_generations.append(text)

@ -261,6 +261,7 @@ class HuggingFacePipeline(BaseLLM):
# List to hold all results
text_generations: List[str] = []
pipeline_kwargs = kwargs.get("pipeline_kwargs", {})
skip_prompt = kwargs.get("skip_prompt", False)
for i in range(0, len(prompts), 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"currently only {VALID_TASKS} are supported"
)
if skip_prompt:
text = text[len(batch_prompts[j]) :]
# Append the processed text to results
text_generations.append(text)

Loading…
Cancel
Save