PremAI LlamaIndex
PremAI 是一个一体化平台,可简化由生成式人工智能驱动的强大、生产就绪应用程序的创建过程。通过简化开发流程,PremAI 让您能够专注于提升用户体验并推动应用程序的整体增长。您可以在此处快速开始使用我们的平台。
我们首先安装 llama-index 和 premai-sdk。您可以通过输入以下命令进行安装:
pip install premai llama-index在继续之前,请确保您已在PremAI上创建账户并已创建项目。如果尚未完成,请参阅快速入门指南开始使用PremAI平台。创建您的第一个项目并获取API密钥。
%pip install llama-index-llms-premaifrom llama_index.llms.premai import PremAIfrom llama_index.core.llms import ChatMessage使用LlamaIndex设置PremAI客户端
Section titled “Setup PremAI client with LlamaIndex”导入所需模块后,让我们设置客户端。目前我们假设 project_id 是 8。但请确保使用您自己的项目ID,否则会抛出错误。
为了将 llama-index 与 PremAI 结合使用,您无需通过我们的聊天客户端传递任何模型名称或设置任何参数。默认情况下,它将使用 LaunchPad 中使用的模型名称和参数。
如果您在设置客户端时更改
model或任何其他参数,例如temperature或max_tokens,它将覆盖 LaunchPad 中使用的现有默认配置。
import osimport 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 存储库的原生 RAG 支持
Section titled “Native RAG Support with Prem Repositories”Prem 存储库允许用户上传文档(.txt、.pdf 等)并将这些存储库连接到大型语言模型。您可以将 Prem 存储库视为原生 RAG,其中每个存储库可被视为一个向量数据库。您可以连接多个存储库。您可以在此处了解更多关于存储库的信息。
存储库在 langchain premai 中也受支持。以下是操作方法。
query = "what is the diameter of individual Galaxy"repository_ids = [ 1991,]repositories = dict(ids=repository_ids, similarity_threshold=0.3, limit=3)首先我们通过定义包含一些仓库ID的存储库开始。请确保这些ID是有效的仓库ID。您可以在此处了解更多关于如何获取仓库ID的信息。
请注意:类似于
model_name当你调用参数repositories时,你可能会覆盖发射台中连接的存储库。
现在,我们将存储库与聊天对象连接起来,以调用基于RAG的生成。
messages = [ ChatMessage(role="user", content=query),]
response = prem_chat.chat(messages, repositories=repositories)print(response)因此,这也意味着在使用Prem平台时,您无需构建自己的RAG管道。Prem采用其专有的RAG技术,为检索增强生成提供业界领先的性能。
理想情况下,您无需在此处连接存储库ID即可获得检索增强生成。如果您已在prem平台中连接了存储库,仍然可以获得相同的结果。
编写提示模板可能会非常混乱。提示模板冗长、难以管理,并且必须持续调整以改进并在整个应用程序中保持一致。
通过 Prem,编写和管理提示词可以变得非常简单。启动台内的 模板 标签页可帮助您编写所需的所有提示词模板,并在 SDK 中使用这些模板来运行您的应用程序。您可以在此处了解更多关于提示词模板的信息。
要在 llama-index 中本地使用 Prem 模板,您需要传递一个字典,其中包含一个 id 作为键,值将是模板变量。这个键值对应传递到 ChatMessage 的 additional_kwargs 中。
例如,假设你的提示模板是这样的:
Say hello to my name and say a feel-good quotefrom 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 客户端。请注意:不要忘记传递额外的 template_id 来调用 Prem 模板生成。如果您不了解 template_id,可以在我们的文档中了解更多信息。以下是一个示例:
template_id = "78069ce8-xxxxx-xxxxx-xxxx-xxx"response = prem_chat.chat(messages, template_id=template_id)Prem 模板同样支持流式处理。
在本节中,让我们看看如何使用 llama-index 和 PremAI 实现令牌流式传输。这与上述方法非常相似。具体操作如下。
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!这将逐个流式传输令牌。类似于 complete 方法,我们还有 stream_complete 方法用于实现补全功能的令牌流式传输。
# 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?