主题
PremAI 是一个一体化平台,可简化由生成式 AI 驱动的稳健、生产级应用的创建。通过简化开发流程,PremAI 让你能够专注于提升用户体验并推动应用的总体增长。你可以在此处快速开始使用我们的平台。
安装与设置
我们首先安装 llama-index 和 premai-sdk。可以输入以下命令进行安装:
终端窗口
pip install premai llama-index在继续之前,请确保你已在 PremAI 上创建账户并创建了项目。如果没有,请参阅快速开始指南以开始使用 PremAI 平台。创建你的第一个项目并获取 API 密钥。
%pip install llama-index-llms-premaifrom llama_index.llms.premai import PremAI
from llama_index.core.llms import ChatMessage使用 LlamaIndex 设置 PremAI 客户端
导入所需模块后,让我们设置客户端。现在假设我们的 project_id 是 8。但请务必使用你自己的 project-id,否则会抛出错误。
要在 PremAI 中使用 llama-index,你无需传递任何模型名称,也无需为聊天客户端设置任何参数。默认情况下,它会使用 LaunchPad 中使用的模型名称和参数。
如果你在设置客户端时更改
model或任何其他参数(如temperature或max_tokens),它将覆盖 LaunchPad 中使用的现有默认配置。
import os
import getpass
if os.environ.get("PREMAI_API_KEY") is None:
os.environ["PREMAI_API_KEY"] = getpass.getpass("PremAI API Key:")
prem_chat = PremAI(project_id=8)聊天补全
现在一切就绪。我们可以开始与应用程序交互了。让我们先用 llama-index 构建简单的聊天请求和响应。
messages = [
ChatMessage(role="user", content="What is your name"),
ChatMessage(
role="user", content="Write an essay about your school in 500 words"
),
]请注意:你可以像这样在 ChatMessage 中提供系统提示词:
messages = [
ChatMessage(role="system", content="Act like a pirate"),
ChatMessage(role="user", content="What is your name"),
ChatMessage(role="user", content="Where do you live, write an essay in 500 words"),
]此外,你也可以像这样在实例化客户端时提供系统提示词:
chat = PremAI(project_id=8, system_prompt="Act like nemo fish")在这两种场景中,你都会覆盖在平台上部署应用时固定的系统提示词。而且具体来说,如果你在实例化 PremAI 类时覆盖了系统提示词,那么
ChatMessage中的系统消息将不会产生任何效果。
因此,如果你希望在任何实验场景中覆盖系统提示词,你需要么在实例化客户端时提供,要么在
ChatMessage中以system角色写入。
现在让我们调用模型
response = prem_chat.chat(messages)
print(response)[ChatResponse(message=ChatMessage(role=<MessageRole.ASSISTANT: 'assistant'>, content="I'm here to assist you with any questions or tasks you have, but I'm not able to write essays. However, if you need help brainstorming ideas or organizing your thoughts for your essay about your school, I'd be happy to help with that. Just let me know how I can assist you further!", additional_kwargs={}), raw={'role': <RoleEnum.ASSISTANT: 'assistant'>, 'content': "I'm here to assist you with any questions or tasks you have, but I'm not able to write essays. However, if you need help brainstorming ideas or organizing your thoughts for your essay about your school, I'd be happy to help with that. Just let me know how I can assist you further!"}, delta=None, additional_kwargs={})]你还可以将聊天函数转换为补全函数。其工作方式如下
completion = prem_chat.complete("Paul Graham is ")如果你要在此处放置系统提示词,那么它将覆盖你在平台上部署应用时固定的系统提示词。
Prem Repositories 原生 RAG 支持
Prem Repositories 允许用户上传文档(.txt、.pdf 等)并将这些仓库连接到 LLM。你可以将 Prem repositories 视为原生 RAG,其中每个仓库都可以看作一个向量数据库。你可以连接多个仓库。你可以在此处详细了解 repositories。
langchain premai 也支持 repositories。具体做法如下。
query = "what is the diameter of individual Galaxy"
repository_ids = [
1991,
]
repositories = dict(ids=repository_ids, similarity_threshold=0.3, limit=3)首先,我们通过一些 repository id 来定义我们的仓库。请确保这些 id 是有效的仓库 id。你可以在此处详细了解如何获取 repository id。
请注意:与
model_name类似,当你调用repositories参数时,你可能会覆盖 launchpad 中连接的仓库。
现在,我们将仓库连接到聊天对象,以调用基于 RAG 的生成。
messages = [
ChatMessage(role="user", content=query),
]
response = prem_chat.chat(messages, repositories=repositories)
print(response)因此,这也意味着在使用 Prem Platform 时,你无需构建自己的 RAG 流水线。Prem 使用其自身的 RAG 技术,为检索增强生成提供同类最佳的性能。
理想情况下,你无需在此处连接 Repository ID 即可获得检索增强生成。如果你已在 prem 平台中连接了仓库,仍然可以获得相同的结果。
Prem 模板
编写提示词模板可能非常混乱。提示词模板冗长、难以管理,而且必须不断调整,才能在应用中持续改进并保持一致。
使用 Prem,编写和管理提示词可以非常轻松。launchpad 中的 Templates 标签页可帮助你编写所需的任意数量的提示词模板,并在 SDK 中使用它们,让你的应用通过这些提示词运行。你可以在此处详细了解提示词模板。
要在 llama-index 中原生使用 Prem Templates,你需要传入一个字典,其中 id 作为键,值则是模板变量。这个键值对应该传入 ChatMessage 的 additional_kwargs 中。
举例来说,如果你的提示词模板是这样的:
Say hello to my name and say a feel-good quote
from my age. My name is: {name} and age is {age}那么现在你的消息应该如下所示:
messages = [
ChatMessage(
role="user", content="Shawn", additional_kwargs={"id": "name"}
),
ChatMessage(role="user", content="22", additional_kwargs={"id": "age"}),
]将此 messages 传给 llama-index PremAI Client。请注意:不要忘记传入额外的 template_id 以调用 Prem Templates 进行生成。如果你不了解 template_id,可以在我们的文档中了解更多。以下是一个示例:
template_id = "78069ce8-xxxxx-xxxxx-xxxx-xxx"
response = prem_chat.chat(messages, template_id=template_id)Prem Templates 同样支持流式输出。
流式传输
在本节中,让我们看看如何使用 llama-index 和 PremAI 流式输出 token。这与上述方法非常相似。具体做法如下。
streamed_response = prem_chat.stream_chat(messages)
for response_delta in streamed_response:
print(response_delta.delta, end="")I'm here to assist you with writing tasks, but I don't have personal experiences or attend school. However, I can help you brainstorm ideas, outline your essay, or provide information on various school-related topics. Just let me know how I can assist you further!这将逐个流式输出 token。与 complete 方法类似,我们还有 stream_complete 方法,用于对补全进行 token 流式输出。
# This will stream tokens one by one
streamed_response = prem_chat.stream_complete("hello how are you")
for response_delta in streamed_response:
print(response_delta.delta, end="")Hello! I'm here and ready to assist you. How can I help you today?