主题
引言
MariTalk 是由巴西公司 Maritaca AI 开发的助手。MariTalk 基于经过专门训练、能够很好地理解葡萄牙语的语言模型。
本 notebook 通过两个示例演示如何将 MariTalk 与 Llama Index 一起使用:
- 使用 chat 方法获取宠物名字建议;
- 使用 complete 方法和少样本示例将影评分类为负面或正面。
安装
如果你在 Colab 中打开这个 Notebook,可能需要安装 LlamaIndex。
!pip install llama-index
!pip install llama-index-llms-maritalk
!pip install asyncioAPI 密钥
你需要一个 API 密钥,可以从 chat.maritaca.ai 获取(“Chaves da API”部分)。
示例 1 - 使用 Chat 获取宠物名字建议
from llama_index.core.llms import ChatMessage
from llama_index.llms.maritalk import Maritalk
import asyncio
# To customize your API key, do this
# otherwise it will lookup MARITALK_API_KEY from your env variable
llm = Maritalk(api_key="<your_maritalk_api_key>", model="sabia-2-medium")
# Call chat with a list of messages
messages = [
ChatMessage(
role="system",
content="You are an assistant specialized in suggesting pet names. Given the animal, you must suggest 4 names.",
),
ChatMessage(role="user", content="I have a dog."),
]
# Sync chat
response = llm.chat(messages)
print(response)
# Async chat
async def get_dog_name(llm, messages):
response = await llm.achat(messages)
print(response)
asyncio.run(get_dog_name(llm, messages))流式生成
对于涉及生成长文本的任务,例如撰写长篇文章或翻译大型文档,随着文本生成分部分接收响应可能更有优势,而不是等待完整文本。这使应用更具响应性且更高效,尤其是在生成的文本很长时。我们提供两种方法来满足这一需求:一种同步,另一种异步。
# Sync streaming chat
response = llm.stream_chat(messages)
for chunk in response:
print(chunk.delta, end="", flush=True)
# Async streaming chat
async def get_dog_name_streaming(llm, messages):
async for chunk in await llm.astream_chat(messages):
print(chunk.delta, end="", flush=True)
asyncio.run(get_dog_name_streaming(llm, messages))示例 2 - 使用 Complete 的少样本示例
在使用带少样本示例的模型时,我们建议使用 llm.complete() 方法
prompt = """Classifique a resenha de filme como "positiva" ou "negativa".
Resenha: Gostei muito do filme, é o melhor do ano!
Classe: positiva
Resenha: O filme deixa muito a desejar.
Classe: negativa
Resenha: Apesar de longo, valeu o ingresso..
Classe:"""
# Sync complete
response = llm.complete(prompt)
print(response)
# Async complete
async def classify_review(llm, prompt):
response = await llm.acomplete(prompt)
print(response)
asyncio.run(classify_review(llm, prompt))# Sync streaming complete
response = llm.stream_complete(prompt)
for chunk in response:
print(chunk.delta, end="", flush=True)
# Async streaming complete
async def classify_review_streaming(llm, prompt):
async for chunk in await llm.astream_complete(prompt):
print(chunk.delta, end="", flush=True)
asyncio.run(classify_review_streaming(llm, prompt))