fix(ollama): handle optional initial_prompt_value and final_prompt_value in custom prompts (#39759)

This commit is contained in:
VimalN2005 2026-09-12 10:22:27 +05:30
parent c2c2a623c0
commit 3ef5a354e3
2 changed files with 48 additions and 2 deletions

View file

@ -384,8 +384,10 @@ class OllamaConfig(BaseConfig):
model_prompt_details: Final = custom_prompt_dict[model]
ollama_prompt = custom_prompt(
role_dict=model_prompt_details["roles"],
initial_prompt_value=model_prompt_details["initial_prompt_value"],
final_prompt_value=model_prompt_details["final_prompt_value"],
initial_prompt_value=model_prompt_details.get("initial_prompt_value", ""),
final_prompt_value=model_prompt_details.get("final_prompt_value", ""),
bos_token=model_prompt_details.get("bos_token", ""),
eos_token=model_prompt_details.get("eos_token", ""),
messages=messages,
)
elif text_completion_request: # handle `/completions` requests

View file

@ -544,3 +544,47 @@ async def test_ollama_async_completion_inlines_remote_images_off_the_event_loop(
assert response.choices[0].message.content == "Green"
assert async_only_image_fetch.fetched == [image_url]
assert captured["body"]["images"] == [async_only_image_fetch.base64_png]
def test_ollama_transform_request_custom_prompt_optional_values():
config = OllamaConfig()
model = "custom-ollama-model"
messages = [{"role": "user", "content": "hello world"}]
litellm_params = {
"custom_prompt_dict": {
model: {
"roles": {
"user": {"pre_message": "[INST] ", "post_message": " [/INST]"}
}
}
}
}
result = config.transform_request(
model=model,
messages=messages,
optional_params={},
litellm_params=litellm_params,
headers={},
)
assert result["prompt"] == "[INST] hello world [/INST]"
litellm_params_initial_only = {
"custom_prompt_dict": {
model: {
"roles": {
"user": {"pre_message": "Human: ", "post_message": "\n"}
},
"initial_prompt_value": "Instructions:\n",
}
}
}
result_initial = config.transform_request(
model=model,
messages=messages,
optional_params={},
litellm_params=litellm_params_initial_only,
headers={},
)
assert result_initial["prompt"] == "Instructions:\nHuman: hello world\n"