%pip install llama-index-llms-premai
from llama_index.llms.premai import PremAI
from llama_index.core.llms import ChatMessage
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 ")
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
时,您可能会覆盖在 launchpad 中连接的存储库。
现在,我们将存储库连接到我们的聊天对象,以调用基于 RAG 的生成。
messages = [
ChatMessage(role="user", content=query),
]
response = prem_chat.chat(messages, repositories=repositories)
print(response)
因此,这意味着在使用 Prem 平台时,您无需构建自己的 RAG 管道。Prem 使用其自己的 RAG 技术,为检索增强生成提供一流的性能。
理想情况下,您无需在此处连接存储库 ID 即可获得检索增强生成。如果您已在 Prem 平台上连接了存储库,仍然可以获得相同的结果。
Prem 模板¶
编写提示词模板可能会非常混乱。提示词模板很长,难以管理,并且必须不断调整以改进并在整个应用中保持一致。
使用 Prem,编写和管理提示词变得非常简单。launchpad 中的 Templates 选项卡可以帮助您编写所需的任意数量的提示词模板,并在 SDK 中使用它们来运行您的应用。您可以在此处阅读更多关于提示词模板的信息。
要在 llama-index 中原生使用 Prem 模板,您需要传递一个字典,其中包含一个键 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 客户端。请注意:不要忘记传递额外的 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 进行 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?