问题验证
- 我已经在文档和discord上搜索过答案。
问题
我设置了使用远程llama-3模型:
import os
from llama_index.core import PromptTemplate
from llama_index.llms.openai_like import OpenAILike
SERVER_LLAMACPP = os.getenv("SERVER_LLAMACPP", "http://localhost:8181")
os.environ["OPENAI_API_KEY"] = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
os.environ["OPENAI_API_BASE"] = f"{SERVER_LLAMACPP}/v1"
os.environ["OPENAI_API_HOST"] = f"{SERVER_LLAMACPP}"
model = OpenAILike()
p = """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{system_prompt}<|eot_id|><|start_header_id|>user<|end_header_id|>
{prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>"""
print(
model.complete(
prompt=p.format(
system_prompt="You reply accurately to the user",
prompt="What is the capital of France?",
)
)
)
在OpenAILike
中,我可以分配一个query_wrapper_prompt
参数,但这似乎没有任何区别,如果我稍后调用这个(输出是乱码):
model.complete(prompt="What is the capital of France?")
一种可能的方法是子类化OpenAILike
,然后用llama-3提示模板格式化我的内容,但这似乎有点过分。我在文档或问题中找不到关于默认提示模板的信息,我的问题是:
- 如何将
PromptTemplate
设置为模型的默认 Package 器? - 这是与llama-index正确合作的方式吗?
2条答案
按热度按时间ztmd8pv51#
要将
PromptTemplate
设置为与llama-index
一起使用的模型的默认 Package 器,您可以使用SelectorPromptTemplate
类来定义您的默认提示和任何条件。以下是如何执行此操作的示例:此代码片段演示了如何创建一个
SelectorPromptTemplate
,定义自定义提示和条件,并将其设置为模型的默认 Package 器。SelectorPromptTemplate
类允许您指定默认模板和条件,类似于在llama-index
库中定义默认提示选择器的方式。关于您的第二个问题,当您需要为您的模型设置默认提示模板时,这种方法确实是一种合适的处理方式。它避免了需要继承
OpenAILike
并提供了一种灵活的管理提示模板的方法。c0vxltue2#
@egeres,您可以在OpenAILike中设置一些函数钩子,例如
completion_to_prompt()
和messages_to_prompt()
。