目前,我正在返回多个响应,或者模型不知道何时结束响应,并且响应中似乎重复了系统提示(?)。我只想得到一个回复。我的设置非常简单,所以我想我缺少实现细节,但是我该怎么做才能只返回单个响应?
from langchain_community.llms import Ollama
llm = Ollama(model="llama3")
def get_model_response(user_prompt, system_prompt):
prompt = f"""
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
{ system_prompt }
<|eot_id|>
<|start_header_id|>user<|end_header_id|>
{ user_prompt }
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""
response = llm.invoke(prompt)
return response
使用 Langchain 的
PromptTemplate
,并为模型设置停止令牌,我能够得到一个正确的响应。
from langchain_community.llms import Ollama
from langchain import PromptTemplate # Added
llm = Ollama(model="llama3", stop=["<|eot_id|>"]) # Added stop token
def get_model_response(user_prompt, system_prompt):
# NOTE: No f string and no whitespace in curly braces
template = """
<|begin_of_text|>
<|start_header_id|>system<|end_header_id|>
{system_prompt}
<|eot_id|>
<|start_header_id|>user<|end_header_id|>
{user_prompt}
<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""
# Added prompt template
prompt = PromptTemplate(
input_variables=["system_prompt", "user_prompt"],
template=template
)
# Modified invoking the model
response = llm(prompt.format(system_prompt=system_prompt, user_prompt=user_prompt))
return response